1 /* Copyright (c) 2007-2010, 2012-2015. 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
18 #include <xbt/function_types.h>
19 #include <xbt/future.hpp>
20 #include <xbt/functional.hpp>
22 #include <simgrid/simix.h>
24 XBT_PUBLIC(void) simcall_run_kernel(std::function<void()> const& code);
26 /** Execute some code in the kernel and block
28 * run_blocking() is a generic blocking simcall. It is given a callback
29 * which is executed immediately in the SimGrid kernel. The callback is
30 * responsible for setting the suitable logic for waking up the process
33 * @ref simix::kernelSync() is a higher level wrapper for this.
35 XBT_PUBLIC(void) simcall_run_blocking(std::function<void()> const& code);
37 template<class F> inline
38 void simcall_run_kernel(F& f)
40 simcall_run_kernel(std::function<void()>(std::ref(f)));
42 template<class F> inline
43 void simcall_run_blocking(F& f)
45 simcall_run_blocking(std::function<void()>(std::ref(f)));
52 /** Execute some code in the kernel/maestro
54 * This can be used to enforce mutual exclusion with other simcall.
55 * More importantly, this enforces a deterministic/reproducible ordering
56 * of the operation with respect to other simcalls.
59 typename std::result_of<F()>::type kernelImmediate(F&& code)
61 // If we are in the maestro, we take the fast path and execute the
62 // code directly without simcall mashalling/unmarshalling/dispatch:
63 if (SIMIX_is_maestro())
64 return std::forward<F>(code)();
66 // If we are in the application, pass the code to the maestro which
67 // executes it for us and reports the result. We use a std::future which
68 // conveniently handles the success/failure value for us.
69 typedef typename std::result_of<F()>::type R;
70 simgrid::xbt::Result<R> result;
71 simcall_run_kernel([&]{
72 xbt_assert(SIMIX_is_maestro(), "Not in maestro");
73 simgrid::xbt::fulfillPromise(result, std::forward<F>(code));
81 XBT_PUBLIC_CLASS ContextFactory {
86 explicit ContextFactory(std::string name) : name_(std::move(name)) {}
87 virtual ~ContextFactory();
88 virtual Context* create_context(std::function<void()> code,
89 void_pfn_smxprocess_t cleanup, smx_process_t process) = 0;
91 // Optional methods for attaching main() as a context:
93 /** Creates a context from the current context of execution
95 * This will not work on all implementation of `ContextFactory`.
97 virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_process_t process);
98 virtual Context* create_maestro(std::function<void()> code, smx_process_t process);
100 virtual void run_all() = 0;
101 virtual Context* self();
102 std::string const& name() const
107 void declare_context(void* T, std::size_t size);
109 template<class T, class... Args>
110 T* new_context(Args&&... args)
112 T* context = new T(std::forward<Args>(args)...);
113 this->declare_context(context, sizeof(T));
118 XBT_PUBLIC_CLASS Context {
120 std::function<void()> code_;
121 void_pfn_smxprocess_t cleanup_func_ = nullptr;
122 smx_process_t process_ = nullptr;
126 Context(std::function<void()> code,
127 void_pfn_smxprocess_t cleanup_func,
128 smx_process_t process);
133 bool has_code() const
137 smx_process_t process()
139 return this->process_;
141 void set_cleanup(void_pfn_smxprocess_t cleanup)
143 cleanup_func_ = cleanup;
149 virtual void suspend() = 0;
152 XBT_PUBLIC_CLASS AttachContext : public Context {
155 AttachContext(std::function<void()> code,
156 void_pfn_smxprocess_t cleanup_func,
157 smx_process_t process)
158 : Context(std::move(code), cleanup_func, process)
161 ~AttachContext() override;
163 /** Called by the context when it is ready to give control
166 virtual void attach_start() = 0;
168 /** Called by the context when it has finished its job */
169 virtual void attach_stop() = 0;
172 XBT_PUBLIC(void) set_maestro(std::function<void()> code);
173 XBT_PUBLIC(void) create_maestro(std::function<void()> code);
175 // What's executed as SIMIX actor code:
176 typedef std::function<void()> ActorCode;
178 // Create ActorCode based on argv:
179 typedef std::function<ActorCode(simgrid::xbt::args args)> ActorCodeFactory;
181 XBT_PUBLIC(void) registerFunction(const char* name, ActorCodeFactory factory);
187 * Type of function that creates a process.
188 * The function must accept the following parameters:
189 * void* process: the process created will be stored there
190 * const char *name: a name for the object. It is for user-level information and can be NULL
191 * xbt_main_func_t code: is a function describing the behavior of the process
192 * void *data: data a pointer to any data one may want to attach to the new object.
193 * sg_host_t host: the location where the new process is executed
194 * int argc, char **argv: parameters passed to code
195 * xbt_dict_t pros: properties
197 typedef smx_process_t (*smx_creation_func_t) (
198 /* name */ const char*,
199 std::function<void()> code,
200 /* userdata */ void*,
201 /* hostname */ const char*,
202 /* kill_time */ double,
203 /* props */ xbt_dict_t,
204 /* auto_restart */ int,
205 /* parent_process */ smx_process_t);
208 XBT_PUBLIC(void) SIMIX_function_register_process_create(smx_creation_func_t function);
210 XBT_PUBLIC(smx_process_t) simcall_process_create(const char *name,
211 std::function<void()> code,
213 const char *hostname,
215 xbt_dict_t properties,
218 XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, std::packaged_task<void()> callback);
220 template<class F> inline
221 XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, F callback)
223 return SIMIX_timer_set(date, std::packaged_task<void()>(std::move(callback)));
226 template<class R, class T> inline
227 XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, R(*callback)(T*), T* arg)
229 return SIMIX_timer_set(date, [=](){ callback(arg); });