Logo AND Algorithmique Numérique Distribuée

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