1 /* Copyright (c) 2016. 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_process_t process);
29 /** Execute some code in kernel mode and wakes up the process when
30 * the result is available.
32 * It is given a callback which is executed in the kernel SimGrid and
33 * returns a simgrid::kernel::Future<T>. The kernel blocks the process
34 * until the Future is ready and either the value wrapped in the future
35 * to the process or raises the exception stored in the Future in the process.
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 process 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
47 auto kernelSync(F code) -> decltype(code().get())
49 typedef decltype(code().get()) T;
50 if (SIMIX_is_maestro())
51 xbt_die("Can't execute blocking call in kernel mode");
53 smx_process_t self = SIMIX_process_self();
54 simgrid::xbt::Result<T> result;
56 simcall_run_blocking([&result, self, &code]{
59 future.then_([&result, self](simgrid::kernel::Future<T> value) {
60 simgrid::xbt::setPromise(result, value);
61 simgrid::simix::unblock(self);
65 result.set_exception(std::current_exception());
66 simgrid::simix::unblock(self);
72 /** A blocking (`wait()`-based) future for SIMIX processes */
74 // TODO, .wait_until()
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
82 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_process_t self = SIMIX_process_self();
90 simgrid::xbt::Result<T> result;
91 simcall_run_blocking([this, &result, self]{
93 // When the kernel future is ready...
94 this->future_.then_([this, &result, self](simgrid::kernel::Future<T> value) {
95 // ... wake up the process with the result of the kernel future.
96 simgrid::xbt::setPromise(result, value);
97 simgrid::simix::unblock(self);
101 result.set_exception(std::current_exception());
102 simgrid::simix::unblock(self);
107 bool is_ready() const
110 throw std::future_error(std::future_errc::no_state);
111 return future_.is_ready();
115 // The future is ready! We don't have to wait:
116 if (this->is_ready())
118 // The future is not ready. We have to delegate to the SimGrid kernel:
119 std::exception_ptr exception;
120 smx_process_t self = SIMIX_process_self();
121 simcall_run_blocking([this, &exception, self]{
123 // When the kernel future is ready...
124 this->future_.then_([this, self](simgrid::kernel::Future<T> value) {
125 // ...store it the simix kernel and wake up.
126 this->future_ = std::move(value);
127 simgrid::simix::unblock(self);
131 exception = std::current_exception();
132 simgrid::simix::unblock(self);
137 // We wrap an event-based kernel future:
138 simgrid::kernel::Future<T> future_;
141 /** Start some asynchronous work
143 * @param code SimGrid kernel code which returns a simgrid::kernel::Future
144 * @return User future
147 auto kernelAsync(F code)
148 -> Future<decltype(code().get())>
150 typedef decltype(code().get()) T;
152 // Execute the code in the kernel and get the kernel simcall:
153 simgrid::kernel::Future<T> future =
154 simgrid::simix::kernelImmediate(std::move(code));
156 // Wrap tyhe kernel simcall in a user simcall:
157 return simgrid::simix::Future<T>(std::move(future));