Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[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 <simgrid/simix.h>
12 #include <xbt/promise.hpp>
13 #include <xbt/signal.hpp>
14
15 #include <string>
16 #include <unordered_map>
17
18 XBT_PUBLIC void simcall_run_kernel(std::function<void()> const& code,
19                                    simgrid::kernel::actor::SimcallObserver* observer);
20 XBT_PUBLIC void simcall_run_blocking(std::function<void()> const& code,
21                                      simgrid::kernel::actor::SimcallObserver* observer);
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.
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(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_kernel([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); }, observer);
59   return result.get();
60 }
61
62 /** Execute some code (that does not return immediately) in kernel context
63  *
64  * This is very similar to simcall() right above, but the calling actor will not get rescheduled until
65  * actor->simcall_answer() is called explicitly.
66  *
67  * This is meant for blocking actions. For example, locking a mutex is a blocking simcall.
68  * First it's a simcall because that's obviously a modification of the world. Then, that's a blocking simcall because if
69  * the mutex happens not to be free, the actor is added to a queue of actors in the mutex. Every mutex->unlock() takes
70  * the first actor from the queue, mark it as current owner of the mutex and call actor->simcall_answer() to mark that
71  * this mutex is now unblocked and ready to run again. If the mutex is initially free, the calling actor is unblocked
72  * right away with actor->simcall_answer() once the mutex is marked as locked.
73  *
74  * If your code never calls actor->simcall_answer() itself, the actor will never return from its simcall.
75  *
76  * The return value is obtained from observer->get_result() if it exists. Otherwise void is returned.
77  */
78 template <class F> void simcall_blocking(F&& code, SimcallObserver* observer = nullptr)
79 {
80   xbt_assert(not s4u::Actor::is_maestro(), "Cannot execute blocking call in kernel mode");
81
82   // Pass the code to the maestro which executes it for us and reports the result. We use a std::future which
83   // conveniently handles the success/failure value for us.
84   simgrid::xbt::Result<void> result;
85   simcall_run_blocking([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); }, observer);
86   result.get(); // rethrow stored exception if any
87 }
88
89 template <class F, class Observer>
90 auto simcall_blocking(F&& code, Observer* observer) -> decltype(observer->get_result())
91 {
92   simcall_blocking(std::forward<F>(code), static_cast<SimcallObserver*>(observer));
93   return observer->get_result();
94 }
95 } // namespace actor
96 } // namespace kernel
97
98 namespace simix {
99
100 XBT_PUBLIC void unblock(smx_actor_t process);
101
102 } // namespace simix
103 } // namespace simgrid
104
105 #endif