Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8e1a7ae3ebb7240916b59e0ebf8919a18b3f5c64
[simgrid.git] / src / simix / 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 /* 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     /* destroy all contexts in the list of contexts to destroy */
90     xbt_context_empty_trash();
91
92     /* remove the context of the scheduler from the list of the contexts in use */
93     xbt_swag_remove(maestro_context, context_living);
94
95     /*
96      * kill all the contexts in use :
97      * the killed contexts are added in the list of the contexts to destroy
98      */
99     while ((context = xbt_swag_extract(context_living)))
100       (*(context_factory->kill)) (context);
101
102     /* destroy all contexts in the list of contexts to destroy */
103     xbt_context_empty_trash();
104
105     /* finalize the context factory */
106     finalize_factory = context_factory->finalize;
107
108     (*finalize_factory) (&context_factory);
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_factory->free)) (context);
180 }
181
182
183 void xbt_context_kill(xbt_context_t context)
184 {
185   (*(context_factory->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_factory->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   (*(context_factory->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_factory->schedule)) (context);
224 }
225
226 void xbt_context_stop(int exit_code)
227 {
228
229   (*(context_factory->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 #ifdef HAVE_JAVA     
273     xbt_ctx_java_factory_init(factory);
274 #else
275     THROW0(not_found_error, 0, "Factory 'Java' does not exist: Java support was not compiled in the SimGrid library");
276 #endif /* HAVE_JAVA */
277    
278   else if (!strcmp(name, "thread"))
279 #ifdef CONTEXT_THREADS
280     xbt_ctx_thread_factory_init(factory);
281 #else
282     THROW0(not_found_error, 0, "Factory 'thread' does not exist: thread support was not compiled in the SimGrid library");
283 #endif /* CONTEXT_THREADS */
284    
285   else if (!strcmp(name, "sysv"))
286 #if !defined(WIN32) && !defined(CONTEXT_THREADS)
287     xbt_ctx_sysv_factory_init(factory);
288 #else
289     THROW0(not_found_error, 0, "Factory 'sysv' does not exist: no System V thread support under Windows");
290 #endif   
291   else
292     THROW1(not_found_error, 0, "Factory '%s' does not exist", name);
293 }
294
295 /** Garbage collection
296  *
297  * Should be called some time to time to free the memory allocated for contexts
298  * that have finished executing their main functions.
299  */
300 void xbt_context_empty_trash(void)
301 {
302   xbt_context_t context = NULL;
303
304   while ((context = xbt_swag_extract(context_to_destroy)))
305     (*(context_factory->free)) (context);
306 }