Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cee8b5c2f41b4e533addf1a78f7aad3c71685169
[simgrid.git] / teshsuite / kernel / simcall-generic / blocking_simcall.hpp
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 #ifndef SIMGRID_SIMIX_BLOCKING_SIMCALL_HPP
7 #define SIMGRID_SIMIX_BLOCKING_SIMCALL_HPP
8
9 #include <exception>
10 #include <functional>
11 #include <future>
12 #include <utility>
13
14 #include <xbt/sysdep.h>
15
16 #include <simgrid/kernel/future.hpp>
17 #include <xbt/promise.hpp>
18
19 #include "src/kernel/actor/ActorImpl.hpp"
20
21 namespace simgrid {
22 namespace simix {
23
24 /** Execute some code in kernel mode and wakes up the actor when
25  *  the result is available.
26  *
27  * It is given a callback which is executed in the SimGrid kernel and
28  * returns a `simgrid::kernel::Future<T>`. The kernel blocks the actor
29  * until the Future is ready and:
30  *
31  *  - either returns the value wrapped in the future to the actor
32  *
33  *  - or raises the exception stored in the future in the actor.
34  *
35  * This can be used to implement blocking calls without adding new simcalls.
36  * One downside of this approach is that we don't have any semantic on what
37  * the actor is waiting. This might be a problem for the model-checker and
38  * we'll have to devise a way to make it work.
39  *
40  * @param     code Kernel code returning a `simgrid::kernel::Future<T>`
41  * @return         Value of the kernel future
42  * @exception      Exception from the kernel future
43  */
44 template <class F> auto kernel_sync(F code) -> decltype(code().get())
45 {
46   using T = decltype(code().get());
47   xbt_assert(not s4u::Actor::is_maestro(), "Cannot execute blocking call in kernel mode");
48
49   auto self = kernel::actor::ActorImpl::self();
50   xbt::Result<T> result;
51   simcall_run_blocking(
52       [&result, self, &code] {
53         try {
54           auto future = code();
55           future.then_([&result, self](std::shared_ptr<kernel::FutureState<T>> value) {
56             xbt::set_promise(result, kernel::Future<T>(std::move(value)));
57             unblock(self);
58           });
59         } catch (...) {
60           result.set_exception(std::current_exception());
61           unblock(self);
62         }
63       },
64       nullptr);
65   return result.get();
66 }
67
68 /** A blocking (`wait()`-based) future for SIMIX processes */
69 // TODO:
70 // - .wait_for
71 // - .wait_until
72 // - SharedFuture
73 // - simgrid::simix::when_all - wait for all future to be ready (this one is simple!)
74 // - simgrid::simix::when_any - wait for any future to be ready
75 template <class T> class Future {
76 public:
77   Future() = default;
78   explicit Future(simgrid::kernel::Future<T> future) : future_(std::move(future)) {}
79   Future(Future&&) noexcept = default;
80   Future& operator=(Future&&) noexcept = default;
81
82   bool valid() const { return future_.valid(); }
83   T get()
84   {
85     if (not valid())
86       throw std::future_error(std::future_errc::no_state);
87     auto self = kernel::actor::ActorImpl::self();
88     xbt::Result<T> result;
89     simcall_run_blocking(
90         [this, &result, self] {
91           try {
92             // When the kernel future is ready...
93             this->future_.then_([&result, self](std::shared_ptr<kernel::FutureState<T>> value) {
94               // ... wake up the process with the result of the kernel future.
95               xbt::set_promise(result, kernel::Future<T>(std::move(value)));
96               unblock(self);
97             });
98           } catch (...) {
99             result.set_exception(std::current_exception());
100             unblock(self);
101           }
102         },
103         nullptr);
104     return result.get();
105   }
106   bool is_ready() const
107   {
108     if (not valid())
109       throw std::future_error(std::future_errc::no_state);
110     return future_.is_ready();
111   }
112   void wait()
113   {
114     // The future is ready! We don't have to wait:
115     if (this->is_ready())
116       return;
117     // The future is not ready. We have to delegate to the SimGrid kernel:
118     std::exception_ptr exception;
119     auto self = kernel::actor::ActorImpl::self();
120     simcall_run_blocking(
121         [this, &exception, self] {
122           try {
123             // When the kernel future is ready...
124             this->future_.then_([this, self](std::shared_ptr<kernel::FutureState<T>> value) {
125               // ...store it the simix kernel and wake up.
126               this->future_ = kernel::Future<T>(std::move(value));
127               unblock(self);
128             });
129           } catch (...) {
130             exception = std::current_exception();
131             unblock(self);
132           }
133         },
134         nullptr);
135   }
136
137 private:
138   // We wrap an event-based kernel future:
139   kernel::Future<T> future_;
140 };
141
142 /** Start some asynchronous work
143  *
144  *  @param code SimGrid kernel code which returns a simgrid::kernel::Future
145  *  @return     Actor future
146  */
147 template <class F> auto kernel_async(F code) -> Future<decltype(code().get())>
148 {
149   using T = decltype(code().get());
150
151   // Execute the code in the kernel and get the kernel future:
152   kernel::Future<T> future = kernel::actor::simcall(std::move(code));
153
154   // Wrap the kernel future in an actor future:
155   return Future<T>(std::move(future));
156 }
157 } // namespace simix
158 } // namespace simgrid
159
160 #endif