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 "xbt/threadpool.h"
10 #include "simix/private.h"
12 #ifdef HAVE_VALGRIND_VALGRIND_H
13 # include <valgrind/valgrind.h>
14 #endif /* HAVE_VALGRIND_VALGRIND_H */
17 #include "win32_ucontext.h"
18 #include "win32_ucontext.c"
23 /* lower this if you want to reduce the memory consumption */
24 #ifndef CONTEXT_STACK_SIZE /* allow lua to override this */
25 #define CONTEXT_STACK_SIZE 128*1024
26 #endif /*CONTEXT_STACK_SIZE */
29 typedef char * raw_stack_t;
30 typedef void (*rawctx_entry_point_t)(void *);
32 typedef struct s_smx_ctx_raw {
33 s_smx_ctx_base_t super; /* Fields of super implementation */
34 char *malloced_stack; /* malloced area containing the stack */
35 raw_stack_t stack_top; /* pointer to stack top (within previous area) */
36 #ifdef HAVE_VALGRIND_VALGRIND_H
37 unsigned int valgrind_stack_id; /* the valgrind stack id */
39 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
41 smx_ctx_raw_t maestro_context;
43 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
44 rawctx_entry_point_t entry_point, void* arg);
45 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t* new);
50 ".globl raw_makecontext\n"
51 ".type raw_makecontext,@function\n"
53 " movl 4(%esp),%eax\n" /* stack */
54 " addl 8(%esp),%eax\n" /* size */
55 " movl 12(%esp),%ecx\n" /* func */
56 " movl 16(%esp),%edx\n" /* arg */
57 " movl %edx, -4(%eax)\n"
58 " movl $0, -8(%eax)\n" /* @return for func */
59 " movl %ecx,-12(%eax)\n"
60 " movl $0, -16(%eax)\n" /* ebp */
61 " movl $0, -20(%eax)\n" /* ebx */
62 " movl $0, -24(%eax)\n" /* esi */
63 " movl $0, -28(%eax)\n" /* edi */
70 ".globl raw_swapcontext\n"
71 ".type raw_swapcontext,@function\n"
73 " movl 4(%esp),%eax\n" /* old */
74 " movl 8(%esp),%edx\n" /* new */
87 #elif PROCESSOR_x86_64
90 ".globl raw_makecontext\n"
91 ".type raw_makecontext,@function\n"
92 "raw_makecontext:\n" /* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
93 " movq %rdi,%rax\n" /* stack */
94 " addq %rsi,%rax\n" /* size */
95 " movq $0, -8(%rax)\n" /* @return for func */
96 " movq %rdx,-16(%rax)\n" /* func */
97 " movq %rcx,-24(%rax)\n" /* arg/rdi */
98 " movq $0, -32(%rax)\n" /* rsi */
99 " movq $0, -40(%rax)\n" /* rdx */
100 " movq $0, -48(%rax)\n" /* rcx */
101 " movq $0, -56(%rax)\n" /* r8 */
102 " movq $0, -64(%rax)\n" /* r9 */
103 " movq $0, -72(%rax)\n" /* rbp */
104 " movq $0, -80(%rax)\n" /* rbx */
105 " movq $0, -88(%rax)\n" /* r12 */
106 " movq $0, -96(%rax)\n" /* r13 */
107 " movq $0, -104(%rax)\n" /* r14 */
108 " movq $0, -112(%rax)\n" /* r15 */
115 ".globl raw_swapcontext\n"
116 ".type raw_swapcontext,@function\n"
117 "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
130 " movq %rsp,(%rdi)\n" /* old */
131 " movq (%rsi),%rsp\n" /* new */
147 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
148 rawctx_entry_point_t entry_point, void* arg) {
152 void raw_swapcontext(raw_stack_t* old, raw_stack_t* new) {
158 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
160 static xbt_tpool_t tpool;
162 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
165 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
168 xbt_tpool_destroy(tpool);
169 return smx_ctx_base_factory_finalize(factory);
173 smx_ctx_raw_create_context_sized(size_t size, xbt_main_func_t code,
174 int argc, char **argv,
175 void_pfn_smxprocess_t cleanup_func,
179 smx_ctx_raw_t context =
180 (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(size,
187 /* If the user provided a function for the process then use it
188 otherwise is the context for maestro */
190 context->malloced_stack = xbt_malloc0(CONTEXT_STACK_SIZE);
192 raw_makecontext(context->malloced_stack,CONTEXT_STACK_SIZE,
193 (void(*)(void*))smx_ctx_raw_wrapper,context);
195 #ifdef HAVE_VALGRIND_VALGRIND_H
196 context->valgrind_stack_id =
197 VALGRIND_STACK_REGISTER(context->malloced_stack,
198 context->malloced_stack + CONTEXT_STACK_SIZE);
199 #endif /* HAVE_VALGRIND_VALGRIND_H */
202 maestro_context = context;
205 return (smx_context_t) context;
210 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
211 void_pfn_smxprocess_t cleanup_func,
215 return smx_ctx_raw_create_context_sized(sizeof(s_smx_ctx_raw_t),
216 code, argc, argv, cleanup_func,
221 static void smx_ctx_raw_free(smx_context_t context)
226 #ifdef HAVE_VALGRIND_VALGRIND_H
227 VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
228 context)->valgrind_stack_id);
229 #endif /* HAVE_VALGRIND_VALGRIND_H */
232 smx_ctx_base_free(context);
235 static void smx_ctx_raw_suspend(smx_context_t context)
237 smx_current_context = (smx_context_t)maestro_context;
238 raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top, &maestro_context->stack_top);
241 static void smx_ctx_raw_stop(smx_context_t context)
243 smx_ctx_base_stop(context);
244 smx_ctx_raw_suspend(context);
247 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
249 (context->super.code) (context->super.argc, context->super.argv);
251 smx_ctx_raw_stop((smx_context_t) context);
254 static void smx_ctx_raw_resume(smx_context_t context)
256 smx_current_context = context;
257 raw_swapcontext(&maestro_context->stack_top, &((smx_ctx_raw_t) context)->stack_top);
260 static void smx_ctx_raw_runall(xbt_swag_t processes)
262 smx_process_t process;
264 while ((process = xbt_swag_extract(processes)))
265 smx_ctx_raw_resume(process->context);
268 static void smx_ctx_raw_resume_parallel(smx_context_t context)
270 xbt_os_thread_set_extra_data(context);
271 raw_swapcontext(&maestro_context->stack_top, &((smx_ctx_raw_t) context)->stack_top);
274 static void smx_ctx_raw_runall_parallel(xbt_swag_t processes)
276 smx_process_t process;
277 while((process = xbt_swag_extract(processes))){
278 xbt_tpool_queue_job(tpool, (void_f_pvoid_t)smx_ctx_raw_resume_parallel, process->context);
280 xbt_tpool_wait_all(tpool);
283 static smx_context_t smx_ctx_raw_self_parallel(void)
285 smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
286 return self_context ? self_context : (smx_context_t) maestro_context;
289 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
291 smx_ctx_base_factory_init(factory);
293 (*factory)->finalize = smx_ctx_raw_factory_finalize;
294 (*factory)->create_context = smx_ctx_raw_create_context;
295 /* Do not overload that method (*factory)->finalize */
296 (*factory)->free = smx_ctx_raw_free;
297 (*factory)->stop = smx_ctx_raw_stop;
298 (*factory)->suspend = smx_ctx_raw_suspend;
299 (*factory)->name = "smx_raw_context_factory";
301 if(_surf_parallel_contexts){
302 #ifdef CONTEXT_THREADS /* To use parallel ucontexts a thread pool is needed */
303 tpool = xbt_tpool_new(2, 10);
304 (*factory)->runall = smx_ctx_raw_runall_parallel;
305 (*factory)->self = smx_ctx_raw_self_parallel;
307 THROW0(arg_error, 0, "No thread support for parallel context execution");
310 (*factory)->runall = smx_ctx_raw_runall;