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);
49 ".globl raw_makecontext\n"
51 " movl 4(%esp),%eax\n" /* stack */
52 " addl 8(%esp),%eax\n" /* size */
53 " movl 12(%esp),%ecx\n" /* func */
54 " movl 16(%esp),%edx\n" /* arg */
55 " movl %edx, -4(%eax)\n"
56 " movl $0, -8(%eax)\n"
57 " movl %ecx,-12(%eax)\n"
58 " movl $0, -16(%eax)\n"
59 " movl $0, -20(%eax)\n"
60 " movl $0, -24(%eax)\n"
61 " movl $0, -28(%eax)\n"
67 ".globl raw_swapcontext\n"
69 " movl 4(%esp),%eax\n" /* old */
70 " movl 8(%esp),%edx\n" /* new */
84 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
85 rawctx_entry_point_t entry_point, void* arg) {
89 void raw_swapcontext(raw_stack_t* old, raw_stack_t* new) {
95 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
97 static xbt_tpool_t tpool;
99 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
102 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
105 xbt_tpool_destroy(tpool);
106 return smx_ctx_base_factory_finalize(factory);
110 smx_ctx_raw_create_context_sized(size_t size, xbt_main_func_t code,
111 int argc, char **argv,
112 void_pfn_smxprocess_t cleanup_func,
116 smx_ctx_raw_t context =
117 (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(size,
124 /* If the user provided a function for the process then use it
125 otherwise is the context for maestro */
127 context->malloced_stack = xbt_malloc0(CONTEXT_STACK_SIZE);
129 raw_makecontext(context->malloced_stack,CONTEXT_STACK_SIZE,
130 (void(*)(void*))smx_ctx_raw_wrapper,context);
132 #ifdef HAVE_VALGRIND_VALGRIND_H
133 context->valgrind_stack_id =
134 VALGRIND_STACK_REGISTER(context->malloced_stack,
135 context->malloced_stack + CONTEXT_STACK_SIZE);
136 #endif /* HAVE_VALGRIND_VALGRIND_H */
139 maestro_context = context;
142 return (smx_context_t) context;
147 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
148 void_pfn_smxprocess_t cleanup_func,
152 return smx_ctx_raw_create_context_sized(sizeof(s_smx_ctx_raw_t),
153 code, argc, argv, cleanup_func,
158 static void smx_ctx_raw_free(smx_context_t context)
163 #ifdef HAVE_VALGRIND_VALGRIND_H
164 VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
165 context)->valgrind_stack_id);
166 #endif /* HAVE_VALGRIND_VALGRIND_H */
169 smx_ctx_base_free(context);
172 static void smx_ctx_raw_suspend(smx_context_t context)
174 smx_current_context = (smx_context_t)maestro_context;
175 raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top, &maestro_context->stack_top);
178 static void smx_ctx_raw_stop(smx_context_t context)
180 smx_ctx_base_stop(context);
181 smx_ctx_raw_suspend(context);
184 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
186 (context->super.code) (context->super.argc, context->super.argv);
188 smx_ctx_raw_stop((smx_context_t) context);
191 static void smx_ctx_raw_resume(smx_context_t context)
193 smx_current_context = context;
194 raw_swapcontext(&maestro_context->stack_top, &((smx_ctx_raw_t) context)->stack_top);
197 static void smx_ctx_raw_runall(xbt_swag_t processes)
199 smx_process_t process;
201 while ((process = xbt_swag_extract(processes)))
202 smx_ctx_raw_resume(process->context);
205 static void smx_ctx_raw_resume_parallel(smx_context_t context)
207 xbt_os_thread_set_extra_data(context);
208 raw_swapcontext(&maestro_context->stack_top, &((smx_ctx_raw_t) context)->stack_top);
211 static void smx_ctx_raw_runall_parallel(xbt_swag_t processes)
213 smx_process_t process;
214 while((process = xbt_swag_extract(processes))){
215 xbt_tpool_queue_job(tpool, (void_f_pvoid_t)smx_ctx_raw_resume_parallel, process->context);
217 xbt_tpool_wait_all(tpool);
220 static smx_context_t smx_ctx_raw_self_parallel(void)
222 smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
223 return self_context ? self_context : (smx_context_t) maestro_context;
226 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
228 smx_ctx_base_factory_init(factory);
230 (*factory)->finalize = smx_ctx_raw_factory_finalize;
231 (*factory)->create_context = smx_ctx_raw_create_context;
232 /* Do not overload that method (*factory)->finalize */
233 (*factory)->free = smx_ctx_raw_free;
234 (*factory)->stop = smx_ctx_raw_stop;
235 (*factory)->suspend = smx_ctx_raw_suspend;
236 (*factory)->name = "smx_raw_context_factory";
238 if(_surf_parallel_contexts){
239 #ifdef CONTEXT_THREADS /* To use parallel ucontexts a thread pool is needed */
240 tpool = xbt_tpool_new(2, 10);
241 (*factory)->runall = smx_ctx_raw_runall_parallel;
242 (*factory)->self = smx_ctx_raw_self_parallel;
244 THROW0(arg_error, 0, "No thread support for parallel context execution");
247 (*factory)->runall = smx_ctx_raw_runall;