1 /* context_sysv - context switching with ucontextes from System V */
3 /* Copyright (c) 2009, 2010. 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 <win32_ucontext.h> /* context relative declarations */
20 # include <ucontext.h> /* context relative declarations */
23 #ifdef HAVE_VALGRIND_VALGRIND_H
24 # include <valgrind/valgrind.h>
25 #endif /* HAVE_VALGRIND_VALGRIND_H */
27 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
29 typedef struct s_smx_ctx_sysv {
30 s_smx_ctx_base_t super; /* Fields of super implementation */
31 ucontext_t uc; /* the ucontext that executes the code */
32 #ifdef HAVE_VALGRIND_VALGRIND_H
33 unsigned int valgrind_stack_id; /* the valgrind stack id */
35 char stack[0]; /* the thread stack (must remain the last element of the structure) */
36 } s_smx_ctx_sysv_t, *smx_ctx_sysv_t;
38 #ifdef CONTEXT_THREADS
39 static xbt_parmap_t sysv_parmap;
40 static ucontext_t* sysv_workers_stacks; /* space to save the worker's stack in each thread */
41 static unsigned long sysv_threads_working; /* number of threads that have started their work */
42 static xbt_os_thread_key_t sysv_worker_id_key; /* thread-specific storage for the thread id */
44 static unsigned long sysv_process_index = 0; /* index of the next process to run in the
45 * list of runnable processes */
46 static smx_ctx_sysv_t sysv_maestro_context;
48 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory);
50 smx_ctx_sysv_create_context_sized(size_t structure_size,
51 xbt_main_func_t code, int argc,
53 void_pfn_smxprocess_t cleanup_func,
55 static void smx_ctx_sysv_free(smx_context_t context);
57 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
58 void_pfn_smxprocess_t cleanup_func, void* data);
60 static void smx_ctx_sysv_wrapper(int count, ...);
62 static void smx_ctx_sysv_stop_serial(smx_context_t context);
63 static void smx_ctx_sysv_suspend_serial(smx_context_t context);
64 static void smx_ctx_sysv_resume_serial(smx_process_t first_process);
65 static void smx_ctx_sysv_runall_serial(void);
67 static void smx_ctx_sysv_stop_parallel(smx_context_t context);
68 static void smx_ctx_sysv_suspend_parallel(smx_context_t context);
69 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process);
70 static void smx_ctx_sysv_runall_parallel(void);
72 /* This is a bit paranoid about sizeof(smx_ctx_sysv_t) not being a multiple of
73 * sizeof(int), but it doesn't harm. */
74 #define CTX_ADDR_LEN \
75 (sizeof(smx_ctx_sysv_t) / sizeof(int) + \
76 !!(sizeof(smx_ctx_sysv_t) % sizeof(int)))
78 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
80 smx_ctx_base_factory_init(factory);
81 XBT_VERB("Activating SYSV context factory");
83 (*factory)->finalize = smx_ctx_sysv_factory_finalize;
84 (*factory)->create_context = smx_ctx_sysv_create_context;
85 /* Do not overload that method (*factory)->finalize */
86 (*factory)->free = smx_ctx_sysv_free;
87 (*factory)->name = "smx_sysv_context_factory";
89 if (SIMIX_context_is_parallel()) {
90 #ifdef CONTEXT_THREADS /* To use parallel ucontexts a thread pool is needed */
91 int nthreads = SIMIX_context_get_nthreads();
92 sysv_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
93 sysv_workers_stacks = xbt_new(ucontext_t, nthreads);
94 xbt_os_thread_key_create(&sysv_worker_id_key);
95 (*factory)->stop = smx_ctx_sysv_stop_parallel;
96 (*factory)->suspend = smx_ctx_sysv_suspend_parallel;
97 (*factory)->runall = smx_ctx_sysv_runall_parallel;
99 THROWF(arg_error, 0, "No thread support for parallel context execution");
102 (*factory)->stop = smx_ctx_sysv_stop_serial;
103 (*factory)->suspend = smx_ctx_sysv_suspend_serial;
104 (*factory)->runall = smx_ctx_sysv_runall_serial;
108 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
110 #ifdef CONTEXT_THREADS
112 xbt_parmap_destroy(sysv_parmap);
113 xbt_free(sysv_workers_stacks);
115 return smx_ctx_base_factory_finalize(factory);
119 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
120 int argc, char **argv,
121 void_pfn_smxprocess_t cleanup_func,
124 int ctx_addr[CTX_ADDR_LEN];
125 smx_ctx_sysv_t context =
126 (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
133 /* if the user provided a function for the process then use it,
134 otherwise it is the context for maestro */
137 getcontext(&(context->uc));
139 context->uc.uc_link = NULL;
141 context->uc.uc_stack.ss_sp =
142 pth_skaddr_makecontext(context->stack, smx_context_stack_size);
144 context->uc.uc_stack.ss_size =
145 pth_sksize_makecontext(context->stack, smx_context_stack_size);
147 #ifdef HAVE_VALGRIND_VALGRIND_H
148 context->valgrind_stack_id =
149 VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
150 ((char *) context->uc.uc_stack.ss_sp) +
151 context->uc.uc_stack.ss_size);
152 #endif /* HAVE_VALGRIND_VALGRIND_H */
153 memcpy(ctx_addr, &context, sizeof(smx_ctx_sysv_t));
154 switch (CTX_ADDR_LEN) {
156 makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
160 makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
161 2, ctx_addr[0], ctx_addr[1]);
164 xbt_die("Ucontexts are not supported on this arch yet (addr len = %zu/%zu = %zu)",
165 sizeof(smx_ctx_sysv_t), sizeof(int), CTX_ADDR_LEN);
168 sysv_maestro_context = context;
171 if(MC_is_active() && code)
172 MC_new_stack_area(context, ((smx_process_t)((smx_context_t)context)->data)->name, &(context->uc), size);
174 return (smx_context_t) context;
178 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
179 void_pfn_smxprocess_t cleanup_func,
183 return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t) + smx_context_stack_size,
184 code, argc, argv, cleanup_func,
189 static void smx_ctx_sysv_free(smx_context_t context)
194 #ifdef HAVE_VALGRIND_VALGRIND_H
195 VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
196 context)->valgrind_stack_id);
197 #endif /* HAVE_VALGRIND_VALGRIND_H */
200 smx_ctx_base_free(context);
203 static void smx_ctx_sysv_wrapper(int first, ...)
205 int ctx_addr[CTX_ADDR_LEN];
206 smx_ctx_sysv_t context;
209 if (CTX_ADDR_LEN > 1) {
213 for (i = 1; i < CTX_ADDR_LEN; i++)
214 ctx_addr[i] = va_arg(ap, int);
217 memcpy(&context, ctx_addr, sizeof(smx_ctx_sysv_t));
218 (context->super.code) (context->super.argc, context->super.argv);
220 simix_global->context_factory->stop((smx_context_t) context);
223 static void smx_ctx_sysv_stop_serial(smx_context_t context)
225 smx_ctx_base_stop(context);
226 smx_ctx_sysv_suspend_serial(context);
229 static void smx_ctx_sysv_suspend_serial(smx_context_t context)
231 /* determine the next context */
232 smx_context_t next_context;
233 unsigned long int i = sysv_process_index++;
236 MC_ignore_stack("next_context", "smx_ctx_sysv_suspend_serial");
237 MC_ignore_stack("i", "smx_ctx_sysv_suspend_serial");
240 if (i < xbt_dynar_length(simix_global->process_to_run)) {
241 /* execute the next process */
242 XBT_DEBUG("Run next process");
243 next_context = xbt_dynar_get_as(
244 simix_global->process_to_run,i, smx_process_t)->context;
247 /* all processes were run, return to maestro */
248 XBT_DEBUG("No more process to run");
249 next_context = (smx_context_t) sysv_maestro_context;
251 SIMIX_context_set_current(next_context);
252 swapcontext(&((smx_ctx_sysv_t) context)->uc,
253 &((smx_ctx_sysv_t) next_context)->uc);
256 static void smx_ctx_sysv_resume_serial(smx_process_t first_process)
258 smx_context_t context = first_process->context;
259 SIMIX_context_set_current(context);
260 swapcontext(&sysv_maestro_context->uc,
261 &((smx_ctx_sysv_t) context)->uc);
264 static void smx_ctx_sysv_runall_serial(void)
266 smx_process_t first_process =
267 xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
268 sysv_process_index = 1;
270 /* execute the first process */
271 smx_ctx_sysv_resume_serial(first_process);
274 static void smx_ctx_sysv_stop_parallel(smx_context_t context)
276 smx_ctx_base_stop(context);
277 smx_ctx_sysv_suspend_parallel(context);
280 static void smx_ctx_sysv_suspend_parallel(smx_context_t context)
282 #ifdef CONTEXT_THREADS
283 /* determine the next context */
284 smx_process_t next_work = xbt_parmap_next(sysv_parmap);
285 smx_context_t next_context;
286 ucontext_t* next_stack;
288 if (next_work != NULL) {
289 /* there is a next process to resume */
290 XBT_DEBUG("Run next process");
291 next_context = next_work->context;
292 next_stack = &((smx_ctx_sysv_t) next_context)->uc;
295 /* all processes were run, go to the barrier */
296 XBT_DEBUG("No more processes to run");
297 next_context = (smx_context_t) sysv_maestro_context;
298 unsigned long worker_id =
299 (unsigned long) xbt_os_thread_get_specific(sysv_worker_id_key);
300 next_stack = &sysv_workers_stacks[worker_id];
303 SIMIX_context_set_current(next_context);
304 swapcontext(&((smx_ctx_sysv_t) context)->uc, next_stack);
308 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process)
310 #ifdef CONTEXT_THREADS
311 unsigned long worker_id = __sync_fetch_and_add(&sysv_threads_working, 1);
312 xbt_os_thread_set_specific(sysv_worker_id_key, (void*) worker_id);
313 ucontext_t* worker_stack = &sysv_workers_stacks[worker_id];
315 smx_context_t context = first_process->context;
316 SIMIX_context_set_current(context);
317 swapcontext(worker_stack, &((smx_ctx_sysv_t) context)->uc);
321 static void smx_ctx_sysv_runall_parallel(void)
323 #ifdef CONTEXT_THREADS
324 sysv_threads_working = 0;
325 xbt_parmap_apply(sysv_parmap, (void_f_pvoid_t) smx_ctx_sysv_resume_parallel,
326 simix_global->process_to_run);