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() : SwappedContextFactory("BoostContextFactory") {}
21 BoostContextFactory::~BoostContextFactory() = default;
23 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
27 return this->new_context<ParallelBoostContext>(std::move(code), cleanup_func, process, this);
28 return this->new_context<BoostContext>(std::move(code), cleanup_func, process, this);
33 BoostContext::BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
34 SwappedContextFactory* factory)
35 : SwappedContext(std::move(code), cleanup_func, process, factory)
38 /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
40 /* We need to pass the bottom of the stack to make_fcontext,
41 depending on the stack direction it may be the lower or higher address: */
42 #if PTH_STACKGROWTH == -1
43 void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
45 void* stack = this->stack_;
47 ASAN_ONLY(this->asan_stack_ = stack);
48 #if BOOST_VERSION < 106100
49 this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
51 this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
54 #if BOOST_VERSION < 105600
55 this->fc_ = new boost::context::fcontext_t();
57 if (get_maestro() == nullptr)
62 BoostContext::~BoostContext()
64 #if BOOST_VERSION < 105600
68 if (this == get_maestro())
72 void BoostContext::wrapper(BoostContext::arg_type arg)
74 #if BOOST_VERSION < 106100
75 BoostContext* context = reinterpret_cast<BoostContext*>(arg);
77 BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
78 ASAN_ONLY(xbt_assert(context->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
79 ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
80 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
84 } catch (StopRequest const&) {
85 XBT_DEBUG("Caught a StopRequest");
86 } catch (simgrid::Exception const& e) {
87 XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
90 context->Context::stop();
91 ASAN_ONLY(context->asan_stop_ = true);
95 void BoostContext::swap_into(SwappedContext* to_)
97 BoostContext* to = static_cast<BoostContext*>(to_);
98 #if BOOST_VERSION < 105600
99 boost::context::jump_fcontext(this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
100 #elif BOOST_VERSION < 106100
101 boost::context::jump_fcontext(&this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
103 BoostContext* ctx[2] = {this, to};
104 ASAN_ONLY(void* fake_stack = nullptr);
105 ASAN_ONLY(to->asan_ctx_ = from);
106 ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
107 boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
108 ASAN_ONLY(xbt_assert(from->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
109 ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
110 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
114 void BoostContext::stop()
120 // ParallelBoostContext
121 void ParallelBoostContext::suspend()
123 boost::optional<smx_actor_t> next_work = parmap_->next();
124 SwappedContext* next_context;
126 XBT_DEBUG("Run next process");
127 next_context = static_cast<ParallelBoostContext*>(next_work.get()->context_);
129 XBT_DEBUG("No more processes to run");
130 next_context = workers_context_[worker_id_];
133 Context::set_current(next_context);
134 this->swap_into(next_context);
137 void ParallelBoostContext::resume()
139 worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
141 SwappedContext* worker_context = static_cast<SwappedContext*>(self());
142 workers_context_[worker_id_] = worker_context;
144 Context::set_current(this);
145 worker_context->swap_into(this);
149 XBT_PRIVATE ContextFactory* boost_factory()
151 XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
152 return new BoostContextFactory();