Logo AND Algorithmique Numérique Distribuée

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