Logo AND Algorithmique Numérique Distribuée

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