Logo AND Algorithmique Numérique Distribuée

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