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 <simgrid/simix.h>
23 XBT_PUBLIC(void) simcall_run_kernel(std::function<void()> const& code);
28 /** Fulfill a promise by executing a given code */
29 template<class R, class F>
30 void fulfill_promise(std::promise<R>& promise, F&& code)
33 promise.set_value(std::forward<F>(code)());
36 promise.set_exception(std::current_exception());
40 /** Fulfill a promise by executing a given code
42 * This is a special version for `std::promise<void>` because the default
43 * version does not compile in this case.
46 void fulfill_promise(std::promise<void>& promise, F&& code)
49 std::forward<F>(code)();
53 promise.set_exception(std::current_exception());
57 /** Execute some code in the kernel/maestro
59 * This can be used to enforce mutual exclusion with other simcall.
60 * More importantly, this enforces a deterministic/reproducible ordering
61 * of the operation with respect to other simcalls.
64 typename std::result_of<F()>::type kernel(F&& code)
66 // If we are in the maestro, we take the fast path and execute the
67 // code directly without simcall mashalling/unmarshalling/dispatch:
68 if (SIMIX_is_maestro())
69 return std::forward<F>(code)();
71 // If we are in the application, pass the code to the maestro which is
72 // executes it for us and reports the result. We use a std::future which
73 // conveniently handles the success/failure value for us.
74 typedef typename std::result_of<F()>::type R;
75 std::promise<R> promise;
76 simcall_run_kernel([&]{
77 xbt_assert(SIMIX_is_maestro(), "Not in maestro");
78 fulfill_promise(promise, std::forward<F>(code));
80 return promise.get_future().get();
86 char** argv_ = nullptr;
92 void assign(int argc, const char*const* argv)
95 char** new_argv = xbt_new(char*,argc + 1);
96 for (int i = 0; i < argc; i++)
97 new_argv[i] = xbt_strdup(argv[i]);
98 new_argv[argc] = nullptr;
100 this->argv_ = new_argv;
102 args(int argc, const char*const* argv)
104 this->assign(argc, argv);
107 char** to_argv() const
109 const int argc = argc_;
110 char** argv = xbt_new(char*, argc + 1);
111 for (int i=0; i< argc; i++)
112 argv[i] = xbt_strdup(argv_[i]);
113 argv[argc] = nullptr;
120 for (int i = 0; i < this->argc_; i++)
121 free(this->argv_[i]);
124 this->argv_ = nullptr;
129 args(args const& that)
131 this->assign(that.argc(), that.argv());
133 args& operator=(args const& that)
135 this->assign(that.argc(), that.argv());
140 args(args&& that) : argc_(that.argc_), argv_(that.argv_)
143 that.argv_ = nullptr;
145 args& operator=(args&& that)
147 this->argc_ = that.argc_;
148 this->argv_ = that.argv_;
150 that.argv_ = nullptr;
154 int argc() const { return argc_; }
155 char** argv() { return argv_; }
156 const char*const* argv() const { return argv_; }
157 char* operator[](std::size_t i) { return argv_[i]; }
160 inline std::function<void()> wrap_main(
161 xbt_main_func_t code, std::shared_ptr<simgrid::simix::args> args)
165 code(args->argc(), args->argv());
168 else return std::function<void()>();
172 std::function<void()> wrap_main(xbt_main_func_t code, simgrid::simix::args args)
175 return wrap_main(code, std::unique_ptr<simgrid::simix::args>(
176 new simgrid::simix::args(std::move(args))));
177 else return std::function<void()>();
181 std::function<void()> wrap_main(xbt_main_func_t code, int argc, const char*const* argv)
183 return wrap_main(code, simgrid::simix::args(argc, argv));
187 class ContextFactory;
189 XBT_PUBLIC_CLASS ContextFactory {
194 ContextFactory(std::string name) : name_(std::move(name)) {}
195 virtual ~ContextFactory();
196 virtual Context* create_context(std::function<void()> code,
197 void_pfn_smxprocess_t cleanup, smx_process_t process) = 0;
199 // Optional methods for attaching main() as a context:
201 /** Creates a context from the current context of execution
203 * This will not work on all implementation of `ContextFactory`.
205 virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_process_t process);
206 virtual Context* create_maestro(std::function<void()> code, smx_process_t process);
208 virtual void run_all() = 0;
209 virtual Context* self();
210 std::string const& name() const
215 void declare_context(void* T, std::size_t size);
217 template<class T, class... Args>
218 T* new_context(Args&&... args)
220 T* context = new T(std::forward<Args>(args)...);
221 this->declare_context(context, sizeof(T));
226 XBT_PUBLIC_CLASS Context {
228 std::function<void()> code_;
229 void_pfn_smxprocess_t cleanup_func_ = nullptr;
230 smx_process_t process_ = nullptr;
234 Context(std::function<void()> code,
235 void_pfn_smxprocess_t cleanup_func,
236 smx_process_t process);
241 bool has_code() const
245 smx_process_t process()
247 return this->process_;
249 void set_cleanup(void_pfn_smxprocess_t cleanup)
251 cleanup_func_ = cleanup;
257 virtual void suspend() = 0;
260 XBT_PUBLIC_CLASS AttachContext : public Context {
263 AttachContext(std::function<void()> code,
264 void_pfn_smxprocess_t cleanup_func,
265 smx_process_t process)
266 : Context(std::move(code), cleanup_func, process)
271 /** Called by the context when it is ready to give control
274 virtual void attach_start() = 0;
276 /** Called by the context when it has finished its job */
277 virtual void attach_stop() = 0;
280 XBT_PUBLIC(void) set_maestro(std::function<void()> code);
281 XBT_PUBLIC(void) create_maestro(std::function<void()> code);
287 * Type of function that creates a process.
288 * The function must accept the following parameters:
289 * void* process: the process created will be stored there
290 * const char *name: a name for the object. It is for user-level information and can be NULL
291 * xbt_main_func_t code: is a function describing the behavior of the process
292 * void *data: data a pointer to any data one may want to attach to the new object.
293 * sg_host_t host: the location where the new process is executed
294 * int argc, char **argv: parameters passed to code
295 * xbt_dict_t pros: properties
297 typedef smx_process_t (*smx_creation_func_t) (
298 /* name */ const char*,
299 std::function<void()> code,
300 /* userdata */ void*,
301 /* hostname */ const char*,
302 /* kill_time */ double,
303 /* props */ xbt_dict_t,
304 /* auto_restart */ int,
305 /* parent_process */ smx_process_t);
308 XBT_PUBLIC(void) SIMIX_function_register_process_create(smx_creation_func_t function);
310 XBT_PUBLIC(smx_process_t) simcall_process_create(const char *name,
311 std::function<void()> code,
313 const char *hostname,
315 xbt_dict_t properties,