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);
27 template<class F> inline
28 void simcall_run_kernel(F& f)
30 simcall_run_kernel(std::function<void()>(std::ref(f)));
36 /** Execute some code in the kernel/maestro
38 * This can be used to enforce mutual exclusion with other simcall.
39 * More importantly, this enforces a deterministic/reproducible ordering
40 * of the operation with respect to other simcalls.
43 typename std::result_of<F()>::type kernel(F&& code)
45 // If we are in the maestro, we take the fast path and execute the
46 // code directly without simcall mashalling/unmarshalling/dispatch:
47 if (SIMIX_is_maestro())
48 return std::forward<F>(code)();
50 // If we are in the application, pass the code to the maestro which
51 // executes it for us and reports the result. We use a std::future which
52 // conveniently handles the success/failure value for us.
53 typedef typename std::result_of<F()>::type R;
54 simgrid::xbt::Result<R> result;
55 simcall_run_kernel([&]{
56 xbt_assert(SIMIX_is_maestro(), "Not in maestro");
57 simgrid::xbt::fulfillPromise(result, std::forward<F>(code));
65 XBT_PUBLIC_CLASS ContextFactory {
70 explicit ContextFactory(std::string name) : name_(std::move(name)) {}
71 virtual ~ContextFactory();
72 virtual Context* create_context(std::function<void()> code,
73 void_pfn_smxprocess_t cleanup, smx_process_t process) = 0;
75 // Optional methods for attaching main() as a context:
77 /** Creates a context from the current context of execution
79 * This will not work on all implementation of `ContextFactory`.
81 virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_process_t process);
82 virtual Context* create_maestro(std::function<void()> code, smx_process_t process);
84 virtual void run_all() = 0;
85 virtual Context* self();
86 std::string const& name() const
91 void declare_context(void* T, std::size_t size);
93 template<class T, class... Args>
94 T* new_context(Args&&... args)
96 T* context = new T(std::forward<Args>(args)...);
97 this->declare_context(context, sizeof(T));
102 XBT_PUBLIC_CLASS Context {
104 std::function<void()> code_;
105 void_pfn_smxprocess_t cleanup_func_ = nullptr;
106 smx_process_t process_ = nullptr;
110 Context(std::function<void()> code,
111 void_pfn_smxprocess_t cleanup_func,
112 smx_process_t process);
117 bool has_code() const
121 smx_process_t process()
123 return this->process_;
125 void set_cleanup(void_pfn_smxprocess_t cleanup)
127 cleanup_func_ = cleanup;
133 virtual void suspend() = 0;
136 XBT_PUBLIC_CLASS AttachContext : public Context {
139 AttachContext(std::function<void()> code,
140 void_pfn_smxprocess_t cleanup_func,
141 smx_process_t process)
142 : Context(std::move(code), cleanup_func, process)
145 ~AttachContext() override;
147 /** Called by the context when it is ready to give control
150 virtual void attach_start() = 0;
152 /** Called by the context when it has finished its job */
153 virtual void attach_stop() = 0;
156 XBT_PUBLIC(void) set_maestro(std::function<void()> code);
157 XBT_PUBLIC(void) create_maestro(std::function<void()> code);
163 * Type of function that creates a process.
164 * The function must accept the following parameters:
165 * void* process: the process created will be stored there
166 * const char *name: a name for the object. It is for user-level information and can be NULL
167 * xbt_main_func_t code: is a function describing the behavior of the process
168 * void *data: data a pointer to any data one may want to attach to the new object.
169 * sg_host_t host: the location where the new process is executed
170 * int argc, char **argv: parameters passed to code
171 * xbt_dict_t pros: properties
173 typedef smx_process_t (*smx_creation_func_t) (
174 /* name */ const char*,
175 std::function<void()> code,
176 /* userdata */ void*,
177 /* hostname */ const char*,
178 /* kill_time */ double,
179 /* props */ xbt_dict_t,
180 /* auto_restart */ int,
181 /* parent_process */ smx_process_t);
184 XBT_PUBLIC(void) SIMIX_function_register_process_create(smx_creation_func_t function);
186 XBT_PUBLIC(smx_process_t) simcall_process_create(const char *name,
187 std::function<void()> code,
189 const char *hostname,
191 xbt_dict_t properties,
194 XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, std::function<void()> callback);
196 template<class R, class T> inline
197 XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, R(*callback)(T*), T* arg)
199 return SIMIX_timer_set(date, [=](){ callback(arg); });