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())
34 BoostContext::setMaestro(nullptr);
36 #if HAVE_THREAD_CONTEXTS
37 ParallelBoostContext::initialize();
39 xbt_die("No thread support for parallel context execution");
44 BoostContextFactory::~BoostContextFactory()
46 #if HAVE_THREAD_CONTEXTS
48 ParallelBoostContext::finalize();
52 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
55 #if HAVE_THREAD_CONTEXTS
57 return this->new_context<ParallelBoostContext>(std::move(code), cleanup_func, process);
60 return this->new_context<SerialBoostContext>(std::move(code), cleanup_func, process);
63 void BoostContextFactory::run_all()
65 #if HAVE_THREAD_CONTEXTS
67 ParallelBoostContext::run_all();
70 SerialBoostContext::run_all();
75 BoostContext* BoostContext::maestro_context_ = nullptr;
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)
81 /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
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;
89 void* stack = this->stack_;
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);
95 this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
98 #if BOOST_VERSION < 105600
99 this->fc_ = new boost::context::fcontext_t();
101 if (BoostContext::maestro_context_ == nullptr)
102 BoostContext::maestro_context_ = this;
106 BoostContext::~BoostContext()
108 #if BOOST_VERSION < 105600
109 if (not this->stack_)
112 if (this == maestro_context_)
113 maestro_context_ = nullptr;
114 SIMIX_context_stack_delete(this->stack_);
117 void BoostContext::wrapper(BoostContext::arg_type arg)
119 #if BOOST_VERSION < 106100
120 BoostContext* context = reinterpret_cast<BoostContext*>(arg);
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];
129 context->Context::stop();
130 } catch (StopRequest const&) {
131 XBT_DEBUG("Caught a StopRequest");
133 ASAN_EVAL(context->asan_stop_ = true);
137 inline void BoostContext::swap(BoostContext* from, BoostContext* to)
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));
144 BoostContext* ctx[2] = {from, to};
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;
154 void BoostContext::stop()
160 // SerialBoostContext
162 unsigned long SerialBoostContext::process_index_;
164 void SerialBoostContext::suspend()
166 /* determine the next context */
167 SerialBoostContext* next_context;
168 unsigned long int i = process_index_;
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);
176 /* all processes were run, return to maestro */
177 XBT_DEBUG("No more process to run");
178 next_context = static_cast<SerialBoostContext*>(BoostContext::getMaestro());
180 SIMIX_context_set_current(next_context);
181 BoostContext::swap(this, next_context);
184 void SerialBoostContext::resume()
186 SIMIX_context_set_current(this);
187 BoostContext::swap(BoostContext::getMaestro(), this);
190 void SerialBoostContext::run_all()
192 if (simix_global->process_to_run.empty())
194 smx_actor_t first_process = simix_global->process_to_run.front();
196 /* execute the first process */
197 static_cast<SerialBoostContext*>(first_process->context)->resume();
200 // ParallelBoostContext
202 #if HAVE_THREAD_CONTEXTS
204 simgrid::xbt::Parmap<smx_actor_t>* ParallelBoostContext::parmap_;
205 std::atomic<uintptr_t> ParallelBoostContext::threads_working_;
206 xbt_os_thread_key_t ParallelBoostContext::worker_id_key_;
207 std::vector<ParallelBoostContext*> ParallelBoostContext::workers_context_;
209 void ParallelBoostContext::initialize()
212 workers_context_.clear();
213 workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
214 xbt_os_thread_key_create(&worker_id_key_);
217 void ParallelBoostContext::finalize()
221 workers_context_.clear();
222 xbt_os_thread_key_destroy(worker_id_key_);
225 void ParallelBoostContext::run_all()
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());
231 [](smx_actor_t process) {
232 ParallelBoostContext* context = static_cast<ParallelBoostContext*>(process->context);
235 simix_global->process_to_run);
238 void ParallelBoostContext::suspend()
240 boost::optional<smx_actor_t> next_work = parmap_->next();
241 ParallelBoostContext* next_context;
243 XBT_DEBUG("Run next process");
244 next_context = static_cast<ParallelBoostContext*>(next_work.get()->context);
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];
251 SIMIX_context_set_current(next_context);
252 BoostContext::swap(this, next_context);
255 void ParallelBoostContext::resume()
257 uintptr_t worker_id = threads_working_.fetch_add(1, std::memory_order_relaxed);
258 xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast<void*>(worker_id));
260 ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(SIMIX_context_self());
261 workers_context_[worker_id] = worker_context;
263 SIMIX_context_set_current(this);
264 BoostContext::swap(worker_context, this);
269 XBT_PRIVATE ContextFactory* boost_factory()
271 XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
272 return new BoostContextFactory();