Logo AND Algorithmique Numérique Distribuée

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