1 /* context_sysv - context switching with ucontexts from System V */
3 /* Copyright (c) 2009-2014. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "xbt/parmap.h"
12 #include "smx_private.h"
13 #include "internal_config.h"
14 #include "context_sysv_config.h" /* loads context system definitions */
18 # include <xbt/win32_ucontext.h> /* context relative declarations */
20 # include <ucontext.h> /* context relative declarations */
23 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
25 typedef struct s_smx_ctx_sysv {
26 s_smx_ctx_base_t super; /* Fields of super implementation */
27 ucontext_t uc; /* the ucontext that executes the code */
28 char *stack; /* the thread stack */
29 } s_smx_ctx_sysv_t, *smx_ctx_sysv_t;
31 #ifdef CONTEXT_THREADS
32 static xbt_parmap_t sysv_parmap;
33 static smx_ctx_sysv_t* sysv_workers_context; /* space to save the worker's context in each thread */
34 static unsigned long sysv_threads_working; /* number of threads that have started their work */
35 static xbt_os_thread_key_t sysv_worker_id_key; /* thread-specific storage for the thread id */
37 static unsigned long sysv_process_index = 0; /* index of the next process to run in the
38 * list of runnable processes */
39 static smx_ctx_sysv_t sysv_maestro_context;
41 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory);
42 static void smx_ctx_sysv_free(smx_context_t context);
44 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
45 void_pfn_smxprocess_t cleanup_func, smx_process_t process);
47 static void smx_ctx_sysv_wrapper(int count, ...);
49 static void smx_ctx_sysv_stop_serial(smx_context_t context);
50 static void smx_ctx_sysv_suspend_serial(smx_context_t context);
51 static void smx_ctx_sysv_resume_serial(smx_process_t first_process);
52 static void smx_ctx_sysv_runall_serial(void);
54 static void smx_ctx_sysv_stop_parallel(smx_context_t context);
55 static void smx_ctx_sysv_suspend_parallel(smx_context_t context);
56 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process);
57 static void smx_ctx_sysv_runall_parallel(void);
59 /* This is a bit paranoid about sizeof(smx_ctx_sysv_t) not being a multiple of
60 * sizeof(int), but it doesn't harm. */
61 #define CTX_ADDR_LEN \
62 (sizeof(smx_ctx_sysv_t) / sizeof(int) + \
63 !!(sizeof(smx_ctx_sysv_t) % sizeof(int)))
65 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
67 smx_ctx_base_factory_init(factory);
68 XBT_VERB("Activating SYSV context factory");
70 (*factory)->finalize = smx_ctx_sysv_factory_finalize;
71 (*factory)->create_context = smx_ctx_sysv_create_context;
72 /* Do not overload that method (*factory)->finalize */
73 (*factory)->free = smx_ctx_sysv_free;
74 (*factory)->name = "smx_sysv_context_factory";
76 if (SIMIX_context_is_parallel()) {
77 #ifdef CONTEXT_THREADS /* To use parallel ucontexts a thread pool is needed */
78 int nthreads = SIMIX_context_get_nthreads();
79 sysv_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
80 sysv_workers_context = xbt_new(smx_ctx_sysv_t, nthreads);
81 sysv_maestro_context = NULL;
82 xbt_os_thread_key_create(&sysv_worker_id_key);
83 (*factory)->stop = smx_ctx_sysv_stop_parallel;
84 (*factory)->suspend = smx_ctx_sysv_suspend_parallel;
85 (*factory)->runall = smx_ctx_sysv_runall_parallel;
87 THROWF(arg_error, 0, "No thread support for parallel context execution");
90 (*factory)->stop = smx_ctx_sysv_stop_serial;
91 (*factory)->suspend = smx_ctx_sysv_suspend_serial;
92 (*factory)->runall = smx_ctx_sysv_runall_serial;
96 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
98 #ifdef CONTEXT_THREADS
100 xbt_parmap_destroy(sysv_parmap);
101 xbt_free(sysv_workers_context);
103 return smx_ctx_base_factory_finalize(factory);
107 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
108 void_pfn_smxprocess_t cleanup_func,
109 smx_process_t process)
111 int ctx_addr[CTX_ADDR_LEN];
112 smx_ctx_sysv_t context =
113 (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(
114 sizeof(s_smx_ctx_sysv_t),
121 /* if the user provided a function for the process then use it,
122 otherwise it is the context for maestro */
125 context->stack = SIMIX_context_stack_new();
126 getcontext(&(context->uc));
128 context->uc.uc_link = NULL;
130 context->uc.uc_stack.ss_sp =
131 pth_skaddr_makecontext(context->stack, smx_context_usable_stack_size);
133 context->uc.uc_stack.ss_size =
134 pth_sksize_makecontext(context->stack, smx_context_usable_stack_size);
136 memcpy(ctx_addr, &context, sizeof(smx_ctx_sysv_t));
137 switch (CTX_ADDR_LEN) {
139 makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
143 makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
144 2, ctx_addr[0], ctx_addr[1]);
147 xbt_die("Ucontexts are not supported on this arch yet (addr len = %zu/%zu = %zu)",
148 sizeof(smx_ctx_sysv_t), sizeof(int), CTX_ADDR_LEN);
151 if (process != NULL && sysv_maestro_context == NULL)
152 sysv_maestro_context = context;
155 if(MC_is_active() && code)
156 MC_new_stack_area(context->stack, ((smx_context_t)context)->process->name,
157 &(context->uc), smx_context_usable_stack_size);
159 return (smx_context_t) context;
162 static void smx_ctx_sysv_free(smx_context_t context)
166 SIMIX_context_stack_delete(((smx_ctx_sysv_t)context)->stack);
168 smx_ctx_base_free(context);
171 static void smx_ctx_sysv_wrapper(int first, ...)
173 int ctx_addr[CTX_ADDR_LEN];
174 smx_ctx_sysv_t context;
177 if (CTX_ADDR_LEN > 1) {
181 for (i = 1; i < CTX_ADDR_LEN; i++)
182 ctx_addr[i] = va_arg(ap, int);
185 memcpy(&context, ctx_addr, sizeof(smx_ctx_sysv_t));
186 (context->super.code) (context->super.argc, context->super.argv);
188 simix_global->context_factory->stop((smx_context_t) context);
191 static void smx_ctx_sysv_stop_serial(smx_context_t context)
193 smx_ctx_base_stop(context);
194 smx_ctx_sysv_suspend_serial(context);
197 static void smx_ctx_sysv_suspend_serial(smx_context_t context)
199 /* determine the next context */
200 smx_context_t next_context;
201 unsigned long int i = sysv_process_index++;
203 if (i < xbt_dynar_length(simix_global->process_to_run)) {
204 /* execute the next process */
205 XBT_DEBUG("Run next process");
206 next_context = xbt_dynar_get_as(
207 simix_global->process_to_run,i, smx_process_t)->context;
210 /* all processes were run, return to maestro */
211 XBT_DEBUG("No more process to run");
212 next_context = (smx_context_t) sysv_maestro_context;
214 SIMIX_context_set_current(next_context);
215 swapcontext(&((smx_ctx_sysv_t) context)->uc,
216 &((smx_ctx_sysv_t) next_context)->uc);
219 static void smx_ctx_sysv_resume_serial(smx_process_t first_process)
221 smx_context_t context = first_process->context;
222 SIMIX_context_set_current(context);
223 swapcontext(&sysv_maestro_context->uc,
224 &((smx_ctx_sysv_t) context)->uc);
227 static void smx_ctx_sysv_runall_serial(void)
229 smx_process_t first_process =
230 xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
231 sysv_process_index = 1;
233 /* execute the first process */
234 smx_ctx_sysv_resume_serial(first_process);
237 static void smx_ctx_sysv_stop_parallel(smx_context_t context)
239 smx_ctx_base_stop(context);
240 smx_ctx_sysv_suspend_parallel(context);
243 static void smx_ctx_sysv_suspend_parallel(smx_context_t context)
245 #ifdef CONTEXT_THREADS
246 /* determine the next context */
247 smx_process_t next_work = xbt_parmap_next(sysv_parmap);
248 smx_context_t next_context;
249 ucontext_t* next_stack;
251 if (next_work != NULL) {
252 /* there is a next process to resume */
253 XBT_DEBUG("Run next process");
254 next_context = next_work->context;
255 next_stack = &((smx_ctx_sysv_t) next_context)->uc;
258 /* all processes were run, go to the barrier */
259 XBT_DEBUG("No more processes to run");
260 unsigned long worker_id =
261 (unsigned long) xbt_os_thread_get_specific(sysv_worker_id_key);
262 next_context = (smx_context_t)sysv_workers_context[worker_id];
263 next_stack = &((smx_ctx_sysv_t)next_context)->uc;
266 SIMIX_context_set_current(next_context);
267 swapcontext(&((smx_ctx_sysv_t) context)->uc, next_stack);
271 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process)
273 #ifdef CONTEXT_THREADS
274 unsigned long worker_id = __sync_fetch_and_add(&sysv_threads_working, 1);
275 xbt_os_thread_set_specific(sysv_worker_id_key, (void*) worker_id);
276 smx_ctx_sysv_t worker_context = (smx_ctx_sysv_t)SIMIX_context_self();
277 sysv_workers_context[worker_id] = worker_context;
278 ucontext_t* worker_stack = &worker_context->uc;
280 smx_context_t context = first_process->context;
281 SIMIX_context_set_current(context);
282 swapcontext(worker_stack, &((smx_ctx_sysv_t) context)->uc);
286 static void smx_ctx_sysv_runall_parallel(void)
288 #ifdef CONTEXT_THREADS
289 sysv_threads_working = 0;
290 xbt_parmap_apply(sysv_parmap, (void_f_pvoid_t) smx_ctx_sysv_resume_parallel,
291 simix_global->process_to_run);