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
18 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
22 return this->new_context<ParallelBoostContext>(std::move(code), cleanup_func, process, this);
23 return this->new_context<BoostContext>(std::move(code), cleanup_func, process, this);
28 BoostContext::BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
29 SwappedContextFactory* factory)
30 : SwappedContext(std::move(code), cleanup_func, process, factory)
33 /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
35 /* We need to pass the bottom of the stack to make_fcontext,
36 depending on the stack direction it may be the lower or higher address: */
37 #if PTH_STACKGROWTH == -1
38 void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
40 void* stack = this->stack_;
42 ASAN_ONLY(this->asan_stack_ = stack);
43 #if BOOST_VERSION < 106100
44 this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
46 this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
49 #if BOOST_VERSION < 105600
50 this->fc_ = new boost::context::fcontext_t();
52 if (get_maestro() == nullptr)
57 BoostContext::~BoostContext()
59 #if BOOST_VERSION < 105600
63 if (this == get_maestro())
67 void BoostContext::wrapper(BoostContext::arg_type arg)
69 #if BOOST_VERSION < 106100
70 BoostContext* context = reinterpret_cast<BoostContext*>(arg);
72 BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
73 ASAN_ONLY(xbt_assert(context->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
74 ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
75 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
79 } catch (StopRequest const&) {
80 XBT_DEBUG("Caught a StopRequest");
81 } catch (simgrid::Exception const& e) {
82 XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
85 context->Context::stop();
86 ASAN_ONLY(context->asan_stop_ = true);
90 void BoostContext::swap_into(SwappedContext* to_)
92 BoostContext* to = static_cast<BoostContext*>(to_);
93 #if BOOST_VERSION < 105600
94 boost::context::jump_fcontext(this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
95 #elif BOOST_VERSION < 106100
96 boost::context::jump_fcontext(&this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
98 BoostContext* ctx[2] = {this, to};
99 ASAN_ONLY(void* fake_stack = nullptr);
100 ASAN_ONLY(to->asan_ctx_ = from);
101 ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
102 boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
103 ASAN_ONLY(xbt_assert(from->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
104 ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
105 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
109 // ParallelBoostContext
110 void ParallelBoostContext::suspend()
112 boost::optional<smx_actor_t> next_work = parmap_->next();
113 SwappedContext* next_context;
115 XBT_DEBUG("Run next process");
116 next_context = static_cast<ParallelBoostContext*>(next_work.get()->context_);
118 XBT_DEBUG("No more processes to run");
119 next_context = workers_context_[worker_id_];
122 Context::set_current(next_context);
123 this->swap_into(next_context);
126 void ParallelBoostContext::resume()
128 worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
130 SwappedContext* worker_context = static_cast<SwappedContext*>(self());
131 workers_context_[worker_id_] = worker_context;
133 Context::set_current(this);
134 worker_context->swap_into(this);
138 XBT_PRIVATE ContextFactory* boost_factory()
140 XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
141 return new BoostContextFactory();