Logo AND Algorithmique Numérique Distribuée

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