Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
49cbec80a852184fb27f61f7d442800a315f70f8
[simgrid.git] / src / kernel / context / ContextBoost.cpp
1 /* Copyright (c) 2015-2018. 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, void_pfn_smxprocess_t cleanup_func,
19                                                   smx_actor_t process)
20 {
21   return this->new_context<BoostContext>(std::move(code), cleanup_func, process, this);
22 }
23
24 // BoostContext
25
26 BoostContext::BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
27                            SwappedContextFactory* factory)
28     : SwappedContext(std::move(code), cleanup_func, process, factory)
29 {
30
31   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
32   if (has_code()) {
33     /* We need to pass the bottom of the stack to make_fcontext,
34        depending on the stack direction it may be the lower or higher address: */
35 #if PTH_STACKGROWTH == -1
36     void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
37 #else
38     void* stack = this->stack_;
39 #endif
40     ASAN_ONLY(this->asan_stack_ = stack);
41 #if BOOST_VERSION < 106100
42     this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
43 #else
44     this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
45 #endif
46   } else {
47 #if BOOST_VERSION < 105600
48     this->fc_ = new boost::context::fcontext_t();
49 #endif
50     if (get_maestro() == nullptr)
51       set_maestro(this);
52   }
53 }
54
55 BoostContext::~BoostContext()
56 {
57 #if BOOST_VERSION < 105600
58   if (not this->stack_)
59     delete this->fc_;
60 #endif
61   if (this == get_maestro())
62     set_maestro(nullptr);
63 }
64
65 void BoostContext::wrapper(BoostContext::arg_type arg)
66 {
67 #if BOOST_VERSION < 106100
68   BoostContext* context = reinterpret_cast<BoostContext*>(arg);
69 #else
70   BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
71   ASAN_ONLY(xbt_assert(context->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
72   ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
73   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
74 #endif
75   try {
76     (*context)();
77   } catch (StopRequest const&) {
78     XBT_DEBUG("Caught a StopRequest");
79   } catch (simgrid::Exception const& e) {
80     XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
81     throw;
82   }
83   context->Context::stop();
84   ASAN_ONLY(context->asan_stop_ = true);
85   context->suspend();
86 }
87
88 void BoostContext::swap_into(SwappedContext* to_)
89 {
90   BoostContext* to = static_cast<BoostContext*>(to_);
91 #if BOOST_VERSION < 105600
92   boost::context::jump_fcontext(this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
93 #elif BOOST_VERSION < 106100
94   boost::context::jump_fcontext(&this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
95 #else
96   BoostContext* ctx[2] = {this, to};
97   ASAN_ONLY(void* fake_stack = nullptr);
98   ASAN_ONLY(to->asan_ctx_ = from);
99   ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
100   boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
101   ASAN_ONLY(xbt_assert(from->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
102   ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
103   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
104 #endif
105 }
106
107 XBT_PRIVATE ContextFactory* boost_factory()
108 {
109   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
110   return new BoostContextFactory();
111 }
112 }}} // namespace