1 /* Copyright (c) 2007-2010, 2012-2017. 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>
18 XBT_PUBLIC(void) simcall_run_kernel(std::function<void()> const& code);
20 /** Execute some code in the kernel and block
22 * run_blocking() is a generic blocking simcall. It is given a callback
23 * which is executed immediately in the SimGrid kernel. The callback is
24 * responsible for setting the suitable logic for waking up the process
27 * @ref simix::kernelSync() is a higher level wrapper for this.
29 XBT_PUBLIC(void) simcall_run_blocking(std::function<void()> const& code);
31 template<class F> inline
32 void simcall_run_kernel(F& f)
34 simcall_run_kernel(std::function<void()>(std::ref(f)));
36 template<class F> inline
37 void simcall_run_blocking(F& f)
39 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.
53 typename std::result_of<F()>::type kernelImmediate(F&& code)
55 // If we are in the maestro, we take the fast path and execute the
56 // code directly without simcall mashalling/unmarshalling/dispatch:
57 if (SIMIX_is_maestro())
58 return std::forward<F>(code)();
60 // If we are in the application, pass the code to the maestro which
61 // executes it for us and reports the result. We use a std::future which
62 // conveniently handles the success/failure value for us.
63 typedef typename std::result_of<F()>::type R;
64 simgrid::xbt::Result<R> result;
65 simcall_run_kernel([&]{
66 xbt_assert(SIMIX_is_maestro(), "Not in maestro");
67 simgrid::xbt::fulfillPromise(result, std::forward<F>(code));
72 XBT_PUBLIC(const std::vector<smx_actor_t>&) process_get_runnable();
74 XBT_PUBLIC(void) set_maestro(std::function<void()> code);
75 XBT_PUBLIC(void) create_maestro(std::function<void()> code);
77 // What's executed as SIMIX actor code:
78 typedef std::function<void()> ActorCode;
80 // Create ActorCode based on argv:
81 typedef std::function<ActorCode(std::vector<std::string> args)> ActorCodeFactory;
83 XBT_PUBLIC(void) registerFunction(const char* name, ActorCodeFactory factory);
85 /** These functions will be called when we detect a deadlock: any remaining process is locked on an action
87 * If these functions manage to unlock some of the processes, then the deadlock will be avoided.
89 extern simgrid::xbt::signal<void()> onDeadlock;
94 * Type of function that creates a process.
95 * The function must accept the following parameters:
96 * void* process: the process created will be stored there
97 * const char *name: a name for the object. It is for user-level information and can be NULL
98 * xbt_main_func_t code: is a function describing the behavior of the process
99 * void *data: data a pointer to any data one may want to attach to the new object.
100 * sg_host_t host: the location where the new process is executed
101 * int argc, char **argv: parameters passed to code
102 * std::map<std::string, std::string>* props: properties
104 typedef smx_actor_t (*smx_creation_func_t)(
105 /* name */ const char*, std::function<void()> code,
106 /* userdata */ void*,
107 /* hostname */ sg_host_t,
108 /* props */ std::map<std::string, std::string>*,
109 /* parent_process */ smx_actor_t);
112 XBT_PUBLIC(void) SIMIX_function_register_process_create(smx_creation_func_t function);
114 XBT_PUBLIC(smx_actor_t)
115 simcall_process_create(const char* name, std::function<void()> code, void* data, sg_host_t host,
116 std::map<std::string, std::string>* properties);
118 XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, simgrid::xbt::Task<void()> callback);
120 template<class F> inline
121 smx_timer_t SIMIX_timer_set(double date, F callback)
123 return SIMIX_timer_set(date, simgrid::xbt::Task<void()>(std::move(callback)));
126 template<class R, class T> inline
127 smx_timer_t SIMIX_timer_set(double date, R(*callback)(T*), T* arg)
129 return SIMIX_timer_set(date, [=](){ callback(arg); });