Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a1b719ee91b1bac88432b19a3ffda159d5f231ce
[simgrid.git] / src / xbt / xbt_context.c
1 /* a fast and simple context switching library                              */
2
3 /* Copyright (c) 2004 Arnaud Legrand.                                       */
4 /* Copyright (c) 2004, 2005 Martin Quinson.                                 */
5 /* All rights reserved.                                                     */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "portable.h"
11 #include "xbt/log.h"
12 #include "xbt/swag.h"
13 #include "xbt_context_private.h"
14
15 /* the context associated with the current process                              */
16 xbt_context_t current_context = NULL;
17
18 /* the context associated with the maestro                                              */
19 xbt_context_t maestro_context = NULL;
20
21
22 /* this list contains the contexts to destroy                                   */
23 xbt_swag_t context_to_destroy = NULL;
24
25 /* this list contains the contexts in use                                               */
26 xbt_swag_t context_living = NULL;
27
28 /* the context factory used to create the appropriate context   
29  * each context implementation define its own context factory
30  * a context factory is responsable of the creation of the context
31  * associated with the maestro and of all the context based on
32  * the selected implementation.
33  *
34  * for example, the context switch based on java thread use the
35  * java implementation of the context and the java factory build
36  * the context depending of this implementation.
37  */
38 static xbt_context_factory_t context_factory = NULL;
39
40 /**
41  * This function is call by the xbt_init() function to initialize the context module.
42  */
43 void xbt_context_mod_init(void)
44 {
45   if (!context_factory) {
46     /* select context factory to use to create the context(depends of the macro definitions) */
47
48 #ifdef CONTEXT_THREADS
49     /* context switch based os thread */
50     xbt_ctx_thread_factory_init(&context_factory);
51 #elif !defined(WIN32)
52     /* context switch based ucontext */
53     xbt_ctx_sysv_factory_init(&context_factory);
54 #else
55     /* context switch is not allowed on Windows */
56 #error ERROR [__FILE__, line __LINE__]: no context based implementation specified.
57 #endif
58
59     /* maestro context specialisation (this create the maestro with the good implementation */
60     (*(context_factory->create_maestro_context)) (&maestro_context);
61
62     /* the current context is the context of the maestro */
63     current_context = maestro_context;
64
65     /* the current context doesn't want to die */
66     current_context->iwannadie = 0;
67
68     /* intantiation of the lists containing the contexts to destroy and the contexts in use */
69     context_to_destroy =
70       xbt_swag_new(xbt_swag_offset(*current_context, hookup));
71     context_living = xbt_swag_new(xbt_swag_offset(*current_context, hookup));
72
73     /* insert the current context in the list of the contexts in use */
74     xbt_swag_insert(current_context, context_living);
75
76   }
77 }
78
79 /**
80  * This function is call by the xbt_exit() function to finalize the context module.
81  */
82 void xbt_context_mod_exit(void)
83 {
84   if (context_factory) {
85     xbt_context_t context = NULL;
86     xbt_pfn_context_factory_finalize_t finalize_factory;
87
88     /* finalize the context factory */
89     finalize_factory = context_factory->finalize;
90
91     (*finalize_factory) (&context_factory);
92
93     /* destroy all contexts in the list of contexts to destroy */
94     xbt_context_empty_trash();
95
96     /* remove the context of the scheduler from the list of the contexts in use */
97     xbt_swag_remove(maestro_context, context_living);
98
99     /*  
100      * kill all the contexts in use :
101      * the killed contexts are added in the list of the contexts to destroy
102      */
103     while ((context = xbt_swag_extract(context_living)))
104       (*(context->kill)) (context);
105
106
107     /* destroy all contexts in the list of contexts to destroy */
108     xbt_context_empty_trash();
109
110     free(maestro_context);
111     maestro_context = current_context = NULL;
112
113     /* destroy the lists */
114     xbt_swag_free(context_to_destroy);
115     xbt_swag_free(context_living);
116   }
117 }
118
119 /*******************************/
120 /* Object creation/destruction */
121 /*******************************/
122 /** 
123  * \param code a main function
124  * \param startup_func a function to call when running the context for
125  *      the first time and just before the main function \a code
126  * \param startup_arg the argument passed to the previous function (\a startup_func)
127  * \param cleanup_func a function to call when running the context, just after 
128         the termination of the main function \a code
129  * \param cleanup_arg the argument passed to the previous function (\a cleanup_func)
130  * \param argc first argument of function \a code
131  * \param argv seconde argument of function \a code
132  */
133 xbt_context_t
134 xbt_context_new(const char *name,
135                 xbt_main_func_t code,
136                 void_f_pvoid_t startup_func,
137                 void *startup_arg,
138                 void_f_pvoid_t cleanup_func,
139                 void *cleanup_arg, int argc, char *argv[]
140   )
141 {
142   /* use the appropriate context factory to create the appropriate context */
143   xbt_context_t context =
144     (*(context_factory->create_context)) (name, code, startup_func,
145                                           startup_arg, cleanup_func,
146                                           cleanup_arg, argc, argv);
147
148   /* add the context in the list of the contexts in use */
149   xbt_swag_insert(context, context_living);
150
151   return context;
152 }
153
154 /* Scenario for the end of a context:
155  * 
156  * CASE 1: death after end of function
157  *   __context_wrapper, called by os thread, calls xbt_context_stop after user code stops
158  *   xbt_context_stop calls user cleanup_func if any (in context settings),
159  *                    add current to trashbin
160  *                    yields back to maestro (destroy os thread on need)
161  *   From time to time, maestro calls xbt_context_empty_trash, 
162  *       which maps xbt_context_free on the content
163  *   xbt_context_free frees some more memory, 
164  *                    joins os thread
165  * 
166  * CASE 2: brutal death
167  *   xbt_context_kill (from any context)
168  *                    set context->wannadie to 1
169  *                    yields to the context
170  *   the context is awaken in the middle of __yield. 
171  *   At the end of it, it checks that wannadie == 1, and call xbt_context_stop
172  *   (same than first case afterward)
173  */
174
175
176 /* Argument must be stopped first -- runs in maestro context */
177 void xbt_context_free(xbt_context_t context)
178 {
179   (*(context->free)) (context);
180 }
181
182
183 void xbt_context_kill(xbt_context_t context)
184 {
185   (*(context->kill)) (context);
186 }
187
188 /** 
189  * \param context the context to start
190  * 
191  * Calling this function prepares \a context to be run. It will 
192    however run effectively only when calling #xbt_context_schedule
193  */
194 void xbt_context_start(xbt_context_t context)
195 {
196   (*(context->start)) (context);
197 }
198
199 /** 
200  * Calling this function makes the current context yield. The context
201  * that scheduled it returns from xbt_context_schedule as if nothing
202  * had happened.
203  * 
204  * Only the processes can call this function, giving back the control
205  * to the maestro
206  */
207 void xbt_context_yield(void)
208 {
209   (*(current_context->yield)) ();
210 }
211
212 /** 
213  * \param context the winner
214  *
215  * Calling this function blocks the current context and schedule \a context.  
216  * When \a context will call xbt_context_yield, it will return
217  * to this function as if nothing had happened.
218  * 
219  * Only the maestro can call this function to run a given process.
220  */
221 void xbt_context_schedule(xbt_context_t context)
222 {
223   (*(context->schedule)) (context);
224 }
225
226 void xbt_context_stop(int exit_code)
227 {
228
229   (*(current_context->stop)) (exit_code);
230 }
231
232 int xbt_context_select_factory(const char *name)
233 {
234   /* if a factory is already instantiated (xbt_context_mod_init() was called) */
235   if (NULL != context_factory) {
236     /* if the desired factory is different of the current factory, call xbt_context_mod_exit() */
237     if (strcmp(context_factory->name, name))
238       xbt_context_mod_exit();
239     else
240       /* the same context factory is requested return directly */
241       return 0;
242   }
243
244   /* get the desired factory */
245   xbt_context_init_factory_by_name(&context_factory, name);
246
247   /* maestro context specialisation */
248   (*(context_factory->create_maestro_context)) (&maestro_context);
249
250   /* the current context is the context of the maestro */
251   current_context = maestro_context;
252
253   /* the current context doesn't want to die */
254   current_context->iwannadie = 0;
255
256   /* intantiation of the lists containing the contexts to destroy and the contexts in use */
257   context_to_destroy =
258     xbt_swag_new(xbt_swag_offset(*current_context, hookup));
259   context_living = xbt_swag_new(xbt_swag_offset(*current_context, hookup));
260
261   /* insert the current context in the list of the contexts in use */
262   xbt_swag_insert(current_context, context_living);
263
264   return 0;
265 }
266
267 void
268 xbt_context_init_factory_by_name(xbt_context_factory_t * factory,
269                                  const char *name)
270 {
271   if (!strcmp(name, "java"))
272     xbt_ctx_java_factory_init(factory);
273 #ifdef CONTEXT_THREADS
274   else if (!strcmp(name, "thread"))
275     xbt_ctx_thread_factory_init(factory);
276 #elif !defined(WIN32)
277   else if (!strcmp(name, "sysv"))
278     xbt_ctx_sysv_factory_init(factory);
279 #endif
280   else
281     THROW1(not_found_error, 0, "Factory '%s' does not exist", name);
282 }
283
284 /** Garbage collection
285  *
286  * Should be called some time to time to free the memory allocated for contexts
287  * that have finished executing their main functions.
288  */
289 void xbt_context_empty_trash(void)
290 {
291   xbt_context_t context = NULL;
292
293   while ((context = xbt_swag_extract(context_to_destroy)))
294     (*(context->free)) (context);
295 }