Logo AND Algorithmique Numérique Distribuée

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