3 /* context_sysv - implementation of context switching with ucontextes from Sys V */
5 /* Copyright (c) 2004-2008 the SimGrid team. All right reserved */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/ex_interface.h"
13 #include "context_sysv_config.h" /* loads context system definitions */
15 #include <ucontext.h> /* context relative declarations */
16 #define STACK_SIZE 128*1024 /* lower this if you want to reduce the memory consumption */
17 #ifdef HAVE_VALGRIND_VALGRIND_H
18 # include <valgrind/valgrind.h>
19 #endif /* HAVE_VALGRIND_VALGRIND_H */
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smx_context);
23 typedef struct s_smx_ctx_sysv {
25 ucontext_t uc; /* the thread that execute the code */
26 char stack[STACK_SIZE]; /* the thread stack size */
27 smx_process_t prev; /* the previous process */
28 #ifdef HAVE_VALGRIND_VALGRIND_H
29 unsigned int valgrind_stack_id; /* the valgrind stack id */
31 } s_smx_ctx_sysv_t, *smx_ctx_sysv_t;
34 /* callback: context fetching */
35 static ex_ctx_t *xbt_jcontext_ex_ctx(void);
37 /* callback: termination */
38 static void xbt_jcontext_ex_terminate(xbt_ex_t * e);
41 smx_ctx_sysv_factory_create_context(smx_process_t *smx_process, xbt_main_func_t code);
43 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t * factory);
45 static int smx_ctx_sysv_factory_create_maestro_context(smx_process_t * maestro);
47 static void smx_ctx_sysv_free(smx_process_t process);
49 static void smx_ctx_sysv_kill(smx_process_t process);
51 static void smx_ctx_sysv_schedule(smx_process_t process);
53 static void smx_ctx_sysv_yield(void);
55 static void smx_ctx_sysv_start(smx_process_t process);
57 static void smx_ctx_sysv_stop(int exit_code);
59 static void smx_ctx_sysv_swap(smx_process_t process);
61 static void smx_ctx_sysv_schedule(smx_process_t process);
63 static void smx_ctx_sysv_yield(void);
65 static void smx_ctx_sysv_suspend(smx_process_t process);
67 static void smx_ctx_sysv_resume(smx_process_t process);
69 static void smx_ctx_sysv_wrapper(void);
71 /* callback: context fetching */
72 static ex_ctx_t *xbt_ctx_sysv_ex_ctx(void)
74 return simix_global->current_process->context->exception;
77 /* callback: termination */
78 static void xbt_ctx_sysv_ex_terminate(xbt_ex_t * e)
84 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t * factory)
86 *factory = xbt_new0(s_smx_context_factory_t, 1);
88 (*factory)->create_context = smx_ctx_sysv_factory_create_context;
89 (*factory)->finalize = smx_ctx_sysv_factory_finalize;
90 (*factory)->create_maestro_context = smx_ctx_sysv_factory_create_maestro_context;
91 (*factory)->free = smx_ctx_sysv_free;
92 (*factory)->kill = smx_ctx_sysv_kill;
93 (*factory)->schedule = smx_ctx_sysv_schedule;
94 (*factory)->yield = smx_ctx_sysv_yield;
95 (*factory)->start = smx_ctx_sysv_start;
96 (*factory)->stop = smx_ctx_sysv_stop;
97 (*factory)->name = "smx_sysv_context_factory";
99 /* context exception handlers */
100 __xbt_ex_ctx = xbt_ctx_sysv_ex_ctx;
101 __xbt_ex_terminate = xbt_ctx_sysv_ex_terminate;
104 static int smx_ctx_sysv_factory_create_maestro_context(smx_process_t *maestro)
106 smx_ctx_sysv_t context = xbt_new0(s_smx_ctx_sysv_t, 1);
108 context->exception = xbt_new(ex_ctx_t, 1);
109 XBT_CTX_INITIALIZE(context->exception);
111 (*maestro)->context = (smx_context_t) context;
117 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t * factory)
119 /*FIXME free(maestro_context->exception);*/
126 smx_ctx_sysv_factory_create_context(smx_process_t *smx_process, xbt_main_func_t code)
128 VERB1("Create context %s", (*smx_process)->name);
129 smx_ctx_sysv_t context = xbt_new0(s_smx_ctx_sysv_t, 1);
131 context->code = code;
133 xbt_assert2(getcontext(&(context->uc)) == 0,
134 "Error in context saving: %d (%s)", errno, strerror(errno));
135 context->uc.uc_link = NULL;
136 context->uc.uc_stack.ss_sp =
137 pth_skaddr_makecontext(context->stack, STACK_SIZE);
138 context->uc.uc_stack.ss_size =
139 pth_sksize_makecontext(context->stack, STACK_SIZE);
140 #ifdef HAVE_VALGRIND_VALGRIND_H
141 context->valgrind_stack_id =
142 VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
143 ((char *) context->uc.uc_stack.ss_sp) +
144 context->uc.uc_stack.ss_size);
145 #endif /* HAVE_VALGRIND_VALGRIND_H */
147 context->exception = xbt_new(ex_ctx_t, 1);
148 XBT_CTX_INITIALIZE(context->exception);
149 (*smx_process)->context = (smx_context_t)context;
150 (*smx_process)->iwannadie = 0;
152 /* FIXME: Check what should return */
156 static void smx_ctx_sysv_free(smx_process_t process)
158 smx_ctx_sysv_t context = (smx_ctx_sysv_t)process->context;
161 if (context->exception)
162 free(context->exception);
164 #ifdef HAVE_VALGRIND_VALGRIND_H
165 VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t) context)->valgrind_stack_id);
166 #endif /* HAVE_VALGRIND_VALGRIND_H */
168 /* destroy the context */
173 static void smx_ctx_sysv_kill(smx_process_t process)
175 DEBUG2("Kill process '%s' (from '%s')", process->name,
176 simix_global->current_process->name);
177 process->iwannadie = 1;
178 smx_ctx_sysv_swap(process);
182 * \param context the winner
184 * Calling this function blocks the current context and schedule \a context.
185 * When \a context will call xbt_context_yield, it will return
186 * to this function as if nothing had happened.
188 * Only the maestro can call this function to run a given process.
190 static void smx_ctx_sysv_schedule(smx_process_t process)
192 DEBUG1("Schedule process '%s'", process->name);
193 xbt_assert0((simix_global->current_process == simix_global->maestro_process),
194 "You are not supposed to run this function here!");
195 smx_ctx_sysv_swap(process);
199 * Calling this function makes the current context yield. The context
200 * that scheduled it returns from xbt_context_schedule as if nothing
203 * Only the processes can call this function, giving back the control
206 static void smx_ctx_sysv_yield(void)
208 DEBUG1("Yielding process '%s'", simix_global->current_process->name);
209 xbt_assert0((simix_global->current_process != simix_global->maestro_process),
210 "You are not supposed to run this function here!");
211 smx_ctx_sysv_swap(simix_global->current_process);
214 static void smx_ctx_sysv_start(smx_process_t process)
216 /*DEBUG1("Start context '%s'", context->name);*/
217 makecontext(&(((smx_ctx_sysv_t) process->context)->uc), smx_ctx_sysv_wrapper, 0);
220 static void smx_ctx_sysv_stop(int exit_code)
222 /* please no debug here: our procdata was already free'd */
223 if (simix_global->current_process->cleanup_func)
224 ((*simix_global->current_process->cleanup_func)) (simix_global->current_process->cleanup_arg);
226 smx_ctx_sysv_swap(simix_global->current_process);
229 static void smx_ctx_sysv_swap(smx_process_t process)
231 DEBUG2("Swap context: '%s' -> '%s'", simix_global->current_process->name, process->name);
232 xbt_assert0(simix_global->current_process, "You have to call context_init() first.");
233 xbt_assert0(process, "Invalid argument");
235 if (((smx_ctx_sysv_t) process->context)->prev == NULL)
236 smx_ctx_sysv_resume(process);
238 smx_ctx_sysv_suspend(process);
240 if (simix_global->current_process->iwannadie)
241 smx_ctx_sysv_stop(1);
244 static void smx_ctx_sysv_wrapper(void)
246 smx_ctx_sysv_stop((*(simix_global->current_process->context->code))
247 (simix_global->current_process->argc, simix_global->current_process->argv));
250 static void smx_ctx_sysv_suspend(smx_process_t process)
254 DEBUG1("Suspend context: '%s'", simix_global->current_process->name);
255 smx_process_t prev_process = ((smx_ctx_sysv_t) process->context)->prev;
257 simix_global->current_process = prev_process;
259 ((smx_ctx_sysv_t) process->context)->prev = NULL;
261 rv = swapcontext(&(((smx_ctx_sysv_t) process->context)->uc), &(((smx_ctx_sysv_t)prev_process->context)->uc));
263 xbt_assert0((rv == 0), "Context swapping failure");
266 static void smx_ctx_sysv_resume(smx_process_t process)
269 smx_ctx_sysv_t new_context = (smx_ctx_sysv_t)process->context;
270 smx_ctx_sysv_t prev_context = (smx_ctx_sysv_t)simix_global->current_process->context;
272 DEBUG2("Resume context: '%s' (from '%s')", process->name,
273 simix_global->current_process->name);
275 ((smx_ctx_sysv_t) process->context)->prev = simix_global->current_process;
277 simix_global->current_process = process;
279 rv = swapcontext(&prev_context->uc, &new_context->uc);
281 xbt_assert0((rv == 0), "Context swapping failure");