Logo AND Algorithmique Numérique Distribuée

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