Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please sonar
[simgrid.git] / include / simgrid / simix.hpp
1 /* Copyright (c) 2007-2010, 2012-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifndef SIMGRID_SIMIX_HPP
8 #define SIMGRID_SIMIX_HPP
9
10 #include <cstddef>
11
12 #include <string>
13 #include <utility>
14 #include <memory>
15 #include <functional>
16
17 #include <xbt/function_types.h>
18 #include <xbt/future.hpp>
19 #include <xbt/functional.hpp>
20 #include <xbt/signal.hpp>
21
22 #include <simgrid/simix.h>
23
24 XBT_PUBLIC(void) simcall_run_kernel(std::function<void()> const& code);
25
26 /** Execute some code in the kernel and block
27  *
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
31  * when needed.
32  *
33  * @ref simix::kernelSync() is a higher level wrapper for this.
34  */
35 XBT_PUBLIC(void) simcall_run_blocking(std::function<void()> const& code);
36
37 template<class F> inline
38 void simcall_run_kernel(F& f)
39 {
40   simcall_run_kernel(std::function<void()>(std::ref(f)));
41 }
42 template<class F> inline
43 void simcall_run_blocking(F& f)
44 {
45   simcall_run_blocking(std::function<void()>(std::ref(f)));
46 }
47
48 namespace simgrid {
49
50 namespace simix {
51
52 /** Execute some code in the kernel/maestro
53  *
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.
57  */
58 template<class F>
59 typename std::result_of<F()>::type kernelImmediate(F&& code)
60 {
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)();
65
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));
74   });
75   return result.get();
76 }
77
78
79 XBT_PUBLIC(void) set_maestro(std::function<void()> code);
80 XBT_PUBLIC(void) create_maestro(std::function<void()> code);
81
82 // What's executed as SIMIX actor code:
83 typedef std::function<void()> ActorCode;
84
85 // Create ActorCode based on argv:
86 typedef std::function<ActorCode(std::vector<std::string> args)> ActorCodeFactory;
87
88 XBT_PUBLIC(void) registerFunction(const char* name, ActorCodeFactory factory);
89
90 /** These functions will be called when we detect a deadlock: any remaining process is locked on an action
91  *
92  * If these functions manage to unlock some of the processes, then the deadlock will be avoided.
93  */
94 extern simgrid::xbt::signal<void()> onDeadlock;
95 }
96 }
97
98 /*
99  * Type of function that creates a process.
100  * The function must accept the following parameters:
101  * void* process: the process created will be stored there
102  * const char *name: a name for the object. It is for user-level information and can be NULL
103  * xbt_main_func_t code: is a function describing the behavior of the process
104  * void *data: data a pointer to any data one may want to attach to the new object.
105  * sg_host_t host: the location where the new process is executed
106  * int argc, char **argv: parameters passed to code
107  * xbt_dict_t pros: properties
108  */
109 typedef smx_actor_t (*smx_creation_func_t) (
110                                       /* name */ const char*,
111                                       std::function<void()> code,
112                                       /* userdata */ void*,
113                                       /* hostname */ sg_host_t,
114                                       /* props */ xbt_dict_t,
115                                       /* parent_process */ smx_actor_t);
116
117 extern "C"
118 XBT_PUBLIC(void) SIMIX_function_register_process_create(smx_creation_func_t function);
119
120 XBT_PUBLIC(smx_actor_t)
121 simcall_process_create(const char* name, std::function<void()> code, void* data, sg_host_t host, xbt_dict_t properties);
122
123 XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, simgrid::xbt::Task<void()> callback);
124
125 template<class F> inline
126 smx_timer_t SIMIX_timer_set(double date, F callback)
127 {
128   return SIMIX_timer_set(date, simgrid::xbt::Task<void()>(std::move(callback)));
129 }
130
131 template<class R, class T> inline
132 smx_timer_t SIMIX_timer_set(double date, R(*callback)(T*), T* arg)
133 {
134   return SIMIX_timer_set(date, [=](){ callback(arg); });
135 }
136
137 #endif