Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make SIMIX_context_self() useless and use Context::self() instead
[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   Context::set_current(this);
167   BoostContext::swap(BoostContext::get_maestro(), this);
168 }
169
170 void SerialBoostContext::run_all()
171 {
172   if (simix_global->process_to_run.empty())
173     return;
174   smx_actor_t first_process = simix_global->process_to_run.front();
175   process_index_            = 1;
176   /* execute the first process */
177   static_cast<SerialBoostContext*>(first_process->context_)->resume();
178 }
179
180 // ParallelBoostContext
181
182 simgrid::xbt::Parmap<smx_actor_t>* ParallelBoostContext::parmap_;
183 std::atomic<uintptr_t> ParallelBoostContext::threads_working_;
184 thread_local uintptr_t ParallelBoostContext::worker_id_;
185 std::vector<ParallelBoostContext*> ParallelBoostContext::workers_context_;
186
187 void ParallelBoostContext::initialize()
188 {
189   parmap_ = nullptr;
190   workers_context_.clear();
191   workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
192 }
193
194 void ParallelBoostContext::finalize()
195 {
196   delete parmap_;
197   parmap_ = nullptr;
198   workers_context_.clear();
199 }
200
201 void ParallelBoostContext::run_all()
202 {
203   threads_working_ = 0;
204   if (parmap_ == nullptr)
205     parmap_ = new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
206   parmap_->apply(
207       [](smx_actor_t process) {
208         ParallelBoostContext* context = static_cast<ParallelBoostContext*>(process->context_);
209         context->resume();
210       },
211       simix_global->process_to_run);
212 }
213
214 void ParallelBoostContext::suspend()
215 {
216   boost::optional<smx_actor_t> next_work = parmap_->next();
217   ParallelBoostContext* next_context;
218   if (next_work) {
219     XBT_DEBUG("Run next process");
220     next_context = static_cast<ParallelBoostContext*>(next_work.get()->context_);
221   } else {
222     XBT_DEBUG("No more processes to run");
223     next_context = workers_context_[worker_id_];
224   }
225
226   Context::set_current(next_context);
227   BoostContext::swap(this, next_context);
228 }
229
230 void ParallelBoostContext::resume()
231 {
232   worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
233
234   ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(self());
235   workers_context_[worker_id_]         = worker_context;
236
237   Context::set_current(this);
238   BoostContext::swap(worker_context, this);
239 }
240
241
242 XBT_PRIVATE ContextFactory* boost_factory()
243 {
244   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
245   return new BoostContextFactory();
246 }
247 }}} // namespace