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 "simix/private.h"
14 #include "gras_config.h"
16 #ifdef HAVE_VALGRIND_VALGRIND_H
17 # include <valgrind/valgrind.h>
18 #endif /* HAVE_VALGRIND_VALGRIND_H */
21 #include "win32_ucontext.h"
26 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
28 #ifdef CONTEXT_THREADS
29 static xbt_parmap_t parmap;
33 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
34 void_pfn_smxprocess_t cleanup_func, void* data);
36 static void smx_ctx_sysv_wrapper(int count, ...);
38 /* This is a bit paranoid about SIZEOF_VOIDP not being a multiple of SIZEOF_INT,
39 * but it doesn't harm. */
40 #define CTX_ADDR_LEN (SIZEOF_VOIDP / SIZEOF_INT + !!(SIZEOF_VOIDP % SIZEOF_INT))
43 int intv[CTX_ADDR_LEN];
45 #if (CTX_ADDR_LEN == 1)
46 # define CTX_ADDR_SPLIT(u) (u).intv[0]
47 #elif (CTX_ADDR_LEN == 2)
48 # define CTX_ADDR_SPLIT(u) (u).intv[0], (u).intv[1]
50 # error Your architecture is not supported yet
53 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
55 smx_ctx_base_factory_init(factory);
56 XBT_VERB("Activating SYSV context factory");
58 (*factory)->finalize = smx_ctx_sysv_factory_finalize;
59 (*factory)->create_context = smx_ctx_sysv_create_context;
60 /* Do not overload that method (*factory)->finalize */
61 (*factory)->free = smx_ctx_sysv_free;
62 (*factory)->stop = smx_ctx_sysv_stop;
63 (*factory)->suspend = smx_ctx_sysv_suspend;
64 (*factory)->name = "smx_sysv_context_factory";
66 if (SIMIX_context_is_parallel()) {
67 #ifdef CONTEXT_THREADS /* To use parallel ucontexts a thread pool is needed */
68 parmap = xbt_parmap_new(2);
69 (*factory)->runall = smx_ctx_sysv_runall_parallel;
70 (*factory)->self = smx_ctx_sysv_self_parallel;
72 THROWF(arg_error, 0, "No thread support for parallel context execution");
75 (*factory)->runall = smx_ctx_sysv_runall;
79 int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
81 #ifdef CONTEXT_THREADS
83 xbt_parmap_destroy(parmap);
85 return smx_ctx_base_factory_finalize(factory);
89 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
90 int argc, char **argv,
91 void_pfn_smxprocess_t cleanup_func,
94 union u_ctx_addr ctx_addr;
95 smx_ctx_sysv_t context =
96 (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
103 /* If the user provided a function for the process then use it
104 otherwise is the context for maestro */
108 res = getcontext(&(context->uc));
110 "Error in context saving: %d (%s)", errno,
113 context->uc.uc_link = NULL;
115 context->uc.uc_stack.ss_sp =
116 pth_skaddr_makecontext(context->stack, smx_context_stack_size);
118 context->uc.uc_stack.ss_size =
119 pth_sksize_makecontext(context->stack, smx_context_stack_size);
121 #ifdef HAVE_VALGRIND_VALGRIND_H
122 context->valgrind_stack_id =
123 VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
124 ((char *) context->uc.uc_stack.ss_sp) +
125 context->uc.uc_stack.ss_size);
126 #endif /* HAVE_VALGRIND_VALGRIND_H */
127 ctx_addr.addr = context;
128 makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
129 CTX_ADDR_LEN, CTX_ADDR_SPLIT(ctx_addr));
131 maestro_context = context;
134 return (smx_context_t) context;
139 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
140 void_pfn_smxprocess_t cleanup_func,
144 return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t) + smx_context_stack_size,
145 code, argc, argv, cleanup_func,
150 void smx_ctx_sysv_free(smx_context_t context)
155 #ifdef HAVE_VALGRIND_VALGRIND_H
156 VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
157 context)->valgrind_stack_id);
158 #endif /* HAVE_VALGRIND_VALGRIND_H */
161 smx_ctx_base_free(context);
164 void smx_ctx_sysv_stop(smx_context_t context)
166 smx_ctx_base_stop(context);
167 smx_ctx_sysv_suspend(context);
170 void smx_ctx_sysv_wrapper(int first, ...)
172 union u_ctx_addr ctx_addr;
173 smx_ctx_sysv_t context;
175 ctx_addr.intv[0] = first;
176 if (CTX_ADDR_LEN > 1) {
180 for(i = 1; i < CTX_ADDR_LEN; i++)
181 ctx_addr.intv[i] = va_arg(ap, int);
184 context = ctx_addr.addr;
185 (context->super.code) (context->super.argc, context->super.argv);
187 smx_ctx_sysv_stop((smx_context_t) context);
190 void smx_ctx_sysv_suspend(smx_context_t context)
192 SIMIX_context_set_current((smx_context_t) maestro_context);
194 rv = swapcontext(&((smx_ctx_sysv_t) context)->uc, &((smx_ctx_sysv_t)context)->old_uc);
196 xbt_assert((rv == 0), "Context swapping failure");
199 void smx_ctx_sysv_resume(smx_context_t context)
201 SIMIX_context_set_current(context);
203 rv = swapcontext(&((smx_ctx_sysv_t)context)->old_uc, &((smx_ctx_sysv_t) context)->uc);
205 xbt_assert((rv == 0), "Context swapping failure");
208 void smx_ctx_sysv_runall(xbt_dynar_t processes)
210 smx_process_t process;
213 xbt_dynar_foreach(processes, cursor, process) {
214 XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
215 smx_ctx_sysv_resume(process->context);
219 void smx_ctx_sysv_resume_parallel(smx_process_t process)
221 smx_context_t context = process->context;
222 SIMIX_context_set_current((smx_context_t) context);
224 rv = swapcontext(&((smx_ctx_sysv_t)context)->old_uc, &((smx_ctx_sysv_t) context)->uc);
225 SIMIX_context_set_current((smx_context_t) maestro_context);
227 xbt_assert((rv == 0), "Context swapping failure");
230 void smx_ctx_sysv_runall_parallel(xbt_dynar_t processes)
232 #ifdef CONTEXT_THREADS
233 xbt_parmap_apply(parmap, (void_f_pvoid_t)smx_ctx_sysv_resume_parallel, processes);
237 smx_context_t smx_ctx_sysv_self_parallel(void)
239 /*smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
240 return self_context ? self_context : (smx_context_t) maestro_context;*/
241 return SIMIX_context_get_current();