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"
11 #include "xbt_context_private.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(xbt_context);
23 typedef struct s_xbt_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_xbt_ctx_sysv_t, *xbt_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 xbt_ctx_sysv_t context = xbt_new0(s_xbt_ctx_sysv_t, 1);
108 context->exception = xbt_new(ex_ctx_t, 1);
109 XBT_CTX_INITIALIZE(context->exception);
111 (*maestro)->context = (xbt_context_t) context;
118 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t * factory)
120 /*FIXME free(maestro_context->exception);*/
127 smx_ctx_sysv_factory_create_context(smx_process_t *smx_process, xbt_main_func_t code)
129 VERB1("Create context %s", (*smx_process)->name);
130 xbt_ctx_sysv_t context = xbt_new0(s_xbt_ctx_sysv_t, 1);
132 context->code = code;
134 xbt_assert2(getcontext(&(context->uc)) == 0,
135 "Error in context saving: %d (%s)", errno, strerror(errno));
136 context->uc.uc_link = NULL;
137 context->uc.uc_stack.ss_sp =
138 pth_skaddr_makecontext(context->stack, STACK_SIZE);
139 context->uc.uc_stack.ss_size =
140 pth_sksize_makecontext(context->stack, STACK_SIZE);
141 #ifdef HAVE_VALGRIND_VALGRIND_H
142 context->valgrind_stack_id =
143 VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
144 ((char *) context->uc.uc_stack.ss_sp) +
145 context->uc.uc_stack.ss_size);
146 #endif /* HAVE_VALGRIND_VALGRIND_H */
148 context->exception = xbt_new(ex_ctx_t, 1);
149 XBT_CTX_INITIALIZE(context->exception);
150 (*smx_process)->context = (xbt_context_t)context;
151 (*smx_process)->iwannadie = 0;
153 /* FIXME: Check what should return */
157 static void smx_ctx_sysv_free(smx_process_t process)
159 xbt_ctx_sysv_t context = (xbt_ctx_sysv_t)process->context;
162 if (context->exception)
163 free(context->exception);
165 #ifdef HAVE_VALGRIND_VALGRIND_H
166 VALGRIND_STACK_DEREGISTER(((xbt_ctx_sysv_t) context)->valgrind_stack_id);
167 #endif /* HAVE_VALGRIND_VALGRIND_H */
169 /* destroy the context */
174 static void smx_ctx_sysv_kill(smx_process_t process)
176 DEBUG2("Kill process '%s' (from '%s')", process->name,
177 simix_global->current_process->name);
178 process->iwannadie = 1;
179 smx_ctx_sysv_swap(process);
183 * \param context the winner
185 * Calling this function blocks the current context and schedule \a context.
186 * When \a context will call xbt_context_yield, it will return
187 * to this function as if nothing had happened.
189 * Only the maestro can call this function to run a given process.
191 static void smx_ctx_sysv_schedule(smx_process_t process)
193 DEBUG1("Schedule process '%s'", process->name);
194 xbt_assert0((simix_global->current_process == simix_global->maestro_process),
195 "You are not supposed to run this function here!");
196 smx_ctx_sysv_swap(process);
200 * Calling this function makes the current context yield. The context
201 * that scheduled it returns from xbt_context_schedule as if nothing
204 * Only the processes can call this function, giving back the control
207 static void smx_ctx_sysv_yield(void)
209 DEBUG1("Yielding context '%s'", simix_global->current_process->name);
210 xbt_assert0((simix_global->current_process != simix_global->maestro_process),
211 "You are not supposed to run this function here!");
212 smx_ctx_sysv_swap(simix_global->current_process);
215 static void smx_ctx_sysv_start(smx_process_t process)
217 /*DEBUG1("Start context '%s'", context->name);*/
218 makecontext(&(((xbt_ctx_sysv_t) process->context)->uc), smx_ctx_sysv_wrapper, 0);
221 static void smx_ctx_sysv_stop(int exit_code)
223 /* please no debug here: our procdata was already free'd */
224 if (simix_global->current_process->cleanup_func)
225 ((*simix_global->current_process->cleanup_func)) (simix_global->current_process->cleanup_arg);
227 smx_ctx_sysv_swap(simix_global->current_process);
230 static void smx_ctx_sysv_swap(smx_process_t process)
232 DEBUG2("Swap context: '%s' -> '%s'", simix_global->current_process->name, process->name);
233 xbt_assert0(simix_global->current_process, "You have to call context_init() first.");
234 xbt_assert0(process, "Invalid argument");
236 if (((xbt_ctx_sysv_t) process->context)->prev == NULL)
237 smx_ctx_sysv_resume(process);
239 smx_ctx_sysv_suspend(process);
241 if (simix_global->current_process->iwannadie)
242 smx_ctx_sysv_stop(1);
245 static void smx_ctx_sysv_wrapper(void)
247 smx_ctx_sysv_stop((*(simix_global->current_process->context->code))
248 (simix_global->current_process->argc, simix_global->current_process->argv));
251 static void smx_ctx_sysv_suspend(smx_process_t process)
255 DEBUG1("Suspend context: '%s'", simix_global->current_process->name);
256 smx_process_t prev_process = ((xbt_ctx_sysv_t) process->context)->prev;
258 simix_global->current_process = (smx_process_t) (((xbt_ctx_sysv_t) process->context)->prev);
260 ((xbt_ctx_sysv_t) process->context)->prev = NULL;
262 rv = swapcontext(&(((xbt_ctx_sysv_t) process->context)->uc), &(((xbt_ctx_sysv_t)prev_process->context)->uc));
264 xbt_assert0((rv == 0), "Context swapping failure");
267 static void smx_ctx_sysv_resume(smx_process_t process)
270 xbt_ctx_sysv_t new_context = (xbt_ctx_sysv_t)process->context;
271 xbt_ctx_sysv_t prev_context = (xbt_ctx_sysv_t)simix_global->current_process->context;
273 DEBUG2("Resume context: '%s' (from '%s')", process->name,
274 simix_global->current_process->name);
276 ((xbt_ctx_sysv_t) process->context)->prev = simix_global->current_process;
278 simix_global->current_process = process;
280 rv = swapcontext(&prev_context->uc, &new_context->uc);
282 xbt_assert0((rv == 0), "Context swapping failure");