Logo AND Algorithmique Numérique Distribuée

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