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 "context_private.hpp"
8 #include "src/simix/smx_private.hpp"
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
16 // BoostContextFactory
18 BoostContextFactory::BoostContextFactory()
19 : ContextFactory("BoostContextFactory"), parallel_(SIMIX_context_is_parallel())
21 BoostContext::setMaestro(nullptr);
23 #if HAVE_THREAD_CONTEXTS
24 ParallelBoostContext::initialize();
26 xbt_die("No thread support for parallel context execution");
31 BoostContextFactory::~BoostContextFactory()
33 #if HAVE_THREAD_CONTEXTS
35 ParallelBoostContext::finalize();
39 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
42 #if HAVE_THREAD_CONTEXTS
44 return this->new_context<ParallelBoostContext>(std::move(code), cleanup_func, process);
47 return this->new_context<SerialBoostContext>(std::move(code), cleanup_func, process);
50 void BoostContextFactory::run_all()
52 #if HAVE_THREAD_CONTEXTS
54 ParallelBoostContext::run_all();
57 SerialBoostContext::run_all();
62 BoostContext* BoostContext::maestro_context_ = nullptr;
64 BoostContext::BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
65 : Context(std::move(code), cleanup_func, process)
68 /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
70 this->stack_ = SIMIX_context_stack_new();
71 /* We need to pass the bottom of the stack to make_fcontext,
72 depending on the stack direction it may be the lower or higher address: */
73 #if PTH_STACKGROWTH == -1
74 void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
76 void* stack = this->stack_;
78 ASAN_EVAL(this->asan_stack_ = stack);
79 #if BOOST_VERSION < 106100
80 this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
82 this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
85 #if BOOST_VERSION < 105600
86 this->fc_ = new boost::context::fcontext_t();
88 if (BoostContext::maestro_context_ == nullptr)
89 BoostContext::maestro_context_ = this;
93 BoostContext::~BoostContext()
95 #if BOOST_VERSION < 105600
99 if (this == maestro_context_)
100 maestro_context_ = nullptr;
101 SIMIX_context_stack_delete(this->stack_);
104 void BoostContext::wrapper(BoostContext::arg_type arg)
106 #if BOOST_VERSION < 106100
107 BoostContext* context = reinterpret_cast<BoostContext*>(arg);
109 BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
110 ASAN_ASSERT(context->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]);
111 ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
112 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
116 context->Context::stop();
117 } catch (StopRequest const&) {
118 XBT_DEBUG("Caught a StopRequest");
120 ASAN_EVAL(context->asan_stop_ = true);
124 inline void BoostContext::swap(BoostContext* from, BoostContext* to)
126 #if BOOST_VERSION < 105600
127 boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
128 #elif BOOST_VERSION < 106100
129 boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
131 BoostContext* ctx[2] = {from, to};
132 void* fake_stack = nullptr;
133 ASAN_EVAL(to->asan_ctx_ = from);
134 ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
135 boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
136 ASAN_ASSERT(from->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]);
137 ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
138 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
142 void BoostContext::stop()
148 // SerialBoostContext
150 unsigned long SerialBoostContext::process_index_;
152 void SerialBoostContext::suspend()
154 /* determine the next context */
155 SerialBoostContext* next_context;
156 unsigned long int i = process_index_;
159 if (i < simix_global->process_to_run.size()) {
160 /* execute the next process */
161 XBT_DEBUG("Run next process");
162 next_context = static_cast<SerialBoostContext*>(simix_global->process_to_run[i]->context);
164 /* all processes were run, return to maestro */
165 XBT_DEBUG("No more process to run");
166 next_context = static_cast<SerialBoostContext*>(BoostContext::getMaestro());
168 SIMIX_context_set_current(next_context);
169 BoostContext::swap(this, next_context);
172 void SerialBoostContext::resume()
174 SIMIX_context_set_current(this);
175 BoostContext::swap(BoostContext::getMaestro(), this);
178 void SerialBoostContext::run_all()
180 if (simix_global->process_to_run.empty())
182 smx_actor_t first_process = simix_global->process_to_run.front();
184 /* execute the first process */
185 static_cast<SerialBoostContext*>(first_process->context)->resume();
188 // ParallelBoostContext
190 #if HAVE_THREAD_CONTEXTS
192 simgrid::xbt::Parmap<smx_actor_t>* ParallelBoostContext::parmap_;
193 std::atomic<uintptr_t> ParallelBoostContext::threads_working_;
194 xbt_os_thread_key_t ParallelBoostContext::worker_id_key_;
195 std::vector<ParallelBoostContext*> ParallelBoostContext::workers_context_;
197 void ParallelBoostContext::initialize()
200 workers_context_.clear();
201 workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
202 xbt_os_thread_key_create(&worker_id_key_);
205 void ParallelBoostContext::finalize()
209 workers_context_.clear();
210 xbt_os_thread_key_destroy(worker_id_key_);
213 void ParallelBoostContext::run_all()
215 threads_working_ = 0;
216 if (parmap_ == nullptr)
217 parmap_ = new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
219 [](smx_actor_t process) {
220 ParallelBoostContext* context = static_cast<ParallelBoostContext*>(process->context);
223 simix_global->process_to_run);
226 void ParallelBoostContext::suspend()
228 boost::optional<smx_actor_t> next_work = parmap_->next();
229 ParallelBoostContext* next_context;
231 XBT_DEBUG("Run next process");
232 next_context = static_cast<ParallelBoostContext*>(next_work.get()->context);
234 XBT_DEBUG("No more processes to run");
235 uintptr_t worker_id = reinterpret_cast<uintptr_t>(xbt_os_thread_get_specific(worker_id_key_));
236 next_context = workers_context_[worker_id];
239 SIMIX_context_set_current(next_context);
240 BoostContext::swap(this, next_context);
243 void ParallelBoostContext::resume()
245 uintptr_t worker_id = threads_working_.fetch_add(1, std::memory_order_relaxed);
246 xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast<void*>(worker_id));
248 ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(SIMIX_context_self());
249 workers_context_[worker_id] = worker_context;
251 SIMIX_context_set_current(this);
252 BoostContext::swap(worker_context, this);
257 XBT_PRIVATE ContextFactory* boost_factory()
259 XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
260 return new BoostContextFactory();