Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
snake_case xbt/future.hpp
[simgrid.git] / include / simgrid / simix.hpp
1 /* Copyright (c) 2007-2018. 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 <simgrid/simix.h>
11 #include <xbt/functional.hpp>
12 #include <xbt/future.hpp>
13 #include <xbt/signal.hpp>
14
15 #include <string>
16 #include <unordered_map>
17
18 XBT_PUBLIC void simcall_run_kernel(std::function<void()> const& code);
19
20 /** Execute some code in the kernel and block
21  *
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
25  * when needed.
26  *
27  * @ref simix::kernelSync() is a higher level wrapper for this.
28  */
29 XBT_PUBLIC void simcall_run_blocking(std::function<void()> const& code);
30
31 template<class F> inline
32 void simcall_run_kernel(F& f)
33 {
34   simcall_run_kernel(std::function<void()>(std::ref(f)));
35 }
36 template<class F> inline
37 void simcall_run_blocking(F& f)
38 {
39   simcall_run_blocking(std::function<void()>(std::ref(f)));
40 }
41
42 namespace simgrid {
43
44 namespace simix {
45
46 /** Execute some code in the kernel/maestro
47  *
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.
51  */
52 template <class F> typename std::result_of<F()>::type simcall(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([&] { simgrid::xbt::fulfill_promise(result, std::forward<F>(code)); });
65   return result.get();
66 }
67
68 XBT_PUBLIC const std::vector<smx_actor_t>& process_get_runnable();
69
70 // What's executed as SIMIX actor code:
71 typedef std::function<void()> ActorCode;
72
73 // Create ActorCode based on argv:
74 typedef std::function<ActorCode(std::vector<std::string> args)> ActorCodeFactory;
75
76 XBT_PUBLIC void register_function(const char* name, ActorCodeFactory factory);
77
78 /** These functions will be called when we detect a deadlock: any remaining process is locked on an action
79  *
80  * If these functions manage to unlock some of the processes, then the deadlock will be avoided.
81  */
82 XBT_PUBLIC_DATA simgrid::xbt::signal<void()> onDeadlock;
83 }
84 }
85
86 /*
87  * Type of function that creates a process.
88  * The function must accept the following parameters:
89  * void* process: the process created will be stored there
90  * const char *name: a name for the object. It is for user-level information and can be NULL
91  * xbt_main_func_t code: is a function describing the behavior of the process
92  * void *data: data a pointer to any data one may want to attach to the new object.
93  * sg_host_t host: the location where the new process is executed
94  * int argc, char **argv: parameters passed to code
95  * std::map<std::string, std::string>* props: properties
96  */
97 typedef smx_actor_t (*smx_creation_func_t)(
98     /* name */ const char*, std::function<void()> code,
99     /* userdata */ void*,
100     /* hostname */ sg_host_t,
101     /* props */ std::unordered_map<std::string, std::string>*,
102     /* parent_process */ smx_actor_t);
103
104 XBT_PUBLIC void SIMIX_function_register_process_create(smx_creation_func_t function);
105
106 XBT_PUBLIC smx_actor_t simcall_process_create(const char* name, std::function<void()> code, void* data, sg_host_t host,
107                                               std::unordered_map<std::string, std::string>* properties);
108
109 XBT_PUBLIC smx_timer_t SIMIX_timer_set(double date, simgrid::xbt::Task<void()> callback);
110
111 template<class F> inline
112 smx_timer_t SIMIX_timer_set(double date, F callback)
113 {
114   return SIMIX_timer_set(date, simgrid::xbt::Task<void()>(std::move(callback)));
115 }
116
117 template<class R, class T> inline
118 smx_timer_t SIMIX_timer_set(double date, R(*callback)(T*), T* arg)
119 {
120   return SIMIX_timer_set(date, [=](){ callback(arg); });
121 }
122
123 #endif