Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branches 'master' and 'master' of github.com:simgrid/simgrid
[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 <xbt/function_types.h>
11 #include <xbt/future.hpp>
12 #include <xbt/functional.hpp>
13 #include <xbt/signal.hpp>
14
15 #include <simgrid/simix.h>
16
17 XBT_PUBLIC(void) simcall_run_kernel(std::function<void()> const& code);
18
19 /** Execute some code in the kernel and block
20  *
21  * run_blocking() is a generic blocking simcall. It is given a callback
22  * which is executed immediately in the SimGrid kernel. The callback is
23  * responsible for setting the suitable logic for waking up the process
24  * when needed.
25  *
26  * @ref simix::kernelSync() is a higher level wrapper for this.
27  */
28 XBT_PUBLIC(void) simcall_run_blocking(std::function<void()> const& code);
29
30 template<class F> inline
31 void simcall_run_kernel(F& f)
32 {
33   simcall_run_kernel(std::function<void()>(std::ref(f)));
34 }
35 template<class F> inline
36 void simcall_run_blocking(F& f)
37 {
38   simcall_run_blocking(std::function<void()>(std::ref(f)));
39 }
40
41 namespace simgrid {
42
43 namespace simix {
44
45 /** Execute some code in the kernel/maestro
46  *
47  *  This can be used to enforce mutual exclusion with other simcall.
48  *  More importantly, this enforces a deterministic/reproducible ordering
49  *  of the operation with respect to other simcalls.
50  */
51 template<class F>
52 typename std::result_of<F()>::type kernelImmediate(F&& code)
53 {
54   // If we are in the maestro, we take the fast path and execute the
55   // code directly without simcall mashalling/unmarshalling/dispatch:
56   if (SIMIX_is_maestro())
57     return std::forward<F>(code)();
58
59   // If we are in the application, pass the code to the maestro which
60   // executes it for us and reports the result. We use a std::future which
61   // conveniently handles the success/failure value for us.
62   typedef typename std::result_of<F()>::type R;
63   simgrid::xbt::Result<R> result;
64   simcall_run_kernel([&]{
65     xbt_assert(SIMIX_is_maestro(), "Not in maestro");
66     simgrid::xbt::fulfillPromise(result, std::forward<F>(code));
67   });
68   return result.get();
69 }
70
71
72 XBT_PUBLIC(void) set_maestro(std::function<void()> code);
73 XBT_PUBLIC(void) create_maestro(std::function<void()> code);
74
75 // What's executed as SIMIX actor code:
76 typedef std::function<void()> ActorCode;
77
78 // Create ActorCode based on argv:
79 typedef std::function<ActorCode(std::vector<std::string> args)> ActorCodeFactory;
80
81 XBT_PUBLIC(void) registerFunction(const char* name, ActorCodeFactory factory);
82
83 /** These functions will be called when we detect a deadlock: any remaining process is locked on an action
84  *
85  * If these functions manage to unlock some of the processes, then the deadlock will be avoided.
86  */
87 extern simgrid::xbt::signal<void()> onDeadlock;
88 }
89 }
90
91 /*
92  * Type of function that creates a process.
93  * The function must accept the following parameters:
94  * void* process: the process created will be stored there
95  * const char *name: a name for the object. It is for user-level information and can be NULL
96  * xbt_main_func_t code: is a function describing the behavior of the process
97  * void *data: data a pointer to any data one may want to attach to the new object.
98  * sg_host_t host: the location where the new process is executed
99  * int argc, char **argv: parameters passed to code
100  * xbt_dict_t pros: properties
101  */
102 typedef smx_actor_t (*smx_creation_func_t) (
103                                       /* name */ const char*,
104                                       std::function<void()> code,
105                                       /* userdata */ void*,
106                                       /* hostname */ sg_host_t,
107                                       /* props */ xbt_dict_t,
108                                       /* parent_process */ smx_actor_t);
109
110 extern "C"
111 XBT_PUBLIC(void) SIMIX_function_register_process_create(smx_creation_func_t function);
112
113 XBT_PUBLIC(smx_actor_t)
114 simcall_process_create(const char* name, std::function<void()> code, void* data, sg_host_t host, xbt_dict_t properties);
115
116 XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, simgrid::xbt::Task<void()> callback);
117
118 template<class F> inline
119 smx_timer_t SIMIX_timer_set(double date, F callback)
120 {
121   return SIMIX_timer_set(date, simgrid::xbt::Task<void()>(std::move(callback)));
122 }
123
124 template<class R, class T> inline
125 smx_timer_t SIMIX_timer_set(double date, R(*callback)(T*), T* arg)
126 {
127   return SIMIX_timer_set(date, [=](){ callback(arg); });
128 }
129
130 #endif