Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] Mailbox::receiver return a s4u::ActorPtr
[simgrid.git] / include / simgrid / simix / blocking_simcall.hpp
1 /* Copyright (c) 2016. 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_process_t process);
28
29 /** Execute some code in kernel mode and wakes up the process when
30  *  the result is available.
31  *
32  * It is given a callback which is executed in the kernel SimGrid and
33  * returns a simgrid::kernel::Future<T>. The kernel blocks the process
34  * until the Future is ready and either the value wrapped in the future
35  * to the process or raises the exception stored in the Future in the process.
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 process 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>
47 auto kernelSync(F code) -> decltype(code().get())
48 {
49   typedef decltype(code().get()) T;
50   if (SIMIX_is_maestro())
51     xbt_die("Can't execute blocking call in kernel mode");
52
53   smx_process_t self = SIMIX_process_self();
54   simgrid::xbt::Result<T> result;
55
56   simcall_run_blocking([&result, self, &code]{
57     try {
58       auto future = code();
59       future.then_([&result, self](simgrid::kernel::Future<T> value) {
60         simgrid::xbt::setPromise(result, value);
61         simgrid::simix::unblock(self);
62       });
63     }
64     catch (...) {
65       result.set_exception(std::current_exception());
66       simgrid::simix::unblock(self);
67     }
68   });
69   return result.get();
70 }
71
72 /** A blocking (`wait()`-based) future for SIMIX processes */
73 // TODO, .wait_for()
74 // TODO, .wait_until()
75 // TODO, SharedFuture
76 // TODO, simgrid::simix::when_all - wait for all future to be ready (this one is simple!)
77 // TODO, simgrid::simix::when_any - wait for any future to be ready
78 template <class T>
79 class Future {
80 public:
81   Future() {}
82   Future(simgrid::kernel::Future<T> future) : future_(std::move(future)) {}
83
84   bool valid() const { return future_.valid(); }
85   T get()
86   {
87     if (!valid())
88       throw std::future_error(std::future_errc::no_state);
89     smx_process_t self = SIMIX_process_self();
90     simgrid::xbt::Result<T> result;
91     simcall_run_blocking([this, &result, self]{
92       try {
93         // When the kernel future is ready...
94         this->future_.then_([this, &result, self](simgrid::kernel::Future<T> value) {
95           // ... wake up the process with the result of the kernel future.
96           simgrid::xbt::setPromise(result, value);
97           simgrid::simix::unblock(self);
98         });
99       }
100       catch (...) {
101         result.set_exception(std::current_exception());
102         simgrid::simix::unblock(self);
103       }
104     });
105     return result.get();
106   }
107   bool is_ready() const
108   {
109     if (!valid())
110       throw std::future_error(std::future_errc::no_state);
111     return future_.is_ready();
112   }
113   void wait()
114   {
115     // The future is ready! We don't have to wait:
116     if (this->is_ready())
117       return;
118     // The future is not ready. We have to delegate to the SimGrid kernel:
119     std::exception_ptr exception;
120     smx_process_t self = SIMIX_process_self();
121     simcall_run_blocking([this, &exception, self]{
122       try {
123         // When the kernel future is ready...
124         this->future_.then_([this, self](simgrid::kernel::Future<T> value) {
125           // ...store it the simix kernel and wake up.
126           this->future_ = std::move(value);
127           simgrid::simix::unblock(self);
128         });
129       }
130       catch (...) {
131         exception = std::current_exception();
132         simgrid::simix::unblock(self);
133       }
134     });
135   }
136 private:
137   // We wrap an event-based kernel future:
138   simgrid::kernel::Future<T> future_;
139 };
140
141 /** Start some asynchronous work
142  *
143  *  @param code SimGrid kernel code which returns a simgrid::kernel::Future
144  *  @return     User future
145  */
146 template<class F>
147 auto kernelAsync(F code)
148   -> Future<decltype(code().get())>
149 {
150   typedef decltype(code().get()) T;
151
152   // Execute the code in the kernel and get the kernel simcall:
153   simgrid::kernel::Future<T> future =
154     simgrid::simix::kernelImmediate(std::move(code));
155
156   // Wrap tyhe kernel simcall in a user simcall:
157   return simgrid::simix::Future<T>(std::move(future));
158 }
159
160 }
161 }
162
163 #endif