Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This is already exported from the corresponding header (include/xbt/ex.h), as it...
[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 "xbt/xbt_thread.h"
17
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.
23   */
24 #include "xbt/xbt_thread.c"
25 #endif
26
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ctx, xbt, "Context");
28
29 #define VOIRP(expr) DEBUG1("  {" #expr " = %p }", expr)
30
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;
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) ---------",current_context,context);
41    
42         #ifdef CONTEXT_THREADS
43         if (context){
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;
57         }
58         #else /* use SUSv2 contexts */
59         if(current_context)
60                 VOIRP(current_context->save);
61         
62         VOIRP(context);
63         
64         if(context) 
65                 VOIRP(context->save);
66         
67         if (context){
68         
69                 int return_value = 0;
70                 
71                 if(context->save==NULL){
72                 
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);
82                 } else {
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);
93                 
94                 }
95         }
96         #endif
97         return;
98 }
99
100 static void xbt_context_free(xbt_context_t context)
101 {
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);
108
109         context->thread = NULL;
110         context->mutex = NULL;
111         context->cond = NULL;
112 #endif
113         
114         if(context->exception) 
115                 free(context->exception);
116                 
117         free(context);
118         return;
119 }
120         
121 static void __context_exit(xbt_context_t context ,int value)
122 {
123         int i;
124         DEBUG0("Freeing arguments");
125         for(i=0;i<context->argc; i++) 
126                 if(context->argv[i]) 
127                         free(context->argv[i]);
128         
129         if(context->argv) 
130                 free(context->argv);
131         
132         if(context->cleanup_func){
133                 DEBUG0("Calling cleanup function");
134                 context->cleanup_func(context->cleanup_arg);
135         }
136         
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");
141         DEBUG0("Yielding");
142         
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 ****");
153         xbt_thread_exit(0);
154         #else
155         __xbt_context_yield(context);
156         #endif
157         xbt_assert0(0,"You can't be here!");
158 }
159
160 static void *
161 __context_wrapper(void* c) {
162         xbt_context_t context = (xbt_context_t)c;
163         
164         #ifdef CONTEXT_THREADS
165         context->thread = xbt_thread_self();
166         
167         DEBUG2("**[%p:%p]** Lock ****",context,(void*)xbt_thread_self());
168         xbt_mutex_lock(context->mutex);
169         
170         DEBUG2("**[%p:%p]** Releasing the prisonner ****",context,(void*)xbt_thread_self());
171         xbt_thcond_signal(context->cond);
172         
173         DEBUG2("**[%p:%p]** Going to Jail ****",context,(void*)xbt_thread_self());
174         xbt_thcond_wait(context->cond, context->mutex);
175         
176         DEBUG2("**[%p:%p]** Unlocking ****",context,(void*)xbt_thread_self());
177         xbt_mutex_unlock(context->mutex);
178         
179         #endif
180         
181         if(context->startup_func)
182                 context->startup_func(context->startup_arg);
183         
184         DEBUG0("Calling the main function");
185         
186         __context_exit(context, (context->code) (context->argc,context->argv));
187         return NULL;
188 }
189
190 /* callback: context fetching */
191 static ex_ctx_t *__context_ex_ctx(void)
192 {
193         return current_context->exception;
194 }
195
196 /* callback: termination */
197 static void __context_ex_terminate(xbt_ex_t *e) {
198   xbt_ex_display(e);
199
200   abort();
201    /* FIXME: there should be a configuration variable to choose this
202   if(current_context!=init_context) 
203     __context_exit(current_context, e->value);
204   else
205     abort();
206     */
207 }
208
209 /** \name Functions 
210  *  \ingroup XBT_context
211  */
212 /* @{ */
213 /** Context module initialization
214  *
215  * \warning It has to be called before using any other function of this module.
216  */
217 void xbt_context_init(void)
218 {
219         if(!current_context){
220                 current_context = init_context = xbt_new0(s_xbt_context_t,1);
221                 DEBUG1("Init Context (%p)",init_context);
222                 
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);
230         }
231 }
232
233 /** Garbage collection
234  *
235  * Should be called some time to time to free the memory allocated for contexts
236  * that have finished executing their main functions.
237  */
238 void xbt_context_empty_trash(void)
239 {
240         xbt_context_t context=NULL;
241         DEBUG0("Emptying trashbin");
242         while((context=xbt_swag_extract(context_to_destroy)))
243                 xbt_context_free(context);
244 }
245
246 /** 
247  * \param context the context to start
248  * 
249  * Calling this function prepares \a context to be run. It will 
250    however run effectively only when calling #xbt_context_schedule
251  */
252 void xbt_context_start(xbt_context_t context) 
253 {
254         #ifdef CONTEXT_THREADS
255         /* Launch the thread */
256         DEBUG1("**[%p]** Locking ****",context);
257         xbt_mutex_lock(context->mutex);
258    
259         DEBUG1("**[%p]** Thread create ****",context);
260         context->thread = xbt_thread_create(__context_wrapper, context);   
261         DEBUG2("**[%p]** Thread created : %p ****",context,context->thread);
262    
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);
267         #else
268         makecontext (&(context->uc), (void (*) (void)) __context_wrapper,1, context);
269         #endif
270         return;
271 }
272
273 /** 
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
283  */
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[])
288 {
289         xbt_context_t res = NULL;
290         
291         res = xbt_new0(s_xbt_context_t,1);
292         
293         res->code = code;
294         #ifdef CONTEXT_THREADS
295         res->mutex = xbt_mutex_init();
296         res->cond = xbt_thcond_init();
297         #else 
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 
304         this feature. */
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 */
308         
309         res->argc = argc;
310         res->argv = argv;
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);
317         
318         xbt_swag_insert(res, context_living);
319         
320         return res;
321 }
322
323 /** 
324  * Calling this function makes the current context yield. The context
325  * that scheduled it returns from xbt_context_schedule as if nothing
326  * had happened.
327  */
328 void xbt_context_yield(void)
329 {
330         __xbt_context_yield(current_context);
331 }
332
333 /** 
334  * \param context the winner
335  *
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.
339  */
340 void xbt_context_schedule(xbt_context_t context)
341 {
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);
345 }
346
347 /** 
348  * This function kill all existing context and free all the memory
349  * that has been allocated in this module.
350  */
351 void xbt_context_exit(void) {
352         xbt_context_t context=NULL;
353
354         xbt_context_empty_trash();
355         xbt_swag_free(context_to_destroy);
356         
357         while((context=xbt_swag_extract(context_living))) {
358           if(context!=init_context) xbt_context_kill(context);
359         }
360         xbt_context_kill(init_context);
361         xbt_swag_free(context_living);
362         
363         init_context = current_context = NULL ;
364 }
365
366 /** 
367  * \param context poor victim
368  *
369  * This function simply kills \a context... scarry isn't it ?
370  */
371 void xbt_context_kill(xbt_context_t context)
372 {
373         int i ;
374         
375         DEBUG1("Killing %p", context);
376    
377         xbt_swag_remove(context, context_living);  
378         
379         if(context->cleanup_func) {
380           DEBUG1("Calling cleanup function %p", context->cleanup_func);
381           context->cleanup_func(context->cleanup_arg);
382         }
383
384         DEBUG0("Freeing arguments");
385         for(i=0;i<context->argc; i++) 
386                 if(context->argv[i]) 
387                         free(context->argv[i]);
388         
389         if(context->argv) 
390                 free(context->argv);
391         
392         xbt_context_free(context);
393         
394         return;
395 }
396 /* @} */