Logo AND Algorithmique Numérique Distribuée

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