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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ctx, xbt, "Context");
20 #define VOIRP(expr) DEBUG1(" {" #expr " = %p }", expr)
22 static xbt_context_t current_context = NULL;
23 static xbt_context_t init_context = NULL;
24 static xbt_swag_t context_to_destroy = NULL;
25 static xbt_swag_t context_living = NULL;
27 static void __xbt_context_yield(xbt_context_t context)
29 xbt_assert0(current_context,"You have to call context_init() first.");
31 DEBUG2("--------- current_context (%p) is yielding to context(%p) ---------",current_context,context);
33 #ifdef CONTEXT_THREADS
35 xbt_context_t self = current_context;
36 DEBUG0("**** Locking ****");
37 xbt_mutex_lock(context->mutex);
38 DEBUG0("**** Updating current_context ****");
39 current_context = context;
40 DEBUG0("**** Releasing the prisonner ****");
41 xbt_thcond_signal(context->cond);
42 DEBUG0("**** Going to jail ****");
43 xbt_thcond_wait(context->cond, context->mutex);
44 DEBUG0("**** Unlocking ****");
45 xbt_mutex_unlock(context->mutex);
46 DEBUG0("**** Updating current_context ****");
47 current_context = self;
49 #else /* use SUSv2 contexts */
51 VOIRP(current_context->save);
62 if(context->save==NULL){
64 DEBUG0("**** Yielding to somebody else ****");
65 DEBUG2("Saving current_context value (%p) to context(%p)->save",current_context,context);
66 context->save = current_context ;
67 DEBUG1("current_context becomes context(%p) ",context);
68 current_context = context ;
69 DEBUG1("Current position memorized (context->save). Jumping to context (%p)",context);
70 return_value = swapcontext (&(context->save->uc), &(context->uc));
71 xbt_assert0((return_value==0),"Context swapping failure");
72 DEBUG1("I am (%p). Coming back\n",context);
74 xbt_context_t old_context = context->save ;
75 DEBUG0("**** Back ! ****");
76 DEBUG2("Setting current_context (%p) to context(%p)->save",current_context,context);
77 current_context = context->save ;
78 DEBUG1("Setting context(%p)->save to NULL",context);
79 context->save = NULL ;
80 DEBUG2("Current position memorized (%p). Jumping to context (%p)",context,old_context);
81 return_value = swapcontext (&(context->uc), &(old_context->uc));
82 xbt_assert0((return_value==0),"Context swapping failure");
83 DEBUG1("I am (%p). Coming back\n",context);
91 static void xbt_context_destroy(xbt_context_t context)
94 #ifdef CONTEXT_THREADS
95 xbt_free(context->thread);
96 xbt_mutex_destroy(context->mutex);
97 xbt_thcond_destroy(context->cond);
99 context->thread = NULL;
100 context->mutex = NULL;
101 context->cond = NULL;
104 if(context->exception)
105 free(context->exception);
111 static void __context_exit(xbt_context_t context ,int value)
114 DEBUG0("Freeing arguments");
115 for(i=0;i<context->argc; i++)
117 free(context->argv[i]);
122 if(context->cleanup_func){
123 DEBUG0("Calling cleanup function");
124 context->cleanup_func(context->cleanup_arg);
127 DEBUG0("Putting context in the to_destroy set");
128 xbt_swag_remove(context, context_living);
129 xbt_swag_insert(context, context_to_destroy);
130 DEBUG0("Context put in the to_destroy set");
133 #ifdef CONTEXT_THREADS
134 DEBUG0("**** Locking ****");
135 xbt_mutex_lock(context->mutex);
136 DEBUG0("**** Updating current_context ****");
137 current_context = context;
138 DEBUG0("**** Releasing the prisonner ****");
139 xbt_thcond_signal(context->cond);
140 DEBUG0("**** Unlocking ****");
141 xbt_mutex_unlock(context->mutex);
142 DEBUG0("**** Exiting ****");
145 __xbt_context_yield(context);
147 xbt_assert0(0,"You can't be here!");
151 __context_wrapper(void* c) {
152 xbt_context_t context = (xbt_context_t)c;
154 #ifdef CONTEXT_THREADS
155 context->thread = xbt_thread_self();
157 DEBUG2("**[%p:%p]** Lock ****",context,(void*)xbt_thread_self());
158 xbt_mutex_lock(context->mutex);
160 DEBUG2("**[%p:%p]** Releasing the prisonner ****",context,(void*)xbt_thread_self());
161 xbt_thcond_signal(context->cond);
163 DEBUG2("**[%p:%p]** Going to Jail ****",context,(void*)xbt_thread_self());
164 xbt_thcond_wait(context->cond, context->mutex);
166 DEBUG2("**[%p:%p]** Unlocking ****",context,(void*)xbt_thread_self());
167 xbt_mutex_unlock(context->mutex);
171 if(context->startup_func)
172 context->startup_func(context->startup_arg);
174 DEBUG0("Calling the main function");
176 __context_exit(context, (context->code) (context->argc,context->argv));
180 /* callback: context fetching */
181 static ex_ctx_t *__context_ex_ctx(void)
183 return current_context->exception;
186 /* callback: termination */
187 static void __context_ex_terminate(xbt_ex_t *e) {
191 /* FIXME: there should be a configuration variable to choose this
192 if(current_context!=init_context)
193 __context_exit(current_context, e->value);
200 * \ingroup XBT_context
203 /** Context module initialization
205 * \warning It has to be called before using any other function of this module.
207 void xbt_context_init(void)
209 if(!current_context){
210 current_context = init_context = xbt_new0(s_xbt_context_t,1);
212 init_context->exception = xbt_new(ex_ctx_t,1);
213 XBT_CTX_INITIALIZE(init_context->exception);
214 __xbt_ex_ctx = __context_ex_ctx;
215 __xbt_ex_terminate = __context_ex_terminate;
216 context_to_destroy = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
217 context_living = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
218 xbt_swag_insert(init_context, context_living);
222 /** Garbage collection
224 * Should be called some time to time to free the memory allocated for contexts
225 * that have finished executing their main functions.
227 void xbt_context_empty_trash(void)
229 xbt_context_t context=NULL;
231 while((context=xbt_swag_extract(context_to_destroy)))
232 xbt_context_destroy(context);
236 * \param context the context to start
238 * Calling this function prepares \a context to be run. It will
239 however run effectively only when calling #xbt_context_schedule
241 void xbt_context_start(xbt_context_t context)
243 #ifdef CONTEXT_THREADS
244 /* Launch the thread */
245 DEBUG1("**[%p]** Locking ****",context);
246 xbt_mutex_lock(context->mutex);
248 DEBUG1("**[%p]** Thread create ****",context);
249 context->thread = xbt_thread_create(__context_wrapper, context);
250 DEBUG2("**[%p]** Thread created : %p ****",context,context->thread);
252 DEBUG1("**[%p]** Going to jail ****",context);
253 xbt_thcond_wait(context->cond, context->mutex);
254 DEBUG1("**[%p]** Unlocking ****",context);
255 xbt_mutex_unlock(context->mutex);
257 makecontext (&(context->uc), (void (*) (void)) __context_wrapper,1, context);
263 * \param code a main function
264 * \param startup_func a function to call when running the context for
265 * the first time and just before the main function \a code
266 * \param startup_arg the argument passed to the previous function (\a startup_func)
267 * \param cleanup_func a function to call when running the context, just after
268 the termination of the main function \a code
269 * \param cleanup_arg the argument passed to the previous function (\a cleanup_func)
270 * \param argc first argument of function \a code
271 * \param argv seconde argument of function \a code
273 xbt_context_t xbt_context_new(xbt_context_function_t code,
274 void_f_pvoid_t startup_func, void *startup_arg,
275 void_f_pvoid_t cleanup_func, void *cleanup_arg,
276 int argc, char *argv[])
278 xbt_context_t res = NULL;
280 res = xbt_new0(s_xbt_context_t,1);
283 #ifdef CONTEXT_THREADS
284 res->mutex = xbt_mutex_init();
285 res->cond = xbt_thcond_init();
287 /* FIXME: strerror is not thread safe */
288 xbt_assert2(getcontext(&(res->uc))==0,"Error in context saving: %d (%s)", errno, strerror(errno));
289 res->uc.uc_link = NULL;
290 /* res->uc.uc_link = &(current_context->uc); */
291 /* WARNING : when this context is over, the current_context (i.e. the
292 father), is awaken... Theorically, the wrapper should prevent using
294 res->uc.uc_stack.ss_sp = pth_skaddr_makecontext(res->stack,STACK_SIZE);
295 res->uc.uc_stack.ss_size = pth_sksize_makecontext(res->stack,STACK_SIZE);
296 #endif /* CONTEXT_THREADS or not */
300 res->startup_func = startup_func;
301 res->startup_arg = startup_arg;
302 res->cleanup_func = cleanup_func;
303 res->cleanup_arg = cleanup_arg;
304 res->exception = xbt_new(ex_ctx_t,1);
305 XBT_CTX_INITIALIZE(res->exception);
307 xbt_swag_insert(res, context_living);
313 * Calling this function makes the current context yield. The context
314 * that scheduled it returns from xbt_context_schedule as if nothing
317 void xbt_context_yield(void)
319 __xbt_context_yield(current_context);
323 * \param context the winner
325 * Calling this function blocks the current context and schedule \a context.
326 * When \a context will call xbt_context_yield, it will return
327 * to this function as if nothing had happened.
329 void xbt_context_schedule(xbt_context_t context)
331 DEBUG1("Scheduling %p",context);
332 xbt_assert0((current_context==init_context),"You are not supposed to run this function here!");
333 __xbt_context_yield(context);
337 * This function kill all existing context and free all the memory
338 * that has been allocated in this module.
340 void xbt_context_exit(void) {
341 xbt_context_t context=NULL;
343 xbt_context_empty_trash();
344 xbt_swag_free(context_to_destroy);
346 while((context=xbt_swag_extract(context_living))) {
347 xbt_context_free(context);
350 xbt_swag_free(context_living);
352 init_context = current_context = NULL ;
356 * \param context poor victim
358 * This function simply kills \a context... scarry isn't it ?
360 void xbt_context_free(xbt_context_t context)
365 xbt_swag_remove(context, context_living);
367 for(i=0;i<context->argc; i++)
369 free(context->argv[i]);
374 if(context->cleanup_func)
375 context->cleanup_func(context->cleanup_arg);
377 xbt_context_destroy(context);