1 /* context_raw - 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. */
9 #include "simix/private.h"
10 #include "xbt/parmap.h"
13 #ifdef HAVE_VALGRIND_VALGRIND_H
14 # include <valgrind/valgrind.h>
15 #endif /* HAVE_VALGRIND_VALGRIND_H */
18 #include "win32_ucontext.h"
19 #include "win32_ucontext.c"
24 /* lower this if you want to reduce the memory consumption */
25 #ifndef CONTEXT_STACK_SIZE /* allow lua to override this */
26 #define CONTEXT_STACK_SIZE 128*1024
27 #endif /*CONTEXT_STACK_SIZE */
30 typedef char * raw_stack_t;
31 typedef void (*rawctx_entry_point_t)(void *);
33 typedef struct s_smx_ctx_raw {
34 s_smx_ctx_base_t super; /* Fields of super implementation */
35 char *malloced_stack; /* malloced area containing the stack */
36 raw_stack_t stack_top; /* pointer to stack top (within previous area) */
37 raw_stack_t old_stack_top; /* to whom I should return the control */
38 #ifdef HAVE_VALGRIND_VALGRIND_H
39 unsigned int valgrind_stack_id; /* the valgrind stack id */
41 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
43 smx_ctx_raw_t maestro_raw_context;
45 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
46 rawctx_entry_point_t entry_point, void* arg);
47 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t new);
52 ".globl raw_makecontext\n"
53 ".type raw_makecontext,@function\n"
55 " movl 4(%esp),%eax\n" /* stack */
56 " addl 8(%esp),%eax\n" /* size */
57 " movl 12(%esp),%ecx\n" /* func */
58 " movl 16(%esp),%edx\n" /* arg */
59 " movl %edx, -4(%eax)\n"
60 " movl $0, -8(%eax)\n" /* @return for func */
61 " movl %ecx,-12(%eax)\n"
62 " movl $0, -16(%eax)\n" /* ebp */
63 " movl $0, -20(%eax)\n" /* ebx */
64 " movl $0, -24(%eax)\n" /* esi */
65 " movl $0, -28(%eax)\n" /* edi */
72 ".globl raw_swapcontext\n"
73 ".type raw_swapcontext,@function\n"
75 " movl 4(%esp),%eax\n" /* old */
76 " movl 8(%esp),%edx\n" /* new */
89 #elif PROCESSOR_x86_64
92 ".globl raw_makecontext\n"
93 ".type raw_makecontext,@function\n"
94 "raw_makecontext:\n" /* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
95 " movq %rdi,%rax\n" /* stack */
96 " addq %rsi,%rax\n" /* size */
97 " movq $0, -8(%rax)\n" /* @return for func */
98 " movq %rdx,-16(%rax)\n" /* func */
99 " movq %rcx,-24(%rax)\n" /* arg/rdi */
100 " movq $0, -32(%rax)\n" /* rsi */
101 " movq $0, -40(%rax)\n" /* rdx */
102 " movq $0, -48(%rax)\n" /* rcx */
103 " movq $0, -56(%rax)\n" /* r8 */
104 " movq $0, -64(%rax)\n" /* r9 */
105 " movq $0, -72(%rax)\n" /* rbp */
106 " movq $0, -80(%rax)\n" /* rbx */
107 " movq $0, -88(%rax)\n" /* r12 */
108 " movq $0, -96(%rax)\n" /* r13 */
109 " movq $0, -104(%rax)\n" /* r14 */
110 " movq $0, -112(%rax)\n" /* r15 */
117 ".globl raw_swapcontext\n"
118 ".type raw_swapcontext,@function\n"
119 "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
132 " movq %rsp,(%rdi)\n" /* old */
133 " movq %rsi,%rsp\n" /* new */
149 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
150 rawctx_entry_point_t entry_point, void* arg) {
154 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
160 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
162 static xbt_parmap_t parmap;
164 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
166 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
169 xbt_parmap_destroy(parmap);
170 return smx_ctx_base_factory_finalize(factory);
174 smx_ctx_raw_create_context_sized(size_t size, xbt_main_func_t code,
175 int argc, char **argv,
176 void_pfn_smxprocess_t cleanup_func,
180 smx_ctx_raw_t context =
181 (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(size,
188 /* If the user provided a function for the process then use it
189 otherwise is the context for maestro */
191 context->malloced_stack = xbt_malloc0(CONTEXT_STACK_SIZE);
193 raw_makecontext(context->malloced_stack,CONTEXT_STACK_SIZE,
194 (void(*)(void*))smx_ctx_raw_wrapper,context);
196 #ifdef HAVE_VALGRIND_VALGRIND_H
197 context->valgrind_stack_id =
198 VALGRIND_STACK_REGISTER(context->malloced_stack,
199 context->malloced_stack + CONTEXT_STACK_SIZE);
200 #endif /* HAVE_VALGRIND_VALGRIND_H */
203 maestro_raw_context = context;
206 return (smx_context_t) context;
211 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
212 void_pfn_smxprocess_t cleanup_func,
216 return smx_ctx_raw_create_context_sized(sizeof(s_smx_ctx_raw_t),
217 code, argc, argv, cleanup_func,
222 static void smx_ctx_raw_free(smx_context_t context)
227 #ifdef HAVE_VALGRIND_VALGRIND_H
228 VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
229 context)->valgrind_stack_id);
230 #endif /* HAVE_VALGRIND_VALGRIND_H */
233 smx_ctx_base_free(context);
236 static void smx_ctx_raw_suspend(smx_context_t context)
238 smx_current_context = (smx_context_t)maestro_raw_context;
240 &((smx_ctx_raw_t) context)->stack_top,
241 ((smx_ctx_raw_t) context)->old_stack_top);
244 static void smx_ctx_raw_stop(smx_context_t context)
246 smx_ctx_base_stop(context);
247 smx_ctx_raw_suspend(context);
250 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
252 (context->super.code) (context->super.argc, context->super.argv);
254 smx_ctx_raw_stop((smx_context_t) context);
257 static void smx_ctx_raw_resume(smx_context_t context)
259 smx_current_context = context;
261 &((smx_ctx_raw_t) context)->old_stack_top,
262 ((smx_ctx_raw_t) context)->stack_top);
265 static void smx_ctx_raw_runall(xbt_dynar_t processes)
267 smx_process_t process;
270 xbt_dynar_foreach(processes, cursor, process) {
271 DEBUG2("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
272 smx_ctx_raw_resume(process->context);
274 xbt_dynar_reset(processes);
277 static void smx_ctx_raw_resume_parallel(smx_process_t process)
279 smx_ctx_raw_t context = (smx_ctx_raw_t)process->context;
280 xbt_os_thread_set_extra_data(context);
282 &context->old_stack_top,
284 xbt_os_thread_set_extra_data(NULL);
287 static void smx_ctx_raw_runall_parallel(xbt_dynar_t processes)
289 xbt_parmap_apply(parmap, (void_f_pvoid_t)smx_ctx_raw_resume_parallel, processes);
290 xbt_dynar_reset(processes);
293 static smx_context_t smx_ctx_raw_self_parallel(void)
295 smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
296 return self_context ? self_context : (smx_context_t) maestro_raw_context;
299 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
301 VERB0("Using raw contexts. Because the glibc is just not good enough for us.");
302 smx_ctx_base_factory_init(factory);
304 (*factory)->finalize = smx_ctx_raw_factory_finalize;
305 (*factory)->create_context = smx_ctx_raw_create_context;
306 /* Do not overload that method (*factory)->finalize */
307 (*factory)->free = smx_ctx_raw_free;
308 (*factory)->stop = smx_ctx_raw_stop;
309 (*factory)->suspend = smx_ctx_raw_suspend;
310 (*factory)->name = "smx_raw_context_factory";
312 if(_surf_parallel_contexts){
313 #ifdef CONTEXT_THREADS /* To use parallel ucontexts a thread pool is needed */
314 parmap = xbt_parmap_new(2);
315 (*factory)->runall = smx_ctx_raw_runall_parallel;
316 (*factory)->self = smx_ctx_raw_self_parallel;
318 THROW0(arg_error, 0, "No thread support for parallel context execution");
321 (*factory)->runall = smx_ctx_raw_runall;