Logo AND Algorithmique Numérique Distribuée

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