Logo AND Algorithmique Numérique Distribuée

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