Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ac3313465fe0297e39185aaa365c7d893c0bb17c
[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()
20     : ContextFactory("BoostContextFactory"), parallel_(SIMIX_context_is_parallel())
21 {
22   BoostContext::set_maestro(nullptr);
23   if (parallel_)
24     ParallelBoostContext::initialize();
25 }
26
27 BoostContextFactory::~BoostContextFactory()
28 {
29   if (parallel_)
30     ParallelBoostContext::finalize();
31 }
32
33 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
34                                                   smx_actor_t process)
35 {
36   if (parallel_)
37     return this->new_context<ParallelBoostContext>(std::move(code), cleanup_func, process);
38   return this->new_context<BoostContext>(std::move(code), cleanup_func, process);
39 }
40
41 void BoostContextFactory::run_all()
42 {
43   if (parallel_)
44     ParallelBoostContext::run_all();
45   else
46     SwappedContext::run_all();
47 }
48
49 // BoostContext
50
51 BoostContext::BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
52     : SwappedContext(std::move(code), cleanup_func, process)
53 {
54
55   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
56   if (has_code()) {
57     /* We need to pass the bottom of the stack to make_fcontext,
58        depending on the stack direction it may be the lower or higher address: */
59 #if PTH_STACKGROWTH == -1
60     void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
61 #else
62     void* stack = this->stack_;
63 #endif
64     ASAN_ONLY(this->asan_stack_ = stack);
65 #if BOOST_VERSION < 106100
66     this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
67 #else
68     this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
69 #endif
70   } else {
71 #if BOOST_VERSION < 105600
72     this->fc_ = new boost::context::fcontext_t();
73 #endif
74     if (get_maestro() == nullptr)
75       set_maestro(this);
76   }
77 }
78
79 BoostContext::~BoostContext()
80 {
81 #if BOOST_VERSION < 105600
82   if (not this->stack_)
83     delete this->fc_;
84 #endif
85   if (this == get_maestro())
86     set_maestro(nullptr);
87 }
88
89 void BoostContext::wrapper(BoostContext::arg_type arg)
90 {
91 #if BOOST_VERSION < 106100
92   BoostContext* context = reinterpret_cast<BoostContext*>(arg);
93 #else
94   BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
95   ASAN_ONLY(xbt_assert(context->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
96   ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
97   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
98 #endif
99   try {
100     (*context)();
101   } catch (StopRequest const&) {
102     XBT_DEBUG("Caught a StopRequest");
103   } catch (simgrid::Exception const& e) {
104     XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
105     throw;
106   }
107   context->Context::stop();
108   ASAN_ONLY(context->asan_stop_ = true);
109   context->suspend();
110 }
111
112 void BoostContext::swap_into(SwappedContext* to_)
113 {
114   BoostContext* to = static_cast<BoostContext*>(to_);
115 #if BOOST_VERSION < 105600
116   boost::context::jump_fcontext(this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
117 #elif BOOST_VERSION < 106100
118   boost::context::jump_fcontext(&this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
119 #else
120   BoostContext* ctx[2] = {this, to};
121   ASAN_ONLY(void* fake_stack = nullptr);
122   ASAN_ONLY(to->asan_ctx_ = from);
123   ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
124   boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
125   ASAN_ONLY(xbt_assert(from->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
126   ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
127   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
128 #endif
129 }
130
131 void BoostContext::stop()
132 {
133   Context::stop();
134   throw StopRequest();
135 }
136
137 // ParallelBoostContext
138
139 simgrid::xbt::Parmap<smx_actor_t>* ParallelBoostContext::parmap_;
140 std::atomic<uintptr_t> ParallelBoostContext::threads_working_;
141 thread_local uintptr_t ParallelBoostContext::worker_id_;
142 std::vector<ParallelBoostContext*> ParallelBoostContext::workers_context_;
143
144 void ParallelBoostContext::initialize()
145 {
146   parmap_ = nullptr;
147   workers_context_.clear();
148   workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
149 }
150
151 void ParallelBoostContext::finalize()
152 {
153   delete parmap_;
154   parmap_ = nullptr;
155   workers_context_.clear();
156 }
157
158 void ParallelBoostContext::run_all()
159 {
160   threads_working_ = 0;
161   if (parmap_ == nullptr)
162     parmap_ = new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
163   parmap_->apply(
164       [](smx_actor_t process) {
165         ParallelBoostContext* context = static_cast<ParallelBoostContext*>(process->context_);
166         context->resume();
167       },
168       simix_global->process_to_run);
169 }
170
171 void ParallelBoostContext::suspend()
172 {
173   boost::optional<smx_actor_t> next_work = parmap_->next();
174   ParallelBoostContext* next_context;
175   if (next_work) {
176     XBT_DEBUG("Run next process");
177     next_context = static_cast<ParallelBoostContext*>(next_work.get()->context_);
178   } else {
179     XBT_DEBUG("No more processes to run");
180     next_context = workers_context_[worker_id_];
181   }
182
183   Context::set_current(next_context);
184   this->swap_into(next_context);
185 }
186
187 void ParallelBoostContext::resume()
188 {
189   worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
190
191   ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(self());
192   workers_context_[worker_id_]         = worker_context;
193
194   Context::set_current(this);
195   worker_context->swap_into(this);
196 }
197
198
199 XBT_PRIVATE ContextFactory* boost_factory()
200 {
201   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
202   return new BoostContextFactory();
203 }
204 }}} // namespace