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/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 struct s_xbt_ctx_sysv *prev; /* the previous thread */
28 #ifdef HAVE_VALGRIND_VALGRIND_H
29 unsigned int valgrind_stack_id; /* the valgrind stack id. */
30 #endif /* HAVE_VALGRIND_VALGRIND_H */
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 xbt_ctx_sysv_factory_create_context(const char *name, xbt_main_func_t code,
42 void_f_pvoid_t startup_func,
44 void_f_pvoid_t cleanup_func,
45 void *cleanup_arg, int argc, char **argv);
47 static int xbt_ctx_sysv_factory_finalize(xbt_context_factory_t * factory);
50 xbt_ctx_sysv_factory_create_maestro_context(xbt_context_t * maestro);
52 static void xbt_ctx_sysv_free(xbt_context_t context);
54 static void xbt_ctx_sysv_kill(xbt_context_t context);
56 static void xbt_ctx_sysv_schedule(xbt_context_t context);
58 static void xbt_ctx_sysv_yield(void);
60 static void xbt_ctx_sysv_start(xbt_context_t context);
62 static void xbt_ctx_sysv_stop(int exit_code);
64 static void xbt_ctx_sysv_swap(xbt_context_t context);
66 static void xbt_ctx_sysv_schedule(xbt_context_t context);
68 static void xbt_ctx_sysv_yield(void);
70 static void xbt_ctx_sysv_suspend(xbt_context_t context);
72 static void xbt_ctx_sysv_resume(xbt_context_t context);
74 static void xbt_ctx_sysv_wrapper(void);
76 /* callback: context fetching */
77 static ex_ctx_t *xbt_ctx_sysv_ex_ctx(void)
79 return current_context->exception;
82 /* callback: termination */
83 static void xbt_ctx_sysv_ex_terminate(xbt_ex_t * e)
90 void xbt_ctx_sysv_factory_init(xbt_context_factory_t * factory)
92 /* context exception */
93 *factory = xbt_new0(s_xbt_context_factory_t, 1);
95 (*factory)->create_context = xbt_ctx_sysv_factory_create_context;
96 (*factory)->finalize = xbt_ctx_sysv_factory_finalize;
97 (*factory)->create_maestro_context =
98 xbt_ctx_sysv_factory_create_maestro_context;
99 (*factory)->name = "ctx_sysv_context_factory";
101 /* context exception handlers */
102 __xbt_ex_ctx = xbt_ctx_sysv_ex_ctx;
103 __xbt_ex_terminate = xbt_ctx_sysv_ex_terminate;
107 xbt_ctx_sysv_factory_create_maestro_context(xbt_context_t * maestro)
110 xbt_ctx_sysv_t context = xbt_new0(s_xbt_ctx_sysv_t, 1);
112 context->name = (char *) "maestro";
114 context->exception = xbt_new(ex_ctx_t, 1);
115 XBT_CTX_INITIALIZE(context->exception);
117 *maestro = (xbt_context_t) context;
124 static int xbt_ctx_sysv_factory_finalize(xbt_context_factory_t * factory)
126 free(maestro_context->exception);
133 xbt_ctx_sysv_factory_create_context(const char *name, xbt_main_func_t code,
134 void_f_pvoid_t startup_func,
136 void_f_pvoid_t cleanup_func,
137 void *cleanup_arg, int argc, char **argv)
139 VERB1("Create context %s", name);
140 xbt_ctx_sysv_t context = xbt_new0(s_xbt_ctx_sysv_t, 1);
142 context->code = code;
143 context->name = xbt_strdup(name);
145 xbt_assert2(getcontext(&(context->uc)) == 0,
146 "Error in context saving: %d (%s)", errno, strerror(errno));
147 context->uc.uc_link = NULL;
148 context->uc.uc_stack.ss_sp =
149 pth_skaddr_makecontext(context->stack, STACK_SIZE);
150 context->uc.uc_stack.ss_size =
151 pth_sksize_makecontext(context->stack, STACK_SIZE);
152 #ifdef HAVE_VALGRIND_VALGRIND_H
153 context->valgrind_stack_id =
154 VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
155 ((char *) context->uc.uc_stack.ss_sp) +
156 context->uc.uc_stack.ss_size);
157 #endif /* HAVE_VALGRIND_VALGRIND_H */
159 context->exception = xbt_new(ex_ctx_t, 1);
160 XBT_CTX_INITIALIZE(context->exception);
161 context->iwannadie = 0; /* useless but makes valgrind happy */
162 context->argc = argc;
163 context->argv = argv;
164 context->startup_func = startup_func;
165 context->startup_arg = startup_arg;
166 context->cleanup_func = cleanup_func;
167 context->cleanup_arg = cleanup_arg;
170 context->free = xbt_ctx_sysv_free;
171 context->kill = xbt_ctx_sysv_kill;
172 context->schedule = xbt_ctx_sysv_schedule;
173 context->yield = xbt_ctx_sysv_yield;
174 context->start = xbt_ctx_sysv_start;
175 context->stop = xbt_ctx_sysv_stop;
177 return (xbt_context_t) context;
180 static void xbt_ctx_sysv_free(xbt_context_t context)
188 for (i = 0; i < context->argc; i++)
189 if (context->argv[i])
190 free(context->argv[i]);
195 if (context->exception)
196 free(context->exception);
198 #ifdef HAVE_VALGRIND_VALGRIND_H
199 VALGRIND_STACK_DEREGISTER(((xbt_ctx_sysv_t) context)->valgrind_stack_id);
200 #endif /* HAVE_VALGRIND_VALGRIND_H */
202 /* finally destroy the context */
207 static void xbt_ctx_sysv_kill(xbt_context_t context)
209 DEBUG2("Kill context '%s' (from '%s')", context->name,
210 current_context->name);
211 context->iwannadie = 1;
212 xbt_ctx_sysv_swap(context);
216 * \param context the winner
218 * Calling this function blocks the current context and schedule \a context.
219 * When \a context will call xbt_context_yield, it will return
220 * to this function as if nothing had happened.
222 * Only the maestro can call this function to run a given process.
224 static void xbt_ctx_sysv_schedule(xbt_context_t context)
226 DEBUG1("Schedule context '%s'", context->name);
227 xbt_assert0((current_context == maestro_context),
228 "You are not supposed to run this function here!");
229 xbt_ctx_sysv_swap(context);
233 * Calling this function makes the current context yield. The context
234 * that scheduled it returns from xbt_context_schedule as if nothing
237 * Only the processes can call this function, giving back the control
240 static void xbt_ctx_sysv_yield(void)
242 DEBUG1("Yielding context '%s'", current_context->name);
243 xbt_assert0((current_context != maestro_context),
244 "You are not supposed to run this function here!");
245 xbt_ctx_sysv_swap(current_context);
248 static void xbt_ctx_sysv_start(xbt_context_t context)
250 DEBUG1("Start context '%s'", context->name);
251 makecontext(&(((xbt_ctx_sysv_t) context)->uc), xbt_ctx_sysv_wrapper, 0);
254 static void xbt_ctx_sysv_stop(int exit_code)
256 /* please no debug here: our procdata was already free'd */
257 if (current_context->cleanup_func)
258 ((*current_context->cleanup_func)) (current_context->cleanup_arg);
260 xbt_swag_remove(current_context, context_living);
261 xbt_swag_insert(current_context, context_to_destroy);
263 xbt_ctx_sysv_swap(current_context);
266 static void xbt_ctx_sysv_swap(xbt_context_t context)
268 DEBUG2("Swap context: '%s' -> '%s'", current_context->name, context->name);
269 xbt_assert0(current_context, "You have to call context_init() first.");
270 xbt_assert0(context, "Invalid argument");
272 if (((xbt_ctx_sysv_t) context)->prev == NULL)
273 xbt_ctx_sysv_resume(context);
275 xbt_ctx_sysv_suspend(context);
277 if (current_context->iwannadie)
278 xbt_ctx_sysv_stop(1);
281 static void xbt_ctx_sysv_wrapper(void)
283 if (current_context->startup_func)
284 (*current_context->startup_func) (current_context->startup_arg);
286 xbt_ctx_sysv_stop((*(current_context->code))
287 (current_context->argc, current_context->argv));
290 static void xbt_ctx_sysv_suspend(xbt_context_t context)
294 DEBUG1("Suspend context: '%s'", current_context->name);
295 xbt_ctx_sysv_t prev_context = ((xbt_ctx_sysv_t) context)->prev;
297 current_context = (xbt_context_t) (((xbt_ctx_sysv_t) context)->prev);
299 ((xbt_ctx_sysv_t) context)->prev = NULL;
301 rv = swapcontext(&(((xbt_ctx_sysv_t) context)->uc), &(prev_context->uc));
303 xbt_assert0((rv == 0), "Context swapping failure");
306 static void xbt_ctx_sysv_resume(xbt_context_t context)
310 DEBUG2("Resume context: '%s' (from '%s')", context->name,
311 current_context->name);
312 ((xbt_ctx_sysv_t) context)->prev = (xbt_ctx_sysv_t) current_context;
314 current_context = context;
317 swapcontext(&(((xbt_ctx_sysv_t) context)->prev->uc),
318 &(((xbt_ctx_sysv_t) context)->uc));
320 xbt_assert0((rv == 0), "Context swapping failure");