1 /* Copyright (c) 2015-2017. The SimGrid Team. All rights reserved. */
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. */
6 #include "ContextBoost.hpp"
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)
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)
23 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
29 // BoostContextFactory
31 BoostContextFactory::BoostContextFactory()
32 : ContextFactory("BoostContextFactory"), parallel_(SIMIX_context_is_parallel())
35 #if HAVE_THREAD_CONTEXTS
36 BoostContext::parmap_ = nullptr;
37 BoostContext::workers_context_.clear();
38 BoostContext::workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
39 BoostContext::maestro_context_ = nullptr;
40 xbt_os_thread_key_create(&BoostContext::worker_id_key_);
42 xbt_die("No thread support for parallel context execution");
47 BoostContextFactory::~BoostContextFactory()
49 #if HAVE_THREAD_CONTEXTS
50 delete BoostContext::parmap_;
51 BoostContext::parmap_ = nullptr;
52 BoostContext::workers_context_.clear();
56 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
59 #if HAVE_THREAD_CONTEXTS
61 return this->new_context<ParallelBoostContext>(std::move(code), cleanup_func, process);
64 return this->new_context<SerialBoostContext>(std::move(code), cleanup_func, process);
67 void BoostContextFactory::run_all()
69 #if HAVE_THREAD_CONTEXTS
71 BoostContext::threads_working_ = 0;
72 if (not BoostContext::parmap_)
73 BoostContext::parmap_ =
74 new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
75 BoostContext::parmap_->apply(
76 [](smx_actor_t process) {
77 BoostContext* context = static_cast<BoostContext*>(process->context);
78 return context->resume();
80 simix_global->process_to_run);
85 if (simix_global->process_to_run.empty())
87 smx_actor_t first_process = simix_global->process_to_run.front();
88 BoostContext::process_index_ = 1;
89 /* execute the first process */
90 static_cast<BoostContext*>(first_process->context)->resume();
96 simgrid::xbt::Parmap<smx_actor_t>* BoostContext::parmap_ = nullptr;
97 uintptr_t BoostContext::threads_working_ = 0;
98 xbt_os_thread_key_t BoostContext::worker_id_key_;
99 unsigned long BoostContext::process_index_ = 0;
100 BoostContext* BoostContext::maestro_context_ = nullptr;
101 std::vector<BoostContext*> BoostContext::workers_context_;
103 BoostContext::BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
104 : Context(std::move(code), cleanup_func, process)
107 /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
109 this->stack_ = SIMIX_context_stack_new();
110 /* We need to pass the bottom of the stack to make_fcontext,
111 depending on the stack direction it may be the lower or higher address: */
112 #if PTH_STACKGROWTH == -1
113 void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
115 void* stack = this->stack_;
117 ASAN_EVAL(this->asan_stack_ = stack);
118 #if BOOST_VERSION < 106100
119 this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
121 this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
124 #if BOOST_VERSION < 105600
125 this->fc_ = new boost::context::fcontext_t();
127 if (BoostContext::maestro_context_ == nullptr)
128 BoostContext::maestro_context_ = this;
132 BoostContext::~BoostContext()
134 #if BOOST_VERSION < 105600
135 if (not this->stack_)
138 if (this == maestro_context_)
139 maestro_context_ = nullptr;
140 SIMIX_context_stack_delete(this->stack_);
143 void BoostContext::wrapper(BoostContext::arg_type arg)
145 #if BOOST_VERSION < 106100
146 BoostContext* context = reinterpret_cast<BoostContext*>(arg);
148 ASAN_FINISH_SWITCH(nullptr, &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 BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
155 context->Context::stop();
156 } catch (StopRequest const&) {
157 XBT_DEBUG("Caught a StopRequest");
159 ASAN_EVAL(context->asan_stop_ = true);
163 inline void BoostContext::swap(BoostContext* from, BoostContext* to)
165 #if BOOST_VERSION < 105600
166 boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
167 #elif BOOST_VERSION < 106100
168 boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
170 BoostContext* ctx[2] = {from, to};
172 ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
173 boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
174 ASAN_FINISH_SWITCH(fake_stack, &static_cast<BoostContext**>(arg.data)[0]->asan_stack_,
175 &static_cast<BoostContext**>(arg.data)[0]->asan_stack_size_);
176 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
180 void BoostContext::stop()
186 // SerialBoostContext
188 void SerialBoostContext::suspend()
190 /* determine the next context */
191 SerialBoostContext* next_context;
192 unsigned long int i = process_index_;
195 if (i < simix_global->process_to_run.size()) {
196 /* execute the next process */
197 XBT_DEBUG("Run next process");
198 next_context = static_cast<SerialBoostContext*>(simix_global->process_to_run[i]->context);
200 /* all processes were run, return to maestro */
201 XBT_DEBUG("No more process to run");
202 next_context = static_cast<SerialBoostContext*>(maestro_context_);
204 SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
205 BoostContext::swap(this, next_context);
208 void SerialBoostContext::resume()
210 SIMIX_context_set_current(this);
211 BoostContext::swap(maestro_context_, this);
214 // ParallelBoostContext
216 #if HAVE_THREAD_CONTEXTS
218 void ParallelBoostContext::suspend()
220 boost::optional<smx_actor_t> next_work = parmap_->next();
221 ParallelBoostContext* next_context;
223 XBT_DEBUG("Run next process");
224 next_context = static_cast<ParallelBoostContext*>(next_work.get()->context);
226 XBT_DEBUG("No more processes to run");
227 uintptr_t worker_id = reinterpret_cast<uintptr_t>(xbt_os_thread_get_specific(worker_id_key_));
228 next_context = static_cast<ParallelBoostContext*>(workers_context_[worker_id]);
231 SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
232 BoostContext::swap(this, next_context);
235 void ParallelBoostContext::resume()
237 uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1);
238 xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast<void*>(worker_id));
240 ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(SIMIX_context_self());
241 workers_context_[worker_id] = worker_context;
243 SIMIX_context_set_current(this);
244 BoostContext::swap(worker_context, this);
249 XBT_PRIVATE ContextFactory* boost_factory()
251 XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
252 return new BoostContextFactory();