Logo AND Algorithmique Numérique Distribuée

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