Logo AND Algorithmique Numérique Distribuée

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