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/simix/smx_private.h"
18 #include "src/internal_config.h"
19 #include "src/kernel/context/ContextBoost.hpp"
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
27 class BoostSerialContext : public BoostContext {
29 BoostSerialContext(std::function<void()> code,
30 void_pfn_smxprocess_t cleanup_func,
32 : BoostContext(std::move(code), cleanup_func, process) {}
33 void suspend() override;
36 #if HAVE_THREAD_CONTEXTS
37 class BoostParallelContext : public BoostContext {
39 BoostParallelContext(std::function<void()> code,
40 void_pfn_smxprocess_t cleanup_func,
42 : BoostContext(std::move(code), cleanup_func, process) {}
43 void suspend() override;
44 void resume() override;
48 // BoostContextFactory
50 bool BoostContext::parallel_ = false;
51 simgrid::xbt::Parmap<smx_actor_t>* BoostContext::parmap_ = nullptr;
52 uintptr_t BoostContext::threads_working_ = 0;
53 xbt_os_thread_key_t BoostContext::worker_id_key_;
54 unsigned long BoostContext::process_index_ = 0;
55 BoostContext* BoostContext::maestro_context_ = nullptr;
56 std::vector<BoostContext*> BoostContext::workers_context_;
58 BoostContextFactory::BoostContextFactory()
59 : ContextFactory("BoostContextFactory")
61 BoostContext::parallel_ = SIMIX_context_is_parallel();
62 if (BoostContext::parallel_) {
63 #if HAVE_THREAD_CONTEXTS
64 BoostContext::parmap_ = nullptr;
65 BoostContext::workers_context_.clear();
66 BoostContext::workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
67 BoostContext::maestro_context_ = nullptr;
68 xbt_os_thread_key_create(&BoostContext::worker_id_key_);
70 xbt_die("No thread support for parallel context execution");
75 BoostContextFactory::~BoostContextFactory()
77 #if HAVE_THREAD_CONTEXTS
78 delete BoostContext::parmap_;
79 BoostContext::parmap_ = nullptr;
80 BoostContext::workers_context_.clear();
84 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
87 BoostContext* context = nullptr;
88 if (BoostContext::parallel_)
89 #if HAVE_THREAD_CONTEXTS
90 context = this->new_context<BoostParallelContext>(std::move(code), cleanup_func, process);
92 xbt_die("No support for parallel execution");
95 context = this->new_context<BoostSerialContext>(std::move(code), cleanup_func, process);
99 void BoostContextFactory::run_all()
101 #if HAVE_THREAD_CONTEXTS
102 if (BoostContext::parallel_) {
103 BoostContext::threads_working_ = 0;
104 if (not BoostContext::parmap_)
105 BoostContext::parmap_ =
106 new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
107 BoostContext::parmap_->apply(
108 [](smx_actor_t process) {
109 BoostContext* context = static_cast<BoostContext*>(process->context);
110 return context->resume();
112 simix_global->process_to_run);
116 if (simix_global->process_to_run.empty())
118 smx_actor_t first_process = simix_global->process_to_run.front();
119 BoostContext::process_index_ = 1;
120 /* execute the first process */
121 static_cast<BoostContext*>(first_process->context)->resume();
128 void BoostContext::smx_ctx_boost_wrapper(BoostContext::ctx_arg_type arg)
130 #if BOOST_VERSION < 106100
131 BoostContext* context = reinterpret_cast<BoostContext*>(arg);
133 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
134 BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
138 context->Context::stop();
139 } catch (StopRequest const&) {
140 XBT_DEBUG("Caught a StopRequest");
145 inline void BoostContext::smx_ctx_boost_jump_fcontext(BoostContext* from, BoostContext* to)
147 #if BOOST_VERSION < 105600
148 boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
149 #elif BOOST_VERSION < 106100
150 boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
152 BoostContext* ctx[2] = {from, to};
153 boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
154 static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
158 BoostContext::BoostContext(std::function<void()> code,
159 void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
160 : Context(std::move(code), cleanup_func, process)
163 /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
165 this->stack_ = SIMIX_context_stack_new();
166 // We need to pass the bottom of the stack to make_fcontext, depending on the stack direction it may be the lower
167 // or higher address:
168 #if PTH_STACKGROWTH == -1
169 void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
171 void* stack = this->stack_;
173 #if BOOST_VERSION < 106100
174 this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, smx_ctx_boost_wrapper);
176 this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, smx_ctx_boost_wrapper);
179 #if BOOST_VERSION < 105600
180 this->fc_ = new boost::context::fcontext_t();
182 if (BoostContext::maestro_context_ == nullptr)
183 BoostContext::maestro_context_ = this;
187 BoostContext::~BoostContext()
189 #if BOOST_VERSION < 105600
190 if (not this->stack_)
193 if (this == maestro_context_)
194 maestro_context_ = nullptr;
195 SIMIX_context_stack_delete(this->stack_);
198 // BoostSerialContext
200 void BoostContext::stop()
206 void BoostContext::resume()
208 SIMIX_context_set_current(this);
209 smx_ctx_boost_jump_fcontext(maestro_context_, this);
212 void BoostSerialContext::suspend()
214 /* determine the next context */
215 BoostSerialContext* next_context;
216 unsigned long int i = process_index_;
219 if (i < simix_global->process_to_run.size()) {
220 /* execute the next process */
221 XBT_DEBUG("Run next process");
222 next_context = static_cast<BoostSerialContext*>(simix_global->process_to_run[i]->context);
224 /* all processes were run, return to maestro */
225 XBT_DEBUG("No more process to run");
226 next_context = static_cast<BoostSerialContext*>(maestro_context_);
228 SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
229 smx_ctx_boost_jump_fcontext(this, next_context);
232 // BoostParallelContext
234 #if HAVE_THREAD_CONTEXTS
236 void BoostParallelContext::suspend()
238 boost::optional<smx_actor_t> next_work = parmap_->next();
239 BoostParallelContext* next_context;
241 XBT_DEBUG("Run next process");
242 next_context = static_cast<BoostParallelContext*>(next_work.get()->context);
244 XBT_DEBUG("No more processes to run");
245 uintptr_t worker_id = reinterpret_cast<uintptr_t>(xbt_os_thread_get_specific(worker_id_key_));
246 next_context = static_cast<BoostParallelContext*>(workers_context_[worker_id]);
249 SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
250 smx_ctx_boost_jump_fcontext(this, next_context);
253 void BoostParallelContext::resume()
255 uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1);
256 xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast<void*>(worker_id));
258 BoostParallelContext* worker_context = static_cast<BoostParallelContext*>(SIMIX_context_self());
259 workers_context_[worker_id] = worker_context;
261 SIMIX_context_set_current(this);
262 smx_ctx_boost_jump_fcontext(worker_context, this);
267 XBT_PRIVATE ContextFactory* boost_factory()
269 XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
270 return new BoostContextFactory();