Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use CRTP to factor refcounting across activity types
[simgrid.git] / include / simgrid / simix.hpp
1 /* Copyright (c) 2007-2019. 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/future.hpp>
13 #include <xbt/signal.hpp>
14
15 #include <boost/heap/fibonacci_heap.hpp>
16 #include <string>
17 #include <unordered_map>
18
19 XBT_PUBLIC void simcall_run_kernel(std::function<void()> const& code, simgrid::mc::SimcallInspector* t);
20 XBT_PUBLIC void simcall_run_blocking(std::function<void()> const& code, simgrid::mc::SimcallInspector* t);
21
22 namespace simgrid {
23 namespace kernel {
24 namespace 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.
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  * finitness but that's all. The main goal remain to ensure reproductible ordering of uncomparable events (in [parallel]
38  * 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<F()>::type simcall(F&& code, mc::SimcallInspector* t = nullptr)
46 {
47   // If we are in the maestro, we take the fast path and execute the
48   // code directly without simcall mashalling/unmarshalling/dispatch:
49   if (SIMIX_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   typedef typename std::result_of<F()>::type R;
56   simgrid::xbt::Result<R> result;
57   simcall_run_kernel([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); }, t);
58   return result.get();
59 }
60
61 /** Execute some code (that does not return immediately) in kernel context
62  *
63  * This is very similar to simcall() right above, but the calling actor will not get rescheduled until
64  * actor->simcall_answer() is called explicitely.
65  *
66  * Since the return value does not come from the lambda directly, its type cannot be guessed automatically and must
67  * be provided as template parameter.
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 template <class R, class F> R simcall_blocking(F&& code, mc::SimcallInspector* t = nullptr)
79 {
80   // If we are in the maestro, we take the fast path and execute the
81   // code directly without simcall mashalling/unmarshalling/dispatch:
82   if (SIMIX_is_maestro())
83     return std::forward<F>(code)();
84
85   // If we are in the application, pass the code to the maestro which
86   // executes it for us and reports the result. We use a std::future which
87   // conveniently handles the success/failure value for us.
88   simgrid::xbt::Result<R> result;
89   simcall_run_blocking([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); }, t);
90   return result.get();
91 }
92 } // namespace actor
93 } // namespace kernel
94 } // namespace simgrid
95 namespace simgrid {
96 namespace simix {
97
98 XBT_ATTRIB_DEPRECATED_v325("Please manifest if you actually need this function")
99     XBT_PUBLIC const std::vector<smx_actor_t>& process_get_runnable();
100
101 // What's executed as SIMIX actor code:
102 typedef std::function<void()> ActorCode;
103
104 // Create an ActorCode based on a std::string
105 typedef std::function<ActorCode(std::vector<std::string> args)> ActorCodeFactory;
106
107 XBT_PUBLIC void register_function(const std::string& name, const ActorCodeFactory& factory);
108
109 typedef std::pair<double, Timer*> TimerQelt;
110 static boost::heap::fibonacci_heap<TimerQelt, boost::heap::compare<xbt::HeapComparator<TimerQelt>>> simix_timers;
111
112 /** @brief Timer datatype */
113 class Timer {
114   double date = 0.0;
115
116 public:
117   decltype(simix_timers)::handle_type handle_;
118
119   Timer(double date, simgrid::xbt::Task<void()>&& callback) : date(date), callback(std::move(callback)) {}
120
121   simgrid::xbt::Task<void()> callback;
122   double get_date() { return date; }
123   void remove();
124
125   template <class F> static inline Timer* set(double date, F callback)
126   {
127     return set(date, simgrid::xbt::Task<void()>(std::move(callback)));
128   }
129
130   template <class R, class T>
131   XBT_ATTRIB_DEPRECATED_v325("Please use a lambda or std::bind") static inline Timer* set(double date,
132                                                                                           R (*callback)(T*), T* arg)
133   {
134     return set(date, std::bind(callback, arg));
135   }
136
137   XBT_ATTRIB_DEPRECATED_v325("Please use a lambda or std::bind") static Timer* set(double date, void (*callback)(void*),
138                                                                                    void* arg)
139   {
140     return set(date, std::bind(callback, arg));
141   }
142   static Timer* set(double date, simgrid::xbt::Task<void()>&& callback);
143   static double next() { return simix_timers.empty() ? -1.0 : simix_timers.top().first; }
144 };
145
146 } // namespace simix
147 } // namespace simgrid
148
149 XBT_PUBLIC smx_actor_t simcall_process_create(const std::string& name, const simgrid::simix::ActorCode& code,
150                                               void* data, sg_host_t host,
151                                               std::unordered_map<std::string, std::string>* properties);
152
153 XBT_ATTRIB_DEPRECATED_v325("Please use simgrid::xbt::Timer::set") XBT_PUBLIC smx_timer_t
154     SIMIX_timer_set(double date, simgrid::xbt::Task<void()>&& callback);
155
156 #endif