1 /* Copyright (c) 2016-2018. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #ifndef SIMGRID_SIMIX_BLOCKING_SIMCALL_HPP
8 #define SIMGRID_SIMIX_BLOCKING_SIMCALL_HPP
15 #include <xbt/sysdep.h>
17 #include <xbt/future.hpp>
18 #include <simgrid/kernel/future.hpp>
19 #include <simgrid/simix.h>
20 #include <simgrid/simix.hpp>
22 XBT_PUBLIC void simcall_run_blocking(std::function<void()> const& code);
27 XBT_PUBLIC void unblock(smx_actor_t process);
29 /** Execute some code in kernel mode and wakes up the actor when
30 * the result is available.
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:
36 * - either returns the value wrapped in the future to the actor;
38 * - or raises the exception stored in the future in the actor.
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.
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
49 template <class F> auto kernel_sync(F code) -> decltype(code().get())
51 typedef decltype(code().get()) T;
52 if (SIMIX_is_maestro())
53 xbt_die("Can't execute blocking call in kernel mode");
55 smx_actor_t self = SIMIX_process_self();
56 simgrid::xbt::Result<T> result;
58 simcall_run_blocking([&result, self, &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);
67 result.set_exception(std::current_exception());
68 simgrid::simix::unblock(self);
74 XBT_ATTRIB_DEPRECATED_v323("Please use simix::kernel_sync()") auto kernelSync(F code) -> decltype(code().get())
76 return kernel_sync(code);
79 /** A blocking (`wait()`-based) future for SIMIX processes */
81 // TODO, .wait_until()
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
88 Future() { /* Nothing to do*/}
89 explicit Future(simgrid::kernel::Future<T> future) : future_(std::move(future)) {}
91 bool valid() const { return future_.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]{
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);
108 result.set_exception(std::current_exception());
109 simgrid::simix::unblock(self);
114 bool is_ready() const
117 throw std::future_error(std::future_errc::no_state);
118 return future_.is_ready();
122 // The future is ready! We don't have to wait:
123 if (this->is_ready())
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]{
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);
138 exception = std::current_exception();
139 simgrid::simix::unblock(self);
144 // We wrap an event-based kernel future:
145 simgrid::kernel::Future<T> future_;
148 /** Start some asynchronous work
150 * @param code SimGrid kernel code which returns a simgrid::kernel::Future
151 * @return Actor future
153 template <class F> auto kernel_async(F code) -> Future<decltype(code().get())>
155 typedef decltype(code().get()) T;
157 // Execute the code in the kernel and get the kernel future:
158 simgrid::kernel::Future<T> future = simgrid::simix::simcall(std::move(code));
160 // Wrap the kernel future in a actor future:
161 return simgrid::simix::Future<T>(std::move(future));
164 XBT_ATTRIB_DEPRECATED_v323("Please use simix::kernel_sync()") auto kernelAsync(F code) -> Future<decltype(code().get())>
166 return kernel_async(code);