Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Properly kill the context on HostFailureException
[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::HostFailureException const&) {
120     XBT_DEBUG("Caught an HostFailureException");
121   }
122   context->Context::stop();
123   ASAN_ONLY(context->asan_stop_ = true);
124   context->suspend();
125 }
126
127 inline void BoostContext::swap(BoostContext* from, BoostContext* to)
128 {
129 #if BOOST_VERSION < 105600
130   boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
131 #elif BOOST_VERSION < 106100
132   boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
133 #else
134   BoostContext* ctx[2] = {from, to};
135   ASAN_ONLY(void* fake_stack = nullptr);
136   ASAN_ONLY(to->asan_ctx_ = from);
137   ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
138   boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
139   ASAN_ONLY(xbt_assert(from->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
140   ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
141   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
142 #endif
143 }
144
145 void BoostContext::stop()
146 {
147   Context::stop();
148   throw StopRequest();
149 }
150
151 // SerialBoostContext
152
153 unsigned long SerialBoostContext::process_index_;
154
155 void SerialBoostContext::suspend()
156 {
157   /* determine the next context */
158   SerialBoostContext* next_context;
159   unsigned long int i = process_index_;
160   process_index_++;
161
162   if (i < simix_global->process_to_run.size()) {
163     /* execute the next process */
164     XBT_DEBUG("Run next process");
165     next_context = static_cast<SerialBoostContext*>(simix_global->process_to_run[i]->context_);
166   } else {
167     /* all processes were run, return to maestro */
168     XBT_DEBUG("No more process to run");
169     next_context = static_cast<SerialBoostContext*>(BoostContext::getMaestro());
170   }
171   SIMIX_context_set_current(next_context);
172   BoostContext::swap(this, next_context);
173 }
174
175 void SerialBoostContext::resume()
176 {
177   SIMIX_context_set_current(this);
178   BoostContext::swap(BoostContext::getMaestro(), this);
179 }
180
181 void SerialBoostContext::run_all()
182 {
183   if (simix_global->process_to_run.empty())
184     return;
185   smx_actor_t first_process = simix_global->process_to_run.front();
186   process_index_            = 1;
187   /* execute the first process */
188   static_cast<SerialBoostContext*>(first_process->context_)->resume();
189 }
190
191 // ParallelBoostContext
192
193 #if HAVE_THREAD_CONTEXTS
194
195 simgrid::xbt::Parmap<smx_actor_t>* ParallelBoostContext::parmap_;
196 std::atomic<uintptr_t> ParallelBoostContext::threads_working_;
197 thread_local uintptr_t ParallelBoostContext::worker_id_;
198 std::vector<ParallelBoostContext*> ParallelBoostContext::workers_context_;
199
200 void ParallelBoostContext::initialize()
201 {
202   parmap_ = nullptr;
203   workers_context_.clear();
204   workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
205 }
206
207 void ParallelBoostContext::finalize()
208 {
209   delete parmap_;
210   parmap_ = nullptr;
211   workers_context_.clear();
212 }
213
214 void ParallelBoostContext::run_all()
215 {
216   threads_working_ = 0;
217   if (parmap_ == nullptr)
218     parmap_ = new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
219   parmap_->apply(
220       [](smx_actor_t process) {
221         ParallelBoostContext* context = static_cast<ParallelBoostContext*>(process->context_);
222         context->resume();
223       },
224       simix_global->process_to_run);
225 }
226
227 void ParallelBoostContext::suspend()
228 {
229   boost::optional<smx_actor_t> next_work = parmap_->next();
230   ParallelBoostContext* next_context;
231   if (next_work) {
232     XBT_DEBUG("Run next process");
233     next_context = static_cast<ParallelBoostContext*>(next_work.get()->context_);
234   } else {
235     XBT_DEBUG("No more processes to run");
236     next_context = workers_context_[worker_id_];
237   }
238
239   SIMIX_context_set_current(next_context);
240   BoostContext::swap(this, next_context);
241 }
242
243 void ParallelBoostContext::resume()
244 {
245   worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
246
247   ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(SIMIX_context_self());
248   workers_context_[worker_id_]         = worker_context;
249
250   SIMIX_context_set_current(this);
251   BoostContext::swap(worker_context, this);
252 }
253
254 #endif
255
256 XBT_PRIVATE ContextFactory* boost_factory()
257 {
258   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
259   return new BoostContextFactory();
260 }
261 }}} // namespace