Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
help compilation on grid5000 with the paranoid compilation mode...
[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_wrapper(void *c)
109 {
110   xbt_context_t context = c;
111   int i;
112
113 #ifdef USE_PTHREADS
114   DEBUG2("**[%p:%p]** Lock ****",context,(void*)pthread_self());
115   pthread_mutex_lock(&(context->mutex));
116   DEBUG2("**[%p:%p]** Releasing the prisonner ****",context,(void*)pthread_self());
117   pthread_cond_signal(&(context->cond));
118   DEBUG2("**[%p:%p]** Going to Jail ****",context,(void*)pthread_self());
119   pthread_cond_wait(&(context->cond), &(context->mutex));
120   DEBUG2("**[%p:%p]** Unlocking ****",context,(void*)pthread_self());
121   pthread_mutex_unlock(&(context->mutex));
122 #endif
123
124   if(context->startup_func)
125     context->startup_func(context->startup_arg);
126
127   DEBUG0("Calling the main function");
128   (context->code) (context->argc,context->argv);
129
130   DEBUG0("Freeing arguments");
131   for(i=0;i<context->argc; i++) 
132     if(context->argv[i]) free(context->argv[i]);
133   if(context->argv) free(context->argv);
134
135   if(context->cleanup_func) {
136     DEBUG0("Calling cleanup function");
137     context->cleanup_func(context->cleanup_arg);
138   }
139
140   DEBUG0("Putting context in the to_destroy set");
141   xbt_swag_remove(context, context_living);
142   xbt_swag_insert(context, context_to_destroy);
143   DEBUG0("Context put in the to_destroy set");
144
145   DEBUG0("Yielding");
146 #ifdef USE_PTHREADS
147   DEBUG0("**** Locking ****");
148   pthread_mutex_lock(&(context->mutex));
149   DEBUG0("**** Updating current_context ****");
150   current_context = context;
151   DEBUG0("**** Releasing the prisonner ****");
152   pthread_cond_signal(&(context->cond));  
153   DEBUG0("**** Unlocking ****");
154   pthread_mutex_unlock(&(context->mutex));
155   DEBUG0("**** Exiting ****");
156   pthread_exit(0);
157 #else
158   __xbt_context_yield(context);
159 #endif
160   xbt_assert0(0,"You can't be here!");
161   return NULL;
162 }
163
164 /* callback: context fetching */
165 static ex_ctx_t *__context_ex_ctx(void)
166 {
167   return current_context->exception;
168 }
169
170 /* callback: termination */
171 static void __context_ex_terminate(xbt_ex_t *e)
172 {
173   exit(e->value);
174 }
175
176 /** \name Functions 
177  *  \ingroup XBT_context
178  */
179 /* @{ */
180 /** Context module initialization
181  *
182  * \warning It has to be called before using any other function of this module.
183  */
184 void xbt_context_init(void)
185 {
186   if(!current_context) {
187     current_context = init_context = xbt_new0(s_xbt_context_t,1);
188     init_context->exception = xbt_new(ex_ctx_t,1);
189     XBT_CTX_INITIALIZE(init_context->exception);
190     __xbt_ex_ctx       = __context_ex_ctx;
191     __xbt_ex_terminate = __context_ex_terminate;
192     context_to_destroy = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
193     context_living = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
194     xbt_swag_insert(init_context, context_living);
195   }
196 }
197
198 /** Garbage collection
199  *
200  * Should be called some time to time to free the memory allocated for contexts
201  * that have finished executing their main functions.
202  */
203 void xbt_context_empty_trash(void)
204 {
205   xbt_context_t context=NULL;
206
207   while((context=xbt_swag_extract(context_to_destroy)))
208     xbt_context_destroy(context);
209 }
210
211 /** 
212  * \param context the context to start
213  * 
214  * Calling this function prepares \a context to be run. It will 
215    however run effectively only when calling #xbt_context_schedule
216  */
217 void xbt_context_start(xbt_context_t context) 
218 {
219 #ifdef USE_PTHREADS
220   /* Launch the thread */
221   DEBUG1("**[%p]** Locking ****",context);
222   pthread_mutex_lock(&(context->mutex));
223   DEBUG1("**[%p]** Pthread create ****",context);
224   xbt_assert0(!pthread_create(context->thread, NULL, __context_wrapper, context),
225               "Unable to create a thread.");
226   DEBUG2("**[%p]** Pthread created : %p ****",context,(void*)(*(context->thread)));
227   DEBUG1("**[%p]** Going to jail ****",context);
228   pthread_cond_wait(&(context->cond), &(context->mutex));
229   DEBUG1("**[%p]** Unlocking ****",context);
230   pthread_mutex_unlock(&(context->mutex));  
231 #else
232   makecontext (&(context->uc), (void (*) (void)) __context_wrapper,
233                1, context);
234 #endif
235   return;
236 }
237
238 /** 
239  * \param code a main function
240  * \param startup_func a function to call when running the context for
241  *      the first time and just before the main function \a code
242  * \param startup_arg the argument passed to the previous function (\a startup_func)
243  * \param cleanup_func a function to call when running the context, just after 
244         the termination of the main function \a code
245  * \param cleanup_arg the argument passed to the previous function (\a cleanup_func)
246  * \param argc first argument of function \a code
247  * \param argv seconde argument of function \a code
248  */
249 xbt_context_t xbt_context_new(xbt_context_function_t code, 
250                               void_f_pvoid_t startup_func, void *startup_arg,
251                               void_f_pvoid_t cleanup_func, void *cleanup_arg,
252                               int argc, char *argv[])
253 {
254   xbt_context_t res = NULL;
255
256   res = xbt_new0(s_xbt_context_t,1);
257
258   res->code = code;
259 #ifdef USE_PTHREADS
260   res->thread = xbt_new0(pthread_t,1);
261   xbt_assert0(!pthread_mutex_init(&(res->mutex), NULL), "Mutex initialization error");
262   xbt_assert0(!pthread_cond_init(&(res->cond), NULL), "Condition initialization error");
263 #else 
264   /* FIXME: strerror is not thread safe */
265   xbt_assert2(getcontext(&(res->uc))==0,"Error in context saving: %d (%s)", errno, strerror(errno));
266   res->uc.uc_link = NULL;
267   /*   res->uc.uc_link = &(current_context->uc); */
268   /* WARNING : when this context is over, the current_context (i.e. the 
269      father), is awaken... Theorically, the wrapper should prevent using 
270      this feature. */
271 # ifndef USE_WIN_CONTEXT    
272   res->uc.uc_stack.ss_sp = pth_skaddr_makecontext(res->stack,STACK_SIZE);
273   res->uc.uc_stack.ss_size = pth_sksize_makecontext(res->stack,STACK_SIZE);
274 # endif /* USE_WIN_CONTEXT */
275 #endif /* USE_PTHREADS or not */
276   res->argc = argc;
277   res->argv = argv;
278   res->startup_func = startup_func;
279   res->startup_arg = startup_arg;
280   res->cleanup_func = cleanup_func;
281   res->cleanup_arg = cleanup_arg;
282   res->exception = xbt_new(ex_ctx_t,1);
283   XBT_CTX_INITIALIZE(res->exception);
284
285   xbt_swag_insert(res, context_living);
286
287   return res;
288 }
289
290 /** 
291  * Calling this function makes the current context yield. The context
292  * that scheduled it returns from xbt_context_schedule as if nothing
293  * had happened.
294  */
295 void xbt_context_yield(void)
296 {
297   __xbt_context_yield(current_context);
298 }
299
300 /** 
301  * \param context the winner
302  *
303  * Calling this function blocks the current context and schedule \a context.  
304  * When \a context will call xbt_context_yield, it will return
305  * to this function as if nothing had happened.
306  */
307 void xbt_context_schedule(xbt_context_t context)
308 {
309   DEBUG1("Scheduling %p",context);
310   xbt_assert0((current_context==init_context),
311               "You are not supposed to run this function here!");
312   __xbt_context_yield(context);
313 }
314
315 /** 
316  * This function kill all existing context and free all the memory
317  * that has been allocated in this module.
318  */
319 void xbt_context_exit(void) {
320   xbt_context_t context=NULL;
321
322   xbt_context_empty_trash();
323   xbt_swag_free(context_to_destroy);
324
325   while((context=xbt_swag_extract(context_living)))
326     xbt_context_free(context);
327
328   xbt_swag_free(context_living);
329
330   init_context = current_context = NULL ;
331 }
332
333 /** 
334  * \param context poor victim
335  *
336  * This function simply kills \a context... scarry isn't it ?
337  */
338 void xbt_context_free(xbt_context_t context)
339 {
340   int i ;
341
342   xbt_swag_remove(context, context_living);  
343   for(i=0;i<context->argc; i++) 
344     if(context->argv[i]) free(context->argv[i]);
345   if(context->argv) free(context->argv);
346   
347   if(context->cleanup_func)
348     context->cleanup_func(context->cleanup_arg);
349   xbt_context_destroy(context);
350
351   return;
352 }
353 /* @} */