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) {}
34 void suspend() override;
37 #if HAVE_THREAD_CONTEXTS
38 class BoostParallelContext : public BoostContext {
40 BoostParallelContext(std::function<void()> code,
41 void_pfn_smxprocess_t cleanup_func,
43 : BoostContext(std::move(code), cleanup_func, process) {}
45 void suspend() override;
46 void resume() override;
50 // BoostContextFactory
52 bool BoostContext::parallel_ = false;
53 simgrid::xbt::Parmap<smx_actor_t>* BoostContext::parmap_ = nullptr;
54 uintptr_t BoostContext::threads_working_ = 0;
55 xbt_os_thread_key_t BoostContext::worker_id_key_;
56 unsigned long BoostContext::process_index_ = 0;
57 BoostContext* BoostContext::maestro_context_ = nullptr;
58 std::vector<BoostContext*> BoostContext::workers_context_;
60 BoostContextFactory::BoostContextFactory()
61 : ContextFactory("BoostContextFactory")
63 BoostContext::parallel_ = SIMIX_context_is_parallel();
64 if (BoostContext::parallel_) {
65 #if HAVE_THREAD_CONTEXTS
66 int nthreads = SIMIX_context_get_nthreads();
67 BoostContext::parmap_ = new simgrid::xbt::Parmap<smx_actor_t>(nthreads, SIMIX_context_get_parallel_mode());
68 BoostContext::workers_context_.clear();
69 BoostContext::workers_context_.resize(nthreads, nullptr);
70 BoostContext::maestro_context_ = nullptr;
71 xbt_os_thread_key_create(&BoostContext::worker_id_key_);
73 xbt_die("No thread support for parallel context execution");
78 BoostContextFactory::~BoostContextFactory()
80 #if HAVE_THREAD_CONTEXTS
81 if (BoostContext::parmap_) {
82 delete BoostContext::parmap_;
83 BoostContext::parmap_ = nullptr;
85 BoostContext::workers_context_.clear();
89 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
92 BoostContext* context = nullptr;
93 if (BoostContext::parallel_)
94 #if HAVE_THREAD_CONTEXTS
95 context = this->new_context<BoostParallelContext>(std::move(code), cleanup_func, process);
97 xbt_die("No support for parallel execution");
100 context = this->new_context<BoostSerialContext>(std::move(code), cleanup_func, process);
104 void BoostContextFactory::run_all()
106 #if HAVE_THREAD_CONTEXTS
107 if (BoostContext::parallel_) {
108 BoostContext::threads_working_ = 0;
109 BoostContext::parmap_->apply(
110 [](smx_actor_t process) {
111 BoostContext* context = static_cast<BoostContext*>(process->context);
112 return context->resume();
114 simix_global->process_to_run);
118 if (simix_global->process_to_run.empty())
120 smx_actor_t first_process = simix_global->process_to_run.front();
121 BoostContext::process_index_ = 1;
122 /* execute the first process */
123 static_cast<BoostContext*>(first_process->context)->resume();
130 static void smx_ctx_boost_wrapper(std::intptr_t arg)
132 BoostContext* context = reinterpret_cast<BoostContext*>(arg);
137 BoostContext::BoostContext(std::function<void()> code,
138 void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
139 : Context(std::move(code), cleanup_func, process)
142 /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
144 this->stack_ = SIMIX_context_stack_new();
145 // We need to pass the bottom of the stack to make_fcontext, depending on the stack direction it may be the lower
146 // or higher address:
147 #if PTH_STACKGROWTH == -1
148 void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size - 1;
150 void* stack = this->stack_;
152 this->fc_ = boost::context::make_fcontext(
154 smx_context_usable_stack_size,
155 smx_ctx_boost_wrapper);
157 #if HAVE_BOOST_CONTEXTS == 1
158 this->fc_ = new boost::context::fcontext_t();
160 if (BoostContext::maestro_context_ == nullptr)
161 BoostContext::maestro_context_ = this;
165 BoostContext::~BoostContext()
167 #if HAVE_BOOST_CONTEXTS == 1
168 if (not this->stack_)
171 if (this == maestro_context_)
172 maestro_context_ = nullptr;
173 SIMIX_context_stack_delete(this->stack_);
176 // BoostSerialContext
178 void BoostContext::resume()
180 SIMIX_context_set_current(this);
181 #if HAVE_BOOST_CONTEXTS == 1
182 boost::context::jump_fcontext(maestro_context_->fc_, this->fc_, reinterpret_cast<intptr_t>(this));
184 boost::context::jump_fcontext(&maestro_context_->fc_, this->fc_, reinterpret_cast<intptr_t>(this));
188 void BoostSerialContext::suspend()
190 /* determine the next context */
191 BoostSerialContext* next_context = nullptr;
192 unsigned long int i = process_index_;
195 if (i < simix_global->process_to_run.size()) {
196 /* execute the next process */
197 XBT_DEBUG("Run next process");
198 next_context = static_cast<BoostSerialContext*>(simix_global->process_to_run[i]->context);
200 /* all processes were run, return to maestro */
201 XBT_DEBUG("No more process to run");
202 next_context = static_cast<BoostSerialContext*>(maestro_context_);
204 SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
205 #if HAVE_BOOST_CONTEXTS == 1
206 boost::context::jump_fcontext(this->fc_, next_context->fc_, reinterpret_cast<pintptr_t>(next_context));
208 boost::context::jump_fcontext(&this->fc_, next_context->fc_, reinterpret_cast<intptr_t>(next_context));
212 void BoostSerialContext::stop()
214 BoostContext::stop();
218 // BoostParallelContext
220 #if HAVE_THREAD_CONTEXTS
222 void BoostParallelContext::suspend()
224 boost::optional<smx_actor_t> next_work = parmap_->next();
225 BoostParallelContext* next_context;
227 XBT_DEBUG("Run next process");
228 next_context = static_cast<BoostParallelContext*>(next_work.get()->context);
230 XBT_DEBUG("No more processes to run");
231 uintptr_t worker_id = reinterpret_cast<uintptr_t>(xbt_os_thread_get_specific(worker_id_key_));
232 next_context = static_cast<BoostParallelContext*>(workers_context_[worker_id]);
235 SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
236 #if HAVE_BOOST_CONTEXTS == 1
237 boost::context::jump_fcontext(this->fc_, next_context->fc_, reinterpret_cast<intptr_t>(next_context));
239 boost::context::jump_fcontext(&this->fc_, next_context->fc_, reinterpret_cast<intptr_t>(next_context));
243 void BoostParallelContext::stop()
245 BoostContext::stop();
249 void BoostParallelContext::resume()
251 uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1);
252 xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast<void*>(worker_id));
254 BoostParallelContext* worker_context = static_cast<BoostParallelContext*>(SIMIX_context_self());
255 workers_context_[worker_id] = worker_context;
257 SIMIX_context_set_current(this);
258 #if HAVE_BOOST_CONTEXTS == 1
259 boost::context::jump_fcontext(worker_context->fc_, this->fc_, reinterpret_cast<intptr_t>(this));
261 boost::context::jump_fcontext(&worker_context->fc_, this->fc_, reinterpret_cast<intptr_t>(this));
267 XBT_PRIVATE ContextFactory* boost_factory()
269 XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
270 return new BoostContextFactory();