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::set_maestro(nullptr);
24 ParallelBoostContext::initialize();
27 BoostContextFactory::~BoostContextFactory()
30 ParallelBoostContext::finalize();
33 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
37 return this->new_context<ParallelBoostContext>(std::move(code), cleanup_func, process);
38 return this->new_context<SerialBoostContext>(std::move(code), cleanup_func, process);
41 void BoostContextFactory::run_all()
44 ParallelBoostContext::run_all();
46 SerialBoostContext::run_all();
51 BoostContext* BoostContext::maestro_context_ = nullptr;
53 BoostContext::BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
54 : Context(std::move(code), cleanup_func, process)
57 /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
59 this->stack_ = SIMIX_context_stack_new();
60 /* We need to pass the bottom of the stack to make_fcontext,
61 depending on the stack direction it may be the lower or higher address: */
62 #if PTH_STACKGROWTH == -1
63 void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
65 void* stack = this->stack_;
67 ASAN_ONLY(this->asan_stack_ = stack);
68 #if BOOST_VERSION < 106100
69 this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
71 this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
74 #if BOOST_VERSION < 105600
75 this->fc_ = new boost::context::fcontext_t();
77 if (BoostContext::maestro_context_ == nullptr)
78 BoostContext::maestro_context_ = this;
82 BoostContext::~BoostContext()
84 #if BOOST_VERSION < 105600
88 if (this == maestro_context_)
89 maestro_context_ = nullptr;
90 SIMIX_context_stack_delete(this->stack_);
93 void BoostContext::wrapper(BoostContext::arg_type arg)
95 #if BOOST_VERSION < 106100
96 BoostContext* context = reinterpret_cast<BoostContext*>(arg);
98 BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
99 ASAN_ONLY(xbt_assert(context->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
100 ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
101 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
105 } catch (StopRequest const&) {
106 XBT_DEBUG("Caught a StopRequest");
107 } catch (simgrid::Exception const& e) {
108 XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
111 context->Context::stop();
112 ASAN_ONLY(context->asan_stop_ = true);
116 inline void BoostContext::swap(BoostContext* from, BoostContext* to)
118 #if BOOST_VERSION < 105600
119 boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
120 #elif BOOST_VERSION < 106100
121 boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
123 BoostContext* ctx[2] = {from, to};
124 ASAN_ONLY(void* fake_stack = nullptr);
125 ASAN_ONLY(to->asan_ctx_ = from);
126 ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
127 boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
128 ASAN_ONLY(xbt_assert(from->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
129 ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
130 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
134 void BoostContext::stop()
140 // SerialBoostContext
142 unsigned long SerialBoostContext::process_index_;
144 void SerialBoostContext::suspend()
146 /* determine the next context */
147 SerialBoostContext* next_context;
148 unsigned long int i = process_index_;
151 if (i < simix_global->process_to_run.size()) {
152 /* execute the next process */
153 XBT_DEBUG("Run next process");
154 next_context = static_cast<SerialBoostContext*>(simix_global->process_to_run[i]->context_);
156 /* all processes were run, return to maestro */
157 XBT_DEBUG("No more process to run");
158 next_context = static_cast<SerialBoostContext*>(BoostContext::get_maestro());
160 Context::set_current(next_context);
161 BoostContext::swap(this, next_context);
164 void SerialBoostContext::resume()
166 BoostContext* old = static_cast<BoostContext*>(self());
167 Context::set_current(this);
168 BoostContext::swap(old, this);
171 void SerialBoostContext::run_all()
173 if (simix_global->process_to_run.empty())
175 smx_actor_t first_process = simix_global->process_to_run.front();
177 /* execute the first process */
178 static_cast<SerialBoostContext*>(first_process->context_)->resume();
181 // ParallelBoostContext
183 simgrid::xbt::Parmap<smx_actor_t>* ParallelBoostContext::parmap_;
184 std::atomic<uintptr_t> ParallelBoostContext::threads_working_;
185 thread_local uintptr_t ParallelBoostContext::worker_id_;
186 std::vector<ParallelBoostContext*> ParallelBoostContext::workers_context_;
188 void ParallelBoostContext::initialize()
191 workers_context_.clear();
192 workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
195 void ParallelBoostContext::finalize()
199 workers_context_.clear();
202 void ParallelBoostContext::run_all()
204 threads_working_ = 0;
205 if (parmap_ == nullptr)
206 parmap_ = new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
208 [](smx_actor_t process) {
209 ParallelBoostContext* context = static_cast<ParallelBoostContext*>(process->context_);
212 simix_global->process_to_run);
215 void ParallelBoostContext::suspend()
217 boost::optional<smx_actor_t> next_work = parmap_->next();
218 ParallelBoostContext* next_context;
220 XBT_DEBUG("Run next process");
221 next_context = static_cast<ParallelBoostContext*>(next_work.get()->context_);
223 XBT_DEBUG("No more processes to run");
224 next_context = workers_context_[worker_id_];
227 Context::set_current(next_context);
228 BoostContext::swap(this, next_context);
231 void ParallelBoostContext::resume()
233 worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
235 ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(self());
236 workers_context_[worker_id_] = worker_context;
238 Context::set_current(this);
239 BoostContext::swap(worker_context, this);
243 XBT_PRIVATE ContextFactory* boost_factory()
245 XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
246 return new BoostContextFactory();