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 boost::context::fcontext_t* fc;
23 } s_smx_ctx_boost_t, *smx_ctx_boost_t;
25 static int smx_ctx_boost_factory_finalize(smx_context_factory_t *factory);
27 smx_ctx_boost_create_context(xbt_main_func_t code, int argc, char **argv,
28 void_pfn_smxprocess_t cleanup_func, smx_process_t process);
29 static void smx_ctx_boost_free(smx_context_t context);
31 static void smx_ctx_boost_wrapper(std::intptr_t arg);
33 static void smx_ctx_boost_stop_serial(smx_context_t context);
34 static void smx_ctx_boost_suspend_serial(smx_context_t context);
35 static void smx_ctx_boost_resume_serial(smx_process_t first_process);
36 static void smx_ctx_boost_runall_serial(void);
38 #ifdef CONTEXT_THREADS
39 static void smx_ctx_boost_stop_parallel(smx_context_t context);
40 static void smx_ctx_boost_suspend_parallel(smx_context_t context);
41 static void smx_ctx_boost_resume_parallel(smx_process_t first_process);
42 static void smx_ctx_boost_runall_parallel(void);
45 #ifdef CONTEXT_THREADS
46 static xbt_parmap_t boost_parmap;
47 static smx_ctx_boost_t* boost_workers_context;
48 static unsigned long boost_threads_working;
49 static xbt_os_thread_key_t boost_worker_id_key;
52 static unsigned long boost_process_index = 0;
53 static smx_ctx_boost_t boost_maestro_context;
55 void SIMIX_ctx_boost_factory_init(smx_context_factory_t *factory)
57 smx_ctx_base_factory_init(factory);
58 XBT_VERB("Activating boost context factory");
60 (*factory)->finalize = smx_ctx_boost_factory_finalize;
61 (*factory)->create_context = smx_ctx_boost_create_context;
62 /* Do not overload that method (*factory)->finalize */
63 (*factory)->free = smx_ctx_boost_free;
64 (*factory)->name = "smx_boost_context_factory";
66 if (SIMIX_context_is_parallel()) {
67 #ifndef CONTEXT_THREADS
68 THROWF(arg_error, 0, "No thread support for parallel context execution");
70 int nthreads = SIMIX_context_get_nthreads();
71 boost_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
72 boost_workers_context = xbt_new(smx_ctx_boost_t, nthreads);
73 boost_maestro_context = NULL;
75 xbt_os_thread_key_create(&boost_worker_id_key);
77 (*factory)->stop = smx_ctx_boost_stop_parallel;
78 (*factory)->suspend = smx_ctx_boost_suspend_parallel;
79 (*factory)->runall = smx_ctx_boost_runall_parallel;
82 (*factory)->stop = smx_ctx_boost_stop_serial;
83 (*factory)->suspend = smx_ctx_boost_suspend_serial;
84 (*factory)->runall = smx_ctx_boost_runall_serial;
88 /* Initialization functions */
90 static int smx_ctx_boost_factory_finalize(smx_context_factory_t *factory)
92 #ifdef CONTEXT_THREADS
94 xbt_parmap_destroy(boost_parmap);
95 boost_parmap = nullptr;
97 xbt_free(boost_workers_context);
98 boost_workers_context = nullptr;
100 return smx_ctx_base_factory_finalize(factory);
104 smx_ctx_boost_create_context(xbt_main_func_t code, int argc, char **argv,
105 void_pfn_smxprocess_t cleanup_func, smx_process_t process)
107 smx_ctx_boost_t context =
108 (smx_ctx_boost_t) smx_ctx_base_factory_create_context_sized(
109 sizeof(s_smx_ctx_boost_t),
116 /* if the user provided a function for the process then use it,
117 otherwise it is the context for maestro */
119 context->stack = SIMIX_context_stack_new();
120 // We need to pass the bottom of the stack to make_fcontext,
121 // depending on the stack direction it may be the lower or higher address:
122 #if PTH_STACKGROWTH == -1
123 void* stack = (char*) context->stack + smx_context_usable_stack_size - 1;
125 void* stack = context->stack;
127 context->fc = boost::context::make_fcontext(
129 smx_context_usable_stack_size,
130 smx_ctx_boost_wrapper);
132 context->stack = nullptr;
133 context->fc = new boost::context::fcontext_t();
134 if (boost_maestro_context == nullptr)
135 boost_maestro_context = context;
138 return (smx_context_t) context;
141 static void smx_ctx_boost_free(smx_context_t c)
143 smx_ctx_boost_t context = (smx_ctx_boost_t) c;
148 if ((smx_ctx_boost_t) c == boost_maestro_context)
149 boost_maestro_context = nullptr;
150 SIMIX_context_stack_delete(context->stack);
151 smx_ctx_base_free(c);
154 static void smx_ctx_boost_wrapper(std::intptr_t arg)
156 smx_context_t context = (smx_context_t) arg;
157 context->code(context->argc, context->argv);
158 smx_ctx_boost_stop_serial(context);
161 static void smx_ctx_boost_stop_serial(smx_context_t context)
163 smx_ctx_base_stop(context);
164 simix_global->context_factory->suspend(context);
167 static void smx_ctx_boost_suspend_serial(smx_context_t context)
169 /* determine the next context */
170 smx_ctx_boost_t next_context;
171 unsigned long int i = boost_process_index++;
173 if (i < xbt_dynar_length(simix_global->process_to_run)) {
174 /* execute the next process */
175 XBT_DEBUG("Run next process");
176 next_context = (smx_ctx_boost_t) xbt_dynar_get_as(
177 simix_global->process_to_run, i, smx_process_t)->context;
180 /* all processes were run, return to maestro */
181 XBT_DEBUG("No more process to run");
182 next_context = (smx_ctx_boost_t) boost_maestro_context;
184 SIMIX_context_set_current((smx_context_t) next_context);
185 boost::context::jump_fcontext(
186 ((smx_ctx_boost_t)context)->fc, next_context->fc, (intptr_t)next_context);
189 static void smx_ctx_boost_resume_serial(smx_process_t first_process)
191 smx_ctx_boost_t context = (smx_ctx_boost_t) first_process->context;
192 SIMIX_context_set_current((smx_context_t) context);
193 boost::context::jump_fcontext(boost_maestro_context->fc, context->fc,
197 static void smx_ctx_boost_runall_serial(void)
199 smx_process_t first_process =
200 xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
201 boost_process_index = 1;
203 /* execute the first process */
204 smx_ctx_boost_resume_serial(first_process);
207 // **** Parallel code
209 #ifdef CONTEXT_THREADS
211 static void smx_ctx_boost_stop_parallel(smx_context_t context)
213 smx_ctx_base_stop(context);
214 smx_ctx_boost_suspend_parallel(context);
217 static void smx_ctx_boost_suspend_parallel(smx_context_t context)
219 smx_process_t next_work = (smx_process_t) xbt_parmap_next(boost_parmap);
220 smx_ctx_boost_t next_context;
222 if (next_work != NULL) {
223 XBT_DEBUG("Run next process");
224 next_context = (smx_ctx_boost_t) next_work->context;
227 XBT_DEBUG("No more processes to run");
228 unsigned long worker_id =
229 (unsigned long) xbt_os_thread_get_specific(boost_worker_id_key);
230 next_context = boost_workers_context[worker_id];
233 SIMIX_context_set_current((smx_context_t) next_context);
234 boost::context::jump_fcontext(
235 ((smx_ctx_boost_t)context)->fc, next_context->fc, (intptr_t)next_context);
238 static void smx_ctx_boost_resume_parallel(smx_process_t process)
240 unsigned long worker_id = __sync_fetch_and_add(&boost_threads_working, 1);
241 xbt_os_thread_set_specific(boost_worker_id_key, (void*) worker_id);
243 smx_ctx_boost_t worker_context = (smx_ctx_boost_t)SIMIX_context_self();
244 boost_workers_context[worker_id] = worker_context;
245 smx_ctx_boost_t context = (smx_ctx_boost_t) process->context;
247 SIMIX_context_set_current((smx_context_t) context);
248 boost::context::jump_fcontext(worker_context->fc, context->fc,
252 static void smx_ctx_boost_runall_parallel(void)
254 boost_threads_working = 0;
255 xbt_parmap_apply(boost_parmap, (void_f_pvoid_t) smx_ctx_boost_resume_parallel,
256 simix_global->process_to_run);