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 "simgrid/Exception.hpp"
9 #include "src/simix/smx_private.hpp"
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
17 // BoostContextFactory
19 BoostContextFactory::BoostContextFactory()
20 : ContextFactory("BoostContextFactory"), parallel_(SIMIX_context_is_parallel())
22 BoostContext::setMaestro(nullptr);
24 #if HAVE_THREAD_CONTEXTS
25 ParallelBoostContext::initialize();
27 xbt_die("No thread support for parallel context execution");
32 BoostContextFactory::~BoostContextFactory()
34 #if HAVE_THREAD_CONTEXTS
36 ParallelBoostContext::finalize();
40 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
43 #if HAVE_THREAD_CONTEXTS
45 return this->new_context<ParallelBoostContext>(std::move(code), cleanup_func, process);
48 return this->new_context<SerialBoostContext>(std::move(code), cleanup_func, process);
51 void BoostContextFactory::run_all()
53 #if HAVE_THREAD_CONTEXTS
55 ParallelBoostContext::run_all();
58 SerialBoostContext::run_all();
63 BoostContext* BoostContext::maestro_context_ = nullptr;
65 BoostContext::BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
66 : Context(std::move(code), cleanup_func, process)
69 /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
71 this->stack_ = SIMIX_context_stack_new();
72 /* We need to pass the bottom of the stack to make_fcontext,
73 depending on the stack direction it may be the lower or higher address: */
74 #if PTH_STACKGROWTH == -1
75 void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
77 void* stack = this->stack_;
79 ASAN_ONLY(this->asan_stack_ = stack);
80 #if BOOST_VERSION < 106100
81 this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
83 this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
86 #if BOOST_VERSION < 105600
87 this->fc_ = new boost::context::fcontext_t();
89 if (BoostContext::maestro_context_ == nullptr)
90 BoostContext::maestro_context_ = this;
94 BoostContext::~BoostContext()
96 #if BOOST_VERSION < 105600
100 if (this == maestro_context_)
101 maestro_context_ = nullptr;
102 SIMIX_context_stack_delete(this->stack_);
105 void BoostContext::wrapper(BoostContext::arg_type arg)
107 #if BOOST_VERSION < 106100
108 BoostContext* context = reinterpret_cast<BoostContext*>(arg);
110 BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
111 ASAN_ONLY(xbt_assert(context->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
112 ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
113 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
117 } catch (StopRequest const&) {
118 XBT_DEBUG("Caught a StopRequest");
119 } catch (simgrid::Exception const& e) {
120 XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
123 context->Context::stop();
124 ASAN_ONLY(context->asan_stop_ = true);
128 inline void BoostContext::swap(BoostContext* from, BoostContext* to)
130 #if BOOST_VERSION < 105600
131 boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
132 #elif BOOST_VERSION < 106100
133 boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
135 BoostContext* ctx[2] = {from, to};
136 ASAN_ONLY(void* fake_stack = nullptr);
137 ASAN_ONLY(to->asan_ctx_ = from);
138 ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
139 boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
140 ASAN_ONLY(xbt_assert(from->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
141 ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
142 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
146 void BoostContext::stop()
152 // SerialBoostContext
154 unsigned long SerialBoostContext::process_index_;
156 void SerialBoostContext::suspend()
158 /* determine the next context */
159 SerialBoostContext* next_context;
160 unsigned long int i = process_index_;
163 if (i < simix_global->process_to_run.size()) {
164 /* execute the next process */
165 XBT_DEBUG("Run next process");
166 next_context = static_cast<SerialBoostContext*>(simix_global->process_to_run[i]->context_);
168 /* all processes were run, return to maestro */
169 XBT_DEBUG("No more process to run");
170 next_context = static_cast<SerialBoostContext*>(BoostContext::getMaestro());
172 SIMIX_context_set_current(next_context);
173 BoostContext::swap(this, next_context);
176 void SerialBoostContext::resume()
178 SIMIX_context_set_current(this);
179 BoostContext::swap(BoostContext::getMaestro(), this);
182 void SerialBoostContext::run_all()
184 if (simix_global->process_to_run.empty())
186 smx_actor_t first_process = simix_global->process_to_run.front();
188 /* execute the first process */
189 static_cast<SerialBoostContext*>(first_process->context_)->resume();
192 // ParallelBoostContext
194 #if HAVE_THREAD_CONTEXTS
196 simgrid::xbt::Parmap<smx_actor_t>* ParallelBoostContext::parmap_;
197 std::atomic<uintptr_t> ParallelBoostContext::threads_working_;
198 thread_local uintptr_t ParallelBoostContext::worker_id_;
199 std::vector<ParallelBoostContext*> ParallelBoostContext::workers_context_;
201 void ParallelBoostContext::initialize()
204 workers_context_.clear();
205 workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
208 void ParallelBoostContext::finalize()
212 workers_context_.clear();
215 void ParallelBoostContext::run_all()
217 threads_working_ = 0;
218 if (parmap_ == nullptr)
219 parmap_ = new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
221 [](smx_actor_t process) {
222 ParallelBoostContext* context = static_cast<ParallelBoostContext*>(process->context_);
225 simix_global->process_to_run);
228 void ParallelBoostContext::suspend()
230 boost::optional<smx_actor_t> next_work = parmap_->next();
231 ParallelBoostContext* next_context;
233 XBT_DEBUG("Run next process");
234 next_context = static_cast<ParallelBoostContext*>(next_work.get()->context_);
236 XBT_DEBUG("No more processes to run");
237 next_context = workers_context_[worker_id_];
240 SIMIX_context_set_current(next_context);
241 BoostContext::swap(this, next_context);
244 void ParallelBoostContext::resume()
246 worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
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();