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<BoostContext>(std::move(code), cleanup_func, process);
41 void BoostContextFactory::run_all()
44 ParallelBoostContext::run_all();
46 SwappedContext::run_all();
51 BoostContext::BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
52 : SwappedContext(std::move(code), cleanup_func, process)
55 /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
57 /* We need to pass the bottom of the stack to make_fcontext,
58 depending on the stack direction it may be the lower or higher address: */
59 #if PTH_STACKGROWTH == -1
60 void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
62 void* stack = this->stack_;
64 ASAN_ONLY(this->asan_stack_ = stack);
65 #if BOOST_VERSION < 106100
66 this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
68 this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
71 #if BOOST_VERSION < 105600
72 this->fc_ = new boost::context::fcontext_t();
74 if (get_maestro() == nullptr)
79 BoostContext::~BoostContext()
81 #if BOOST_VERSION < 105600
85 if (this == get_maestro())
89 void BoostContext::wrapper(BoostContext::arg_type arg)
91 #if BOOST_VERSION < 106100
92 BoostContext* context = reinterpret_cast<BoostContext*>(arg);
94 BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
95 ASAN_ONLY(xbt_assert(context->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
96 ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
97 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
101 } catch (StopRequest const&) {
102 XBT_DEBUG("Caught a StopRequest");
103 } catch (simgrid::Exception const& e) {
104 XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
107 context->Context::stop();
108 ASAN_ONLY(context->asan_stop_ = true);
112 void BoostContext::swap_into(SwappedContext* to_)
114 BoostContext* to = static_cast<BoostContext*>(to_);
115 #if BOOST_VERSION < 105600
116 boost::context::jump_fcontext(this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
117 #elif BOOST_VERSION < 106100
118 boost::context::jump_fcontext(&this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
120 BoostContext* ctx[2] = {this, to};
121 ASAN_ONLY(void* fake_stack = nullptr);
122 ASAN_ONLY(to->asan_ctx_ = from);
123 ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
124 boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
125 ASAN_ONLY(xbt_assert(from->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
126 ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
127 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
131 void BoostContext::stop()
137 // ParallelBoostContext
139 simgrid::xbt::Parmap<smx_actor_t>* ParallelBoostContext::parmap_;
140 std::atomic<uintptr_t> ParallelBoostContext::threads_working_;
141 thread_local uintptr_t ParallelBoostContext::worker_id_;
142 std::vector<ParallelBoostContext*> ParallelBoostContext::workers_context_;
144 void ParallelBoostContext::initialize()
147 workers_context_.clear();
148 workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
151 void ParallelBoostContext::finalize()
155 workers_context_.clear();
158 void ParallelBoostContext::run_all()
160 threads_working_ = 0;
161 if (parmap_ == nullptr)
162 parmap_ = new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
164 [](smx_actor_t process) {
165 ParallelBoostContext* context = static_cast<ParallelBoostContext*>(process->context_);
168 simix_global->process_to_run);
171 void ParallelBoostContext::suspend()
173 boost::optional<smx_actor_t> next_work = parmap_->next();
174 ParallelBoostContext* next_context;
176 XBT_DEBUG("Run next process");
177 next_context = static_cast<ParallelBoostContext*>(next_work.get()->context_);
179 XBT_DEBUG("No more processes to run");
180 next_context = workers_context_[worker_id_];
183 Context::set_current(next_context);
184 this->swap_into(next_context);
187 void ParallelBoostContext::resume()
189 worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
191 ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(self());
192 workers_context_[worker_id_] = worker_context;
194 Context::set_current(this);
195 worker_context->swap_into(this);
199 XBT_PRIVATE ContextFactory* boost_factory()
201 XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
202 return new BoostContextFactory();