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>
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 * The code given is executed in SimGrid kernel and expected to return
33 * a `simgrid::kernel::Future`. The current process is resumed whenever
34 * the Future becomes ready and gets the value or exception of the future:
36 * This can be used to implement blocking calls in without adding new simcalls.
37 * One downside of this approach is that we don't have any semantic on what
38 * the process is waiting. This might be a problem for the model-checker and
39 * we'll have to device a way to make it work.
41 * @param code Kernel code returning a `simgrid::kernel::Future<T>`
42 * @return Value of the kernel future
43 * @exception Exception from the kernel future
46 auto blocking_simcall(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_process_t self = SIMIX_process_self();
53 simgrid::xbt::Result<T> result;
55 simcall_run_blocking([&result, self, &code]{
58 future.then([&result, self](simgrid::kernel::Future<T> value) {
59 simgrid::xbt::setPromise(result, value);
60 simgrid::simix::unblock(self);
64 result.set_exception(std::current_exception());
65 simgrid::simix::unblock(self);