Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
More verbose messages on errors
[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 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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(context, xbt, "Context");
18
19 /* #define WARNING(format, ...) (fprintf(stderr, "[%s , %s : %d] ", __FILE__, __FUNCTION__, __LINE__),\ */
20 /*                               fprintf(stderr, format, ## __VA_ARGS__), \ */
21 /*                               fprintf(stderr, "\n")) */
22 /* #define VOIRP(expr) WARNING("  {" #expr " = %p }", expr) */
23
24 #ifndef HAVE_UCONTEXT_H
25 /* don't want to play with conditional compilation in automake tonight, sorry.
26    include directly the c file from here when needed. */
27 # include "context_win32.c" 
28 #endif
29
30 static xbt_context_t current_context = NULL;
31 static xbt_context_t init_context = NULL;
32 static xbt_swag_t context_to_destroy = NULL;
33 static xbt_swag_t context_living = NULL;
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 /*   WARNING("--------- current_context (%p) is yielding to context(%p) ---------",current_context,context); */
42 /*   VOIRP(current_context); */
43 /*   if(current_context) VOIRP(current_context->save); */
44 /*   VOIRP(context); */
45 /*   if(context) VOIRP(context->save); */
46
47   if (context) {
48     if(context->save==NULL) {
49 /*       WARNING("**** Yielding to somebody else ****"); */
50 /*       WARNING("Saving current_context value (%p) to context(%p)->save",current_context,context); */
51       context->save = current_context ;
52 /*       WARNING("current_context becomes  context(%p) ",context); */
53       current_context = context ;
54 /*       WARNING("Current position memorized (context->save). Jumping to context (%p)",context); */
55       return_value = swapcontext (&(context->save->uc), &(context->uc));
56       xbt_assert0((return_value==0),"Context swapping failure");
57 /*       WARNING("I am (%p). Coming back\n",context); */
58     } else {
59       xbt_context_t old_context = context->save ;
60 /*       WARNING("**** Back ! ****"); */
61 /*       WARNING("Setting current_context (%p) to context(%p)->save",current_context,context); */
62       current_context = context->save ;
63 /*       WARNING("Setting context(%p)->save to NULL",context); */
64       context->save = NULL ;
65 /*       WARNING("Current position memorized (%p). Jumping to context (%p)",context,old_context); */
66       return_value = swapcontext (&(context->uc), &(old_context->uc));
67       xbt_assert0((return_value==0),"Context swapping failure");
68 /*       WARNING("I am (%p). Coming back\n",context); */
69     }
70   }
71
72   return;
73 }
74
75 static void xbt_context_destroy(xbt_context_t context)
76 {
77   xbt_free(context);
78
79   return;
80 }
81
82 /** \name Functions 
83  *  \ingroup XBT_context
84  */
85 /*@{*/
86 /** Context module initialization
87  *
88  * \warning It has to be called before using any other function of this module.
89  */
90 void xbt_context_init(void)
91 {
92   if(!current_context) {
93     current_context = init_context = xbt_new0(s_xbt_context_t,1);
94     context_to_destroy = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
95     context_living = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
96     xbt_swag_insert(init_context, context_living);
97   }
98 }
99
100 /** Garbage collection
101  *
102  * Should be called some time to time to free the memory allocated for contexts
103  * that have finished executing their main functions.
104  */
105 void xbt_context_empty_trash(void)
106 {
107   xbt_context_t context=NULL;
108
109   while((context=xbt_swag_extract(context_to_destroy)))
110     xbt_context_destroy(context);
111 }
112
113 static void *__context_wrapper(void *c)
114 {
115   xbt_context_t context = c;
116   int i;
117
118 /*   msg_global->current_process = process; */
119
120   if(context->startup_func)
121     context->startup_func(context->startup_arg);
122
123   /*  WARNING("Calling the main function"); */
124   /*   xbt_context_yield(context); */
125   (context->code) (context->argc,context->argv);
126
127   for(i=0;i<context->argc; i++) 
128     if(context->argv[i]) xbt_free(context->argv[i]);
129   if(context->argv) xbt_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 /** 
143  * \param context the context to start
144  * 
145  * Calling this function prepares \a context to be run. It will 
146    however run effectively only when calling #xbt_context_schedule
147  */
148 void xbt_context_start(xbt_context_t context) 
149 {
150 /*   xbt_fifo_insert(msg_global->process, process); */
151 /*   xbt_fifo_insert(msg_global->process_to_run, process); */
152
153   makecontext (&(context->uc), (void (*) (void)) __context_wrapper,
154                1, context);
155   return;
156 }
157
158 /** 
159  * \param code a main function
160  * \param startup_func a function to call when running the context for
161  *      the first time and just before the main function \a code
162  * \param startup_arg the argument passed to the previous function (\a startup_func)
163  * \param cleanup_func a function to call when running the context, just after 
164         the termination of the main function \a code
165  * \param cleanup_arg the argument passed to the previous function (\a cleanup_func)
166  * \param argc first argument of function \a code
167  * \param argv seconde argument of function \a code
168  */
169 xbt_context_t xbt_context_new(xbt_context_function_t code, 
170                               void_f_pvoid_t startup_func, void *startup_arg,
171                               void_f_pvoid_t cleanup_func, void *cleanup_arg,
172                               int argc, char *argv[])
173 {
174   xbt_context_t res = NULL;
175
176   res = xbt_new0(s_xbt_context_t,1);
177
178   /* FIXME: strerror is not thread safe */
179   xbt_assert2(getcontext(&(res->uc))==0,"Error in context saving: %d (%s)", errno, strerror(errno));
180
181   res->code = code;
182   res->uc.uc_link = NULL;
183   res->argc = argc;
184   res->argv = argv;
185 /*   res->uc.uc_link = &(current_context->uc); */
186   /* WARNING : when this context is over, the current_context (i.e. the 
187      father), is awaken... Theorically, the wrapper should prevent using 
188      this feature. */
189   res->uc.uc_stack.ss_sp = res->stack;
190   res->uc.uc_stack.ss_size = STACK_SIZE;
191   res->startup_func = startup_func;
192   res->startup_arg = startup_arg;
193   res->cleanup_func = cleanup_func;
194   res->cleanup_arg = cleanup_arg;
195
196   xbt_swag_insert(res, context_living);
197
198   return res;
199 }
200
201
202 /** 
203  * Calling this function makes the current context yield. The context
204  * that scheduled it returns from xbt_context_schedule as if nothing
205  * had happened.
206  */
207 void xbt_context_yield(void)
208 {
209   __xbt_context_yield(current_context);
210 }
211
212
213 /** 
214  * \param context the winner
215  *
216  * Calling this function blocks the current context and schedule \a context.  
217  * When \a context will call xbt_context_yield, it will return
218  * to this function as if nothing had happened.
219  */
220 void xbt_context_schedule(xbt_context_t context)
221 {
222   xbt_assert0((current_context==init_context),
223               "You are not supposed to run this function here!");
224   __xbt_context_yield(context);
225 }
226
227 /** 
228  * This function kill all existing context and free all the memory
229  * that has been allocated in this module.
230  */
231 void xbt_context_exit(void) {
232   xbt_context_t context=NULL;
233
234   xbt_context_empty_trash();
235   xbt_swag_free(context_to_destroy);
236
237   while((context=xbt_swag_extract(context_living)))
238     xbt_context_free(context);
239
240   xbt_swag_free(context_living);
241
242   init_context = current_context = NULL ;
243 }
244
245 /** 
246  * \param context poor victim
247  *
248  * This function simply kills \a context... scarry isn't it ?
249  */
250 void xbt_context_free(xbt_context_t context)
251 {
252   int i ;
253
254   xbt_swag_remove(context, context_living);  
255   for(i=0;i<context->argc; i++) 
256     if(context->argv[i]) xbt_free(context->argv[i]);
257   if(context->argv) xbt_free(context->argv);
258   
259   if(context->cleanup_func)
260     context->cleanup_func(context->cleanup_arg);
261   xbt_context_destroy(context);
262
263   return;
264 }
265 /*@}*/