Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove useless default capture for lambdas.
[simgrid.git] / teshsuite / simix / generic_simcalls / generic_simcalls.cpp
1 /* Copyright (c) 2016-2017. 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 <memory>
8 #include <stdexcept>
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 /** Create a future which becomes ready when the date is reached */
22 static
23 simgrid::kernel::Future<void> kernel_wait_until(double date)
24 {
25   auto promise = std::make_shared<simgrid::kernel::Promise<void>>();
26   auto future = promise->get_future();
27   SIMIX_timer_set(date, [promise] {
28     promise->set_value();
29   });
30   return future;
31 }
32
33 static int master(int argc, char *argv[])
34 {
35   // Test the simple immediate execution:
36   XBT_INFO("Start");
37   simgrid::simix::kernelImmediate([] {
38     XBT_INFO("kernel");
39   });
40   XBT_INFO("kernel, returned");
41
42   // Synchronize on a successful Future<void>:
43   simgrid::simix::kernelSync([] {
44     return kernel_wait_until(10).then([](simgrid::kernel::Future<void> future) {
45       future.get();
46       XBT_INFO("kernelSync with void");
47     });
48   });
49   XBT_INFO("kernelSync with void, returned");
50
51   // Synchronize on a failing Future<void>:
52   try {
53     simgrid::simix::kernelSync([] {
54       return kernel_wait_until(20).then([](simgrid::kernel::Future<void> future) {
55         future.get();
56         throw std::runtime_error("Exception throwed from kernel_defer");
57       });
58     });
59     XBT_ERROR("No exception caught!");
60   }
61   catch(std::runtime_error& e) {
62     XBT_INFO("Exception caught: %s", e.what());
63   }
64
65   // Synchronize on a successul Future<int> and get the value:
66   int res = simgrid::simix::kernelSync([] {
67     return kernel_wait_until(30).then([](simgrid::kernel::Future<void> future) {
68       future.get();
69       XBT_INFO("kernelSync with value");
70       return 42;
71     });
72   });
73   XBT_INFO("kernelSync with value returned with %i", res);
74
75   // Synchronize on a successul Future<int> and get the value:
76   simgrid::simix::Future<int> future = simgrid::simix::kernelAsync([] {
77     return kernel_wait_until(50).then([](simgrid::kernel::Future<void> future) {
78       future.get();
79       XBT_INFO("kernelAsync with value");
80       return 43;
81     });
82   });
83   res = future.get();
84   XBT_INFO("kernelAsync with value returned with %i", res);
85
86   // Synchronize on a successul Future<int> and get the value:
87   future = simgrid::simix::kernelAsync([] {
88     return kernel_wait_until(60).then([](simgrid::kernel::Future<void> future) {
89       future.get();
90       XBT_INFO("kernelAsync with value");
91       return 43;
92     });
93   });
94   XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
95   future.wait();
96   XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
97   res = future.get();
98   XBT_INFO("kernelAsync with value returned with %i", res);
99
100   return 0;
101 }
102
103 }
104
105 int main(int argc, char *argv[])
106 {
107   SIMIX_global_init(&argc, argv);
108   xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
109   SIMIX_function_register("master", example::master);
110   SIMIX_create_environment(argv[1]);
111   simcall_process_create("master", example::master, NULL, sg_host_by_name("Tremblay"), 0, NULL, NULL);
112   SIMIX_run();
113   return 0;
114 }