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"
22 #include "win32_ucontext.c"
27 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
29 #ifdef CONTEXT_THREADS
30 static xbt_parmap_t parmap;
34 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
35 void_pfn_smxprocess_t cleanup_func, void* data);
37 static void smx_ctx_sysv_wrapper(int count, ...);
39 /* This is a bit paranoid about SIZEOF_VOIDP not being a multiple of SIZEOF_INT,
40 * but it doesn't harm. */
41 #define CTX_ADDR_LEN (SIZEOF_VOIDP / SIZEOF_INT + !!(SIZEOF_VOIDP % SIZEOF_INT))
44 int intv[CTX_ADDR_LEN];
46 #if (CTX_ADDR_LEN == 1)
47 # define CTX_ADDR_SPLIT(u) (u).intv[0]
48 #elif (CTX_ADDR_LEN == 2)
49 # define CTX_ADDR_SPLIT(u) (u).intv[0], (u).intv[1]
51 # error Your architecture is not supported yet
54 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
56 smx_ctx_base_factory_init(factory);
57 XBT_VERB("Activating SYSV context factory");
59 (*factory)->finalize = smx_ctx_sysv_factory_finalize;
60 (*factory)->create_context = smx_ctx_sysv_create_context;
61 /* Do not overload that method (*factory)->finalize */
62 (*factory)->free = smx_ctx_sysv_free;
63 (*factory)->stop = smx_ctx_sysv_stop;
64 (*factory)->suspend = smx_ctx_sysv_suspend;
65 (*factory)->name = "smx_sysv_context_factory";
67 if (SIMIX_context_is_parallel()) {
68 #ifdef CONTEXT_THREADS /* To use parallel ucontexts a thread pool is needed */
69 parmap = xbt_parmap_new(2);
70 (*factory)->runall = smx_ctx_sysv_runall_parallel;
71 (*factory)->self = smx_ctx_sysv_self_parallel;
72 (*factory)->get_thread_id = smx_ctx_sysv_get_thread_id;
74 THROW0(arg_error, 0, "No thread support for parallel context execution");
77 (*factory)->runall = smx_ctx_sysv_runall;
81 int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
83 #ifdef CONTEXT_THREADS
85 xbt_parmap_destroy(parmap);
87 return smx_ctx_base_factory_finalize(factory);
91 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
92 int argc, char **argv,
93 void_pfn_smxprocess_t cleanup_func,
96 union u_ctx_addr ctx_addr;
97 smx_ctx_sysv_t context =
98 (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
105 /* If the user provided a function for the process then use it
106 otherwise is the context for maestro */
110 res = getcontext(&(context->uc));
111 xbt_assert2(res == 0,
112 "Error in context saving: %d (%s)", errno,
115 context->uc.uc_link = NULL;
117 context->uc.uc_stack.ss_sp =
118 pth_skaddr_makecontext(context->stack, smx_context_stack_size);
120 context->uc.uc_stack.ss_size =
121 pth_sksize_makecontext(context->stack, smx_context_stack_size);
123 #ifdef HAVE_VALGRIND_VALGRIND_H
124 context->valgrind_stack_id =
125 VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
126 ((char *) context->uc.uc_stack.ss_sp) +
127 context->uc.uc_stack.ss_size);
128 #endif /* HAVE_VALGRIND_VALGRIND_H */
129 ctx_addr.addr = context;
130 makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
131 CTX_ADDR_LEN, CTX_ADDR_SPLIT(ctx_addr));
133 maestro_context = context;
136 return (smx_context_t) context;
141 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
142 void_pfn_smxprocess_t cleanup_func,
146 return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t) + smx_context_stack_size,
147 code, argc, argv, cleanup_func,
152 void smx_ctx_sysv_free(smx_context_t context)
157 #ifdef HAVE_VALGRIND_VALGRIND_H
158 VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
159 context)->valgrind_stack_id);
160 #endif /* HAVE_VALGRIND_VALGRIND_H */
163 smx_ctx_base_free(context);
166 void smx_ctx_sysv_stop(smx_context_t context)
168 smx_ctx_base_stop(context);
169 smx_ctx_sysv_suspend(context);
172 void smx_ctx_sysv_wrapper(int first, ...)
174 union u_ctx_addr ctx_addr;
175 smx_ctx_sysv_t context;
177 ctx_addr.intv[0] = first;
178 if (CTX_ADDR_LEN > 1) {
182 for(i = 1; i < CTX_ADDR_LEN; i++)
183 ctx_addr.intv[i] = va_arg(ap, int);
186 context = ctx_addr.addr;
187 (context->super.code) (context->super.argc, context->super.argv);
189 smx_ctx_sysv_stop((smx_context_t) context);
192 void smx_ctx_sysv_suspend(smx_context_t context)
194 smx_current_context = (smx_context_t)maestro_context;
196 rv = swapcontext(&((smx_ctx_sysv_t) context)->uc, &((smx_ctx_sysv_t)context)->old_uc);
198 xbt_assert0((rv == 0), "Context swapping failure");
201 void smx_ctx_sysv_resume(smx_context_t context)
203 smx_current_context = context;
205 rv = swapcontext(&((smx_ctx_sysv_t)context)->old_uc, &((smx_ctx_sysv_t) context)->uc);
207 xbt_assert0((rv == 0), "Context swapping failure");
210 void smx_ctx_sysv_runall(xbt_dynar_t processes)
212 smx_process_t process;
215 xbt_dynar_foreach(processes, cursor, process) {
216 XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
217 smx_ctx_sysv_resume(process->context);
219 xbt_dynar_reset(processes);
222 void smx_ctx_sysv_resume_parallel(smx_process_t process)
224 smx_context_t context = process->context;
225 smx_current_context = (smx_context_t)context;
227 rv = swapcontext(&((smx_ctx_sysv_t)context)->old_uc, &((smx_ctx_sysv_t) context)->uc);
228 smx_current_context = (smx_context_t)maestro_context;
230 xbt_assert0((rv == 0), "Context swapping failure");
233 void smx_ctx_sysv_runall_parallel(xbt_dynar_t processes)
235 #ifdef CONTEXT_THREADS
236 xbt_parmap_apply(parmap, (void_f_pvoid_t)smx_ctx_sysv_resume_parallel, processes);
238 xbt_dynar_reset(processes);
241 smx_context_t smx_ctx_sysv_self_parallel(void)
243 /*smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
244 return self_context ? self_context : (smx_context_t) maestro_context;*/
245 return smx_current_context;
248 int smx_ctx_sysv_get_thread_id(void)
250 return (int)(unsigned long)xbt_os_thread_get_extra_data();