1 /* Copyright (c) 2015-2018. 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"
7 #include "src/simix/smx_private.hpp"
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)
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)
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
27 // BoostContextFactory
29 BoostContextFactory::BoostContextFactory()
30 : ContextFactory("BoostContextFactory"), parallel_(SIMIX_context_is_parallel())
32 BoostContext::setMaestro(nullptr);
34 #if HAVE_THREAD_CONTEXTS
35 ParallelBoostContext::initialize();
37 xbt_die("No thread support for parallel context execution");
42 BoostContextFactory::~BoostContextFactory()
44 #if HAVE_THREAD_CONTEXTS
46 ParallelBoostContext::finalize();
50 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
53 #if HAVE_THREAD_CONTEXTS
55 return this->new_context<ParallelBoostContext>(std::move(code), cleanup_func, process);
58 return this->new_context<SerialBoostContext>(std::move(code), cleanup_func, process);
61 void BoostContextFactory::run_all()
63 #if HAVE_THREAD_CONTEXTS
65 ParallelBoostContext::run_all();
68 SerialBoostContext::run_all();
73 BoostContext* BoostContext::maestro_context_ = nullptr;
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)
79 /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
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;
87 void* stack = this->stack_;
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);
93 this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
96 #if BOOST_VERSION < 105600
97 this->fc_ = new boost::context::fcontext_t();
99 if (BoostContext::maestro_context_ == nullptr)
100 BoostContext::maestro_context_ = this;
104 BoostContext::~BoostContext()
106 #if BOOST_VERSION < 105600
107 if (not this->stack_)
110 if (this == maestro_context_)
111 maestro_context_ = nullptr;
112 SIMIX_context_stack_delete(this->stack_);
115 void BoostContext::wrapper(BoostContext::arg_type arg)
117 #if BOOST_VERSION < 106100
118 BoostContext* context = reinterpret_cast<BoostContext*>(arg);
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];
127 context->Context::stop();
128 } catch (StopRequest const&) {
129 XBT_DEBUG("Caught a StopRequest");
131 ASAN_EVAL(context->asan_stop_ = true);
135 inline void BoostContext::swap(BoostContext* from, BoostContext* to)
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));
142 BoostContext* ctx[2] = {from, to};
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;
152 void BoostContext::stop()
158 // SerialBoostContext
160 unsigned long SerialBoostContext::process_index_;
162 void SerialBoostContext::suspend()
164 /* determine the next context */
165 SerialBoostContext* next_context;
166 unsigned long int i = process_index_;
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);
174 /* all processes were run, return to maestro */
175 XBT_DEBUG("No more process to run");
176 next_context = static_cast<SerialBoostContext*>(BoostContext::getMaestro());
178 SIMIX_context_set_current(next_context);
179 BoostContext::swap(this, next_context);
182 void SerialBoostContext::resume()
184 SIMIX_context_set_current(this);
185 BoostContext::swap(BoostContext::getMaestro(), this);
188 void SerialBoostContext::run_all()
190 if (simix_global->process_to_run.empty())
192 smx_actor_t first_process = simix_global->process_to_run.front();
194 /* execute the first process */
195 static_cast<SerialBoostContext*>(first_process->context)->resume();
198 // ParallelBoostContext
200 #if HAVE_THREAD_CONTEXTS
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_;
207 void ParallelBoostContext::initialize()
210 workers_context_.clear();
211 workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
212 xbt_os_thread_key_create(&worker_id_key_);
215 void ParallelBoostContext::finalize()
219 workers_context_.clear();
220 xbt_os_thread_key_destroy(worker_id_key_);
223 void ParallelBoostContext::run_all()
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());
229 [](smx_actor_t process) {
230 ParallelBoostContext* context = static_cast<ParallelBoostContext*>(process->context);
233 simix_global->process_to_run);
236 void ParallelBoostContext::suspend()
238 boost::optional<smx_actor_t> next_work = parmap_->next();
239 ParallelBoostContext* next_context;
241 XBT_DEBUG("Run next process");
242 next_context = static_cast<ParallelBoostContext*>(next_work.get()->context);
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];
249 SIMIX_context_set_current(next_context);
250 BoostContext::swap(this, next_context);
253 void ParallelBoostContext::resume()
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));
258 ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(SIMIX_context_self());
259 workers_context_[worker_id] = worker_context;
261 SIMIX_context_set_current(this);
262 BoostContext::swap(worker_context, this);
267 XBT_PRIVATE ContextFactory* boost_factory()
269 XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
270 return new BoostContextFactory();