Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Add comments about the generic simcalls and rename them
[simgrid.git] / teshsuite / simix / generic_simcalls / generic_simcalls.cpp
1 /* Copyright (c) 2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <future>
8 #include <list>
9
10 #include <xbt/future.hpp>
11
12 #include <simgrid/simix.hpp>
13 #include <simgrid/simix/blocking_simcall.hpp>
14 #include <simgrid/kernel/future.hpp>
15 #include <xbt/log.h>
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "my log messages");
18
19 namespace example {
20
21 /** Execute the code in the kernel at some time
22  *
23  *  @param date  when we should execute the code
24  *  @param code  code to execute
25  *  @return      future with the result of the call
26  */
27 template<class F>
28 auto kernel_defer(double date, F code) -> simgrid::kernel::Future<decltype(code())>
29 {
30   typedef decltype(code()) T;
31   auto promise = std::make_shared<simgrid::kernel::Promise<T>>();
32   auto future = promise->get_future();
33   SIMIX_timer_set(date, [promise, code] {
34     simgrid::xbt::fulfillPromise(*promise, std::move(code));
35   });
36   return future;
37 }
38
39 static int master(int argc, char *argv[])
40 {
41   // Test the simple immediate execution:
42   XBT_INFO("Start");
43   simgrid::simix::kernelImmediate([] {
44     XBT_INFO("kernel");
45   });
46   XBT_INFO("kernel, returned");
47
48   // Synchronize on a successful Future<void>:
49   simgrid::simix::kernelSync([&] {
50     return kernel_defer(10, [] {
51       XBT_INFO("kernelSync with void");
52     });
53   });
54   XBT_INFO("kernelSync with void, returned");
55
56   // Synchronize on a failing Future<void>:
57   try {
58     simgrid::simix::kernelSync([&] {
59       return kernel_defer(20, [] {
60         throw std::runtime_error("Exception throwed from kernel_defer");
61       });
62     });
63     XBT_ERROR("No exception caught!");
64   }
65   catch(std::runtime_error& e) {
66     XBT_INFO("Exception caught: %s", e.what());
67   }
68
69   // Synchronize on a successul Future<int> and get the value:
70   int res = simgrid::simix::kernelSync([&] {
71     return kernel_defer(30, [] {
72       XBT_INFO("kernelSync with value");
73       return 42;
74     });
75   });
76   XBT_INFO("kernelSync with value returned with %i", res);
77
78   // Synchronize on a successul Future<int> and get the value:
79   simgrid::simix::Future<int> future = simgrid::simix::kernelAsync([&] {
80     return kernel_defer(50, [] {
81       XBT_INFO("kernelAsync with value");
82       return 43;
83     });
84   });
85   res = future.get();
86   XBT_INFO("kernelAsync with value returned with %i", res);
87
88   return 0;
89 }
90
91 }
92
93 int main(int argc, char *argv[])
94 {
95   SIMIX_global_init(&argc, argv);
96   xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
97   SIMIX_function_register("master", example::master);
98   SIMIX_create_environment(argv[1]);
99   simcall_process_create("master", example::master, NULL, "Tremblay", -1, 0, NULL, NULL, 0);
100   SIMIX_run();
101   return 0;
102 }