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
14 #include <xbt/sysdep.h>
18 #include <xbt/future.hpp>
19 #include <simgrid/kernel/future.hpp>
20 #include <simgrid/simix.h>
21 #include <simgrid/simix.hpp>
23 XBT_PUBLIC(void) simcall_run_blocking(std::function<void()> const& code);
28 XBT_PUBLIC(void) unblock(smx_process_t process);
30 /** Execute some code in kernel mode and wakes up the process when
31 * the result is available.
33 * The code given is executed in SimGrid kernel and expected to return
34 * a `simgrid::kernel::Future`. The current process is resumed whenever
35 * the Future becomes ready and gets the value or exception of the future:
37 * This can be used to implement blocking calls in 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 device 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 blocking_simcall(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 */
77 Future(simgrid::kernel::Future<T> future) : future_(std::move(future)) {}
79 bool valid() const { return future_.valid(); }
83 throw std::future_error(std::future_errc::no_state);
84 smx_process_t self = SIMIX_process_self();
85 simgrid::xbt::Result<T> result;
86 simcall_run_blocking([this, &result, self]{
88 // When the kernel future is ready...
89 this->future_.then([this, &result, self](simgrid::kernel::Future<T> value) {
90 // ... wake up the process with the result of the kernel future.
91 simgrid::xbt::setPromise(result, value);
92 simgrid::simix::unblock(self);
96 result.set_exception(std::current_exception());
97 simgrid::simix::unblock(self);
104 // TODO, wait_until()
106 // We wrap an event-based kernel future:
107 simgrid::kernel::Future<T> future_;
110 /** Start some asynchronous work
112 * @param code SimGrid kernel code which returns a simgrid::kernel::Future
113 * @return User future
116 auto asynchronous_simcall(F code)
117 -> Future<decltype(code().get())>
119 typedef decltype(code().get()) T;
121 // Execute the code in the kernel and get the kernel simcall:
122 simgrid::kernel::Future<T> future =
123 simgrid::simix::kernel(std::move(code));
125 // Wrap tyhe kernel simcall in a user simcall:
126 return simgrid::simix::Future<T>(std::move(future));