1 /* a fast and simple context switching library */
3 /* Copyright (c) 2004 Arnaud Legrand. */
4 /* Copyright (c) 2004, 2005 Martin Quinson. */
5 /* All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include "xbt_context_private.h"
15 /* the context associated with the current process */
16 xbt_context_t current_context = NULL;
18 /* the context associated with the maestro */
19 xbt_context_t maestro_context = NULL;
22 /* this list contains the contexts to destroy */
23 xbt_swag_t context_to_destroy = NULL;
25 /* this list contains the contexts in use */
26 xbt_swag_t context_living = NULL;
28 /* the context factory used to create the appropriate context
29 * each context implementation define its own context factory
30 * a context factory is responsable of the creation of the context
31 * associated with the maestro and of all the context based on
32 * the selected implementation.
34 * for example, the context switch based on java thread use the
35 * java implementation of the context and the java factory build
36 * the context depending of this implementation.
38 static xbt_context_factory_t context_factory = NULL;
41 * This function is call by the xbt_init() function to initialize the context module.
43 void xbt_context_mod_init(void)
45 if (!context_factory) {
46 /* select context factory to use to create the context(depends of the macro definitions) */
48 #ifdef CONTEXT_THREADS
49 /* context switch based os thread */
50 xbt_ctx_thread_factory_init(&context_factory);
52 /* context switch based ucontext */
53 xbt_ctx_sysv_factory_init(&context_factory);
55 /* context switch is not allowed on Windows */
56 #error ERROR [__FILE__, line __LINE__]: no context based implementation specified.
59 /* maestro context specialisation (this create the maestro with the good implementation */
60 (*(context_factory->create_maestro_context)) (&maestro_context);
62 /* the current context is the context of the maestro */
63 current_context = maestro_context;
65 /* the current context doesn't want to die */
66 current_context->iwannadie = 0;
68 /* intantiation of the lists containing the contexts to destroy and the contexts in use */
70 xbt_swag_new(xbt_swag_offset(*current_context, hookup));
71 context_living = xbt_swag_new(xbt_swag_offset(*current_context, hookup));
73 /* insert the current context in the list of the contexts in use */
74 xbt_swag_insert(current_context, context_living);
80 * This function is call by the xbt_exit() function to finalize the context module.
82 void xbt_context_mod_exit(void)
84 if (context_factory) {
85 xbt_context_t context = NULL;
86 xbt_pfn_context_factory_finalize_t finalize_factory;
88 /* finalize the context factory */
89 finalize_factory = context_factory->finalize;
91 (*finalize_factory) (&context_factory);
93 /* destroy all contexts in the list of contexts to destroy */
94 xbt_context_empty_trash();
96 /* remove the context of the scheduler from the list of the contexts in use */
97 xbt_swag_remove(maestro_context, context_living);
100 * kill all the contexts in use :
101 * the killed contexts are added in the list of the contexts to destroy
103 while ((context = xbt_swag_extract(context_living)))
104 (*(context->kill)) (context);
107 /* destroy all contexts in the list of contexts to destroy */
108 xbt_context_empty_trash();
110 free(maestro_context);
111 maestro_context = current_context = NULL;
113 /* destroy the lists */
114 xbt_swag_free(context_to_destroy);
115 xbt_swag_free(context_living);
119 /*******************************/
120 /* Object creation/destruction */
121 /*******************************/
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
134 xbt_context_new(const char *name,
135 xbt_main_func_t code,
136 void_f_pvoid_t startup_func,
138 void_f_pvoid_t cleanup_func,
139 void *cleanup_arg, int argc, char *argv[]
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);
148 /* add the context in the list of the contexts in use */
149 xbt_swag_insert(context, context_living);
154 /* Scenario for the end of a context:
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,
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)
176 /* Argument must be stopped first -- runs in maestro context */
177 void xbt_context_free(xbt_context_t context)
179 (*(context->free)) (context);
183 void xbt_context_kill(xbt_context_t context)
185 (*(context->kill)) (context);
189 * \param context the context to start
191 * Calling this function prepares \a context to be run. It will
192 however run effectively only when calling #xbt_context_schedule
194 void xbt_context_start(xbt_context_t context)
196 (*(context->start)) (context);
200 * Calling this function makes the current context yield. The context
201 * that scheduled it returns from xbt_context_schedule as if nothing
204 * Only the processes can call this function, giving back the control
207 void xbt_context_yield(void)
209 (*(current_context->yield)) ();
213 * \param context the winner
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.
219 * Only the maestro can call this function to run a given process.
221 void xbt_context_schedule(xbt_context_t context)
223 (*(context->schedule)) (context);
226 void xbt_context_stop(int exit_code)
229 (*(current_context->stop)) (exit_code);
232 int xbt_context_select_factory(const char *name)
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();
240 /* the same context factory is requested return directly */
244 /* get the desired factory */
245 xbt_context_init_factory_by_name(&context_factory, name);
247 /* maestro context specialisation */
248 (*(context_factory->create_maestro_context)) (&maestro_context);
250 /* the current context is the context of the maestro */
251 current_context = maestro_context;
253 /* the current context doesn't want to die */
254 current_context->iwannadie = 0;
256 /* intantiation of the lists containing the contexts to destroy and the contexts in use */
258 xbt_swag_new(xbt_swag_offset(*current_context, hookup));
259 context_living = xbt_swag_new(xbt_swag_offset(*current_context, hookup));
261 /* insert the current context in the list of the contexts in use */
262 xbt_swag_insert(current_context, context_living);
268 xbt_context_init_factory_by_name(xbt_context_factory_t * factory,
271 if (!strcmp(name, "java"))
272 xbt_ctx_java_factory_init(factory);
273 #ifdef CONTEXT_THREADS
274 else if (!strcmp(name, "thread"))
275 xbt_ctx_thread_factory_init(factory);
276 #elif !defined(WIN32)
277 else if (!strcmp(name, "sysv"))
278 xbt_ctx_sysv_factory_init(factory);
281 THROW1(not_found_error, 0, "Factory '%s' does not exist", name);
284 /** Garbage collection
286 * Should be called some time to time to free the memory allocated for contexts
287 * that have finished executing their main functions.
289 void xbt_context_empty_trash(void)
291 xbt_context_t context = NULL;
293 while ((context = xbt_swag_extract(context_to_destroy)))
294 (*(context->free)) (context);