1 /* a fast and simple context switching library */
3 /* Copyright (c) 2004-2008 the SimGrid team. */
4 /* All rights reserved. */
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. */
12 #include "xbt_context_private.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_context,xbt,"Context switching mecanism");
16 /* the context associated with the current process */
17 xbt_context_t current_context = NULL;
19 /* the context associated with the maestro */
20 xbt_context_t maestro_context = NULL;
23 /* this list contains the contexts to destroy */
24 xbt_swag_t context_to_destroy = NULL;
26 /* this list contains the contexts in use */
27 xbt_swag_t context_living = NULL;
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.
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.
39 static xbt_context_factory_t context_factory = NULL;
42 * This function is call by the xbt_init() function to initialize the context module.
44 void xbt_context_mod_init(void)
46 if (!context_factory) {
47 /* select context factory to use to create the context(depends of the macro definitions) */
49 #ifdef CONTEXT_THREADS
50 /* context switch based os thread */
51 xbt_ctx_thread_factory_init(&context_factory);
53 /* context switch based ucontext */
54 xbt_ctx_sysv_factory_init(&context_factory);
56 /* context switch is not allowed on Windows */
57 #error ERROR [__FILE__, line __LINE__]: no context based implementation specified.
60 /* maestro context specialisation (this create the maestro with the good implementation */
61 (*(context_factory->create_maestro_context)) (&maestro_context);
63 /* the current context is the context of the maestro */
64 current_context = maestro_context;
66 /* the current context doesn't want to die */
67 current_context->iwannadie = 0;
69 /* intantiation of the lists containing the contexts to destroy and the contexts in use */
71 xbt_swag_new(xbt_swag_offset(*current_context, hookup));
72 context_living = xbt_swag_new(xbt_swag_offset(*current_context, hookup));
74 /* insert the current context in the list of the contexts in use */
75 xbt_swag_insert(current_context, context_living);
81 * This function is call by the xbt_exit() function to finalize the context module.
83 void xbt_context_mod_exit(void)
85 if (context_factory) {
86 xbt_context_t context = NULL;
87 xbt_pfn_context_factory_finalize_t finalize_factory;
89 /* finalize the context factory */
90 finalize_factory = context_factory->finalize;
92 (*finalize_factory) (&context_factory);
94 /* destroy all contexts in the list of contexts to destroy */
95 xbt_context_empty_trash();
97 /* remove the context of the scheduler from the list of the contexts in use */
98 xbt_swag_remove(maestro_context, context_living);
101 * kill all the contexts in use :
102 * the killed contexts are added in the list of the contexts to destroy
104 while ((context = xbt_swag_extract(context_living)))
105 (*(context->kill)) (context);
108 /* destroy all contexts in the list of contexts to destroy */
109 xbt_context_empty_trash();
111 free(maestro_context);
112 maestro_context = current_context = NULL;
114 /* destroy the lists */
115 xbt_swag_free(context_to_destroy);
116 xbt_swag_free(context_living);
120 /*******************************/
121 /* Object creation/destruction */
122 /*******************************/
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
135 xbt_context_new(const char *name,
136 xbt_main_func_t code,
137 void_f_pvoid_t startup_func,
139 void_f_pvoid_t cleanup_func,
140 void *cleanup_arg, int argc, char *argv[]
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);
149 /* add the context in the list of the contexts in use */
150 xbt_swag_insert(context, context_living);
155 /* Scenario for the end of a context:
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,
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)
177 /* Argument must be stopped first -- runs in maestro context */
178 void xbt_context_free(xbt_context_t context)
180 (*(context->free)) (context);
184 void xbt_context_kill(xbt_context_t context)
186 (*(context->kill)) (context);
190 * \param context the context to start
192 * Calling this function prepares \a context to be run. It will
193 however run effectively only when calling #xbt_context_schedule
195 void xbt_context_start(xbt_context_t context)
197 (*(context->start)) (context);
201 * Calling this function makes the current context yield. The context
202 * that scheduled it returns from xbt_context_schedule as if nothing
205 * Only the processes can call this function, giving back the control
208 void xbt_context_yield(void)
210 (*(current_context->yield)) ();
214 * \param context the winner
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.
220 * Only the maestro can call this function to run a given process.
222 void xbt_context_schedule(xbt_context_t context)
224 (*(context->schedule)) (context);
227 void xbt_context_stop(int exit_code)
230 (*(current_context->stop)) (exit_code);
233 int xbt_context_select_factory(const char *name)
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();
241 /* the same context factory is requested return directly */
245 /* get the desired factory */
246 xbt_context_init_factory_by_name(&context_factory, name);
248 /* maestro context specialisation */
249 (*(context_factory->create_maestro_context)) (&maestro_context);
251 /* the current context is the context of the maestro */
252 current_context = maestro_context;
254 /* the current context doesn't want to die */
255 current_context->iwannadie = 0;
257 /* intantiation of the lists containing the contexts to destroy and the contexts in use */
259 xbt_swag_new(xbt_swag_offset(*current_context, hookup));
260 context_living = xbt_swag_new(xbt_swag_offset(*current_context, hookup));
262 /* insert the current context in the list of the contexts in use */
263 xbt_swag_insert(current_context, context_living);
269 xbt_context_init_factory_by_name(xbt_context_factory_t * factory,
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);
282 THROW1(not_found_error, 0, "Factory '%s' does not exist", name);
285 /** Garbage collection
287 * Should be called some time to time to free the memory allocated for contexts
288 * that have finished executing their main functions.
290 void xbt_context_empty_trash(void)
292 xbt_context_t context = NULL;
294 while ((context = xbt_swag_extract(context_to_destroy)))
295 (*(context->free)) (context);