Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid endless bogus messages when an exception is raised from outside a context.
[simgrid.git] / src / xbt / context.c
1 /*      $Id$     */
2
3 /* a fast and simple context switching library                              */
4
5 /* Copyright (c) 2004 Arnaud Legrand.                                       */
6 /* Copyright (c) 2004, 2005 Martin Quinson.                                 */
7 /* All rights reserved.                                                     */
8
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. */
11
12 #include "portable.h"
13 #include "context_private.h"
14 #include "xbt/log.h"
15 #include "xbt/dynar.h"
16 #include "gras_config.h"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(context, xbt, "Context");
19
20 #define VOIRP(expr) DEBUG1("  {" #expr " = %p }", expr)
21
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;
26
27 #ifndef USE_PTHREADS /* USE_PTHREADS and USE_CONTEXT are exclusive */
28 # ifndef USE_UCONTEXT
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
33 # endif
34 #endif
35
36 static void __xbt_context_yield(xbt_context_t context)
37 {
38   xbt_assert0(current_context,"You have to call context_init() first.");
39   
40   DEBUG2("--------- current_context (%p) is yielding to context(%p) ---------",
41          current_context,context);
42
43 #ifdef USE_PTHREADS
44   if (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;
58   }
59 #else
60   if(current_context)
61     VOIRP(current_context->save);
62
63   VOIRP(context);
64   if(context) VOIRP(context->save);
65   if (context) {
66
67     int return_value = 0;
68
69     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);
79     } else {
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);
90     }
91   }
92 #endif
93   return;
94 }
95
96 static void xbt_context_destroy(xbt_context_t context)
97 {
98 #ifdef USE_PTHREADS
99   xbt_free(context->thread);
100   pthread_mutex_destroy(&(context->mutex));
101   pthread_cond_destroy(&(context->cond));
102 #endif
103   if(context->exception) free(context->exception);
104   free(context);
105   return;
106 }
107
108 static void __context_exit(xbt_context_t context ,int value)
109 {
110   int i;
111   DEBUG0("Freeing arguments");
112   for(i=0;i<context->argc; i++) 
113     if(context->argv[i]) free(context->argv[i]);
114   if(context->argv) free(context->argv);
115
116   if(context->cleanup_func) {
117     DEBUG0("Calling cleanup function");
118     context->cleanup_func(context->cleanup_arg);
119   }
120
121   DEBUG0("Putting context in the to_destroy set");
122   xbt_swag_remove(context, context_living);
123   xbt_swag_insert(context, context_to_destroy);
124   DEBUG0("Context put in the to_destroy set");
125
126   DEBUG0("Yielding");
127 #ifdef USE_PTHREADS
128   DEBUG0("**** Locking ****");
129   pthread_mutex_lock(&(context->mutex));
130   DEBUG0("**** Updating current_context ****");
131   current_context = context;
132   DEBUG0("**** Releasing the prisonner ****");
133   pthread_cond_signal(&(context->cond));  
134   DEBUG0("**** Unlocking ****");
135   pthread_mutex_unlock(&(context->mutex));
136   DEBUG0("**** Exiting ****");
137   pthread_exit(0);
138 #else
139   __xbt_context_yield(context);
140 #endif
141   xbt_assert0(0,"You can't be here!");
142 }
143
144 static void *__context_wrapper(void *c)
145 {
146   xbt_context_t context = c;
147
148 #ifdef USE_PTHREADS
149   DEBUG2("**[%p:%p]** Lock ****",context,(void*)pthread_self());
150   pthread_mutex_lock(&(context->mutex));
151   DEBUG2("**[%p:%p]** Releasing the prisonner ****",context,(void*)pthread_self());
152   pthread_cond_signal(&(context->cond));
153   DEBUG2("**[%p:%p]** Going to Jail ****",context,(void*)pthread_self());
154   pthread_cond_wait(&(context->cond), &(context->mutex));
155   DEBUG2("**[%p:%p]** Unlocking ****",context,(void*)pthread_self());
156   pthread_mutex_unlock(&(context->mutex));
157 #endif
158
159   if(context->startup_func)
160     context->startup_func(context->startup_arg);
161
162   DEBUG0("Calling the main function");
163
164   __context_exit(context, (context->code) (context->argc,context->argv));
165   return NULL;
166 }
167
168 /* callback: context fetching */
169 static ex_ctx_t *__context_ex_ctx(void)
170 {
171   return current_context->exception;
172 }
173
174 /* callback: termination */
175 static void __context_ex_terminate(xbt_ex_t *e) {
176   xbt_ex_display(e);
177
178   if(current_context!=init_context) 
179     __context_exit(current_context, e->value);
180   else
181     abort();
182 }
183
184 /** \name Functions 
185  *  \ingroup XBT_context
186  */
187 /* @{ */
188 /** Context module initialization
189  *
190  * \warning It has to be called before using any other function of this module.
191  */
192 void xbt_context_init(void)
193 {
194   if(!current_context) {
195     current_context = init_context = xbt_new0(s_xbt_context_t,1);
196     init_context->exception = xbt_new(ex_ctx_t,1);
197     XBT_CTX_INITIALIZE(init_context->exception);
198     __xbt_ex_ctx       = __context_ex_ctx;
199     __xbt_ex_terminate = __context_ex_terminate;
200     context_to_destroy = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
201     context_living = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
202     xbt_swag_insert(init_context, context_living);
203   }
204 }
205
206 /** Garbage collection
207  *
208  * Should be called some time to time to free the memory allocated for contexts
209  * that have finished executing their main functions.
210  */
211 void xbt_context_empty_trash(void)
212 {
213   xbt_context_t context=NULL;
214
215   while((context=xbt_swag_extract(context_to_destroy)))
216     xbt_context_destroy(context);
217 }
218
219 /** 
220  * \param context the context to start
221  * 
222  * Calling this function prepares \a context to be run. It will 
223    however run effectively only when calling #xbt_context_schedule
224  */
225 void xbt_context_start(xbt_context_t context) 
226 {
227 #ifdef USE_PTHREADS
228   /* Launch the thread */
229   DEBUG1("**[%p]** Locking ****",context);
230   pthread_mutex_lock(&(context->mutex));
231   DEBUG1("**[%p]** Pthread create ****",context);
232   xbt_assert0(!pthread_create(context->thread, NULL, __context_wrapper, context),
233               "Unable to create a thread.");
234   DEBUG2("**[%p]** Pthread created : %p ****",context,(void*)(*(context->thread)));
235   DEBUG1("**[%p]** Going to jail ****",context);
236   pthread_cond_wait(&(context->cond), &(context->mutex));
237   DEBUG1("**[%p]** Unlocking ****",context);
238   pthread_mutex_unlock(&(context->mutex));  
239 #else
240   makecontext (&(context->uc), (void (*) (void)) __context_wrapper,
241                1, context);
242 #endif
243   return;
244 }
245
246 /** 
247  * \param code a main function
248  * \param startup_func a function to call when running the context for
249  *      the first time and just before the main function \a code
250  * \param startup_arg the argument passed to the previous function (\a startup_func)
251  * \param cleanup_func a function to call when running the context, just after 
252         the termination of the main function \a code
253  * \param cleanup_arg the argument passed to the previous function (\a cleanup_func)
254  * \param argc first argument of function \a code
255  * \param argv seconde argument of function \a code
256  */
257 xbt_context_t xbt_context_new(xbt_context_function_t code, 
258                               void_f_pvoid_t startup_func, void *startup_arg,
259                               void_f_pvoid_t cleanup_func, void *cleanup_arg,
260                               int argc, char *argv[])
261 {
262   xbt_context_t res = NULL;
263
264   res = xbt_new0(s_xbt_context_t,1);
265
266   res->code = code;
267 #ifdef USE_PTHREADS
268   res->thread = xbt_new0(pthread_t,1);
269   xbt_assert0(!pthread_mutex_init(&(res->mutex), NULL), "Mutex initialization error");
270   xbt_assert0(!pthread_cond_init(&(res->cond), NULL), "Condition initialization error");
271 #else 
272   /* FIXME: strerror is not thread safe */
273   xbt_assert2(getcontext(&(res->uc))==0,"Error in context saving: %d (%s)", errno, strerror(errno));
274   res->uc.uc_link = NULL;
275   /*   res->uc.uc_link = &(current_context->uc); */
276   /* WARNING : when this context is over, the current_context (i.e. the 
277      father), is awaken... Theorically, the wrapper should prevent using 
278      this feature. */
279 # ifndef USE_WIN_CONTEXT    
280   res->uc.uc_stack.ss_sp = pth_skaddr_makecontext(res->stack,STACK_SIZE);
281   res->uc.uc_stack.ss_size = pth_sksize_makecontext(res->stack,STACK_SIZE);
282 # endif /* USE_WIN_CONTEXT */
283 #endif /* USE_PTHREADS or not */
284   res->argc = argc;
285   res->argv = argv;
286   res->startup_func = startup_func;
287   res->startup_arg = startup_arg;
288   res->cleanup_func = cleanup_func;
289   res->cleanup_arg = cleanup_arg;
290   res->exception = xbt_new(ex_ctx_t,1);
291   XBT_CTX_INITIALIZE(res->exception);
292
293   xbt_swag_insert(res, context_living);
294
295   return res;
296 }
297
298 /** 
299  * Calling this function makes the current context yield. The context
300  * that scheduled it returns from xbt_context_schedule as if nothing
301  * had happened.
302  */
303 void xbt_context_yield(void)
304 {
305   __xbt_context_yield(current_context);
306 }
307
308 /** 
309  * \param context the winner
310  *
311  * Calling this function blocks the current context and schedule \a context.  
312  * When \a context will call xbt_context_yield, it will return
313  * to this function as if nothing had happened.
314  */
315 void xbt_context_schedule(xbt_context_t context)
316 {
317   DEBUG1("Scheduling %p",context);
318   xbt_assert0((current_context==init_context),
319               "You are not supposed to run this function here!");
320   __xbt_context_yield(context);
321 }
322
323 /** 
324  * This function kill all existing context and free all the memory
325  * that has been allocated in this module.
326  */
327 void xbt_context_exit(void) {
328   xbt_context_t context=NULL;
329
330   xbt_context_empty_trash();
331   xbt_swag_free(context_to_destroy);
332
333   while((context=xbt_swag_extract(context_living)))
334     xbt_context_free(context);
335
336   xbt_swag_free(context_living);
337
338   init_context = current_context = NULL ;
339 }
340
341 /** 
342  * \param context poor victim
343  *
344  * This function simply kills \a context... scarry isn't it ?
345  */
346 void xbt_context_free(xbt_context_t context)
347 {
348   int i ;
349
350   xbt_swag_remove(context, context_living);  
351   for(i=0;i<context->argc; i++) 
352     if(context->argv[i]) free(context->argv[i]);
353   if(context->argv) free(context->argv);
354   
355   if(context->cleanup_func)
356     context->cleanup_func(context->cleanup_arg);
357   xbt_context_destroy(context);
358
359   return;
360 }
361 /* @} */