1 /* Copyright (c) 2007-2023. 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_HPP
8 #define SIMGRID_SIMIX_HPP
10 #include <simgrid/s4u/Actor.hpp>
11 #include <xbt/promise.hpp>
12 #include <xbt/signal.hpp>
15 #include <unordered_map>
17 XBT_PUBLIC void simcall_run_answered(std::function<void()> const& code,
18 simgrid::kernel::actor::SimcallObserver* observer);
19 XBT_PUBLIC void simcall_run_blocking(std::function<void()> const& code,
20 simgrid::kernel::actor::SimcallObserver* observer);
21 XBT_PUBLIC void simcall_run_object_access(std::function<void()> const& code,
22 simgrid::kernel::actor::ObjectAccessSimcallItem* item);
28 /** Execute some code in kernel context on behalf of the user code.
30 * Every modification of the environment must be protected this way: every setter, constructor and similar.
31 * Getters don't have to be protected this way, and setters may use the simcall_object_access() variant (see below).
33 * This allows deterministic parallel simulation without any locking, even if almost nobody uses parallel simulation in
34 * SimGrid. More interestingly it makes every modification of the simulated world observable by the model-checker,
35 * allowing the whole MC business.
37 * It is highly inspired from the syscalls in a regular operating system, allowing the user code to get some specific
38 * code executed in the kernel context. But here, there is almost no security involved. Parameters get checked for
39 * finiteness but that's all. The main goal remain to ensure reproducible ordering of uncomparable events (in
40 * [parallel] simulation) and observability of events (in model-checking).
42 * The code passed as argument is supposed to terminate at the exact same simulated timestamp.
43 * Do not use it if your code may block waiting for a subsequent event, e.g. if you lock a mutex,
44 * you may need to wait for that mutex to be unlocked by its current owner.
45 * Potentially blocking simcall must be issued using simcall_blocking(), right below in this file.
47 template <class F> typename std::result_of_t<F()> simcall_answered(F&& code, SimcallObserver* observer = nullptr)
49 // If we are in the maestro, we take the fast path and execute the
50 // code directly without simcall marshalling/unmarshalling/dispatch:
51 if (s4u::Actor::is_maestro())
52 return std::forward<F>(code)();
54 // If we are in the application, pass the code to the maestro which
55 // executes it for us and reports the result. We use a std::future which
56 // conveniently handles the success/failure value for us.
57 using R = typename std::result_of_t<F()>;
58 simgrid::xbt::Result<R> result;
59 simcall_run_answered([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); }, observer);
63 /** Use a setter on the `item` object. That's a simcall only if running in parallel or with MC activated.
65 * Simulation without MC and without parallelism (contexts/nthreads=1) will not pay the price of a simcall for an
66 * harmless setter. When running in parallel, you want your write access to be done in a mutual exclusion way, while the
67 * getters can still occur out of order.
69 * When running in MC, you want to make this access visible to the checker. Actually in this case, it's not visible from
70 * the checker (and thus still use a fast track) if the setter is called from the actor that created the object.
72 template <class F> typename std::result_of_t<F()> simcall_object_access(ObjectAccessSimcallItem* item, F&& code)
74 // If we are in the maestro, we take the fast path and execute the code directly
75 if (simgrid::s4u::Actor::is_maestro())
76 return std::forward<F>(code)();
78 // If called from another thread, do a real simcall. It will be short-cut on need
79 using R = typename std::result_of_t<F()>;
80 simgrid::xbt::Result<R> result;
81 simcall_run_object_access([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); }, item);
86 /** Execute some code (that does not return immediately) in kernel context
88 * This is very similar to simcall_answered() above, but the calling actor will not get rescheduled until
89 * actor->simcall_answer() is called explicitly.
91 * This is meant for blocking actions. For example, locking a mutex is a blocking simcall.
92 * First it's a simcall because that's obviously a modification of the world. Then, that's a blocking simcall because if
93 * the mutex happens not to be free, the actor is added to a queue of actors in the mutex. Every mutex->unlock() takes
94 * the first actor from the queue, mark it as current owner of the mutex and call actor->simcall_answer() to mark that
95 * this mutex is now unblocked and ready to run again. If the mutex is initially free, the calling actor is unblocked
96 * right away with actor->simcall_answer() once the mutex is marked as locked.
98 * If your code never calls actor->simcall_answer() itself, the actor will never return from its simcall.
100 * The return value is obtained from observer->get_result() if it exists. Otherwise void is returned.
102 template <class F> void simcall_blocking(F&& code, SimcallObserver* observer = nullptr)
104 xbt_assert(not s4u::Actor::is_maestro(), "Cannot execute blocking call in kernel mode");
106 // Pass the code to the maestro which executes it for us and reports the result. We use a std::future which
107 // conveniently handles the success/failure value for us.
108 simgrid::xbt::Result<void> result;
109 simcall_run_blocking([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); }, observer);
110 result.get(); // rethrow stored exception if any
113 template <class F, class Observer>
114 auto simcall_blocking(F&& code, Observer* observer) -> decltype(observer->get_result())
116 simcall_blocking(std::forward<F>(code), static_cast<SimcallObserver*>(observer));
117 return observer->get_result();
119 // compact namespaces are C++17 and this is a public header file so let's stick to C++14
121 } // namespace kernel
122 } // namespace simgrid