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;
31 typedef struct s_smx_ctx_raw {
32 s_smx_ctx_base_t super; /* Fields of super implementation */
33 char *malloced_stack; /* malloced area containing the stack */
34 raw_stack_t stack_top; /* pointer to stack top (within previous area) */
35 #ifdef HAVE_VALGRIND_VALGRIND_H
36 unsigned int valgrind_stack_id; /* the valgrind stack id */
38 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
40 smx_ctx_raw_t maestro_context;
42 static inline __attribute__((regparm(2))) void raw_swapcontext(raw_stack_t*old, raw_stack_t*new) {
43 __asm__ __volatile__ (
47 typedef void (*rawctx_entry_point_t)(void *);
48 static inline raw_stack_t raw_makecontext(char*malloced_stack, int stack_size,
49 rawctx_entry_point_t entry_point, void *arg) {
50 __asm__ __volatile__ (
55 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
57 static xbt_tpool_t tpool;
59 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
62 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
65 xbt_tpool_destroy(tpool);
66 return smx_ctx_base_factory_finalize(factory);
70 smx_ctx_raw_create_context_sized(size_t size, xbt_main_func_t code,
71 int argc, char **argv,
72 void_pfn_smxprocess_t cleanup_func,
76 smx_ctx_raw_t context =
77 (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(size,
84 /* If the user provided a function for the process then use it
85 otherwise is the context for maestro */
87 context->malloced_stack = xbt_malloc0(CONTEXT_STACK_SIZE);
89 raw_makecontext(context->malloced_stack,CONTEXT_STACK_SIZE,
90 (void(*)(void*))smx_ctx_raw_wrapper,context);
92 #ifdef HAVE_VALGRIND_VALGRIND_H
93 context->valgrind_stack_id =
94 VALGRIND_STACK_REGISTER(context->malloced_stack,
95 context->malloced_stack + CONTEXT_STACK_SIZE);
96 #endif /* HAVE_VALGRIND_VALGRIND_H */
99 maestro_context = context;
102 return (smx_context_t) context;
107 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
108 void_pfn_smxprocess_t cleanup_func,
112 return smx_ctx_raw_create_context_sized(sizeof(s_smx_ctx_raw_t),
113 code, argc, argv, cleanup_func,
118 static void smx_ctx_raw_free(smx_context_t context)
123 #ifdef HAVE_VALGRIND_VALGRIND_H
124 VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
125 context)->valgrind_stack_id);
126 #endif /* HAVE_VALGRIND_VALGRIND_H */
129 smx_ctx_base_free(context);
132 static void smx_ctx_raw_suspend(smx_context_t context)
134 smx_current_context = (smx_context_t)maestro_context;
135 raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top, &maestro_context->stack_top);
138 static void smx_ctx_raw_stop(smx_context_t context)
140 smx_ctx_base_stop(context);
141 smx_ctx_raw_suspend(context);
144 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
146 (context->super.code) (context->super.argc, context->super.argv);
148 smx_ctx_raw_stop((smx_context_t) context);
151 static void smx_ctx_raw_resume(smx_context_t context)
153 smx_current_context = context;
154 raw_swapcontext(&maestro_context->stack_top, &((smx_ctx_raw_t) context)->stack_top);
157 static void smx_ctx_raw_runall(xbt_swag_t processes)
159 smx_process_t process;
161 while ((process = xbt_swag_extract(processes)))
162 smx_ctx_raw_resume(process->context);
165 static void smx_ctx_raw_resume_parallel(smx_context_t context)
167 xbt_os_thread_set_extra_data(context);
168 raw_swapcontext(&maestro_context->stack_top, &((smx_ctx_raw_t) context)->stack_top);
171 static void smx_ctx_raw_runall_parallel(xbt_swag_t processes)
173 smx_process_t process;
174 while((process = xbt_swag_extract(processes))){
175 xbt_tpool_queue_job(tpool, (void_f_pvoid_t)smx_ctx_raw_resume_parallel, process->context);
177 xbt_tpool_wait_all(tpool);
180 static smx_context_t smx_ctx_raw_self_parallel(void)
182 smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
183 return self_context ? self_context : (smx_context_t) maestro_context;
186 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
188 smx_ctx_base_factory_init(factory);
190 (*factory)->finalize = smx_ctx_raw_factory_finalize;
191 (*factory)->create_context = smx_ctx_raw_create_context;
192 /* Do not overload that method (*factory)->finalize */
193 (*factory)->free = smx_ctx_raw_free;
194 (*factory)->stop = smx_ctx_raw_stop;
195 (*factory)->suspend = smx_ctx_raw_suspend;
196 (*factory)->name = "smx_raw_context_factory";
198 if(_surf_parallel_contexts){
199 #ifdef CONTEXT_THREADS /* To use parallel ucontexts a thread pool is needed */
200 tpool = xbt_tpool_new(2, 10);
201 (*factory)->runall = smx_ctx_raw_runall_parallel;
202 (*factory)->self = smx_ctx_raw_self_parallel;
204 THROW0(arg_error, 0, "No thread support for parallel context execution");
207 (*factory)->runall = smx_ctx_raw_runall;