Logo AND Algorithmique Numérique Distribuée

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