Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move SimcallObserver from simgrid::mc to simgrid::kernel::actor
[simgrid.git] / include / simgrid / simix.hpp
1 /* Copyright (c) 2007-2021. 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/simix.h>
11 #include <xbt/functional.hpp>
12 #include <xbt/promise.hpp>
13 #include <xbt/signal.hpp>
14 #include <xbt/utility.hpp>
15
16 #include <boost/heap/fibonacci_heap.hpp>
17 #include <string>
18 #include <unordered_map>
19
20 XBT_PUBLIC void simcall_run_kernel(std::function<void()> const& code,
21                                    simgrid::kernel::actor::SimcallObserver* observer);
22 XBT_PUBLIC void simcall_run_blocking(std::function<void()> const& code,
23                                      simgrid::kernel::actor::SimcallObserver* observer);
24
25 namespace simgrid {
26 namespace kernel {
27 namespace actor {
28
29 /** Execute some code in kernel context on behalf of the user code.
30  *
31  * Every modification of the environment must be protected this way: every setter, constructor and similar.
32  * Getters don't have to be protected this way.
33  *
34  * This allows deterministic parallel simulation without any locking, even if almost nobody uses parallel simulation in
35  * SimGrid. More interestingly it makes every modification of the simulated world observable by the model-checker,
36  * allowing the whole MC business.
37  *
38  * It is highly inspired from the syscalls in a regular operating system, allowing the user code to get some specific
39  * code executed in the kernel context. But here, there is almost no security involved. Parameters get checked for
40  * finiteness but that's all. The main goal remain to ensure reproducible ordering of uncomparable events (in
41  * [parallel] simulation) and observability of events (in model-checking).
42  *
43  * The code passed as argument is supposed to terminate at the exact same simulated timestamp.
44  * Do not use it if your code may block waiting for a subsequent event, e.g. if you lock a mutex,
45  * you may need to wait for that mutex to be unlocked by its current owner.
46  * Potentially blocking simcall must be issued using simcall_blocking(), right below in this file.
47  */
48 template <class F> typename std::result_of_t<F()> simcall(F&& code, SimcallObserver* observer = nullptr)
49 {
50   // If we are in the maestro, we take the fast path and execute the
51   // code directly without simcall marshalling/unmarshalling/dispatch:
52   if (SIMIX_is_maestro())
53     return std::forward<F>(code)();
54
55   // If we are in the application, pass the code to the maestro which
56   // executes it for us and reports the result. We use a std::future which
57   // conveniently handles the success/failure value for us.
58   using R = typename std::result_of_t<F()>;
59   simgrid::xbt::Result<R> result;
60   simcall_run_kernel([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); }, observer);
61   return result.get();
62 }
63
64 /** Execute some code (that does not return immediately) in kernel context
65  *
66  * This is very similar to simcall() right above, but the calling actor will not get rescheduled until
67  * actor->simcall_answer() is called explicitly.
68  *
69  * This is meant for blocking actions. For example, locking a mutex is a blocking simcall.
70  * First it's a simcall because that's obviously a modification of the world. Then, that's a blocking simcall because if
71  * the mutex happens not to be free, the actor is added to a queue of actors in the mutex. Every mutex->unlock() takes
72  * the first actor from the queue, mark it as current owner of the mutex and call actor->simcall_answer() to mark that
73  * this mutex is now unblocked and ready to run again. If the mutex is initially free, the calling actor is unblocked
74  * right away with actor->simcall_answer() once the mutex is marked as locked.
75  *
76  * If your code never calls actor->simcall_answer() itself, the actor will never return from its simcall.
77  *
78  * The return value is obtained from observer->get_result() if it exists. Otherwise void is returned.
79  */
80 template <class F> void simcall_blocking(F&& code, SimcallObserver* observer = nullptr)
81 {
82   xbt_assert(not SIMIX_is_maestro(), "Cannot execute blocking call in kernel mode");
83
84   // Pass the code to the maestro which executes it for us and reports the result. We use a std::future which
85   // conveniently handles the success/failure value for us.
86   simgrid::xbt::Result<void> result;
87   simcall_run_blocking([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); }, observer);
88   result.get(); // rethrow stored exception if any
89 }
90
91 template <class F, class Observer>
92 auto simcall_blocking(F&& code, Observer* observer) -> decltype(observer->get_result())
93 {
94   simcall_blocking(std::forward<F>(code), static_cast<SimcallObserver*>(observer));
95   return observer->get_result();
96 }
97 } // namespace actor
98 } // namespace kernel
99 } // namespace simgrid
100 namespace simgrid {
101 namespace simix {
102
103 XBT_PUBLIC void unblock(smx_actor_t process);
104
105 inline auto& simix_timers() // avoid static initialization order fiasco
106 {
107   using TimerQelt = std::pair<double, Timer*>;
108   static boost::heap::fibonacci_heap<TimerQelt, boost::heap::compare<xbt::HeapComparator<TimerQelt>>> value;
109   return value;
110 }
111
112 /** @brief Timer datatype */
113 class Timer {
114 public:
115   const double date;
116   std::remove_reference_t<decltype(simix_timers())>::handle_type handle_;
117
118   Timer(double date, simgrid::xbt::Task<void()>&& callback) : date(date), callback(std::move(callback)) {}
119
120   simgrid::xbt::Task<void()> callback;
121   void remove();
122
123   template <class F> static inline Timer* set(double date, F callback)
124   {
125     return set(date, simgrid::xbt::Task<void()>(std::move(callback)));
126   }
127
128   static Timer* set(double date, simgrid::xbt::Task<void()>&& callback);
129   static double next() { return simix_timers().empty() ? -1.0 : simix_timers().top().first; }
130 };
131
132 // In MC mode, the application sends these pointers to the MC
133 void* simix_global_get_actors_addr();
134 void* simix_global_get_dead_actors_addr();
135
136 } // namespace simix
137 } // namespace simgrid
138
139 #endif