1 /* Copyright (c) 2007-2020. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #ifndef SIMGRID_SIMIX_HPP
8 #define SIMGRID_SIMIX_HPP
10 #include <simgrid/simix.h>
11 #include <xbt/functional.hpp>
12 #include <xbt/future.hpp>
13 #include <xbt/signal.hpp>
14 #include <xbt/utility.hpp>
16 #include <boost/heap/fibonacci_heap.hpp>
18 #include <unordered_map>
20 XBT_PUBLIC void simcall_run_kernel(std::function<void()> const& code, simgrid::mc::SimcallInspector* t);
21 XBT_PUBLIC void simcall_run_blocking(std::function<void()> const& code, simgrid::mc::SimcallInspector* t);
27 /** Execute some code in kernel context on behalf of the user code.
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.
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.
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).
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.
46 template <class F> typename std::result_of<F()>::type simcall(F&& code, mc::SimcallInspector* t = nullptr)
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 (SIMIX_is_maestro())
51 return std::forward<F>(code)();
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<F()>::type;
57 simgrid::xbt::Result<R> result;
58 simcall_run_kernel([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); }, t);
62 /** Execute some code (that does not return immediately) in kernel context
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.
67 * Since the return value does not come from the lambda directly, its type cannot be guessed automatically and must
68 * be provided as template parameter.
70 * This is meant for blocking actions. For example, locking a mutex is a blocking simcall.
71 * First it's a simcall because that's obviously a modification of the world. Then, that's a blocking simcall because if
72 * the mutex happens not to be free, the actor is added to a queue of actors in the mutex. Every mutex->unlock() takes
73 * the first actor from the queue, mark it as current owner of the mutex and call actor->simcall_answer() to mark that
74 * this mutex is now unblocked and ready to run again. If the mutex is initially free, the calling actor is unblocked
75 * right away with actor->simcall_answer() once the mutex is marked as locked.
77 * If your code never calls actor->simcall_answer() itself, the actor will never return from its simcall.
79 template <class R, class F> R simcall_blocking(F&& code, mc::SimcallInspector* t = nullptr)
81 // If we are in the maestro, we take the fast path and execute the
82 // code directly without simcall marshalling/unmarshalling/dispatch:
83 if (SIMIX_is_maestro())
84 return std::forward<F>(code)();
86 // If we are in the application, pass the code to the maestro which
87 // executes it for us and reports the result. We use a std::future which
88 // conveniently handles the success/failure value for us.
89 simgrid::xbt::Result<R> result;
90 simcall_run_blocking([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); }, t);
95 } // namespace simgrid
99 using TimerQelt = std::pair<double, Timer*>;
100 static boost::heap::fibonacci_heap<TimerQelt, boost::heap::compare<xbt::HeapComparator<TimerQelt>>> simix_timers;
102 /** @brief Timer datatype */
107 decltype(simix_timers)::handle_type handle_;
109 Timer(double date, simgrid::xbt::Task<void()>&& callback) : date(date), callback(std::move(callback)) {}
111 simgrid::xbt::Task<void()> callback;
112 double get_date() const { return date; }
115 template <class F> static inline Timer* set(double date, F callback)
117 return set(date, simgrid::xbt::Task<void()>(std::move(callback)));
120 static Timer* set(double date, simgrid::xbt::Task<void()>&& callback);
121 static double next() { return simix_timers.empty() ? -1.0 : simix_timers.top().first; }
125 } // namespace simgrid