Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge remote-tracking branch 'upstream/master'
[simgrid.git] / teshsuite / simix / generic-simcalls / generic-simcalls.cpp
1 /* Copyright (c) 2016-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <memory>
7 #include <stdexcept>
8
9 #include <xbt/future.hpp>
10
11 #include <simgrid/kernel/future.hpp>
12 #include <simgrid/simix.hpp>
13 #include <simgrid/simix/blocking_simcall.hpp>
14 #include <xbt/log.h>
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "my log messages");
17
18 namespace example {
19
20 class exception : public std::runtime_error {
21   using std::runtime_error::runtime_error;
22 };
23
24 /** Create a future which becomes ready when the date is reached */
25 static simgrid::kernel::Future<void> kernel_wait_until(double date)
26 {
27   auto promise = std::make_shared<simgrid::kernel::Promise<void>>();
28   auto future  = promise->get_future();
29   SIMIX_timer_set(date, [promise] { promise->set_value(); });
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::simcall([] { XBT_INFO("kernel"); });
38   XBT_INFO("kernel, returned");
39
40   // Synchronize on a successful Future<void>:
41   simgrid::simix::kernel_sync([] {
42     return kernel_wait_until(10).then([](simgrid::kernel::Future<void> future) {
43       future.get();
44       XBT_INFO("kernel_sync with void");
45     });
46   });
47   XBT_INFO("kernel_sync with void, returned");
48
49   // Synchronize on a failing Future<void>:
50   try {
51     simgrid::simix::kernel_sync([] {
52       return kernel_wait_until(20).then([](simgrid::kernel::Future<void> future) {
53         future.get();
54         throw example::exception("Exception throwed from kernel_defer");
55       });
56     });
57     XBT_ERROR("No exception caught!");
58   } catch (const example::exception& e) {
59     XBT_INFO("Exception caught: %s", e.what());
60   }
61
62   // Synchronize on a successul Future<int> and get the value:
63   int res = simgrid::simix::kernel_sync([] {
64     return kernel_wait_until(30).then([](simgrid::kernel::Future<void> future) {
65       future.get();
66       XBT_INFO("kernel_sync with value");
67       return 42;
68     });
69   });
70   XBT_INFO("kernel_sync with value returned with %i", res);
71
72   // Synchronize on a successul Future<int> and get the value:
73   simgrid::simix::Future<int> future = simgrid::simix::kernel_async([] {
74     return kernel_wait_until(50).then([](simgrid::kernel::Future<void> future) {
75       future.get();
76       XBT_INFO("kernel_async with value");
77       return 43;
78     });
79   });
80   res = future.get();
81   XBT_INFO("kernel_async with value returned with %i", res);
82
83   // Synchronize on a successul Future<int> and get the value:
84   future = simgrid::simix::kernel_async([] {
85     return kernel_wait_until(60).then([](simgrid::kernel::Future<void> future) {
86       future.get();
87       XBT_INFO("kernel_async with value");
88       return 43;
89     });
90   });
91   XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
92   future.wait();
93   XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
94   res = future.get();
95   XBT_INFO("kernel_async with value returned with %i", res);
96
97   return 0;
98 }
99 }
100
101 int main(int argc, char* argv[])
102 {
103   SIMIX_global_init(&argc, argv);
104   xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
105   SIMIX_function_register("master", example::master);
106   SIMIX_create_environment(argv[1]);
107   simcall_process_create("master", example::master, NULL, sg_host_by_name("Tremblay"), 0, NULL, NULL);
108   SIMIX_run();
109   return 0;
110 }