Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change a subclass into a superclass around contexts
[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<SerialBoostContext>(std::move(code), cleanup_func, process);
39 }
40
41 void BoostContextFactory::run_all()
42 {
43   if (parallel_)
44     ParallelBoostContext::run_all();
45   else
46     SerialBoostContext::run_all();
47 }
48
49 // BoostContext
50
51 BoostContext* BoostContext::maestro_context_ = nullptr;
52
53 BoostContext::BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
54     : Context(std::move(code), cleanup_func, process)
55 {
56
57   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
58   if (has_code()) {
59     this->stack_ = SIMIX_context_stack_new();
60     /* We need to pass the bottom of the stack to make_fcontext,
61        depending on the stack direction it may be the lower or higher address: */
62 #if PTH_STACKGROWTH == -1
63     void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
64 #else
65     void* stack = this->stack_;
66 #endif
67     ASAN_ONLY(this->asan_stack_ = stack);
68 #if BOOST_VERSION < 106100
69     this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
70 #else
71     this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
72 #endif
73   } else {
74 #if BOOST_VERSION < 105600
75     this->fc_ = new boost::context::fcontext_t();
76 #endif
77     if (BoostContext::maestro_context_ == nullptr)
78       BoostContext::maestro_context_ = this;
79   }
80 }
81
82 BoostContext::~BoostContext()
83 {
84 #if BOOST_VERSION < 105600
85   if (not this->stack_)
86     delete this->fc_;
87 #endif
88   if (this == maestro_context_)
89     maestro_context_ = nullptr;
90   SIMIX_context_stack_delete(this->stack_);
91 }
92
93 void BoostContext::wrapper(BoostContext::arg_type arg)
94 {
95 #if BOOST_VERSION < 106100
96   BoostContext* context = reinterpret_cast<BoostContext*>(arg);
97 #else
98   BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
99   ASAN_ONLY(xbt_assert(context->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
100   ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
101   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
102 #endif
103   try {
104     (*context)();
105   } catch (StopRequest const&) {
106     XBT_DEBUG("Caught a StopRequest");
107   } catch (simgrid::Exception const& e) {
108     XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
109     throw;
110   }
111   context->Context::stop();
112   ASAN_ONLY(context->asan_stop_ = true);
113   context->suspend();
114 }
115
116 inline void BoostContext::swap(BoostContext* from, BoostContext* to)
117 {
118 #if BOOST_VERSION < 105600
119   boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
120 #elif BOOST_VERSION < 106100
121   boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
122 #else
123   BoostContext* ctx[2] = {from, to};
124   ASAN_ONLY(void* fake_stack = nullptr);
125   ASAN_ONLY(to->asan_ctx_ = from);
126   ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
127   boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
128   ASAN_ONLY(xbt_assert(from->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
129   ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
130   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
131 #endif
132 }
133
134 void BoostContext::stop()
135 {
136   Context::stop();
137   throw StopRequest();
138 }
139
140 // SerialBoostContext
141
142 unsigned long SerialBoostContext::process_index_;
143
144 void SerialBoostContext::suspend()
145 {
146   /* determine the next context */
147   SerialBoostContext* next_context;
148   unsigned long int i = process_index_;
149   process_index_++;
150
151   if (i < simix_global->process_to_run.size()) {
152     /* execute the next process */
153     XBT_DEBUG("Run next process");
154     next_context = static_cast<SerialBoostContext*>(simix_global->process_to_run[i]->context_);
155   } else {
156     /* all processes were run, return to maestro */
157     XBT_DEBUG("No more process to run");
158     next_context = static_cast<SerialBoostContext*>(BoostContext::get_maestro());
159   }
160   Context::set_current(next_context);
161   BoostContext::swap(this, next_context);
162 }
163
164 void SerialBoostContext::resume()
165 {
166   BoostContext* old = static_cast<BoostContext*>(self());
167   Context::set_current(this);
168   BoostContext::swap(old, this);
169 }
170
171 void SerialBoostContext::run_all()
172 {
173   if (simix_global->process_to_run.empty())
174     return;
175   smx_actor_t first_process = simix_global->process_to_run.front();
176   process_index_            = 1;
177   /* execute the first process */
178   static_cast<SerialBoostContext*>(first_process->context_)->resume();
179 }
180
181 // ParallelBoostContext
182
183 simgrid::xbt::Parmap<smx_actor_t>* ParallelBoostContext::parmap_;
184 std::atomic<uintptr_t> ParallelBoostContext::threads_working_;
185 thread_local uintptr_t ParallelBoostContext::worker_id_;
186 std::vector<ParallelBoostContext*> ParallelBoostContext::workers_context_;
187
188 void ParallelBoostContext::initialize()
189 {
190   parmap_ = nullptr;
191   workers_context_.clear();
192   workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
193 }
194
195 void ParallelBoostContext::finalize()
196 {
197   delete parmap_;
198   parmap_ = nullptr;
199   workers_context_.clear();
200 }
201
202 void ParallelBoostContext::run_all()
203 {
204   threads_working_ = 0;
205   if (parmap_ == nullptr)
206     parmap_ = new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
207   parmap_->apply(
208       [](smx_actor_t process) {
209         ParallelBoostContext* context = static_cast<ParallelBoostContext*>(process->context_);
210         context->resume();
211       },
212       simix_global->process_to_run);
213 }
214
215 void ParallelBoostContext::suspend()
216 {
217   boost::optional<smx_actor_t> next_work = parmap_->next();
218   ParallelBoostContext* next_context;
219   if (next_work) {
220     XBT_DEBUG("Run next process");
221     next_context = static_cast<ParallelBoostContext*>(next_work.get()->context_);
222   } else {
223     XBT_DEBUG("No more processes to run");
224     next_context = workers_context_[worker_id_];
225   }
226
227   Context::set_current(next_context);
228   BoostContext::swap(this, next_context);
229 }
230
231 void ParallelBoostContext::resume()
232 {
233   worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
234
235   ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(self());
236   workers_context_[worker_id_]         = worker_context;
237
238   Context::set_current(this);
239   BoostContext::swap(worker_context, this);
240 }
241
242
243 XBT_PRIVATE ContextFactory* boost_factory()
244 {
245   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
246   return new BoostContextFactory();
247 }
248 }}} // namespace