1 /* Copyright (c) 2015-2020. 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 BoostContext* BoostContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor)
20 return this->new_context<BoostContext>(std::move(code), actor, this);
25 BoostContext::BoostContext(std::function<void()>&& code, actor::ActorImpl* actor, SwappedContextFactory* factory)
26 : SwappedContext(std::move(code), actor, factory)
29 /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
31 /* We need to pass the bottom of the stack to make_fcontext,
32 depending on the stack direction it may be the lower or higher address: */
33 #if PTH_STACKGROWTH == -1
34 unsigned char* stack = get_stack() + smx_context_stack_size;
36 unsigned char* stack = get_stack();
38 #if BOOST_VERSION < 106100
39 this->fc_ = boost::context::make_fcontext(stack, smx_context_stack_size, BoostContext::wrapper);
41 this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_stack_size, BoostContext::wrapper);
46 void BoostContext::wrapper(BoostContext::arg_type arg)
48 #if BOOST_VERSION < 106100
49 BoostContext* context = reinterpret_cast<BoostContext*>(arg);
51 BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
52 ASAN_ONLY(xbt_assert(context->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
53 ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
54 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
58 context->Context::stop();
59 } catch (ForcefulKillException const&) {
60 XBT_DEBUG("Caught a ForcefulKillException");
61 } catch (simgrid::Exception const& e) {
62 XBT_INFO("Actor killed by an uncaught exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
65 ASAN_ONLY(context->asan_stop_ = true);
69 void BoostContext::swap_into(SwappedContext* to_)
71 BoostContext* to = static_cast<BoostContext*>(to_);
72 #if BOOST_VERSION < 106100
73 boost::context::jump_fcontext(&this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
75 BoostContext* ctx[2] = {this, to};
76 ASAN_ONLY(void* fake_stack = nullptr);
77 ASAN_ONLY(to->asan_ctx_ = this);
78 ASAN_START_SWITCH(this->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
79 boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
80 ASAN_ONLY(xbt_assert(this->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
81 ASAN_FINISH_SWITCH(fake_stack, &this->asan_ctx_->asan_stack_, &this->asan_ctx_->asan_stack_size_);
82 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
86 XBT_PRIVATE ContextFactory* boost_factory()
88 XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
89 return new BoostContextFactory();
91 } // namespace context
93 } // namespace simgrid