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 "gras_config.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(context, 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 #ifndef USE_PTHREADS /* USE_PTHREADS and USE_CONTEXT are exclusive */
29 /* don't want to play with conditional compilation in automake tonight, sorry.
30 include directly the c file from here when needed. */
31 # include "context_win32.c"
32 # define USE_WIN_CONTEXT
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) ---------",
41 current_context,context);
45 xbt_context_t self = current_context;
46 DEBUG0("**** Locking ****");
47 pthread_mutex_lock(&(context->mutex));
48 DEBUG0("**** Updating current_context ****");
49 current_context = context;
50 DEBUG0("**** Releasing the prisonner ****");
51 pthread_cond_signal(&(context->cond));
52 DEBUG0("**** Going to jail ****");
53 pthread_cond_wait(&(context->cond), &(context->mutex));
54 DEBUG0("**** Unlocking ****");
55 pthread_mutex_unlock(&(context->mutex));
56 DEBUG0("**** Updating current_context ****");
57 current_context = self;
61 VOIRP(current_context->save);
64 if(context) VOIRP(context->save);
66 if(context->save==NULL) {
70 DEBUG0("**** Yielding to somebody else ****");
71 DEBUG2("Saving current_context value (%p) to context(%p)->save",current_context,context);
72 context->save = current_context ;
73 DEBUG1("current_context becomes context(%p) ",context);
74 current_context = context ;
75 DEBUG1("Current position memorized (context->save). Jumping to context (%p)",context);
76 return_value = swapcontext (&(context->save->uc), &(context->uc));
77 xbt_assert0((return_value==0),"Context swapping failure");
78 DEBUG1("I am (%p). Coming back\n",context);
80 xbt_context_t old_context = context->save ;
81 DEBUG0("**** Back ! ****");
82 DEBUG2("Setting current_context (%p) to context(%p)->save",current_context,context);
83 current_context = context->save ;
84 DEBUG1("Setting context(%p)->save to NULL",context);
85 context->save = NULL ;
86 DEBUG2("Current position memorized (%p). Jumping to context (%p)",context,old_context);
87 return_value = swapcontext (&(context->uc), &(old_context->uc));
88 xbt_assert0((return_value==0),"Context swapping failure");
89 DEBUG1("I am (%p). Coming back\n",context);
96 static void xbt_context_destroy(xbt_context_t context)
99 xbt_free(context->thread);
100 pthread_mutex_destroy(&(context->mutex));
101 pthread_cond_destroy(&(context->cond));
103 if(context->exception) free(context->exception);
108 static void *__context_wrapper(void *c)
110 xbt_context_t context = c;
114 DEBUG0("**** Lock ****");
115 pthread_mutex_lock(&(context->mutex));
116 DEBUG0("**** Releasing the prisonner ****");
117 pthread_cond_signal(&(context->cond));
118 DEBUG0("**** Going to Jail ****");
119 pthread_cond_wait(&(context->cond), &(context->mutex));
120 DEBUG0("**** Unlocking ****");
121 pthread_mutex_unlock(&(context->mutex));
124 if(context->startup_func)
125 context->startup_func(context->startup_arg);
127 DEBUG0("Calling the main function");
128 (context->code) (context->argc,context->argv);
130 for(i=0;i<context->argc; i++)
131 if(context->argv[i]) free(context->argv[i]);
132 if(context->argv) free(context->argv);
134 if(context->cleanup_func)
135 context->cleanup_func(context->cleanup_arg);
137 xbt_swag_remove(context, context_living);
138 xbt_swag_insert(context, context_to_destroy);
140 __xbt_context_yield(context);
141 xbt_assert0(0,"You're cannot be here!");
145 /* callback: context fetching */
146 static ex_ctx_t *__context_ex_ctx(void)
148 return current_context->exception;
151 /* callback: termination */
152 static void __context_ex_terminate(xbt_ex_t *e)
158 * \ingroup XBT_context
161 /** Context module initialization
163 * \warning It has to be called before using any other function of this module.
165 void xbt_context_init(void)
167 if(!current_context) {
168 current_context = init_context = xbt_new0(s_xbt_context_t,1);
169 init_context->exception = xbt_new(ex_ctx_t,1);
170 XBT_CTX_INITIALIZE(init_context->exception);
171 __xbt_ex_ctx = __context_ex_ctx;
172 __xbt_ex_terminate = __context_ex_terminate;
173 context_to_destroy = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
174 context_living = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
175 xbt_swag_insert(init_context, context_living);
179 /** Garbage collection
181 * Should be called some time to time to free the memory allocated for contexts
182 * that have finished executing their main functions.
184 void xbt_context_empty_trash(void)
186 xbt_context_t context=NULL;
188 while((context=xbt_swag_extract(context_to_destroy)))
189 xbt_context_destroy(context);
193 * \param context the context to start
195 * Calling this function prepares \a context to be run. It will
196 however run effectively only when calling #xbt_context_schedule
198 void xbt_context_start(xbt_context_t context)
201 /* Launch the thread */
202 DEBUG0("**** Locking ****");
203 pthread_mutex_lock(&(context->mutex));
204 DEBUG0("**** Pthread create ****");
205 xbt_assert0(!pthread_create(context->thread, NULL, __context_wrapper, context),
206 "Unable to create a thread.");
207 DEBUG0("**** Going to jail ****");
208 pthread_cond_wait(&(context->cond), &(context->mutex));
209 DEBUG0("**** Unlocking ****");
210 pthread_mutex_unlock(&(context->mutex));
212 makecontext (&(context->uc), (void (*) (void)) __context_wrapper,
219 * \param code a main function
220 * \param startup_func a function to call when running the context for
221 * the first time and just before the main function \a code
222 * \param startup_arg the argument passed to the previous function (\a startup_func)
223 * \param cleanup_func a function to call when running the context, just after
224 the termination of the main function \a code
225 * \param cleanup_arg the argument passed to the previous function (\a cleanup_func)
226 * \param argc first argument of function \a code
227 * \param argv seconde argument of function \a code
229 xbt_context_t xbt_context_new(xbt_context_function_t code,
230 void_f_pvoid_t startup_func, void *startup_arg,
231 void_f_pvoid_t cleanup_func, void *cleanup_arg,
232 int argc, char *argv[])
234 xbt_context_t res = NULL;
236 res = xbt_new0(s_xbt_context_t,1);
240 res->thread = xbt_new0(pthread_t,1);
241 xbt_assert0(!pthread_mutex_init(&(res->mutex), NULL), "Mutex initialization error");
242 xbt_assert0(!pthread_cond_init(&(res->cond), NULL), "Condition initialization error");
244 /* FIXME: strerror is not thread safe */
245 xbt_assert2(getcontext(&(res->uc))==0,"Error in context saving: %d (%s)", errno, strerror(errno));
246 res->uc.uc_link = NULL;
247 /* res->uc.uc_link = &(current_context->uc); */
248 /* WARNING : when this context is over, the current_context (i.e. the
249 father), is awaken... Theorically, the wrapper should prevent using
251 # ifndef USE_WIN_CONTEXT
252 res->uc.uc_stack.ss_sp = pth_skaddr_makecontext(res->stack,STACK_SIZE);
253 res->uc.uc_stack.ss_size = pth_sksize_makecontext(res->stack,STACK_SIZE);
254 # endif /* USE_WIN_CONTEXT */
255 #endif /* USE_PTHREADS or not */
258 res->startup_func = startup_func;
259 res->startup_arg = startup_arg;
260 res->cleanup_func = cleanup_func;
261 res->cleanup_arg = cleanup_arg;
262 res->exception = xbt_new(ex_ctx_t,1);
263 XBT_CTX_INITIALIZE(res->exception);
265 xbt_swag_insert(res, context_living);
271 * Calling this function makes the current context yield. The context
272 * that scheduled it returns from xbt_context_schedule as if nothing
275 void xbt_context_yield(void)
277 __xbt_context_yield(current_context);
281 * \param context the winner
283 * Calling this function blocks the current context and schedule \a context.
284 * When \a context will call xbt_context_yield, it will return
285 * to this function as if nothing had happened.
287 void xbt_context_schedule(xbt_context_t context)
289 xbt_assert0((current_context==init_context),
290 "You are not supposed to run this function here!");
291 __xbt_context_yield(context);
295 * This function kill all existing context and free all the memory
296 * that has been allocated in this module.
298 void xbt_context_exit(void) {
299 xbt_context_t context=NULL;
301 xbt_context_empty_trash();
302 xbt_swag_free(context_to_destroy);
304 while((context=xbt_swag_extract(context_living)))
305 xbt_context_free(context);
307 xbt_swag_free(context_living);
309 init_context = current_context = NULL ;
313 * \param context poor victim
315 * This function simply kills \a context... scarry isn't it ?
317 void xbt_context_free(xbt_context_t context)
321 xbt_swag_remove(context, context_living);
322 for(i=0;i<context->argc; i++)
323 if(context->argv[i]) free(context->argv[i]);
324 if(context->argv) free(context->argv);
326 if(context->cleanup_func)
327 context->cleanup_func(context->cleanup_arg);
328 xbt_context_destroy(context);