1 /* Copyright (c) 2015. 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 "smx_private.h"
18 #include "src/internal_config.h"
19 #include "src/simix/ContextBoost.hpp"
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
26 class BoostSerialContext : public BoostContext {
28 BoostSerialContext(std::function<void()> code,
29 void_pfn_smxprocess_t cleanup_func,
30 smx_process_t process)
31 : BoostContext(std::move(code), cleanup_func, process) {}
33 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,
42 smx_process_t process)
43 : BoostContext(std::move(code), cleanup_func, process) {}
45 void suspend() override;
50 // BoostContextFactory
52 bool BoostContext::parallel_ = false;
53 xbt_parmap_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 xbt_die("No thread support for parallel context execution");
68 int nthreads = SIMIX_context_get_nthreads();
69 BoostContext::parmap_ = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
70 BoostContext::workers_context_.clear();
71 BoostContext::workers_context_.resize(nthreads, nullptr);
72 BoostContext::maestro_context_ = nullptr;
73 xbt_os_thread_key_create(&BoostContext::worker_id_key_);
78 BoostContextFactory::~BoostContextFactory()
80 #if HAVE_THREAD_CONTEXTS
81 if (BoostContext::parmap_) {
82 xbt_parmap_destroy(BoostContext::parmap_);
83 BoostContext::parmap_ = nullptr;
85 BoostContext::workers_context_.clear();
89 smx_context_t BoostContextFactory::create_context(std::function<void()> code,
90 void_pfn_smxprocess_t cleanup_func, smx_process_t process)
92 BoostContext* context = nullptr;
93 if (BoostContext::parallel_)
94 #if HAVE_THREAD_CONTEXTS
95 context = this->new_context<BoostParallelContext>(
96 std::move(code), cleanup_func, process);
98 xbt_die("No support for parallel execution");
101 context = this->new_context<BoostSerialContext>(
102 std::move(code), cleanup_func, process);
106 void BoostContextFactory::run_all()
108 #if HAVE_THREAD_CONTEXTS
109 if (BoostContext::parallel_) {
110 BoostContext::threads_working_ = 0;
111 xbt_parmap_apply(BoostContext::parmap_,
113 smx_process_t process = static_cast<smx_process_t>(arg);
114 BoostContext* context = static_cast<BoostContext*>(process->context);
115 return context->resume();
117 simix_global->process_to_run);
121 smx_process_t first_process =
122 xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
123 BoostContext::process_index_ = 1;
124 /* execute the first process */
125 static_cast<BoostContext*>(first_process->context)->resume();
132 static void smx_ctx_boost_wrapper(std::intptr_t arg)
134 BoostContext* context = (BoostContext*) arg;
139 BoostContext::BoostContext(std::function<void()> code,
140 void_pfn_smxprocess_t cleanup_func, smx_process_t process)
141 : Context(std::move(code), cleanup_func, process)
144 /* if the user provided a function for the process then use it,
145 otherwise it is the context for maestro */
147 this->stack_ = SIMIX_context_stack_new();
148 // We need to pass the bottom of the stack to make_fcontext,
149 // depending on the stack direction it may be the lower or higher address:
150 #if PTH_STACKGROWTH == -1
151 void* stack = (char*) this->stack_ + smx_context_usable_stack_size - 1;
153 void* stack = this->stack_;
155 this->fc_ = boost::context::make_fcontext(
157 smx_context_usable_stack_size,
158 smx_ctx_boost_wrapper);
160 #if HAVE_BOOST_CONTEXTS == 1
161 this->fc_ = new boost::context::fcontext_t();
163 if (BoostContext::maestro_context_ == nullptr)
164 BoostContext::maestro_context_ = this;
168 BoostContext::~BoostContext()
170 #if HAVE_BOOST_CONTEXTS == 1
174 if (this == maestro_context_)
175 maestro_context_ = nullptr;
176 SIMIX_context_stack_delete(this->stack_);
179 // BoostSerialContext
181 void BoostContext::resume()
183 SIMIX_context_set_current(this);
184 #if HAVE_BOOST_CONTEXTS == 1
185 boost::context::jump_fcontext(
186 maestro_context_->fc_, this->fc_,
189 boost::context::jump_fcontext(
190 &maestro_context_->fc_, this->fc_,
195 void BoostSerialContext::suspend()
197 /* determine the next context */
198 BoostSerialContext* next_context = nullptr;
199 unsigned long int i = process_index_++;
201 if (i < xbt_dynar_length(simix_global->process_to_run)) {
202 /* execute the next process */
203 XBT_DEBUG("Run next process");
204 next_context = static_cast<BoostSerialContext*>(xbt_dynar_get_as(
205 simix_global->process_to_run, i, smx_process_t)->context);
208 /* all processes were run, return to maestro */
209 XBT_DEBUG("No more process to run");
210 next_context = static_cast<BoostSerialContext*>(
213 SIMIX_context_set_current((smx_context_t) next_context);
214 #if HAVE_BOOST_CONTEXTS == 1
215 boost::context::jump_fcontext(
216 this->fc_, next_context->fc_, (intptr_t) next_context);
218 boost::context::jump_fcontext(
219 &this->fc_, next_context->fc_, (intptr_t) next_context);
223 void BoostSerialContext::stop()
225 BoostContext::stop();
229 // BoostParallelContext
231 #if HAVE_THREAD_CONTEXTS
233 void BoostParallelContext::suspend()
235 smx_process_t next_work = (smx_process_t) xbt_parmap_next(parmap_);
236 BoostParallelContext* next_context = nullptr;
238 if (next_work != nullptr) {
239 XBT_DEBUG("Run next process");
240 next_context = static_cast<BoostParallelContext*>(next_work->context);
243 XBT_DEBUG("No more processes to run");
244 uintptr_t worker_id =
245 (uintptr_t) xbt_os_thread_get_specific(worker_id_key_);
246 next_context = static_cast<BoostParallelContext*>(
247 workers_context_[worker_id]);
250 SIMIX_context_set_current((smx_context_t) next_context);
251 #if HAVE_BOOST_CONTEXTS == 1
252 boost::context::jump_fcontext(
253 this->fc_, next_context->fc_, (intptr_t)next_context);
255 boost::context::jump_fcontext(
256 &this->fc_, next_context->fc_, (intptr_t)next_context);
260 void BoostParallelContext::stop()
262 BoostContext::stop();
266 void BoostParallelContext::resume()
268 uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1);
269 xbt_os_thread_set_specific(worker_id_key_, (void*) worker_id);
271 BoostParallelContext* worker_context =
272 static_cast<BoostParallelContext*>(SIMIX_context_self());
273 workers_context_[worker_id] = worker_context;
275 SIMIX_context_set_current(this);
276 #if HAVE_BOOST_CONTEXTS == 1
277 boost::context::jump_fcontext(
278 worker_context->fc_, this->fc_, (intptr_t) this);
280 boost::context::jump_fcontext(
281 &worker_context->fc_, this->fc_, (intptr_t) this);
287 XBT_PRIVATE ContextFactory* boost_factory()
289 XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
290 return new BoostContextFactory();