Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
08bb97b713da65e984610051d107eda0c667a39f
[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 namespace simgrid {
20 namespace kernel {
21 namespace actor {
22
23 class Transition {
24 public:
25   virtual bool fireable()
26   {
27     return true;
28   } // whether this transition can currently be taken (if not, it could block the process)
29   virtual bool visible() { return true; } // whether the model-checker should pay any attention to this simcall
30   virtual std::string to_string() = 0;
31   virtual std::string dot_label() = 0;
32 };
33 } // namespace actor
34 } // namespace kernel
35 } // namespace simgrid
36
37 XBT_PUBLIC void simcall_run_kernel(std::function<void()> const& code, simgrid::kernel::actor::Transition* t);
38 XBT_PUBLIC void simcall_run_blocking(std::function<void()> const& code, simgrid::kernel::actor::Transition* t);
39
40 namespace simgrid {
41 namespace kernel {
42 namespace actor {
43
44 /** Execute some code in kernel context on behalf of the user code.
45  *
46  * Every modification of the environment must be protected this way: every setter, constructor and similar.
47  * Getters don't have to be protected this way.
48  *
49  * This allows deterministic parallel simulation without any locking, even if almost nobody uses parallel simulation in
50  * SimGrid. More interestingly it makes every modification of the simulated world observable by the model-checker,
51  * allowing the whole MC business.
52  *
53  * It is highly inspired from the syscalls in a regular operating system, allowing the user code to get some specific
54  * code executed in the kernel context. But here, there is almost no security involved. Parameters get checked for
55  * finitness but that's all. The main goal remain to ensure reproductible ordering of uncomparable events (in [parallel]
56  * simulation) and observability of events (in model-checking).
57  *
58  * The code passed as argument is supposed to terminate at the exact same simulated timestamp.
59  * Do not use it if your code may block waiting for a subsequent event, e.g. if you lock a mutex,
60  * you may need to wait for that mutex to be unlocked by its current owner.
61  * Potentially blocking simcall must be issued using simcall_blocking(), right below in this file.
62  */
63 template <class F> typename std::result_of<F()>::type simcall(F&& code, Transition* t = nullptr)
64 {
65   // If we are in the maestro, we take the fast path and execute the
66   // code directly without simcall mashalling/unmarshalling/dispatch:
67   if (SIMIX_is_maestro())
68     return std::forward<F>(code)();
69
70   // If we are in the application, pass the code to the maestro which
71   // executes it for us and reports the result. We use a std::future which
72   // conveniently handles the success/failure value for us.
73   typedef typename std::result_of<F()>::type R;
74   simgrid::xbt::Result<R> result;
75   simcall_run_kernel([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); }, t);
76   return result.get();
77 }
78
79 /** Execute some code (that does not return immediately) in kernel context
80  *
81  * This is very similar to simcall() right above, but the calling actor will not get rescheduled until
82  * actor->simcall_answer() is called explicitely.
83  *
84  * This is meant for blocking actions. For example, locking a mutex is a blocking simcall.
85  * First it's a simcall because that's obviously a modification of the world. Then, that's a blocking simcall because if
86  * the mutex happens not to be free, the actor is added to a queue of actors in the mutex. Every mutex->unlock() takes
87  * the first actor from the queue, mark it as current owner of the mutex and call actor->simcall_answer() to mark that
88  * this mutex is now unblocked and ready to run again. If the mutex is initially free, the calling actor is unblocked
89  * right away with actor->simcall_answer() once the mutex is marked as locked.
90  *
91  * If your code never calls actor->simcall_answer() itself, the actor will never return from its simcall.
92  */
93 template <class F> typename std::result_of<F()>::type simcall_blocking(F&& code, Transition* t = nullptr)
94 {
95   // If we are in the maestro, we take the fast path and execute the
96   // code directly without simcall mashalling/unmarshalling/dispatch:
97   if (SIMIX_is_maestro())
98     return std::forward<F>(code)();
99
100   // If we are in the application, pass the code to the maestro which
101   // executes it for us and reports the result. We use a std::future which
102   // conveniently handles the success/failure value for us.
103   typedef typename std::result_of<F()>::type R;
104   simgrid::xbt::Result<R> result;
105   simcall_run_blocking([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); }, t);
106   return result.get();
107 }
108 } // namespace actor
109 } // namespace kernel
110 } // namespace simgrid
111 namespace simgrid {
112 namespace simix {
113
114 XBT_ATTRIB_DEPRECATED_v325("Please manifest if you actually need this function")
115     XBT_PUBLIC const std::vector<smx_actor_t>& process_get_runnable();
116
117 // What's executed as SIMIX actor code:
118 typedef std::function<void()> ActorCode;
119
120 // Create an ActorCode based on a std::string
121 typedef std::function<ActorCode(std::vector<std::string> args)> ActorCodeFactory;
122
123 XBT_PUBLIC void register_function(const std::string& name, const ActorCodeFactory& factory);
124
125 typedef std::pair<double, Timer*> TimerQelt;
126 static boost::heap::fibonacci_heap<TimerQelt, boost::heap::compare<xbt::HeapComparator<TimerQelt>>> simix_timers;
127
128 /** @brief Timer datatype */
129 class Timer {
130   double date = 0.0;
131
132 public:
133   decltype(simix_timers)::handle_type handle_;
134
135   Timer(double date, simgrid::xbt::Task<void()>&& callback) : date(date), callback(std::move(callback)) {}
136
137   simgrid::xbt::Task<void()> callback;
138   double get_date() { return date; }
139   void remove();
140
141   template <class F> static inline Timer* set(double date, F callback)
142   {
143     return set(date, simgrid::xbt::Task<void()>(std::move(callback)));
144   }
145
146   template <class R, class T>
147   XBT_ATTRIB_DEPRECATED_v325("Please use a lambda or std::bind") static inline Timer* set(double date,
148                                                                                           R (*callback)(T*), T* arg)
149   {
150     return set(date, std::bind(callback, arg));
151   }
152
153   XBT_ATTRIB_DEPRECATED_v325("Please use a lambda or std::bind") static Timer* set(double date, void (*callback)(void*),
154                                                                                    void* arg)
155   {
156     return set(date, std::bind(callback, arg));
157   }
158   static Timer* set(double date, simgrid::xbt::Task<void()>&& callback);
159   static double next() { return simix_timers.empty() ? -1.0 : simix_timers.top().first; }
160 };
161
162 } // namespace simix
163 } // namespace simgrid
164
165 XBT_PUBLIC smx_actor_t simcall_process_create(const std::string& name, const simgrid::simix::ActorCode& code,
166                                               void* data, sg_host_t host,
167                                               std::unordered_map<std::string, std::string>* properties);
168
169 XBT_ATTRIB_DEPRECATED_v325("Please use simgrid::xbt::Timer::set") XBT_PUBLIC smx_timer_t
170     SIMIX_timer_set(double date, simgrid::xbt::Task<void()>&& callback);
171
172 #endif