1 /* Copyright (c) 2015-2017. The SimGrid Team. All rights reserved. */
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. */
12 #include <boost/context/all.hpp>
15 #include <xbt/xbt_os_thread.h>
17 #include "src/internal_config.h"
18 #include "src/kernel/context/ContextBoost.hpp"
19 #include "src/simix/smx_private.hpp"
21 #if HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT
22 #include <sanitizer/asan_interface.h>
23 #define ASAN_EVAL(expr) (expr)
24 #define ASAN_START_SWITCH(fake_stack_save, bottom, size) __sanitizer_start_switch_fiber(fake_stack_save, bottom, size)
25 #define ASAN_FINISH_SWITCH(fake_stack_save, bottom_old, size_old) \
26 __sanitizer_finish_switch_fiber(fake_stack_save, bottom_old, size_old)
28 #define ASAN_EVAL(expr) (void)0
29 #define ASAN_START_SWITCH(fake_stack_save, bottom, size) (void)0
30 #define ASAN_FINISH_SWITCH(fake_stack_save, bottom_old, size_old) (void)(fake_stack_save)
33 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
39 class BoostSerialContext : public BoostContext {
41 BoostSerialContext(std::function<void()> code,
42 void_pfn_smxprocess_t cleanup_func,
44 : BoostContext(std::move(code), cleanup_func, process) {}
45 void suspend() override;
48 #if HAVE_THREAD_CONTEXTS
49 class BoostParallelContext : public BoostContext {
51 BoostParallelContext(std::function<void()> code,
52 void_pfn_smxprocess_t cleanup_func,
54 : BoostContext(std::move(code), cleanup_func, process) {}
55 void suspend() override;
56 void resume() override;
60 // BoostContextFactory
62 bool BoostContext::parallel_ = false;
63 simgrid::xbt::Parmap<smx_actor_t>* BoostContext::parmap_ = nullptr;
64 uintptr_t BoostContext::threads_working_ = 0;
65 xbt_os_thread_key_t BoostContext::worker_id_key_;
66 unsigned long BoostContext::process_index_ = 0;
67 BoostContext* BoostContext::maestro_context_ = nullptr;
68 std::vector<BoostContext*> BoostContext::workers_context_;
70 BoostContextFactory::BoostContextFactory()
71 : ContextFactory("BoostContextFactory")
73 BoostContext::parallel_ = SIMIX_context_is_parallel();
74 if (BoostContext::parallel_) {
75 #if HAVE_THREAD_CONTEXTS
76 BoostContext::parmap_ = nullptr;
77 BoostContext::workers_context_.clear();
78 BoostContext::workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
79 BoostContext::maestro_context_ = nullptr;
80 xbt_os_thread_key_create(&BoostContext::worker_id_key_);
82 xbt_die("No thread support for parallel context execution");
87 BoostContextFactory::~BoostContextFactory()
89 #if HAVE_THREAD_CONTEXTS
90 delete BoostContext::parmap_;
91 BoostContext::parmap_ = nullptr;
92 BoostContext::workers_context_.clear();
96 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
99 BoostContext* context = nullptr;
100 if (BoostContext::parallel_)
101 #if HAVE_THREAD_CONTEXTS
102 context = this->new_context<BoostParallelContext>(std::move(code), cleanup_func, process);
104 xbt_die("No support for parallel execution");
107 context = this->new_context<BoostSerialContext>(std::move(code), cleanup_func, process);
111 void BoostContextFactory::run_all()
113 #if HAVE_THREAD_CONTEXTS
114 if (BoostContext::parallel_) {
115 BoostContext::threads_working_ = 0;
116 if (not BoostContext::parmap_)
117 BoostContext::parmap_ =
118 new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
119 BoostContext::parmap_->apply(
120 [](smx_actor_t process) {
121 BoostContext* context = static_cast<BoostContext*>(process->context);
122 return context->resume();
124 simix_global->process_to_run);
128 if (simix_global->process_to_run.empty())
130 smx_actor_t first_process = simix_global->process_to_run.front();
131 BoostContext::process_index_ = 1;
132 /* execute the first process */
133 static_cast<BoostContext*>(first_process->context)->resume();
140 void BoostContext::smx_ctx_boost_wrapper(BoostContext::ctx_arg_type arg)
142 #if BOOST_VERSION < 106100
143 BoostContext* context = reinterpret_cast<BoostContext*>(arg);
145 ASAN_FINISH_SWITCH(nullptr, &static_cast<BoostContext**>(arg.data)[0]->asan_stack_,
146 &static_cast<BoostContext**>(arg.data)[0]->asan_stack_size_);
147 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
148 BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
152 context->Context::stop();
153 } catch (StopRequest const&) {
154 XBT_DEBUG("Caught a StopRequest");
156 ASAN_EVAL(context->asan_stop_ = true);
160 inline void BoostContext::smx_ctx_boost_jump_fcontext(BoostContext* from, BoostContext* to)
162 #if BOOST_VERSION < 105600
163 boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
164 #elif BOOST_VERSION < 106100
165 boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
167 BoostContext* ctx[2] = {from, to};
169 ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
170 boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
171 ASAN_FINISH_SWITCH(fake_stack, &static_cast<BoostContext**>(arg.data)[0]->asan_stack_,
172 &static_cast<BoostContext**>(arg.data)[0]->asan_stack_size_);
173 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
177 BoostContext::BoostContext(std::function<void()> code,
178 void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
179 : Context(std::move(code), cleanup_func, process)
182 /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
184 this->stack_ = SIMIX_context_stack_new();
185 // We need to pass the bottom of the stack to make_fcontext, depending on the stack direction it may be the lower
186 // or higher address:
187 #if PTH_STACKGROWTH == -1
188 void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
190 void* stack = this->stack_;
192 ASAN_EVAL(this->asan_stack_ = stack);
193 #if BOOST_VERSION < 106100
194 this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, smx_ctx_boost_wrapper);
196 this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, smx_ctx_boost_wrapper);
199 #if BOOST_VERSION < 105600
200 this->fc_ = new boost::context::fcontext_t();
202 if (BoostContext::maestro_context_ == nullptr)
203 BoostContext::maestro_context_ = this;
207 BoostContext::~BoostContext()
209 #if BOOST_VERSION < 105600
210 if (not this->stack_)
213 if (this == maestro_context_)
214 maestro_context_ = nullptr;
215 SIMIX_context_stack_delete(this->stack_);
218 // BoostSerialContext
220 void BoostContext::stop()
226 void BoostContext::resume()
228 SIMIX_context_set_current(this);
229 smx_ctx_boost_jump_fcontext(maestro_context_, this);
232 void BoostSerialContext::suspend()
234 /* determine the next context */
235 BoostSerialContext* next_context;
236 unsigned long int i = process_index_;
239 if (i < simix_global->process_to_run.size()) {
240 /* execute the next process */
241 XBT_DEBUG("Run next process");
242 next_context = static_cast<BoostSerialContext*>(simix_global->process_to_run[i]->context);
244 /* all processes were run, return to maestro */
245 XBT_DEBUG("No more process to run");
246 next_context = static_cast<BoostSerialContext*>(maestro_context_);
248 SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
249 smx_ctx_boost_jump_fcontext(this, next_context);
252 // BoostParallelContext
254 #if HAVE_THREAD_CONTEXTS
256 void BoostParallelContext::suspend()
258 boost::optional<smx_actor_t> next_work = parmap_->next();
259 BoostParallelContext* next_context;
261 XBT_DEBUG("Run next process");
262 next_context = static_cast<BoostParallelContext*>(next_work.get()->context);
264 XBT_DEBUG("No more processes to run");
265 uintptr_t worker_id = reinterpret_cast<uintptr_t>(xbt_os_thread_get_specific(worker_id_key_));
266 next_context = static_cast<BoostParallelContext*>(workers_context_[worker_id]);
269 SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
270 smx_ctx_boost_jump_fcontext(this, next_context);
273 void BoostParallelContext::resume()
275 uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1);
276 xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast<void*>(worker_id));
278 BoostParallelContext* worker_context = static_cast<BoostParallelContext*>(SIMIX_context_self());
279 workers_context_[worker_id] = worker_context;
281 SIMIX_context_set_current(this);
282 smx_ctx_boost_jump_fcontext(worker_context, this);
287 XBT_PRIVATE ContextFactory* boost_factory()
289 XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
290 return new BoostContextFactory();