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
50 auto kernelSync(F code) -> decltype(code().get())
52 typedef decltype(code().get()) T;
53 if (SIMIX_is_maestro())
54 xbt_die("Can't execute blocking call in kernel mode");
56 smx_actor_t self = SIMIX_process_self();
57 simgrid::xbt::Result<T> result;
59 simcall_run_blocking([&result, self, &code]{
62 future.then_([&result, self](std::shared_ptr<simgrid::kernel::FutureState<T>>&& value) {
63 simgrid::xbt::set_promise(result, simgrid::kernel::Future<T>(value));
64 simgrid::simix::unblock(self);
68 result.set_exception(std::current_exception());
69 simgrid::simix::unblock(self);
75 /** A blocking (`wait()`-based) future for SIMIX processes */
77 // TODO, .wait_until()
79 // TODO, simgrid::simix::when_all - wait for all future to be ready (this one is simple!)
80 // TODO, simgrid::simix::when_any - wait for any future to be ready
84 Future() { /* Nothing to do*/}
85 explicit Future(simgrid::kernel::Future<T> future) : future_(std::move(future)) {}
87 bool valid() const { return future_.valid(); }
91 throw std::future_error(std::future_errc::no_state);
92 smx_actor_t self = SIMIX_process_self();
93 simgrid::xbt::Result<T> result;
94 simcall_run_blocking([this, &result, self]{
96 // When the kernel future is ready...
97 this->future_.then_([&result, self](std::shared_ptr<simgrid::kernel::FutureState<T>>&& value) {
98 // ... wake up the process with the result of the kernel future.
99 simgrid::xbt::set_promise(result, simgrid::kernel::Future<T>(value));
100 simgrid::simix::unblock(self);
104 result.set_exception(std::current_exception());
105 simgrid::simix::unblock(self);
110 bool is_ready() const
113 throw std::future_error(std::future_errc::no_state);
114 return future_.is_ready();
118 // The future is ready! We don't have to wait:
119 if (this->is_ready())
121 // The future is not ready. We have to delegate to the SimGrid kernel:
122 std::exception_ptr exception;
123 smx_actor_t self = SIMIX_process_self();
124 simcall_run_blocking([this, &exception, self]{
126 // When the kernel future is ready...
127 this->future_.then_([this, self](std::shared_ptr<simgrid::kernel::FutureState<T>>&& value) {
128 // ...store it the simix kernel and wake up.
129 this->future_ = std::move(simgrid::kernel::Future<T>(value));
130 simgrid::simix::unblock(self);
134 exception = std::current_exception();
135 simgrid::simix::unblock(self);
140 // We wrap an event-based kernel future:
141 simgrid::kernel::Future<T> future_;
144 /** Start some asynchronous work
146 * @param code SimGrid kernel code which returns a simgrid::kernel::Future
147 * @return Actor future
150 auto kernelAsync(F code)
151 -> Future<decltype(code().get())>
153 typedef decltype(code().get()) T;
155 // Execute the code in the kernel and get the kernel future:
156 simgrid::kernel::Future<T> future = simgrid::simix::simcall(std::move(code));
158 // Wrap the kernel future in a actor future:
159 return simgrid::simix::Future<T>(std::move(future));