Logo AND Algorithmique Numérique Distribuée

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