Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
21dc80d097011456a661ea6bc613ff0cba094488
[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
19 BoostContextFactory::BoostContextFactory() : SwappedContextFactory("BoostContextFactory") {}
20
21 BoostContextFactory::~BoostContextFactory() = default;
22
23 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
24                                                   smx_actor_t process)
25 {
26   if (parallel_)
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);
29 }
30
31 // BoostContext
32
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)
36 {
37
38   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
39   if (has_code()) {
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;
44 #else
45     void* stack = this->stack_;
46 #endif
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);
50 #else
51     this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
52 #endif
53   } else {
54 #if BOOST_VERSION < 105600
55     this->fc_ = new boost::context::fcontext_t();
56 #endif
57     if (get_maestro() == nullptr)
58       set_maestro(this);
59   }
60 }
61
62 BoostContext::~BoostContext()
63 {
64 #if BOOST_VERSION < 105600
65   if (not this->stack_)
66     delete this->fc_;
67 #endif
68   if (this == get_maestro())
69     set_maestro(nullptr);
70 }
71
72 void BoostContext::wrapper(BoostContext::arg_type arg)
73 {
74 #if BOOST_VERSION < 106100
75   BoostContext* context = reinterpret_cast<BoostContext*>(arg);
76 #else
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;
81 #endif
82   try {
83     (*context)();
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());
88     throw;
89   }
90   context->Context::stop();
91   ASAN_ONLY(context->asan_stop_ = true);
92   context->suspend();
93 }
94
95 void BoostContext::swap_into(SwappedContext* to_)
96 {
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));
102 #else
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;
111 #endif
112 }
113
114 void BoostContext::stop()
115 {
116   Context::stop();
117   throw StopRequest();
118 }
119
120 // ParallelBoostContext
121 void ParallelBoostContext::suspend()
122 {
123   boost::optional<smx_actor_t> next_work = parmap_->next();
124   SwappedContext* next_context;
125   if (next_work) {
126     XBT_DEBUG("Run next process");
127     next_context = static_cast<ParallelBoostContext*>(next_work.get()->context_);
128   } else {
129     XBT_DEBUG("No more processes to run");
130     next_context = workers_context_[worker_id_];
131   }
132
133   Context::set_current(next_context);
134   this->swap_into(next_context);
135 }
136
137 void ParallelBoostContext::resume()
138 {
139   worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
140
141   SwappedContext* worker_context = static_cast<SwappedContext*>(self());
142   workers_context_[worker_id_]   = worker_context;
143
144   Context::set_current(this);
145   worker_context->swap_into(this);
146 }
147
148
149 XBT_PRIVATE ContextFactory* boost_factory()
150 {
151   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
152   return new BoostContextFactory();
153 }
154 }}} // namespace