Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
getting rid of memleaks.
[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   int return_value = 0;
39
40   xbt_assert0(current_context,"You have to call context_init() first.");
41   
42   DEBUG2("--------- current_context (%p) is yielding to context(%p) ---------",
43          current_context,context);
44
45 #ifdef USE_PTHREADS
46   if (context) {
47     xbt_context_t self = current_context;
48     DEBUG0("**** Locking ****");
49     pthread_mutex_lock(&(context->mutex));
50     DEBUG0("**** Updating current_context ****");
51     current_context = context;
52     DEBUG0("**** Releasing the prisonner ****");
53     pthread_cond_signal(&(context->cond));
54     DEBUG0("**** Going to jail ****");
55     pthread_cond_wait(&(context->cond), &(context->mutex));
56     DEBUG0("**** Unlocking ****");
57     pthread_mutex_unlock(&(context->mutex));
58     DEBUG0("**** Updating current_context ****");
59     current_context = self;
60   }
61 #else
62   if(current_context)
63     VOIRP(current_context->save);
64
65   VOIRP(context);
66   if(context) VOIRP(context->save);
67   if (context) {
68     if(context->save==NULL) {
69       DEBUG0("**** Yielding to somebody else ****");
70       DEBUG2("Saving current_context value (%p) to context(%p)->save",current_context,context);
71       context->save = current_context ;
72       DEBUG1("current_context becomes  context(%p) ",context);
73       current_context = context ;
74       DEBUG1("Current position memorized (context->save). Jumping to context (%p)",context);
75       return_value = swapcontext (&(context->save->uc), &(context->uc));
76       xbt_assert0((return_value==0),"Context swapping failure");
77       DEBUG1("I am (%p). Coming back\n",context);
78     } else {
79       xbt_context_t old_context = context->save ;
80       DEBUG0("**** Back ! ****");
81       DEBUG2("Setting current_context (%p) to context(%p)->save",current_context,context);
82       current_context = context->save ;
83       DEBUG1("Setting context(%p)->save to NULL",context);
84       context->save = NULL ;
85       DEBUG2("Current position memorized (%p). Jumping to context (%p)",context,old_context);
86       return_value = swapcontext (&(context->uc), &(old_context->uc));
87       xbt_assert0((return_value==0),"Context swapping failure");
88       DEBUG1("I am (%p). Coming back\n",context);
89     }
90   }
91 #endif
92   return;
93 }
94
95 static void xbt_context_destroy(xbt_context_t context)
96 {
97 #ifdef USE_PTHREADS
98   xbt_free(context->thread);
99   pthread_mutex_destroy(&(context->mutex));
100   pthread_cond_destroy(&(context->cond));
101 #endif
102   if(context->exception) free(context->exception);
103   free(context);
104   return;
105 }
106
107 static void *__context_wrapper(void *c)
108 {
109   xbt_context_t context = c;
110   int i;
111
112 #ifdef USE_PTHREADS
113   DEBUG0("**** Lock ****");
114   pthread_mutex_lock(&(context->mutex));
115   DEBUG0("**** Releasing the prisonner ****");
116   pthread_cond_signal(&(context->cond));
117   DEBUG0("**** Going to Jail ****");
118   pthread_cond_wait(&(context->cond), &(context->mutex));
119   DEBUG0("**** Unlocking ****");
120   pthread_mutex_unlock(&(context->mutex));
121 #endif
122
123   if(context->startup_func)
124     context->startup_func(context->startup_arg);
125
126    DEBUG0("Calling the main function");
127   (context->code) (context->argc,context->argv);
128
129   for(i=0;i<context->argc; i++) 
130     if(context->argv[i]) free(context->argv[i]);
131   if(context->argv) free(context->argv);
132
133   if(context->cleanup_func)
134     context->cleanup_func(context->cleanup_arg);
135
136   xbt_swag_remove(context, context_living);
137   xbt_swag_insert(context, context_to_destroy);
138
139   __xbt_context_yield(context);
140   xbt_assert0(0,"You're cannot be here!");
141   return NULL;
142 }
143
144 /* callback: context fetching */
145 static ex_ctx_t *__context_ex_ctx(void)
146 {
147   return current_context->exception;
148 }
149
150 /* callback: termination */
151 static void __context_ex_terminate(xbt_ex_t *e)
152 {
153   exit(e->value);
154 }
155
156 /** \name Functions 
157  *  \ingroup XBT_context
158  */
159 /* @{ */
160 /** Context module initialization
161  *
162  * \warning It has to be called before using any other function of this module.
163  */
164 void xbt_context_init(void)
165 {
166   if(!current_context) {
167     current_context = init_context = xbt_new0(s_xbt_context_t,1);
168     init_context->exception = xbt_new(ex_ctx_t,1);
169     XBT_CTX_INITIALIZE(init_context->exception);
170     __xbt_ex_ctx       = __context_ex_ctx;
171     __xbt_ex_terminate = __context_ex_terminate;
172     context_to_destroy = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
173     context_living = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
174     xbt_swag_insert(init_context, context_living);
175   }
176 }
177
178 /** Garbage collection
179  *
180  * Should be called some time to time to free the memory allocated for contexts
181  * that have finished executing their main functions.
182  */
183 void xbt_context_empty_trash(void)
184 {
185   xbt_context_t context=NULL;
186
187   while((context=xbt_swag_extract(context_to_destroy)))
188     xbt_context_destroy(context);
189 }
190
191 /** 
192  * \param context the context to start
193  * 
194  * Calling this function prepares \a context to be run. It will 
195    however run effectively only when calling #xbt_context_schedule
196  */
197 void xbt_context_start(xbt_context_t context) 
198 {
199 #ifdef USE_PTHREADS
200   /* Launch the thread */
201   DEBUG0("**** Locking ****");
202   pthread_mutex_lock(&(context->mutex));
203   DEBUG0("**** Pthread create ****");
204   xbt_assert0(!pthread_create(context->thread, NULL, __context_wrapper, context),
205               "Unable to create a thread.");
206   DEBUG0("**** Going to jail ****");
207   pthread_cond_wait(&(context->cond), &(context->mutex));
208   DEBUG0("**** Unlocking ****");
209   pthread_mutex_unlock(&(context->mutex));  
210 #else
211   makecontext (&(context->uc), (void (*) (void)) __context_wrapper,
212                1, context);
213 #endif
214   return;
215 }
216
217 /** 
218  * \param code a main function
219  * \param startup_func a function to call when running the context for
220  *      the first time and just before the main function \a code
221  * \param startup_arg the argument passed to the previous function (\a startup_func)
222  * \param cleanup_func a function to call when running the context, just after 
223         the termination of the main function \a code
224  * \param cleanup_arg the argument passed to the previous function (\a cleanup_func)
225  * \param argc first argument of function \a code
226  * \param argv seconde argument of function \a code
227  */
228 xbt_context_t xbt_context_new(xbt_context_function_t code, 
229                               void_f_pvoid_t startup_func, void *startup_arg,
230                               void_f_pvoid_t cleanup_func, void *cleanup_arg,
231                               int argc, char *argv[])
232 {
233   xbt_context_t res = NULL;
234
235   res = xbt_new0(s_xbt_context_t,1);
236
237   res->code = code;
238 #ifdef USE_PTHREADS
239   res->thread = xbt_new0(pthread_t,1);
240   xbt_assert0(!pthread_mutex_init(&(res->mutex), NULL), "Mutex initialization error");
241   xbt_assert0(!pthread_cond_init(&(res->cond), NULL), "Condition initialization error");
242 #else 
243   /* FIXME: strerror is not thread safe */
244   xbt_assert2(getcontext(&(res->uc))==0,"Error in context saving: %d (%s)", errno, strerror(errno));
245   res->uc.uc_link = NULL;
246   /*   res->uc.uc_link = &(current_context->uc); */
247   /* WARNING : when this context is over, the current_context (i.e. the 
248      father), is awaken... Theorically, the wrapper should prevent using 
249      this feature. */
250 # ifndef USE_WIN_CONTEXT    
251   res->uc.uc_stack.ss_sp = pth_skaddr_makecontext(res->stack,STACK_SIZE);
252   res->uc.uc_stack.ss_size = pth_sksize_makecontext(res->stack,STACK_SIZE);
253 # endif /* USE_WIN_CONTEXT */
254 #endif /* USE_PTHREADS or not */
255   res->argc = argc;
256   res->argv = argv;
257   res->startup_func = startup_func;
258   res->startup_arg = startup_arg;
259   res->cleanup_func = cleanup_func;
260   res->cleanup_arg = cleanup_arg;
261   res->exception = xbt_new(ex_ctx_t,1);
262   XBT_CTX_INITIALIZE(res->exception);
263
264   xbt_swag_insert(res, context_living);
265
266   return res;
267 }
268
269 /** 
270  * Calling this function makes the current context yield. The context
271  * that scheduled it returns from xbt_context_schedule as if nothing
272  * had happened.
273  */
274 void xbt_context_yield(void)
275 {
276   __xbt_context_yield(current_context);
277 }
278
279 /** 
280  * \param context the winner
281  *
282  * Calling this function blocks the current context and schedule \a context.  
283  * When \a context will call xbt_context_yield, it will return
284  * to this function as if nothing had happened.
285  */
286 void xbt_context_schedule(xbt_context_t context)
287 {
288   xbt_assert0((current_context==init_context),
289               "You are not supposed to run this function here!");
290   __xbt_context_yield(context);
291 }
292
293 /** 
294  * This function kill all existing context and free all the memory
295  * that has been allocated in this module.
296  */
297 void xbt_context_exit(void) {
298   xbt_context_t context=NULL;
299
300   xbt_context_empty_trash();
301   xbt_swag_free(context_to_destroy);
302
303   while((context=xbt_swag_extract(context_living)))
304     xbt_context_free(context);
305
306   xbt_swag_free(context_living);
307
308   init_context = current_context = NULL ;
309 }
310
311 /** 
312  * \param context poor victim
313  *
314  * This function simply kills \a context... scarry isn't it ?
315  */
316 void xbt_context_free(xbt_context_t context)
317 {
318   int i ;
319
320   xbt_swag_remove(context, context_living);  
321   for(i=0;i<context->argc; i++) 
322     if(context->argv[i]) free(context->argv[i]);
323   if(context->argv) free(context->argv);
324   
325   if(context->cleanup_func)
326     context->cleanup_func(context->cleanup_arg);
327   xbt_context_destroy(context);
328
329   return;
330 }
331 /* @} */