Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
shy attempt at simplifying the simcall mechanism
[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);
20
21 /** Execute some code in the kernel and block
22  *
23  * run_blocking() is a generic blocking simcall. It is given a callback
24  * which is executed immediately in the SimGrid kernel. The callback is
25  * responsible for setting the suitable logic for waking up the process
26  * when needed.
27  *
28  * @ref simix::kernelSync() is a higher level wrapper for this.
29  */
30 XBT_PUBLIC void simcall_run_blocking(std::function<void()> const& code);
31
32 namespace simgrid {
33 namespace simix {
34
35 /** Execute some code in the kernel/maestro
36  *
37  *  This can be used to enforce mutual exclusion with other simcall.
38  *  More importantly, this enforces a deterministic/reproducible ordering
39  *  of the operation with respect to other simcalls.
40  */
41 template <class F> typename std::result_of<F()>::type simcall(F&& code)
42 {
43   // If we are in the maestro, we take the fast path and execute the
44   // code directly without simcall mashalling/unmarshalling/dispatch:
45   if (SIMIX_is_maestro())
46     return std::forward<F>(code)();
47
48   // If we are in the application, pass the code to the maestro which
49   // executes it for us and reports the result. We use a std::future which
50   // conveniently handles the success/failure value for us.
51   typedef typename std::result_of<F()>::type R;
52   simgrid::xbt::Result<R> result;
53   simcall_run_kernel([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); });
54   return result.get();
55 }
56
57 XBT_ATTRIB_DEPRECATED_v325("Please manifest if you actually need this function")
58     XBT_PUBLIC const std::vector<smx_actor_t>& process_get_runnable();
59
60 // What's executed as SIMIX actor code:
61 typedef std::function<void()> ActorCode;
62
63 // Create an ActorCode based on a std::string
64 typedef std::function<ActorCode(std::vector<std::string> args)> ActorCodeFactory;
65
66 XBT_PUBLIC void register_function(const std::string& name, const ActorCodeFactory& factory);
67
68 typedef std::pair<double, Timer*> TimerQelt;
69 static boost::heap::fibonacci_heap<TimerQelt, boost::heap::compare<xbt::HeapComparator<TimerQelt>>> simix_timers;
70
71 /** @brief Timer datatype */
72 class Timer {
73   double date = 0.0;
74
75 public:
76   decltype(simix_timers)::handle_type handle_;
77
78   Timer(double date, simgrid::xbt::Task<void()>&& callback) : date(date), callback(std::move(callback)) {}
79
80   simgrid::xbt::Task<void()> callback;
81   double get_date() { return date; }
82   void remove();
83
84   template <class F> static inline Timer* set(double date, F callback)
85   {
86     return set(date, simgrid::xbt::Task<void()>(std::move(callback)));
87   }
88
89   template <class R, class T>
90   XBT_ATTRIB_DEPRECATED_v325("Please use a lambda or std::bind") static inline Timer* set(double date,
91                                                                                           R (*callback)(T*), T* arg)
92   {
93     return set(date, std::bind(callback, arg));
94   }
95
96   XBT_ATTRIB_DEPRECATED_v325("Please use a lambda or std::bind") static Timer* set(double date, void (*callback)(void*),
97                                                                                    void* arg)
98   {
99     return set(date, std::bind(callback, arg));
100   }
101   static Timer* set(double date, simgrid::xbt::Task<void()>&& callback);
102   static double next() { return simix_timers.empty() ? -1.0 : simix_timers.top().first; }
103 };
104
105 } // namespace simix
106 } // namespace simgrid
107
108 XBT_PUBLIC smx_actor_t simcall_process_create(const std::string& name, const simgrid::simix::ActorCode& code,
109                                               void* data, sg_host_t host,
110                                               std::unordered_map<std::string, std::string>* properties);
111
112 XBT_ATTRIB_DEPRECATED_v325("Please use simgrid::xbt::Timer::set") XBT_PUBLIC smx_timer_t
113     SIMIX_timer_set(double date, simgrid::xbt::Task<void()>&& callback);
114
115 #endif