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 "simix/private.h"
13 #include "gras_config.h"
14 #include "context_sysv_config.h" /* loads context system definitions */
17 # include <win32_ucontext.h> /* context relative declarations */
19 # include <ucontext.h> /* context relative declarations */
22 #ifdef HAVE_VALGRIND_VALGRIND_H
23 # include <valgrind/valgrind.h>
24 #endif /* HAVE_VALGRIND_VALGRIND_H */
26 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
28 typedef struct s_smx_ctx_sysv {
29 s_smx_ctx_base_t super; /* Fields of super implementation */
30 ucontext_t uc; /* the ucontext that executes the code */
31 #ifdef HAVE_VALGRIND_VALGRIND_H
32 unsigned int valgrind_stack_id; /* the valgrind stack id */
34 char stack[0]; /* the thread stack (must remain the last element of the structure) */
35 } s_smx_ctx_sysv_t, *smx_ctx_sysv_t;
37 #ifdef CONTEXT_THREADS
38 static xbt_parmap_t sysv_parmap;
39 static ucontext_t* sysv_workers_stacks; /* space to save the worker's stack in each thread */
41 static unsigned long sysv_process_index = 0; /* index of the next process to run in the
42 * list of runnable processes */
43 static smx_ctx_sysv_t sysv_maestro_context;
45 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory);
47 smx_ctx_sysv_create_context_sized(size_t structure_size,
48 xbt_main_func_t code, int argc,
50 void_pfn_smxprocess_t cleanup_func,
52 static void smx_ctx_sysv_free(smx_context_t context);
53 static int smx_ctx_sysv_get_thread_id(void);
54 static smx_context_t smx_ctx_sysv_self_parallel(void);
56 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
57 void_pfn_smxprocess_t cleanup_func, void* data);
59 static void smx_ctx_sysv_wrapper(int count, ...);
61 static void smx_ctx_sysv_stop_serial(smx_context_t context);
62 static void smx_ctx_sysv_suspend_serial(smx_context_t context);
63 static void smx_ctx_sysv_resume_serial(smx_process_t first_process);
64 static void smx_ctx_sysv_runall_serial(void);
66 static void smx_ctx_sysv_stop_parallel(smx_context_t context);
67 static void smx_ctx_sysv_suspend_parallel(smx_context_t context);
68 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process);
69 static void smx_ctx_sysv_runall_parallel(void);
71 /* This is a bit paranoid about SIZEOF_VOIDP not being a multiple of SIZEOF_INT,
72 * but it doesn't harm. */
73 #define CTX_ADDR_LEN (SIZEOF_VOIDP / SIZEOF_INT + !!(SIZEOF_VOIDP % SIZEOF_INT))
76 int intv[CTX_ADDR_LEN];
78 #if (CTX_ADDR_LEN == 1)
79 # define CTX_ADDR_SPLIT(u) (u).intv[0]
80 #elif (CTX_ADDR_LEN == 2)
81 # define CTX_ADDR_SPLIT(u) (u).intv[0], (u).intv[1]
83 # error Your architecture is not supported yet
86 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
88 smx_ctx_base_factory_init(factory);
89 XBT_VERB("Activating SYSV context factory");
91 (*factory)->finalize = smx_ctx_sysv_factory_finalize;
92 (*factory)->create_context = smx_ctx_sysv_create_context;
93 /* Do not overload that method (*factory)->finalize */
94 (*factory)->free = smx_ctx_sysv_free;
95 (*factory)->name = "smx_sysv_context_factory";
97 if (SIMIX_context_is_parallel()) {
98 #ifdef CONTEXT_THREADS /* To use parallel ucontexts a thread pool is needed */
99 int nthreads = SIMIX_context_get_nthreads();
100 sysv_parmap = xbt_parmap_new(nthreads);
101 sysv_workers_stacks = xbt_new(ucontext_t, nthreads);
102 (*factory)->stop = smx_ctx_sysv_stop_parallel;
103 (*factory)->suspend = smx_ctx_sysv_suspend_parallel;
104 (*factory)->runall = smx_ctx_sysv_runall_parallel;
106 THROWF(arg_error, 0, "No thread support for parallel context execution");
109 (*factory)->stop = smx_ctx_sysv_stop_serial;
110 (*factory)->suspend = smx_ctx_sysv_suspend_serial;
111 (*factory)->runall = smx_ctx_sysv_runall_serial;
115 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
117 #ifdef CONTEXT_THREADS
119 xbt_parmap_destroy(sysv_parmap);
120 xbt_free(sysv_workers_stacks);
122 return smx_ctx_base_factory_finalize(factory);
126 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
127 int argc, char **argv,
128 void_pfn_smxprocess_t cleanup_func,
131 union u_ctx_addr ctx_addr;
132 smx_ctx_sysv_t context =
133 (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
140 /* if the user provided a function for the process then use it,
141 otherwise it is the context for maestro */
144 getcontext(&(context->uc));
146 context->uc.uc_link = NULL;
148 context->uc.uc_stack.ss_sp =
149 pth_skaddr_makecontext(context->stack, smx_context_stack_size);
151 context->uc.uc_stack.ss_size =
152 pth_sksize_makecontext(context->stack, smx_context_stack_size);
154 #ifdef HAVE_VALGRIND_VALGRIND_H
155 context->valgrind_stack_id =
156 VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
157 ((char *) context->uc.uc_stack.ss_sp) +
158 context->uc.uc_stack.ss_size);
159 #endif /* HAVE_VALGRIND_VALGRIND_H */
160 ctx_addr.addr = context;
161 makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
162 CTX_ADDR_LEN, CTX_ADDR_SPLIT(ctx_addr));
164 sysv_maestro_context = context;
167 return (smx_context_t) context;
171 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
172 void_pfn_smxprocess_t cleanup_func,
176 return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t) + smx_context_stack_size,
177 code, argc, argv, cleanup_func,
182 static void smx_ctx_sysv_free(smx_context_t context)
187 #ifdef HAVE_VALGRIND_VALGRIND_H
188 VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
189 context)->valgrind_stack_id);
190 #endif /* HAVE_VALGRIND_VALGRIND_H */
193 smx_ctx_base_free(context);
196 static void smx_ctx_sysv_wrapper(int first, ...)
198 union u_ctx_addr ctx_addr;
199 smx_ctx_sysv_t context;
201 ctx_addr.intv[0] = first;
202 if (CTX_ADDR_LEN > 1) {
206 for (i = 1; i < CTX_ADDR_LEN; i++)
207 ctx_addr.intv[i] = va_arg(ap, int);
210 context = ctx_addr.addr;
211 (context->super.code) (context->super.argc, context->super.argv);
213 simix_global->context_factory->stop((smx_context_t) context);
216 static void smx_ctx_sysv_stop_serial(smx_context_t context)
218 smx_ctx_base_stop(context);
219 smx_ctx_sysv_suspend_serial(context);
222 static void smx_ctx_sysv_suspend_serial(smx_context_t context)
224 /* determine the next context */
225 smx_context_t next_context;
226 unsigned long int i = sysv_process_index++;
228 if (i < xbt_dynar_length(simix_global->process_to_run)) {
229 /* execute the next process */
230 XBT_DEBUG("Run next process");
231 next_context = xbt_dynar_get_as(
232 simix_global->process_to_run,i, smx_process_t)->context;
235 /* all processes were run, return to maestro */
236 XBT_DEBUG("No more process to run");
237 next_context = (smx_context_t) sysv_maestro_context;
239 SIMIX_context_set_current(next_context);
240 swapcontext(&((smx_ctx_sysv_t) context)->uc,
241 &((smx_ctx_sysv_t) next_context)->uc);
244 static void smx_ctx_sysv_resume_serial(smx_process_t first_process)
246 smx_context_t context = first_process->context;
247 SIMIX_context_set_current(context);
248 swapcontext(&sysv_maestro_context->uc,
249 &((smx_ctx_sysv_t) context)->uc);
252 static void smx_ctx_sysv_runall_serial(void)
254 if (!xbt_dynar_is_empty(simix_global->process_to_run)) {
255 smx_process_t first_process =
256 xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
257 sysv_process_index = 1;
259 /* execute the first process */
260 smx_ctx_sysv_resume_serial(first_process);
264 static void smx_ctx_sysv_stop_parallel(smx_context_t context)
266 smx_ctx_base_stop(context);
267 smx_ctx_sysv_suspend_parallel(context);
270 static void smx_ctx_sysv_suspend_parallel(smx_context_t context)
272 #ifdef CONTEXT_THREADS
273 /* determine the next context */
274 smx_process_t next_work = xbt_parmap_next(sysv_parmap);
275 smx_context_t next_context;
278 if (next_work != NULL) {
279 /* there is a next process to resume */
280 XBT_DEBUG("Run next process");
281 next_context = next_work->context;
282 next_uc = &((smx_ctx_sysv_t) next_context)->uc;
285 /* all processes were run, go to the barrier */
286 XBT_DEBUG("No more processes to run");
287 next_context = (smx_context_t) sysv_maestro_context;
288 unsigned long worker_id = xbt_parmap_get_worker_id(sysv_parmap);
289 next_uc = &sysv_workers_stacks[worker_id];
292 SIMIX_context_set_current(next_context);
293 swapcontext(&((smx_ctx_sysv_t) context)->uc, next_uc);
297 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process)
299 #ifdef CONTEXT_THREADS
300 unsigned long worker_id = xbt_parmap_get_worker_id(sysv_parmap);
301 ucontext_t* worker_stack = &sysv_workers_stacks[worker_id];
303 smx_context_t context = first_process->context;
304 SIMIX_context_set_current(context);
305 swapcontext(worker_stack, &((smx_ctx_sysv_t) context)->uc);
309 static void smx_ctx_sysv_runall_parallel(void)
311 #ifdef CONTEXT_THREADS
312 xbt_parmap_apply(sysv_parmap, (void_f_pvoid_t) smx_ctx_sysv_resume_parallel,
313 simix_global->process_to_run);