Logo AND Algorithmique Numérique Distribuée

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