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::HostFailureException const&) {
120 XBT_DEBUG("Caught an HostFailureException");
122 context->Context::stop();
123 ASAN_ONLY(context->asan_stop_ = true);
127 inline void BoostContext::swap(BoostContext* from, BoostContext* to)
129 #if BOOST_VERSION < 105600
130 boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
131 #elif BOOST_VERSION < 106100
132 boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
134 BoostContext* ctx[2] = {from, to};
135 ASAN_ONLY(void* fake_stack = nullptr);
136 ASAN_ONLY(to->asan_ctx_ = from);
137 ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
138 boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
139 ASAN_ONLY(xbt_assert(from->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
140 ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
141 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
145 void BoostContext::stop()
151 // SerialBoostContext
153 unsigned long SerialBoostContext::process_index_;
155 void SerialBoostContext::suspend()
157 /* determine the next context */
158 SerialBoostContext* next_context;
159 unsigned long int i = process_index_;
162 if (i < simix_global->process_to_run.size()) {
163 /* execute the next process */
164 XBT_DEBUG("Run next process");
165 next_context = static_cast<SerialBoostContext*>(simix_global->process_to_run[i]->context_);
167 /* all processes were run, return to maestro */
168 XBT_DEBUG("No more process to run");
169 next_context = static_cast<SerialBoostContext*>(BoostContext::getMaestro());
171 SIMIX_context_set_current(next_context);
172 BoostContext::swap(this, next_context);
175 void SerialBoostContext::resume()
177 SIMIX_context_set_current(this);
178 BoostContext::swap(BoostContext::getMaestro(), this);
181 void SerialBoostContext::run_all()
183 if (simix_global->process_to_run.empty())
185 smx_actor_t first_process = simix_global->process_to_run.front();
187 /* execute the first process */
188 static_cast<SerialBoostContext*>(first_process->context_)->resume();
191 // ParallelBoostContext
193 #if HAVE_THREAD_CONTEXTS
195 simgrid::xbt::Parmap<smx_actor_t>* ParallelBoostContext::parmap_;
196 std::atomic<uintptr_t> ParallelBoostContext::threads_working_;
197 thread_local uintptr_t ParallelBoostContext::worker_id_;
198 std::vector<ParallelBoostContext*> ParallelBoostContext::workers_context_;
200 void ParallelBoostContext::initialize()
203 workers_context_.clear();
204 workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
207 void ParallelBoostContext::finalize()
211 workers_context_.clear();
214 void ParallelBoostContext::run_all()
216 threads_working_ = 0;
217 if (parmap_ == nullptr)
218 parmap_ = new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
220 [](smx_actor_t process) {
221 ParallelBoostContext* context = static_cast<ParallelBoostContext*>(process->context_);
224 simix_global->process_to_run);
227 void ParallelBoostContext::suspend()
229 boost::optional<smx_actor_t> next_work = parmap_->next();
230 ParallelBoostContext* next_context;
232 XBT_DEBUG("Run next process");
233 next_context = static_cast<ParallelBoostContext*>(next_work.get()->context_);
235 XBT_DEBUG("No more processes to run");
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 worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
247 ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(SIMIX_context_self());
248 workers_context_[worker_id_] = worker_context;
250 SIMIX_context_set_current(this);
251 BoostContext::swap(worker_context, this);
256 XBT_PRIVATE ContextFactory* boost_factory()
258 XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
259 return new BoostContextFactory();