Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add missing #include for std::current_exception()
[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 <simgrid/simix.h>
22
23 XBT_PUBLIC(void) simcall_run_kernel(std::function<void()> const& code);
24
25 namespace simgrid {
26 namespace simix {
27
28 /** Fulfill a promise by executing a given code */
29 template<class R, class F>
30 void fulfill_promise(std::promise<R>& promise, F&& code)
31 {
32   try {
33     promise.set_value(std::forward<F>(code)());
34   }
35   catch(...) {
36     promise.set_exception(std::current_exception());
37   }
38 }
39
40 /** Fulfill a promise by executing a given code
41  *
42  *  This is a special version for `std::promise<void>` because the default
43  *  version does not compile in this case.
44  */
45 template<class F>
46 void fulfill_promise(std::promise<void>& promise, F&& code)
47 {
48   try {
49     std::forward<F>(code)();
50     promise.set_value();
51   }
52   catch(...) {
53     promise.set_exception(std::current_exception());
54   }
55 }
56
57 /** Execute some code in the kernel/maestro
58  *
59  *  This can be used to enforce mutual exclusion with other simcall.
60  *  More importantly, this enforces a deterministic/reproducible ordering
61  *  of the operation with respect to other simcalls.
62  */
63 template<class F>
64 typename std::result_of<F()>::type kernel(F&& code)
65 {
66   // If we are in the maestro, we take the fast path and execute the
67   // code directly without simcall mashalling/unmarshalling/dispatch:
68   if (SIMIX_is_maestro())
69     return std::forward<F>(code)();
70
71   // If we are in the application, pass the code to the maestro which is
72   // executes it for us and reports the result. We use a std::future which
73   // conveniently handles the success/failure value for us.
74   typedef typename std::result_of<F()>::type R;
75   std::promise<R> promise;
76   simcall_run_kernel([&]{
77     xbt_assert(SIMIX_is_maestro(), "Not in maestro");
78     fulfill_promise(promise, std::forward<F>(code));
79   });
80   return promise.get_future().get();
81 }
82
83 class args {
84 private:
85   int argc_;
86   char** argv_;
87 public:
88
89   // Main constructors
90   args() : argc_(0), argv_(nullptr) {}
91   args(int argc, char** argv) : argc_(argc), argv_(argv) {}
92
93   // Free
94   void clear()
95   {
96     for (int i = 0; i < this->argc_; i++)
97       free(this->argv_[i]);
98     free(this->argv_);
99     this->argc_ = 0;
100     this->argv_ = nullptr;
101   }
102   ~args() { clear(); }
103
104   // Copy
105   args(args const& that) = delete;
106   args& operator=(args const& that) = delete;
107
108   // Move:
109   args(args&& that) : argc_(that.argc_), argv_(that.argv_)
110   {
111     that.argc_ = 0;
112     that.argv_ = nullptr;
113   }
114   args& operator=(args&& that)
115   {
116     this->argc_ = that.argc_;
117     this->argv_ = that.argv_;
118     that.argc_ = 0;
119     that.argv_ = nullptr;
120     return *this;
121   }
122
123   int    argc()            const { return argc_; }
124   char** argv()                  { return argv_; }
125   const char*const* argv() const { return argv_; }
126   char* operator[](std::size_t i) { return argv_[i]; }
127 };
128
129 inline
130 std::function<void()> wrap_main(xbt_main_func_t code, int argc, char **argv)
131 {
132   if (code) {
133     auto arg = std::make_shared<simgrid::simix::args>(argc, argv);
134     return [=]() {
135       code(arg->argc(), arg->argv());
136     };
137   }
138   // TODO, we should free argv
139   else return std::function<void()>();
140 }
141
142 class Context;
143 class ContextFactory;
144
145 XBT_PUBLIC_CLASS ContextFactory {
146 private:
147   std::string name_;
148 public:
149
150   ContextFactory(std::string name) : name_(std::move(name)) {}
151   virtual ~ContextFactory();
152   virtual Context* create_context(std::function<void()> code,
153     void_pfn_smxprocess_t cleanup, smx_process_t process) = 0;
154
155   // Optional methods for attaching main() as a context:
156
157   /** Creates a context from the current context of execution
158    *
159    *  This will not work on all implementation of `ContextFactory`.
160    */
161   virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_process_t process);
162   virtual Context* create_maestro(std::function<void()> code, smx_process_t process);
163
164   virtual void run_all() = 0;
165   virtual Context* self();
166   std::string const& name() const
167   {
168     return name_;
169   }
170 private:
171   void declare_context(void* T, std::size_t size);
172 protected:
173   template<class T, class... Args>
174   T* new_context(Args&&... args)
175   {
176     T* context = new T(std::forward<Args>(args)...);
177     this->declare_context(context, sizeof(T));
178     return context;
179   }
180 };
181
182 XBT_PUBLIC_CLASS Context {
183 private:
184   std::function<void()> code_;
185   void_pfn_smxprocess_t cleanup_func_ = nullptr;
186   smx_process_t process_ = nullptr;
187 public:
188   bool iwannadie;
189 public:
190   Context(std::function<void()> code,
191           void_pfn_smxprocess_t cleanup_func,
192           smx_process_t process);
193   void operator()()
194   {
195     code_();
196   }
197   bool has_code() const
198   {
199     return (bool) code_;
200   }
201   smx_process_t process()
202   {
203     return this->process_;
204   }
205   void set_cleanup(void_pfn_smxprocess_t cleanup)
206   {
207     cleanup_func_ = cleanup;
208   }
209
210   // Virtual methods
211   virtual ~Context();
212   virtual void stop();
213   virtual void suspend() = 0;
214 };
215
216 XBT_PUBLIC_CLASS AttachContext : public Context {
217 public:
218
219   AttachContext(std::function<void()> code,
220           void_pfn_smxprocess_t cleanup_func,
221           smx_process_t process)
222     : Context(std::move(code), cleanup_func, process)
223   {}
224
225   ~AttachContext();
226
227   /** Called by the context when it is ready to give control
228    *  to the maestro.
229    */
230   virtual void attach_start() = 0;
231
232   /** Called by the context when it has finished its job */
233   virtual void attach_stop() = 0;
234 };
235
236 XBT_PUBLIC(void) set_maestro(std::function<void()> code);
237 XBT_PUBLIC(void) create_maestro(std::function<void()> code);
238
239 }
240 }
241
242 #endif