Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / teshsuite / simix / generic-simcalls / generic-simcalls.cpp
1 /* Copyright (c) 2016-2020. 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/engine.h>
12 #include <simgrid/kernel/future.hpp>
13 #include <simgrid/simix.hpp>
14 #include <simgrid/simix/blocking_simcall.hpp>
15 #include <xbt/log.h>
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "my log messages");
18
19 namespace example {
20
21 class exception : public std::runtime_error {
22   using std::runtime_error::runtime_error;
23 };
24
25 /** Create a future which becomes ready when the date is reached */
26 static simgrid::kernel::Future<void> kernel_wait_until(double date)
27 {
28   auto promise = std::make_shared<simgrid::kernel::Promise<void>>();
29   auto future  = promise->get_future();
30   simgrid::simix::Timer::set(date, [promise] { promise->set_value(); });
31   return future;
32 }
33
34 static void master()
35 {
36   // Test the simple immediate execution:
37   XBT_INFO("Start");
38   simgrid::kernel::actor::simcall([] { XBT_INFO("kernel"); });
39   XBT_INFO("kernel, returned");
40
41   // Synchronize on a successful Future<void>:
42   simgrid::simix::kernel_sync([] {
43     return kernel_wait_until(10).then([](simgrid::kernel::Future<void> f) {
44       f.get();
45       XBT_INFO("kernel_sync with void");
46     });
47   });
48   XBT_INFO("kernel_sync with void, returned");
49
50   // Synchronize on a failing Future<void>:
51   try {
52     simgrid::simix::kernel_sync([] {
53       return kernel_wait_until(20).then([](simgrid::kernel::Future<void> f) {
54         f.get();
55         throw example::exception("Exception thrown from kernel_defer");
56       });
57     });
58     XBT_ERROR("No exception caught!");
59   } catch (const example::exception& e) {
60     XBT_INFO("Exception caught: %s", e.what());
61   }
62
63   // Synchronize on a successful Future<int> and get the value:
64   int res = simgrid::simix::kernel_sync([] {
65     return kernel_wait_until(30).then([](simgrid::kernel::Future<void> f) {
66       f.get();
67       XBT_INFO("kernel_sync with value");
68       return 42;
69     });
70   });
71   XBT_INFO("kernel_sync with value returned with %i", res);
72
73   // Synchronize on a successful Future<int> and get the value:
74   simgrid::simix::Future<int> future = simgrid::simix::kernel_async([] {
75     return kernel_wait_until(50).then([](simgrid::kernel::Future<void> f) {
76       f.get();
77       XBT_INFO("kernel_async with value");
78       return 43;
79     });
80   });
81   res = future.get();
82   XBT_INFO("kernel_async with value returned with %i", res);
83
84   // Synchronize on a successful Future<int> and get the value:
85   future = simgrid::simix::kernel_async([] {
86     return kernel_wait_until(60).then([](simgrid::kernel::Future<void> f) {
87       f.get();
88       XBT_INFO("kernel_async with value");
89       return 43;
90     });
91   });
92   XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
93   future.wait();
94   XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
95   res = future.get();
96   XBT_INFO("kernel_async with value returned with %i", res);
97 }
98 }
99
100 int main(int argc, char* argv[])
101 {
102   SIMIX_global_init(&argc, argv);
103   xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
104   simgrid_load_platform(argv[1]);
105   simcall_process_create("master", example::master, NULL, sg_host_by_name("Tremblay"), NULL);
106   SIMIX_run();
107   return 0;
108 }