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 "smx_context_sysv_private.h"
12 #include "xbt/parmap.h"
13 #include "xbt/parmap_private.h"
14 #include "simix/private.h"
15 #include "gras_config.h"
17 #ifdef HAVE_VALGRIND_VALGRIND_H
18 # include <valgrind/valgrind.h>
19 #endif /* HAVE_VALGRIND_VALGRIND_H */
22 #include "win32_ucontext.h"
27 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
29 #ifdef CONTEXT_THREADS
30 static xbt_parmap_t parmap;
32 static unsigned long smx_ctx_sysv_process_index = 0; /* index of the next process to run in the
33 * list of runnable processes */
34 static smx_ctx_sysv_t maestro_context;
37 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
38 void_pfn_smxprocess_t cleanup_func, void* data);
40 static void smx_ctx_sysv_wrapper(int count, ...);
42 static void smx_ctx_sysv_stop_serial(smx_context_t context);
43 static void smx_ctx_sysv_suspend_serial(smx_context_t context);
44 static void smx_ctx_sysv_runall_serial(void);
46 static void smx_ctx_sysv_stop_parallel(smx_context_t context);
47 static void smx_ctx_sysv_suspend_parallel(smx_context_t context);
48 static void smx_ctx_sysv_runall_parallel(void);
50 static void smx_ctx_sysv_resume(smx_process_t first_process);
52 /* This is a bit paranoid about SIZEOF_VOIDP not being a multiple of SIZEOF_INT,
53 * but it doesn't harm. */
54 #define CTX_ADDR_LEN (SIZEOF_VOIDP / SIZEOF_INT + !!(SIZEOF_VOIDP % SIZEOF_INT))
57 int intv[CTX_ADDR_LEN];
59 #if (CTX_ADDR_LEN == 1)
60 # define CTX_ADDR_SPLIT(u) (u).intv[0]
61 #elif (CTX_ADDR_LEN == 2)
62 # define CTX_ADDR_SPLIT(u) (u).intv[0], (u).intv[1]
64 # error Your architecture is not supported yet
67 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
69 smx_ctx_base_factory_init(factory);
70 XBT_VERB("Activating SYSV context factory");
72 (*factory)->finalize = smx_ctx_sysv_factory_finalize;
73 (*factory)->create_context = smx_ctx_sysv_create_context;
74 /* Do not overload that method (*factory)->finalize */
75 (*factory)->free = smx_ctx_sysv_free;
76 (*factory)->name = "smx_sysv_context_factory";
78 if (SIMIX_context_is_parallel()) {
79 #ifdef CONTEXT_THREADS /* To use parallel ucontexts a thread pool is needed */
80 parmap = xbt_parmap_new(2);
81 (*factory)->stop = smx_ctx_sysv_stop_parallel;
82 (*factory)->suspend = smx_ctx_sysv_suspend_parallel;
83 (*factory)->runall = smx_ctx_sysv_runall_parallel;
84 (*factory)->self = smx_ctx_sysv_self_parallel;
86 THROWF(arg_error, 0, "No thread support for parallel context execution");
89 (*factory)->stop = smx_ctx_sysv_stop_serial;
90 (*factory)->suspend = smx_ctx_sysv_suspend_serial;
91 (*factory)->runall = smx_ctx_sysv_runall_serial;
95 int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
97 #ifdef CONTEXT_THREADS
99 xbt_parmap_destroy(parmap);
101 return smx_ctx_base_factory_finalize(factory);
105 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
106 int argc, char **argv,
107 void_pfn_smxprocess_t cleanup_func,
110 union u_ctx_addr ctx_addr;
111 smx_ctx_sysv_t context =
112 (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
119 /* if the user provided a function for the process then use it,
120 otherwise it is the context for maestro */
123 int res = getcontext(&(context->uc));
125 "Error in context saving: %d (%s)", errno,
128 context->uc.uc_link = NULL;
130 context->uc.uc_stack.ss_sp =
131 pth_skaddr_makecontext(context->stack, smx_context_stack_size);
133 context->uc.uc_stack.ss_size =
134 pth_sksize_makecontext(context->stack, smx_context_stack_size);
136 #ifdef HAVE_VALGRIND_VALGRIND_H
137 context->valgrind_stack_id =
138 VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
139 ((char *) context->uc.uc_stack.ss_sp) +
140 context->uc.uc_stack.ss_size);
141 #endif /* HAVE_VALGRIND_VALGRIND_H */
142 ctx_addr.addr = context;
143 makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
144 CTX_ADDR_LEN, CTX_ADDR_SPLIT(ctx_addr));
146 maestro_context = context;
149 return (smx_context_t) context;
153 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
154 void_pfn_smxprocess_t cleanup_func,
158 return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t) + smx_context_stack_size,
159 code, argc, argv, cleanup_func,
164 void smx_ctx_sysv_free(smx_context_t context)
169 #ifdef HAVE_VALGRIND_VALGRIND_H
170 VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
171 context)->valgrind_stack_id);
172 #endif /* HAVE_VALGRIND_VALGRIND_H */
175 smx_ctx_base_free(context);
178 void smx_ctx_sysv_wrapper(int first, ...)
180 union u_ctx_addr ctx_addr;
181 smx_ctx_sysv_t context;
183 ctx_addr.intv[0] = first;
184 if (CTX_ADDR_LEN > 1) {
188 for (i = 1; i < CTX_ADDR_LEN; i++)
189 ctx_addr.intv[i] = va_arg(ap, int);
192 context = ctx_addr.addr;
193 (context->super.code) (context->super.argc, context->super.argv);
195 simix_global->context_factory->stop((smx_context_t) context);
198 static void smx_ctx_sysv_stop_serial(smx_context_t context)
200 smx_ctx_base_stop(context);
201 smx_ctx_sysv_suspend_serial(context);
204 static void smx_ctx_sysv_suspend_serial(smx_context_t context)
206 /* determine the next context */
207 smx_context_t next_context;
208 unsigned long int i = smx_ctx_sysv_process_index++;
210 if (i < xbt_dynar_length(simix_global->process_to_run)) {
211 XBT_DEBUG("Run next process");
213 /* execute the next process */
214 next_context = xbt_dynar_get_as(
215 simix_global->process_to_run,i, smx_process_t)->context;
218 /* all processes were run, return to maestro */
219 XBT_DEBUG("No more process to run");
221 next_context = (smx_context_t) maestro_context;
223 SIMIX_context_set_current(next_context);
224 swapcontext(&((smx_ctx_sysv_t) context)->uc,
225 &((smx_ctx_sysv_t) next_context)->uc);
228 static void smx_ctx_sysv_resume(smx_process_t first_process)
230 smx_context_t context = first_process->context;
231 SIMIX_context_set_current(context);
232 swapcontext(&maestro_context->uc,
233 &((smx_ctx_sysv_t) context)->uc);
236 static void smx_ctx_sysv_runall_serial(void)
238 if (xbt_dynar_length(simix_global->process_to_run) > 0) {
239 smx_process_t first_process =
240 xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
241 smx_ctx_sysv_process_index = 1;
243 /* execute the first process */
244 smx_ctx_sysv_resume(first_process);
248 static void smx_ctx_sysv_stop_parallel(smx_context_t context)
250 smx_ctx_base_stop(context);
251 smx_ctx_sysv_suspend_parallel(context);
254 static void smx_ctx_sysv_suspend_parallel(smx_context_t context)
256 #ifdef CONTEXT_THREADS
257 /* determine the next context */
258 smx_context_t next_context;
259 unsigned long int i = __sync_fetch_and_add(&parmap->index, 1);
261 if (i < xbt_dynar_length(simix_global->process_to_run)) {
262 /* execute the next process */
263 XBT_DEBUG("Run next process");
264 next_context = xbt_dynar_get_as(simix_global->process_to_run, i, smx_process_t)->context;
267 /* all processes were run, go to the barrier */
268 XBT_DEBUG("No more processes to run");
269 next_context = (smx_context_t) maestro_context;
272 SIMIX_context_set_current(next_context);
273 swapcontext(&((smx_ctx_sysv_t) context)->uc,
274 &((smx_ctx_sysv_t) next_context)->uc);
278 static void smx_ctx_sysv_runall_parallel(void)
280 #ifdef CONTEXT_THREADS
281 xbt_parmap_apply(parmap, (void_f_pvoid_t) smx_ctx_sysv_resume,
282 simix_global->process_to_run);
286 smx_context_t smx_ctx_sysv_self_parallel(void)
288 /*smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
289 return self_context ? self_context : (smx_context_t) maestro_context;*/
290 return SIMIX_context_get_current();