Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1ba7c367505accf451e91f6b7eb5f28247afb340
[simgrid.git] / src / kernel / context / ContextBoost.cpp
1 /* Copyright (c) 2015-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "ContextBoost.hpp"
7 #include "context_private.hpp"
8 #include "simgrid/Exception.hpp"
9 #include "src/simix/smx_private.hpp"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
12
13 namespace simgrid {
14 namespace kernel {
15 namespace context {
16
17 // BoostContextFactory
18 smx_context_t BoostContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor)
19 {
20   return this->new_context<BoostContext>(std::move(code), actor, this);
21 }
22
23 // BoostContext
24
25 BoostContext::BoostContext(std::function<void()>&& code, actor::ActorImpl* actor, SwappedContextFactory* factory)
26     : SwappedContext(std::move(code), actor, factory)
27 {
28
29   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
30   if (has_code()) {
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;
35 #else
36     unsigned char* stack = get_stack();
37 #endif
38 #if BOOST_VERSION < 106100
39     this->fc_ = boost::context::make_fcontext(stack, smx_context_stack_size, BoostContext::wrapper);
40 #else
41     this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_stack_size, BoostContext::wrapper);
42 #endif
43
44   } else {
45 #if BOOST_VERSION < 105600
46     this->fc_ = new boost::context::fcontext_t();
47 #endif
48   }
49 }
50
51 BoostContext::~BoostContext()
52 {
53 #if BOOST_VERSION < 105600
54   if (not get_stack())
55     delete this->fc_;
56 #endif
57 }
58
59 void BoostContext::wrapper(BoostContext::arg_type arg)
60 {
61 #if BOOST_VERSION < 106100
62   BoostContext* context = reinterpret_cast<BoostContext*>(arg);
63 #else
64   BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
65   ASAN_ONLY(xbt_assert(context->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
66   ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
67   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
68 #endif
69   try {
70     (*context)();
71     context->Context::stop();
72   } catch (ForcefulKillException const&) {
73     XBT_DEBUG("Caught a ForcefulKillException");
74   } catch (simgrid::Exception const& e) {
75     XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
76     throw;
77   }
78   ASAN_ONLY(context->asan_stop_ = true);
79   context->suspend();
80 }
81
82 void BoostContext::swap_into(SwappedContext* to_)
83 {
84   BoostContext* to = static_cast<BoostContext*>(to_);
85 #if BOOST_VERSION < 105600
86   boost::context::jump_fcontext(this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
87 #elif BOOST_VERSION < 106100
88   boost::context::jump_fcontext(&this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
89 #else
90   BoostContext* ctx[2] = {this, to};
91   ASAN_ONLY(void* fake_stack = nullptr);
92   ASAN_ONLY(to->asan_ctx_ = this);
93   ASAN_START_SWITCH(this->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
94   boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
95   ASAN_ONLY(xbt_assert(this->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
96   ASAN_FINISH_SWITCH(fake_stack, &this->asan_ctx_->asan_stack_, &this->asan_ctx_->asan_stack_size_);
97   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
98 #endif
99 }
100
101 XBT_PRIVATE ContextFactory* boost_factory()
102 {
103   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
104   return new BoostContextFactory();
105 }
106 } // namespace context
107 } // namespace kernel
108 } // namespace simgrid