1 /* Copyright (c) 2016-2019. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_SIMIX_BLOCKING_SIMCALL_HPP
7 #define SIMGRID_SIMIX_BLOCKING_SIMCALL_HPP
14 #include <xbt/sysdep.h>
16 #include <xbt/future.hpp>
17 #include <simgrid/kernel/future.hpp>
18 #include <simgrid/simix.h>
19 #include <simgrid/simix.hpp>
24 XBT_PUBLIC void unblock(smx_actor_t process);
26 /** Execute some code in kernel mode and wakes up the actor when
27 * the result is available.
29 * It is given a callback which is executed in the SimGrid kernel and
30 * returns a `simgrid::kernel::Future<T>`. The kernel blocks the actor
31 * until the Future is ready and:
33 * - either returns the value wrapped in the future to the actor
35 * - or raises the exception stored in the future in the actor.
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 actor is waiting. This might be a problem for the model-checker and
40 * we'll have to devise a way to make it work.
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
46 template <class F> auto kernel_sync(F code) -> decltype(code().get())
48 typedef decltype(code().get()) T;
49 if (SIMIX_is_maestro())
50 xbt_die("Can't execute blocking call in kernel mode");
52 smx_actor_t self = SIMIX_process_self();
53 simgrid::xbt::Result<T> result;
56 [&result, self, &code] {
59 future.then_([&result, self](std::shared_ptr<simgrid::kernel::FutureState<T>>&& value) {
60 simgrid::xbt::set_promise(result, simgrid::kernel::Future<T>(value));
61 simgrid::simix::unblock(self);
64 result.set_exception(std::current_exception());
65 simgrid::simix::unblock(self);
72 /** A blocking (`wait()`-based) future for SIMIX processes */
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
81 Future() { /* Nothing to do*/}
82 explicit Future(simgrid::kernel::Future<T> future) : future_(std::move(future)) {}
84 bool valid() const { return future_.valid(); }
88 throw std::future_error(std::future_errc::no_state);
89 smx_actor_t self = SIMIX_process_self();
90 simgrid::xbt::Result<T> result;
92 [this, &result, self] {
94 // When the kernel future is ready...
95 this->future_.then_([&result, self](std::shared_ptr<simgrid::kernel::FutureState<T>>&& value) {
96 // ... wake up the process with the result of the kernel future.
97 simgrid::xbt::set_promise(result, simgrid::kernel::Future<T>(value));
98 simgrid::simix::unblock(self);
101 result.set_exception(std::current_exception());
102 simgrid::simix::unblock(self);
108 bool is_ready() const
111 throw std::future_error(std::future_errc::no_state);
112 return future_.is_ready();
116 // The future is ready! We don't have to wait:
117 if (this->is_ready())
119 // The future is not ready. We have to delegate to the SimGrid kernel:
120 std::exception_ptr exception;
121 smx_actor_t self = SIMIX_process_self();
122 simcall_run_blocking(
123 [this, &exception, self] {
125 // When the kernel future is ready...
126 this->future_.then_([this, self](std::shared_ptr<simgrid::kernel::FutureState<T>>&& value) {
127 // ...store it the simix kernel and wake up.
128 this->future_ = std::move(simgrid::kernel::Future<T>(value));
129 simgrid::simix::unblock(self);
132 exception = std::current_exception();
133 simgrid::simix::unblock(self);
139 // We wrap an event-based kernel future:
140 simgrid::kernel::Future<T> future_;
143 /** Start some asynchronous work
145 * @param code SimGrid kernel code which returns a simgrid::kernel::Future
146 * @return Actor future
148 template <class F> auto kernel_async(F code) -> Future<decltype(code().get())>
150 typedef decltype(code().get()) T;
152 // Execute the code in the kernel and get the kernel future:
153 simgrid::kernel::Future<T> future = simgrid::kernel::actor::simcall(std::move(code));
155 // Wrap the kernel future in a actor future:
156 return simgrid::simix::Future<T>(std::move(future));