Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] add condition variables
[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 #include <future>
17
18 #include <xbt/function_types.h>
19 #include <xbt/future.hpp>
20
21 #include <simgrid/simix.h>
22
23 XBT_PUBLIC(void) simcall_run_kernel(std::function<void()> const& code);
24 XBT_PUBLIC(void) simcall_run_blocking(std::function<void()> const& code);
25
26 template<class F> inline
27 void simcall_run_kernel(F& f)
28 {
29   simcall_run_kernel(std::function<void()>(std::ref(f)));
30 }
31 template<class F> inline
32 void simcall_run_blocking(F& f)
33 {
34   simcall_run_blocking(std::function<void()>(std::ref(f)));
35 }
36
37 namespace simgrid {
38
39 namespace simix {
40
41 /** Execute some code in the kernel/maestro
42  *
43  *  This can be used to enforce mutual exclusion with other simcall.
44  *  More importantly, this enforces a deterministic/reproducible ordering
45  *  of the operation with respect to other simcalls.
46  */
47 template<class F>
48 typename std::result_of<F()>::type kernel(F&& code)
49 {
50   // If we are in the maestro, we take the fast path and execute the
51   // code directly without simcall mashalling/unmarshalling/dispatch:
52   if (SIMIX_is_maestro())
53     return std::forward<F>(code)();
54
55   // If we are in the application, pass the code to the maestro which
56   // executes it for us and reports the result. We use a std::future which
57   // conveniently handles the success/failure value for us.
58   typedef typename std::result_of<F()>::type R;
59   simgrid::xbt::Result<R> result;
60   simcall_run_kernel([&]{
61     xbt_assert(SIMIX_is_maestro(), "Not in maestro");
62     simgrid::xbt::fulfillPromise(result, std::forward<F>(code));
63   });
64   return result.get();
65 }
66
67 class Context;
68 class ContextFactory;
69
70 XBT_PUBLIC_CLASS ContextFactory {
71 private:
72   std::string name_;
73 public:
74
75   explicit ContextFactory(std::string name) : name_(std::move(name)) {}
76   virtual ~ContextFactory();
77   virtual Context* create_context(std::function<void()> code,
78     void_pfn_smxprocess_t cleanup, smx_process_t process) = 0;
79
80   // Optional methods for attaching main() as a context:
81
82   /** Creates a context from the current context of execution
83    *
84    *  This will not work on all implementation of `ContextFactory`.
85    */
86   virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_process_t process);
87   virtual Context* create_maestro(std::function<void()> code, smx_process_t process);
88
89   virtual void run_all() = 0;
90   virtual Context* self();
91   std::string const& name() const
92   {
93     return name_;
94   }
95 private:
96   void declare_context(void* T, std::size_t size);
97 protected:
98   template<class T, class... Args>
99   T* new_context(Args&&... args)
100   {
101     T* context = new T(std::forward<Args>(args)...);
102     this->declare_context(context, sizeof(T));
103     return context;
104   }
105 };
106
107 XBT_PUBLIC_CLASS Context {
108 private:
109   std::function<void()> code_;
110   void_pfn_smxprocess_t cleanup_func_ = nullptr;
111   smx_process_t process_ = nullptr;
112 public:
113   bool iwannadie;
114 public:
115   Context(std::function<void()> code,
116           void_pfn_smxprocess_t cleanup_func,
117           smx_process_t process);
118   void operator()()
119   {
120     code_();
121   }
122   bool has_code() const
123   {
124     return (bool) code_;
125   }
126   smx_process_t process()
127   {
128     return this->process_;
129   }
130   void set_cleanup(void_pfn_smxprocess_t cleanup)
131   {
132     cleanup_func_ = cleanup;
133   }
134
135   // Virtual methods
136   virtual ~Context();
137   virtual void stop();
138   virtual void suspend() = 0;
139 };
140
141 XBT_PUBLIC_CLASS AttachContext : public Context {
142 public:
143
144   AttachContext(std::function<void()> code,
145           void_pfn_smxprocess_t cleanup_func,
146           smx_process_t process)
147     : Context(std::move(code), cleanup_func, process)
148   {}
149
150   ~AttachContext() override;
151
152   /** Called by the context when it is ready to give control
153    *  to the maestro.
154    */
155   virtual void attach_start() = 0;
156
157   /** Called by the context when it has finished its job */
158   virtual void attach_stop() = 0;
159 };
160
161 XBT_PUBLIC(void) set_maestro(std::function<void()> code);
162 XBT_PUBLIC(void) create_maestro(std::function<void()> code);
163
164 }
165 }
166
167 /*
168  * Type of function that creates a process.
169  * The function must accept the following parameters:
170  * void* process: the process created will be stored there
171  * const char *name: a name for the object. It is for user-level information and can be NULL
172  * xbt_main_func_t code: is a function describing the behavior of the process
173  * void *data: data a pointer to any data one may want to attach to the new object.
174  * sg_host_t host: the location where the new process is executed
175  * int argc, char **argv: parameters passed to code
176  * xbt_dict_t pros: properties
177  */
178 typedef smx_process_t (*smx_creation_func_t) (
179                                       /* name */ const char*,
180                                       std::function<void()> code,
181                                       /* userdata */ void*,
182                                       /* hostname */ const char*,
183                                       /* kill_time */ double,
184                                       /* props */ xbt_dict_t,
185                                       /* auto_restart */ int,
186                                       /* parent_process */ smx_process_t);
187
188 extern "C"
189 XBT_PUBLIC(void) SIMIX_function_register_process_create(smx_creation_func_t function);
190
191 XBT_PUBLIC(smx_process_t) simcall_process_create(const char *name,
192                                           std::function<void()> code,
193                                           void *data,
194                                           const char *hostname,
195                                           double kill_time,
196                                           xbt_dict_t properties,
197                                           int auto_restart);
198
199 XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, std::packaged_task<void()> callback);
200
201 template<class F> inline
202 XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, F callback)
203 {
204   return SIMIX_timer_set(date, std::packaged_task<void()>(std::move(callback)));
205 }
206
207 template<class R, class T> inline
208 XBT_PUBLIC(smx_timer_t) SIMIX_timer_set(double date, R(*callback)(T*), T* arg)
209 {
210   return SIMIX_timer_set(date, [=](){ callback(arg); });
211 }
212
213 #endif