Logo AND Algorithmique Numérique Distribuée

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