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 <type_traits>
20 #include <xbt/function_types.h>
21 #include <xbt/future.hpp>
23 #include <simgrid/simix.h>
25 XBT_PUBLIC(void) simcall_run_kernel(std::function<void()> const& code);
30 /** Execute some code in the kernel/maestro
32 * This can be used to enforce mutual exclusion with other simcall.
33 * More importantly, this enforces a deterministic/reproducible ordering
34 * of the operation with respect to other simcalls.
37 typename std::result_of<F()>::type kernel(F&& code)
39 // If we are in the maestro, we take the fast path and execute the
40 // code directly without simcall mashalling/unmarshalling/dispatch:
41 if (SIMIX_is_maestro())
42 return std::forward<F>(code)();
44 // If we are in the application, pass the code to the maestro which is
45 // executes it for us and reports the result. We use a std::future which
46 // conveniently handles the success/failure value for us.
47 typedef typename std::result_of<F()>::type R;
48 std::promise<R> promise;
49 simcall_run_kernel([&]{
50 xbt_assert(SIMIX_is_maestro(), "Not in maestro");
51 simgrid::xbt::fulfillPromise(promise, std::forward<F>(code));
53 return promise.get_future().get();
59 XBT_PUBLIC_CLASS ContextFactory {
64 explicit ContextFactory(std::string name) : name_(std::move(name)) {}
65 virtual ~ContextFactory();
66 virtual Context* create_context(std::function<void()> code,
67 void_pfn_smxprocess_t cleanup, smx_process_t process) = 0;
69 // Optional methods for attaching main() as a context:
71 /** Creates a context from the current context of execution
73 * This will not work on all implementation of `ContextFactory`.
75 virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_process_t process);
76 virtual Context* create_maestro(std::function<void()> code, smx_process_t process);
78 virtual void run_all() = 0;
79 virtual Context* self();
80 std::string const& name() const
85 void declare_context(void* T, std::size_t size);
87 template<class T, class... Args>
88 T* new_context(Args&&... args)
90 T* context = new T(std::forward<Args>(args)...);
91 this->declare_context(context, sizeof(T));
96 XBT_PUBLIC_CLASS Context {
98 std::function<void()> code_;
99 void_pfn_smxprocess_t cleanup_func_ = nullptr;
100 smx_process_t process_ = nullptr;
104 Context(std::function<void()> code,
105 void_pfn_smxprocess_t cleanup_func,
106 smx_process_t process);
111 bool has_code() const
115 smx_process_t process()
117 return this->process_;
119 void set_cleanup(void_pfn_smxprocess_t cleanup)
121 cleanup_func_ = cleanup;
127 virtual void suspend() = 0;
130 XBT_PUBLIC_CLASS AttachContext : public Context {
133 AttachContext(std::function<void()> code,
134 void_pfn_smxprocess_t cleanup_func,
135 smx_process_t process)
136 : Context(std::move(code), cleanup_func, process)
139 ~AttachContext() override;
141 /** Called by the context when it is ready to give control
144 virtual void attach_start() = 0;
146 /** Called by the context when it has finished its job */
147 virtual void attach_stop() = 0;
150 XBT_PUBLIC(void) set_maestro(std::function<void()> code);
151 XBT_PUBLIC(void) create_maestro(std::function<void()> code);
157 * Type of function that creates a process.
158 * The function must accept the following parameters:
159 * void* process: the process created will be stored there
160 * const char *name: a name for the object. It is for user-level information and can be NULL
161 * xbt_main_func_t code: is a function describing the behavior of the process
162 * void *data: data a pointer to any data one may want to attach to the new object.
163 * sg_host_t host: the location where the new process is executed
164 * int argc, char **argv: parameters passed to code
165 * xbt_dict_t pros: properties
167 typedef smx_process_t (*smx_creation_func_t) (
168 /* name */ const char*,
169 std::function<void()> code,
170 /* userdata */ void*,
171 /* hostname */ const char*,
172 /* kill_time */ double,
173 /* props */ xbt_dict_t,
174 /* auto_restart */ int,
175 /* parent_process */ smx_process_t);
178 XBT_PUBLIC(void) SIMIX_function_register_process_create(smx_creation_func_t function);
180 XBT_PUBLIC(smx_process_t) simcall_process_create(const char *name,
181 std::function<void()> code,
183 const char *hostname,
185 xbt_dict_t properties,
188 XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, std::function<void()> callback);
190 template<class R, class T> inline
191 XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, R(*callback)(T*), T* arg)
193 return SIMIX_timer_set(date, [=](){ callback(arg); });