Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Derive custom exception from std::exception.
[simgrid.git] / src / kernel / context / ContextBoost.cpp
1 /* Copyright (c) 2015-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <cstdint>
7
8 #include <functional>
9 #include <utility>
10 #include <vector>
11
12 #include <boost/context/all.hpp>
13
14 #include <xbt/log.h>
15 #include <xbt/xbt_os_thread.h>
16
17 #include "src/simix/smx_private.h"
18 #include "src/internal_config.h"
19 #include "src/kernel/context/ContextBoost.hpp"
20
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
22
23 namespace simgrid {
24 namespace kernel {
25 namespace context {
26
27 class BoostSerialContext : public BoostContext {
28 public:
29   BoostSerialContext(std::function<void()> code,
30       void_pfn_smxprocess_t cleanup_func,
31       smx_actor_t process)
32     : BoostContext(std::move(code), cleanup_func, process) {}
33   void stop() override;
34   void suspend() override;
35 };
36
37 #if HAVE_THREAD_CONTEXTS
38 class BoostParallelContext : public BoostContext {
39 public:
40   BoostParallelContext(std::function<void()> code,
41       void_pfn_smxprocess_t cleanup_func,
42       smx_actor_t process)
43     : BoostContext(std::move(code), cleanup_func, process) {}
44   void stop() override;
45   void suspend() override;
46   void resume() override;
47 };
48 #endif
49
50 // BoostContextFactory
51
52 bool BoostContext::parallel_                             = false;
53 simgrid::xbt::Parmap<smx_actor_t>* BoostContext::parmap_ = nullptr;
54 uintptr_t BoostContext::threads_working_                 = 0;
55 xbt_os_thread_key_t BoostContext::worker_id_key_;
56 unsigned long BoostContext::process_index_   = 0;
57 BoostContext* BoostContext::maestro_context_ = nullptr;
58 std::vector<BoostContext*> BoostContext::workers_context_;
59
60 BoostContextFactory::BoostContextFactory()
61   : ContextFactory("BoostContextFactory")
62 {
63   BoostContext::parallel_ = SIMIX_context_is_parallel();
64   if (BoostContext::parallel_) {
65 #if HAVE_THREAD_CONTEXTS
66     BoostContext::parmap_ = nullptr;
67     BoostContext::workers_context_.clear();
68     BoostContext::workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
69     BoostContext::maestro_context_ = nullptr;
70     xbt_os_thread_key_create(&BoostContext::worker_id_key_);
71 #else
72     xbt_die("No thread support for parallel context execution");
73 #endif
74   }
75 }
76
77 BoostContextFactory::~BoostContextFactory()
78 {
79 #if HAVE_THREAD_CONTEXTS
80   delete BoostContext::parmap_;
81   BoostContext::parmap_ = nullptr;
82   BoostContext::workers_context_.clear();
83 #endif
84 }
85
86 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
87                                                   smx_actor_t process)
88 {
89   BoostContext* context = nullptr;
90   if (BoostContext::parallel_)
91 #if HAVE_THREAD_CONTEXTS
92     context = this->new_context<BoostParallelContext>(std::move(code), cleanup_func, process);
93 #else
94     xbt_die("No support for parallel execution");
95 #endif
96   else
97     context = this->new_context<BoostSerialContext>(std::move(code), cleanup_func, process);
98   return context;
99 }
100
101 void BoostContextFactory::run_all()
102 {
103 #if HAVE_THREAD_CONTEXTS
104   if (BoostContext::parallel_) {
105     BoostContext::threads_working_ = 0;
106     if (not BoostContext::parmap_)
107       BoostContext::parmap_ =
108           new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
109     BoostContext::parmap_->apply(
110         [](smx_actor_t process) {
111           BoostContext* context = static_cast<BoostContext*>(process->context);
112           return context->resume();
113         },
114         simix_global->process_to_run);
115   } else
116 #endif
117   {
118     if (simix_global->process_to_run.empty())
119       return;
120     smx_actor_t first_process    = simix_global->process_to_run.front();
121     BoostContext::process_index_ = 1;
122     /* execute the first process */
123     static_cast<BoostContext*>(first_process->context)->resume();
124   }
125 }
126
127
128 // BoostContext
129
130 void BoostContext::smx_ctx_boost_wrapper(BoostContext::ctx_arg_type arg)
131 {
132 #if BOOST_VERSION < 106100
133   BoostContext* context = reinterpret_cast<BoostContext*>(arg);
134 #else
135   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
136   BoostContext* context                         = static_cast<BoostContext**>(arg.data)[1];
137 #endif
138   try {
139     (*context)();
140     context->stop();
141   } catch (const StopRequest&) {
142     XBT_DEBUG("Caught a StopRequest");
143   }
144   context->suspend();
145 }
146
147 inline void BoostContext::smx_ctx_boost_jump_fcontext(BoostContext* from, BoostContext* to)
148 {
149 #if BOOST_VERSION < 105600
150   boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
151 #elif BOOST_VERSION < 106100
152   boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
153 #else
154   BoostContext* ctx[2]                          = {from, to};
155   boost::context::detail::transfer_t arg        = boost::context::detail::jump_fcontext(to->fc_, ctx);
156   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
157 #endif
158 }
159
160 BoostContext::BoostContext(std::function<void()> code,
161     void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
162   : Context(std::move(code), cleanup_func, process)
163 {
164
165   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
166   if (has_code()) {
167     this->stack_ = SIMIX_context_stack_new();
168 // We need to pass the bottom of the stack to make_fcontext, depending on the stack direction it may be the lower
169 // or higher address:
170 #if PTH_STACKGROWTH == -1
171     void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size - 1;
172 #else
173     void* stack = this->stack_;
174 #endif
175 #if BOOST_VERSION < 106100
176     this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, smx_ctx_boost_wrapper);
177 #else
178     this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, smx_ctx_boost_wrapper);
179 #endif
180   } else {
181 #if BOOST_VERSION < 105600
182     this->fc_ = new boost::context::fcontext_t();
183 #endif
184     if (BoostContext::maestro_context_ == nullptr)
185       BoostContext::maestro_context_ = this;
186   }
187 }
188
189 BoostContext::~BoostContext()
190 {
191 #if BOOST_VERSION < 105600
192   if (not this->stack_)
193     delete this->fc_;
194 #endif
195   if (this == maestro_context_)
196     maestro_context_ = nullptr;
197   SIMIX_context_stack_delete(this->stack_);
198 }
199
200 // BoostSerialContext
201
202 void BoostContext::resume()
203 {
204   SIMIX_context_set_current(this);
205   smx_ctx_boost_jump_fcontext(maestro_context_, this);
206 }
207
208 void BoostSerialContext::suspend()
209 {
210   /* determine the next context */
211   BoostSerialContext* next_context;
212   unsigned long int i = process_index_;
213   process_index_++;
214
215   if (i < simix_global->process_to_run.size()) {
216     /* execute the next process */
217     XBT_DEBUG("Run next process");
218     next_context = static_cast<BoostSerialContext*>(simix_global->process_to_run[i]->context);
219   } else {
220     /* all processes were run, return to maestro */
221     XBT_DEBUG("No more process to run");
222     next_context = static_cast<BoostSerialContext*>(maestro_context_);
223   }
224   SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
225   smx_ctx_boost_jump_fcontext(this, next_context);
226 }
227
228 void BoostSerialContext::stop()
229 {
230   BoostContext::stop();
231   throw StopRequest();
232 }
233
234 // BoostParallelContext
235
236 #if HAVE_THREAD_CONTEXTS
237
238 void BoostParallelContext::suspend()
239 {
240   boost::optional<smx_actor_t> next_work = parmap_->next();
241   BoostParallelContext* next_context;
242   if (next_work) {
243     XBT_DEBUG("Run next process");
244     next_context = static_cast<BoostParallelContext*>(next_work.get()->context);
245   } else {
246     XBT_DEBUG("No more processes to run");
247     uintptr_t worker_id = reinterpret_cast<uintptr_t>(xbt_os_thread_get_specific(worker_id_key_));
248     next_context = static_cast<BoostParallelContext*>(workers_context_[worker_id]);
249   }
250
251   SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
252   smx_ctx_boost_jump_fcontext(this, next_context);
253 }
254
255 void BoostParallelContext::stop()
256 {
257   BoostContext::stop();
258   throw StopRequest();
259 }
260
261 void BoostParallelContext::resume()
262 {
263   uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1);
264   xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast<void*>(worker_id));
265
266   BoostParallelContext* worker_context = static_cast<BoostParallelContext*>(SIMIX_context_self());
267   workers_context_[worker_id] = worker_context;
268
269   SIMIX_context_set_current(this);
270   smx_ctx_boost_jump_fcontext(worker_context, this);
271 }
272
273 #endif
274
275 XBT_PRIVATE ContextFactory* boost_factory()
276 {
277   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
278   return new BoostContextFactory();
279 }
280
281 }}} // namespace