1 /* Copyright (c) 2016-2022. 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 <simgrid/kernel/future.hpp>
17 #include <xbt/promise.hpp>
19 #include "src/kernel/actor/ActorImpl.hpp"
24 /** Execute some code in kernel mode and wakes up the actor when
25 * the result is available.
27 * It is given a callback which is executed in the SimGrid kernel and
28 * returns a `simgrid::kernel::Future<T>`. The kernel blocks the actor
29 * until the Future is ready and:
31 * - either returns the value wrapped in the future to the actor
33 * - or raises the exception stored in the future in the actor.
35 * This can be used to implement blocking calls without adding new simcalls.
36 * One downside of this approach is that we don't have any semantic on what
37 * the actor is waiting. This might be a problem for the model-checker and
38 * we'll have to devise a way to make it work.
40 * @param code Kernel code returning a `simgrid::kernel::Future<T>`
41 * @return Value of the kernel future
42 * @exception Exception from the kernel future
44 template <class F> auto kernel_sync(F code) -> decltype(code().get())
46 using T = decltype(code().get());
47 xbt_assert(not s4u::Actor::is_maestro(), "Cannot execute blocking call in kernel mode");
49 auto self = kernel::actor::ActorImpl::self();
50 xbt::Result<T> result;
52 [&result, self, &code] {
55 future.then_([&result, self](std::shared_ptr<kernel::FutureState<T>> value) {
56 xbt::set_promise(result, kernel::Future<T>(std::move(value)));
60 result.set_exception(std::current_exception());
68 /** A blocking (`wait()`-based) future for SIMIX processes */
73 // - simgrid::simix::when_all - wait for all future to be ready (this one is simple!)
74 // - simgrid::simix::when_any - wait for any future to be ready
75 template <class T> class Future {
78 explicit Future(simgrid::kernel::Future<T> future) : future_(std::move(future)) {}
79 Future(Future&&) noexcept = default;
80 Future& operator=(Future&&) noexcept = default;
82 bool valid() const { return future_.valid(); }
86 throw std::future_error(std::future_errc::no_state);
87 auto self = kernel::actor::ActorImpl::self();
88 xbt::Result<T> result;
90 [this, &result, self] {
92 // When the kernel future is ready...
93 this->future_.then_([&result, self](std::shared_ptr<kernel::FutureState<T>> value) {
94 // ... wake up the process with the result of the kernel future.
95 xbt::set_promise(result, kernel::Future<T>(std::move(value)));
99 result.set_exception(std::current_exception());
106 bool is_ready() const
109 throw std::future_error(std::future_errc::no_state);
110 return future_.is_ready();
114 // The future is ready! We don't have to wait:
115 if (this->is_ready())
117 // The future is not ready. We have to delegate to the SimGrid kernel:
118 std::exception_ptr exception;
119 auto self = kernel::actor::ActorImpl::self();
120 simcall_run_blocking(
121 [this, &exception, self] {
123 // When the kernel future is ready...
124 this->future_.then_([this, self](std::shared_ptr<kernel::FutureState<T>> value) {
125 // ...store it the simix kernel and wake up.
126 this->future_ = kernel::Future<T>(std::move(value));
130 exception = std::current_exception();
138 // We wrap an event-based kernel future:
139 kernel::Future<T> future_;
142 /** Start some asynchronous work
144 * @param code SimGrid kernel code which returns a simgrid::kernel::Future
145 * @return Actor future
147 template <class F> auto kernel_async(F code) -> Future<decltype(code().get())>
149 using T = decltype(code().get());
151 // Execute the code in the kernel and get the kernel future:
152 kernel::Future<T> future = kernel::actor::simcall(std::move(code));
154 // Wrap the kernel future in an actor future:
155 return Future<T>(std::move(future));
158 } // namespace simgrid