Logo AND Algorithmique Numérique Distribuée

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