Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
59f688cab0a9ae50c72e93d6cba74a8dfcc30bc7
[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  *  The code given is executed in SimGrid kernel and expected to return
34  *  a `simgrid::kernel::Future`. The current process is resumed whenever
35  *  the Future becomes ready and gets the value or exception of the future:
36  *
37  *  This can be used to implement blocking calls in 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 device 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 blocking_simcall(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 template <class T>
74 class Future {
75 public:
76   Future() {}
77   Future(simgrid::kernel::Future<T> future) : future_(std::move(future)) {}
78
79   bool valid() const { return future_.valid(); }
80   T get()
81   {
82     if (!valid())
83       throw std::future_error(std::future_errc::no_state);
84     smx_process_t self = SIMIX_process_self();
85     simgrid::xbt::Result<T> result;
86     simcall_run_blocking([this, &result, self]{
87       try {
88         // When the kernel future is ready...
89         this->future_.then([this, &result, self](simgrid::kernel::Future<T> value) {
90           // ... wake up the process with the result of the kernel future.
91           simgrid::xbt::setPromise(result, value);
92           simgrid::simix::unblock(self);
93         });
94       }
95       catch (...) {
96         result.set_exception(std::current_exception());
97         simgrid::simix::unblock(self);
98       }
99     });
100     return result.get();
101   }
102   // TODO, wait()
103   // TODO, wait_for()
104   // TODO, wait_until()
105 private:
106   // We wrap an event-based kernel future:
107   simgrid::kernel::Future<T> future_;
108 };
109
110 /** Start some asynchronous work
111  *
112  *  @param code SimGrid kernel code which returns a simgrid::kernel::Future
113  *  @return     User future
114  */
115 template<class F>
116 auto asynchronous_simcall(F code)
117   -> Future<decltype(code().get())>
118 {
119   typedef decltype(code().get()) T;
120
121   // Execute the code in the kernel and get the kernel simcall:
122   simgrid::kernel::Future<T> future =
123     simgrid::simix::kernel(std::move(code));
124
125   // Wrap tyhe kernel simcall in a user simcall:
126   return simgrid::simix::Future<T>(std::move(future));
127 }
128
129 }
130 }
131
132 #endif