1 /* Copyright (c) 2007-2019. 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>
15 #include <boost/heap/fibonacci_heap.hpp>
17 #include <unordered_map>
19 XBT_PUBLIC void simcall_run_kernel(std::function<void()> const& code);
21 /** Execute some code in the kernel and block
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
28 * @ref simix::kernelSync() is a higher level wrapper for this.
30 XBT_PUBLIC void simcall_run_blocking(std::function<void()> const& code);
32 template<class F> inline
33 void simcall_run_kernel(F& f)
35 simcall_run_kernel(std::function<void()>(std::ref(f)));
37 template<class F> inline
38 void simcall_run_blocking(F& f)
40 simcall_run_blocking(std::function<void()>(std::ref(f)));
46 /** Execute some code in the kernel/maestro
48 * This can be used to enforce mutual exclusion with other simcall.
49 * More importantly, this enforces a deterministic/reproducible ordering
50 * of the operation with respect to other simcalls.
52 template <class F> typename std::result_of<F()>::type simcall(F&& code)
54 // If we are in the maestro, we take the fast path and execute the
55 // code directly without simcall mashalling/unmarshalling/dispatch:
56 if (SIMIX_is_maestro())
57 return std::forward<F>(code)();
59 // If we are in the application, pass the code to the maestro which
60 // executes it for us and reports the result. We use a std::future which
61 // conveniently handles the success/failure value for us.
62 typedef typename std::result_of<F()>::type R;
63 simgrid::xbt::Result<R> result;
64 simcall_run_kernel([&result, &code] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); });
68 XBT_ATTRIB_DEPRECATED_v325("Please manifest if you actually need this function")
69 XBT_PUBLIC const std::vector<smx_actor_t>& process_get_runnable();
71 // What's executed as SIMIX actor code:
72 typedef std::function<void()> ActorCode;
74 // Create an ActorCode based on a std::string
75 typedef std::function<ActorCode(std::vector<std::string> args)> ActorCodeFactory;
77 XBT_PUBLIC void register_function(const std::string& name, const ActorCodeFactory& factory);
79 typedef std::pair<double, Timer*> TimerQelt;
80 static boost::heap::fibonacci_heap<TimerQelt, boost::heap::compare<xbt::HeapComparator<TimerQelt>>> simix_timers;
82 /** @brief Timer datatype */
87 decltype(simix_timers)::handle_type handle_;
89 Timer(double date, simgrid::xbt::Task<void()>&& callback) : date(date), callback(std::move(callback)) {}
91 simgrid::xbt::Task<void()> callback;
92 double get_date() { return date; }
95 template <class F> static inline Timer* set(double date, F callback)
97 return set(date, simgrid::xbt::Task<void()>(std::move(callback)));
100 template <class R, class T>
101 XBT_ATTRIB_DEPRECATED_v325("Please use a lambda or std::bind") static inline Timer* set(double date,
102 R (*callback)(T*), T* arg)
104 return set(date, std::bind(callback, arg));
107 XBT_ATTRIB_DEPRECATED_v325("Please use a lambda or std::bind") static Timer* set(double date, void (*callback)(void*),
110 return set(date, std::bind(callback, arg));
112 static Timer* set(double date, simgrid::xbt::Task<void()>&& callback);
113 static double next() { return simix_timers.empty() ? -1.0 : simix_timers.top().first; }
117 } // namespace simgrid
119 XBT_PUBLIC smx_actor_t simcall_process_create(const std::string& name, const simgrid::simix::ActorCode& code,
120 void* data, sg_host_t host,
121 std::unordered_map<std::string, std::string>* properties);
123 XBT_ATTRIB_DEPRECATED_v325("Please use simgrid::xbt::Timer::set") XBT_PUBLIC smx_timer_t
124 SIMIX_timer_set(double date, simgrid::xbt::Task<void()>&& callback);