Logo AND Algorithmique Numérique Distribuée

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