Logo AND Algorithmique Numérique Distribuée

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