Logo AND Algorithmique Numérique Distribuée

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