3 /* a fast and simple context switching library */
5 /* Copyright (c) 2004 Arnaud Legrand. */
6 /* Copyright (c) 2004, 2005 Martin Quinson. */
7 /* All rights reserved. */
9 /* This program is free software; you can redistribute it and/or modify it
10 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include "context_private.h"
15 #include "xbt/dynar.h"
16 #include "xbt/xbt_thread.h"
18 #ifdef CONTEXT_THREADS
19 /* This file (context.c) is only loaded in libsimgrid, not libgras.
20 * xbt_thread is only loaded in libgras explicitly, and we need it in
21 * libsimgrid, but only when it is the backend used to implement the
22 * xbt_context. So, do load it on need.
24 #include "xbt/xbt_thread.c"
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ctx, xbt, "Context");
29 #define VOIRP(expr) DEBUG1(" {" #expr " = %p }", expr)
31 static xbt_context_t current_context = NULL;
32 static xbt_context_t init_context = NULL;
33 static xbt_swag_t context_to_destroy = NULL;
34 static xbt_swag_t context_living = NULL;
36 static void __xbt_context_yield(xbt_context_t context)
38 xbt_assert0(current_context,"You have to call context_init() first.");
40 DEBUG2("--------- current_context (%p) is yielding to context(%p) ---------",current_context,context);
42 #ifdef CONTEXT_THREADS
44 xbt_context_t self = current_context;
45 DEBUG0("**** Locking ****");
46 xbt_mutex_lock(context->mutex);
47 DEBUG0("**** Updating current_context ****");
48 current_context = context;
49 DEBUG0("**** Releasing the prisonner ****");
50 xbt_thcond_signal(context->cond);
51 DEBUG0("**** Going to jail ****");
52 xbt_thcond_wait(context->cond, context->mutex);
53 DEBUG0("**** Unlocking ****");
54 xbt_mutex_unlock(context->mutex);
55 DEBUG0("**** Updating current_context ****");
56 current_context = self;
58 #else /* use SUSv2 contexts */
60 VOIRP(current_context->save);
71 if(context->save==NULL){
73 DEBUG0("**** Yielding to somebody else ****");
74 DEBUG2("Saving current_context value (%p) to context(%p)->save",current_context,context);
75 context->save = current_context ;
76 DEBUG1("current_context becomes context(%p) ",context);
77 current_context = context ;
78 DEBUG1("Current position memorized (context->save). Jumping to context (%p)",context);
79 return_value = swapcontext (&(context->save->uc), &(context->uc));
80 xbt_assert0((return_value==0),"Context swapping failure");
81 DEBUG1("I am (%p). Coming back\n",context);
83 xbt_context_t old_context = context->save ;
84 DEBUG0("**** Back ! ****");
85 DEBUG2("Setting current_context (%p) to context(%p)->save",current_context,context);
86 current_context = context->save ;
87 DEBUG1("Setting context(%p)->save to NULL",context);
88 context->save = NULL ;
89 DEBUG2("Current position memorized (%p). Jumping to context (%p)",context,old_context);
90 return_value = swapcontext (&(context->uc), &(old_context->uc));
91 xbt_assert0((return_value==0),"Context swapping failure");
92 DEBUG1("I am (%p). Coming back\n",context);
100 static void xbt_context_free(xbt_context_t context)
102 if (!context) return;
103 DEBUG1("Freeing %p",context);
104 #ifdef CONTEXT_THREADS
105 xbt_free(context->thread);
106 xbt_mutex_destroy(context->mutex);
107 xbt_thcond_destroy(context->cond);
109 context->thread = NULL;
110 context->mutex = NULL;
111 context->cond = NULL;
114 if(context->exception)
115 free(context->exception);
121 static void __context_exit(xbt_context_t context ,int value)
124 DEBUG0("Freeing arguments");
125 for(i=0;i<context->argc; i++)
127 free(context->argv[i]);
132 if(context->cleanup_func){
133 DEBUG0("Calling cleanup function");
134 context->cleanup_func(context->cleanup_arg);
137 DEBUG0("Putting context in the to_destroy set");
138 xbt_swag_remove(context, context_living);
139 xbt_swag_insert(context, context_to_destroy);
140 DEBUG0("Context put in the to_destroy set");
143 #ifdef CONTEXT_THREADS
144 DEBUG0("**** Locking ****");
145 xbt_mutex_lock(context->mutex);
146 DEBUG0("**** Updating current_context ****");
147 current_context = context;
148 DEBUG0("**** Releasing the prisonner ****");
149 xbt_thcond_signal(context->cond);
150 DEBUG0("**** Unlocking ****");
151 xbt_mutex_unlock(context->mutex);
152 DEBUG0("**** Exiting ****");
155 __xbt_context_yield(context);
157 xbt_assert0(0,"You can't be here!");
161 __context_wrapper(void* c) {
162 xbt_context_t context = (xbt_context_t)c;
164 #ifdef CONTEXT_THREADS
165 context->thread = xbt_thread_self();
167 DEBUG2("**[%p:%p]** Lock ****",context,(void*)xbt_thread_self());
168 xbt_mutex_lock(context->mutex);
170 DEBUG2("**[%p:%p]** Releasing the prisonner ****",context,(void*)xbt_thread_self());
171 xbt_thcond_signal(context->cond);
173 DEBUG2("**[%p:%p]** Going to Jail ****",context,(void*)xbt_thread_self());
174 xbt_thcond_wait(context->cond, context->mutex);
176 DEBUG2("**[%p:%p]** Unlocking ****",context,(void*)xbt_thread_self());
177 xbt_mutex_unlock(context->mutex);
181 if(context->startup_func)
182 context->startup_func(context->startup_arg);
184 DEBUG0("Calling the main function");
186 __context_exit(context, (context->code) (context->argc,context->argv));
190 /* callback: context fetching */
191 static ex_ctx_t *__context_ex_ctx(void)
193 return current_context->exception;
196 /* callback: termination */
197 static void __context_ex_terminate(xbt_ex_t *e) {
201 /* FIXME: there should be a configuration variable to choose this
202 if(current_context!=init_context)
203 __context_exit(current_context, e->value);
210 * \ingroup XBT_context
213 /** Context module initialization
215 * \warning It has to be called before using any other function of this module.
217 void xbt_context_init(void)
219 if(!current_context){
220 current_context = init_context = xbt_new0(s_xbt_context_t,1);
221 DEBUG1("Init Context (%p)",init_context);
223 init_context->exception = xbt_new(ex_ctx_t,1);
224 XBT_CTX_INITIALIZE(init_context->exception);
225 __xbt_ex_ctx = __context_ex_ctx;
226 __xbt_ex_terminate = __context_ex_terminate;
227 context_to_destroy = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
228 context_living = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
229 xbt_swag_insert(init_context, context_living);
233 /** Garbage collection
235 * Should be called some time to time to free the memory allocated for contexts
236 * that have finished executing their main functions.
238 void xbt_context_empty_trash(void)
240 xbt_context_t context=NULL;
241 DEBUG0("Emptying trashbin");
242 while((context=xbt_swag_extract(context_to_destroy)))
243 xbt_context_free(context);
247 * \param context the context to start
249 * Calling this function prepares \a context to be run. It will
250 however run effectively only when calling #xbt_context_schedule
252 void xbt_context_start(xbt_context_t context)
254 #ifdef CONTEXT_THREADS
255 /* Launch the thread */
256 DEBUG1("**[%p]** Locking ****",context);
257 xbt_mutex_lock(context->mutex);
259 DEBUG1("**[%p]** Thread create ****",context);
260 context->thread = xbt_thread_create(__context_wrapper, context);
261 DEBUG2("**[%p]** Thread created : %p ****",context,context->thread);
263 DEBUG1("**[%p]** Going to jail ****",context);
264 xbt_thcond_wait(context->cond, context->mutex);
265 DEBUG1("**[%p]** Unlocking ****",context);
266 xbt_mutex_unlock(context->mutex);
268 makecontext (&(context->uc), (void (*) (void)) __context_wrapper,1, context);
274 * \param code a main function
275 * \param startup_func a function to call when running the context for
276 * the first time and just before the main function \a code
277 * \param startup_arg the argument passed to the previous function (\a startup_func)
278 * \param cleanup_func a function to call when running the context, just after
279 the termination of the main function \a code
280 * \param cleanup_arg the argument passed to the previous function (\a cleanup_func)
281 * \param argc first argument of function \a code
282 * \param argv seconde argument of function \a code
284 xbt_context_t xbt_context_new(xbt_context_function_t code,
285 void_f_pvoid_t startup_func, void *startup_arg,
286 void_f_pvoid_t cleanup_func, void *cleanup_arg,
287 int argc, char *argv[])
289 xbt_context_t res = NULL;
291 res = xbt_new0(s_xbt_context_t,1);
294 #ifdef CONTEXT_THREADS
295 res->mutex = xbt_mutex_init();
296 res->cond = xbt_thcond_init();
298 /* FIXME: strerror is not thread safe */
299 xbt_assert2(getcontext(&(res->uc))==0,"Error in context saving: %d (%s)", errno, strerror(errno));
300 res->uc.uc_link = NULL;
301 /* res->uc.uc_link = &(current_context->uc); */
302 /* WARNING : when this context is over, the current_context (i.e. the
303 father), is awaken... Theorically, the wrapper should prevent using
305 res->uc.uc_stack.ss_sp = pth_skaddr_makecontext(res->stack,STACK_SIZE);
306 res->uc.uc_stack.ss_size = pth_sksize_makecontext(res->stack,STACK_SIZE);
307 #endif /* CONTEXT_THREADS or not */
311 res->startup_func = startup_func;
312 res->startup_arg = startup_arg;
313 res->cleanup_func = cleanup_func;
314 res->cleanup_arg = cleanup_arg;
315 res->exception = xbt_new(ex_ctx_t,1);
316 XBT_CTX_INITIALIZE(res->exception);
318 xbt_swag_insert(res, context_living);
324 * Calling this function makes the current context yield. The context
325 * that scheduled it returns from xbt_context_schedule as if nothing
328 void xbt_context_yield(void)
330 __xbt_context_yield(current_context);
334 * \param context the winner
336 * Calling this function blocks the current context and schedule \a context.
337 * When \a context will call xbt_context_yield, it will return
338 * to this function as if nothing had happened.
340 void xbt_context_schedule(xbt_context_t context)
342 DEBUG1("Scheduling %p",context);
343 xbt_assert0((current_context==init_context),"You are not supposed to run this function here!");
344 __xbt_context_yield(context);
348 * This function kill all existing context and free all the memory
349 * that has been allocated in this module.
351 void xbt_context_exit(void) {
352 xbt_context_t context=NULL;
354 xbt_context_empty_trash();
355 xbt_swag_free(context_to_destroy);
357 while((context=xbt_swag_extract(context_living))) {
358 if(context!=init_context) xbt_context_kill(context);
360 xbt_context_kill(init_context);
361 xbt_swag_free(context_living);
363 init_context = current_context = NULL ;
367 * \param context poor victim
369 * This function simply kills \a context... scarry isn't it ?
371 void xbt_context_kill(xbt_context_t context)
375 DEBUG1("Killing %p", context);
377 xbt_swag_remove(context, context_living);
379 if(context->cleanup_func) {
380 DEBUG1("Calling cleanup function %p", context->cleanup_func);
381 context->cleanup_func(context->cleanup_arg);
384 DEBUG0("Freeing arguments");
385 for(i=0;i<context->argc; i++)
387 free(context->argv[i]);
392 xbt_context_free(context);