Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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 <iostream>
11
12 #include <exception>
13
14 #include <xbt/sysdep.h>
15
16 #include <future>
17
18 #include <xbt/future.hpp>
19 #include <simgrid/kernel/future.hpp>
20 #include <simgrid/simix.h>
21 #include <simgrid/simix.hpp>
22
23 XBT_PUBLIC(void) simcall_run_blocking(std::function<void()> const& code);
24
25 namespace simgrid {
26 namespace simix {
27
28 XBT_PUBLIC(void) unblock(smx_process_t process);
29
30 /** Execute some code in kernel mode and wakes up the process when
31  *  the result is available.
32  *
33  * It is given a callback which is executed in the kernel SimGrid and
34  * returns a simgrid::kernel::Future<T>. The kernel blocks the process
35  * until the Future is ready and either the value wrapped in the future
36  * to the process or raises the exception stored in the Future in the process.
37  *
38  * This can be used to implement blocking calls without adding new simcalls.
39  * One downside of this approach is that we don't have any semantic on what
40  * the process is waiting. This might be a problem for the model-checker and
41  * we'll have to devise a way to make it work.
42  *
43  * @param     code Kernel code returning a `simgrid::kernel::Future<T>`
44  * @return         Value of the kernel future
45  * @exception      Exception from the kernel future
46  */
47 template<class F>
48 auto kernelSync(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_process_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](simgrid::kernel::Future<T> value) {
61         simgrid::xbt::setPromise(result, 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
73 /** A blocking (`wait()`-based) future for SIMIX processes */
74 // TODO, .wait_for()
75 // TODO, .wait_until()
76 // TODO, SharedFuture
77 // TODO, simgrid::simix::when_all - wait for all future to be ready (this one is simple!)
78 // TODO, simgrid::simix::when_any - wait for any future to be ready
79 template <class T>
80 class Future {
81 public:
82   Future() {}
83   Future(simgrid::kernel::Future<T> future) : future_(std::move(future)) {}
84
85   bool valid() const { return future_.valid(); }
86   T get()
87   {
88     if (!valid())
89       throw std::future_error(std::future_errc::no_state);
90     smx_process_t self = SIMIX_process_self();
91     simgrid::xbt::Result<T> result;
92     simcall_run_blocking([this, &result, self]{
93       try {
94         // When the kernel future is ready...
95         this->future_.then([this, &result, self](simgrid::kernel::Future<T> value) {
96           // ... wake up the process with the result of the kernel future.
97           simgrid::xbt::setPromise(result, value);
98           simgrid::simix::unblock(self);
99         });
100       }
101       catch (...) {
102         result.set_exception(std::current_exception());
103         simgrid::simix::unblock(self);
104       }
105     });
106     return result.get();
107   }
108   bool is_ready() const
109   {
110     if (!valid())
111       throw std::future_error(std::future_errc::no_state);
112     return future_.is_ready();
113   }
114   void wait()
115   {
116     // The future is ready! We don't have to wait:
117     if (this->is_ready())
118       return;
119     // The future is not ready. We have to delegate to the SimGrid kernel:
120     std::exception_ptr exception;
121     smx_process_t self = SIMIX_process_self();
122     simcall_run_blocking([this, &exception, self]{
123       try {
124         // When the kernel future is ready...
125         this->future_.then([this, self](simgrid::kernel::Future<T> value) {
126           // ...store it the simix kernel and wake up.
127           this->future_ = std::move(value);
128           simgrid::simix::unblock(self);
129         });
130       }
131       catch (...) {
132         exception = std::current_exception();
133         simgrid::simix::unblock(self);
134       }
135     });
136   }
137 private:
138   // We wrap an event-based kernel future:
139   simgrid::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     User future
146  */
147 template<class F>
148 auto kernelAsync(F code)
149   -> Future<decltype(code().get())>
150 {
151   typedef decltype(code().get()) T;
152
153   // Execute the code in the kernel and get the kernel simcall:
154   simgrid::kernel::Future<T> future =
155     simgrid::simix::kernelImmediate(std::move(code));
156
157   // Wrap tyhe kernel simcall in a user simcall:
158   return simgrid::simix::Future<T>(std::move(future));
159 }
160
161 }
162 }
163
164 #endif