1 /* Copyright (c) 2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 /** @file smx_context_boost.cpp Userspace context switching implementation based on Boost.Context */
11 #include <boost/context/all.hpp>
14 #include "smx_private.h"
15 #include "internal_config.h"
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
19 typedef struct s_smx_ctx_boost {
20 s_smx_ctx_base_t super; /* Fields of super implementation */
21 #if HAVE_BOOST_CONTEXT == 1
22 boost::context::fcontext_t* fc;
24 boost::context::fcontext_t fc;
27 } s_smx_ctx_boost_t, *smx_ctx_boost_t;
29 static int smx_ctx_boost_factory_finalize(smx_context_factory_t *factory);
31 smx_ctx_boost_create_context(xbt_main_func_t code, int argc, char **argv,
32 void_pfn_smxprocess_t cleanup_func, smx_process_t process);
33 static void smx_ctx_boost_free(smx_context_t context);
35 static void smx_ctx_boost_wrapper(std::intptr_t arg);
37 static void smx_ctx_boost_stop_serial(smx_context_t context);
38 static void smx_ctx_boost_suspend_serial(smx_context_t context);
39 static void smx_ctx_boost_resume_serial(smx_process_t first_process);
40 static void smx_ctx_boost_runall_serial(void);
42 #ifdef CONTEXT_THREADS
43 static void smx_ctx_boost_stop_parallel(smx_context_t context);
44 static void smx_ctx_boost_suspend_parallel(smx_context_t context);
45 static void smx_ctx_boost_resume_parallel(smx_process_t first_process);
46 static void smx_ctx_boost_runall_parallel(void);
49 #ifdef CONTEXT_THREADS
50 static xbt_parmap_t boost_parmap;
51 static smx_ctx_boost_t* boost_workers_context;
52 static unsigned long boost_threads_working;
53 static xbt_os_thread_key_t boost_worker_id_key;
56 static unsigned long boost_process_index = 0;
57 static smx_ctx_boost_t boost_maestro_context;
59 void SIMIX_ctx_boost_factory_init(smx_context_factory_t *factory)
61 smx_ctx_base_factory_init(factory);
62 XBT_VERB("Activating boost context factory");
64 (*factory)->finalize = smx_ctx_boost_factory_finalize;
65 (*factory)->create_context = smx_ctx_boost_create_context;
66 /* Do not overload that method (*factory)->finalize */
67 (*factory)->free = smx_ctx_boost_free;
68 (*factory)->name = "smx_boost_context_factory";
70 if (SIMIX_context_is_parallel()) {
71 #ifndef CONTEXT_THREADS
72 THROWF(arg_error, 0, "No thread support for parallel context execution");
74 int nthreads = SIMIX_context_get_nthreads();
75 boost_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
76 boost_workers_context = xbt_new(smx_ctx_boost_t, nthreads);
77 boost_maestro_context = NULL;
79 xbt_os_thread_key_create(&boost_worker_id_key);
81 (*factory)->stop = smx_ctx_boost_stop_parallel;
82 (*factory)->suspend = smx_ctx_boost_suspend_parallel;
83 (*factory)->runall = smx_ctx_boost_runall_parallel;
86 (*factory)->stop = smx_ctx_boost_stop_serial;
87 (*factory)->suspend = smx_ctx_boost_suspend_serial;
88 (*factory)->runall = smx_ctx_boost_runall_serial;
92 /* Initialization functions */
94 static int smx_ctx_boost_factory_finalize(smx_context_factory_t *factory)
96 #ifdef CONTEXT_THREADS
98 xbt_parmap_destroy(boost_parmap);
99 boost_parmap = nullptr;
101 xbt_free(boost_workers_context);
102 boost_workers_context = nullptr;
104 return smx_ctx_base_factory_finalize(factory);
108 smx_ctx_boost_create_context(xbt_main_func_t code, int argc, char **argv,
109 void_pfn_smxprocess_t cleanup_func, smx_process_t process)
111 smx_ctx_boost_t context =
112 (smx_ctx_boost_t) smx_ctx_base_factory_create_context_sized(
113 sizeof(s_smx_ctx_boost_t),
120 /* if the user provided a function for the process then use it,
121 otherwise it is the context for maestro */
123 context->stack = SIMIX_context_stack_new();
124 // We need to pass the bottom of the stack to make_fcontext,
125 // depending on the stack direction it may be the lower or higher address:
126 #if PTH_STACKGROWTH == -1
127 void* stack = (char*) context->stack + smx_context_usable_stack_size - 1;
129 void* stack = context->stack;
131 context->fc = boost::context::make_fcontext(
133 smx_context_usable_stack_size,
134 smx_ctx_boost_wrapper);
136 context->stack = nullptr;
137 #if HAVE_BOOST_CONTEXT == 1
138 context->fc = new boost::context::fcontext_t();
140 if (boost_maestro_context == nullptr)
141 boost_maestro_context = context;
144 return (smx_context_t) context;
147 static void smx_ctx_boost_free(smx_context_t c)
149 smx_ctx_boost_t context = (smx_ctx_boost_t) c;
152 #if HAVE_BOOST_CONTEXT == 1
156 if ((smx_ctx_boost_t) c == boost_maestro_context)
157 boost_maestro_context = nullptr;
158 SIMIX_context_stack_delete(context->stack);
159 smx_ctx_base_free(c);
162 static void smx_ctx_boost_wrapper(std::intptr_t arg)
164 smx_context_t context = (smx_context_t) arg;
165 context->code(context->argc, context->argv);
166 smx_ctx_boost_stop_serial(context);
169 static void smx_ctx_boost_stop_serial(smx_context_t context)
171 smx_ctx_base_stop(context);
172 simix_global->context_factory->suspend(context);
175 static void smx_ctx_boost_suspend_serial(smx_context_t context)
177 /* determine the next context */
178 smx_ctx_boost_t next_context;
179 unsigned long int i = boost_process_index++;
181 if (i < xbt_dynar_length(simix_global->process_to_run)) {
182 /* execute the next process */
183 XBT_DEBUG("Run next process");
184 next_context = (smx_ctx_boost_t) xbt_dynar_get_as(
185 simix_global->process_to_run, i, smx_process_t)->context;
188 /* all processes were run, return to maestro */
189 XBT_DEBUG("No more process to run");
190 next_context = (smx_ctx_boost_t) boost_maestro_context;
192 SIMIX_context_set_current((smx_context_t) next_context);
193 #if HAVE_BOOST_CONTEXT == 1
194 boost::context::jump_fcontext(
195 ((smx_ctx_boost_t)context)->fc, next_context->fc, (intptr_t)next_context);
197 boost::context::jump_fcontext(
198 &((smx_ctx_boost_t)context)->fc, next_context->fc, (intptr_t)next_context);
202 static void smx_ctx_boost_resume_serial(smx_process_t first_process)
204 smx_ctx_boost_t context = (smx_ctx_boost_t) first_process->context;
205 SIMIX_context_set_current((smx_context_t) context);
206 #if HAVE_BOOST_CONTEXT == 1
207 boost::context::jump_fcontext(boost_maestro_context->fc, context->fc,
210 boost::context::jump_fcontext(&boost_maestro_context->fc, context->fc,
215 static void smx_ctx_boost_runall_serial(void)
217 smx_process_t first_process =
218 xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
219 boost_process_index = 1;
221 /* execute the first process */
222 smx_ctx_boost_resume_serial(first_process);
225 // **** Parallel code
227 #ifdef CONTEXT_THREADS
229 static void smx_ctx_boost_stop_parallel(smx_context_t context)
231 smx_ctx_base_stop(context);
232 smx_ctx_boost_suspend_parallel(context);
235 static void smx_ctx_boost_suspend_parallel(smx_context_t context)
237 smx_process_t next_work = (smx_process_t) xbt_parmap_next(boost_parmap);
238 smx_ctx_boost_t next_context;
240 if (next_work != NULL) {
241 XBT_DEBUG("Run next process");
242 next_context = (smx_ctx_boost_t) next_work->context;
245 XBT_DEBUG("No more processes to run");
246 unsigned long worker_id =
247 (unsigned long) xbt_os_thread_get_specific(boost_worker_id_key);
248 next_context = boost_workers_context[worker_id];
251 SIMIX_context_set_current((smx_context_t) next_context);
252 #if HAVE_BOOST_CONTEXT == 1
253 boost::context::jump_fcontext(
254 ((smx_ctx_boost_t)context)->fc, next_context->fc, (intptr_t)next_context);
256 boost::context::jump_fcontext(
257 &((smx_ctx_boost_t)context)->fc, next_context->fc, (intptr_t)next_context);
261 static void smx_ctx_boost_resume_parallel(smx_process_t process)
263 unsigned long worker_id = __sync_fetch_and_add(&boost_threads_working, 1);
264 xbt_os_thread_set_specific(boost_worker_id_key, (void*) worker_id);
266 smx_ctx_boost_t worker_context = (smx_ctx_boost_t)SIMIX_context_self();
267 boost_workers_context[worker_id] = worker_context;
268 smx_ctx_boost_t context = (smx_ctx_boost_t) process->context;
270 SIMIX_context_set_current((smx_context_t) context);
271 #if HAVE_BOOST_CONTEXT == 1
272 boost::context::jump_fcontext(worker_context->fc, context->fc,
275 boost::context::jump_fcontext(&worker_context->fc, context->fc,
280 static void smx_ctx_boost_runall_parallel(void)
282 boost_threads_working = 0;
283 xbt_parmap_apply(boost_parmap, (void_f_pvoid_t) smx_ctx_boost_resume_parallel,
284 simix_global->process_to_run);