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 /* the context associated with the current process */
15 xbt_context_t current_context = NULL;
17 /* the context associated with the maestro */
18 xbt_context_t maestro_context = NULL;
21 /* this list contains the contexts to destroy */
22 xbt_swag_t context_to_destroy = NULL;
24 /* this list contains the contexts in use */
25 xbt_swag_t context_living = NULL;
27 /* the context factory used to create the appropriate context
28 * each context implementation define its own context factory
29 * a context factory is responsable of the creation of the context
30 * associated with the maestro and of all the context based on
31 * the selected implementation.
33 * for example, the context switch based on java thread use the
34 * java implementation of the context and the java factory build
35 * the context depending of this implementation.
37 static xbt_context_factory_t context_factory = NULL;
40 * This function is call by the xbt_init() function to initialize the context module.
42 void xbt_context_mod_init(void)
44 if (!context_factory) {
45 /* select context factory to use to create the context(depends of the macro definitions) */
47 #ifdef CONTEXT_THREADS
48 /* context switch based os thread */
49 xbt_ctx_thread_factory_init(&context_factory);
51 /* context switch based ucontext */
52 xbt_ctx_sysv_factory_init(&context_factory);
54 /* context switch is not allowed on Windows */
55 #error ERROR [__FILE__, line __LINE__]: no context based implementation specified.
58 /* maestro context specialisation (this create the maestro with the good implementation */
59 (*(context_factory->create_maestro_context)) (&maestro_context);
61 /* the current context is the context of the maestro */
62 current_context = maestro_context;
64 /* the current context doesn't want to die */
65 current_context->iwannadie = 0;
67 /* intantiation of the lists containing the contexts to destroy and the contexts in use */
69 xbt_swag_new(xbt_swag_offset(*current_context, hookup));
70 context_living = xbt_swag_new(xbt_swag_offset(*current_context, hookup));
72 /* insert the current context in the list of the contexts in use */
73 xbt_swag_insert(current_context, context_living);
79 * This function is call by the xbt_exit() function to finalize the context module.
81 void xbt_context_mod_exit(void)
83 if (context_factory) {
84 xbt_context_t context = NULL;
85 xbt_pfn_context_factory_finalize_t finalize_factory;
87 /* finalize the context factory */
88 finalize_factory = context_factory->finalize;
90 (*finalize_factory) (&context_factory);
92 /* destroy all contexts in the list of contexts to destroy */
93 xbt_context_empty_trash();
95 /* remove the context of the scheduler from the list of the contexts in use */
96 xbt_swag_remove(maestro_context, context_living);
99 * kill all the contexts in use :
100 * the killed contexts are added in the list of the contexts to destroy
102 while ((context = xbt_swag_extract(context_living)))
103 (*(context->kill)) (context);
106 /* destroy all contexts in the list of contexts to destroy */
107 xbt_context_empty_trash();
109 free(maestro_context);
110 maestro_context = current_context = NULL;
112 /* destroy the lists */
113 xbt_swag_free(context_to_destroy);
114 xbt_swag_free(context_living);
118 /*******************************/
119 /* Object creation/destruction */
120 /*******************************/
122 * \param code a main function
123 * \param startup_func a function to call when running the context for
124 * the first time and just before the main function \a code
125 * \param startup_arg the argument passed to the previous function (\a startup_func)
126 * \param cleanup_func a function to call when running the context, just after
127 the termination of the main function \a code
128 * \param cleanup_arg the argument passed to the previous function (\a cleanup_func)
129 * \param argc first argument of function \a code
130 * \param argv seconde argument of function \a code
133 xbt_context_new(const char *name,
134 xbt_main_func_t code,
135 void_f_pvoid_t startup_func,
137 void_f_pvoid_t cleanup_func,
138 void *cleanup_arg, int argc, char *argv[]
141 /* use the appropriate context factory to create the appropriate context */
142 xbt_context_t context =
143 (*(context_factory->create_context)) (name, code, startup_func,
144 startup_arg, cleanup_func,
145 cleanup_arg, argc, argv);
147 /* add the context in the list of the contexts in use */
148 xbt_swag_insert(context, context_living);
153 /* Scenario for the end of a context:
155 * CASE 1: death after end of function
156 * __context_wrapper, called by os thread, calls xbt_context_stop after user code stops
157 * xbt_context_stop calls user cleanup_func if any (in context settings),
158 * add current to trashbin
159 * yields back to maestro (destroy os thread on need)
160 * From time to time, maestro calls xbt_context_empty_trash,
161 * which maps xbt_context_free on the content
162 * xbt_context_free frees some more memory,
165 * CASE 2: brutal death
166 * xbt_context_kill (from any context)
167 * set context->wannadie to 1
168 * yields to the context
169 * the context is awaken in the middle of __yield.
170 * At the end of it, it checks that wannadie == 1, and call xbt_context_stop
171 * (same than first case afterward)
175 /* Argument must be stopped first -- runs in maestro context */
176 void xbt_context_free(xbt_context_t context)
178 (*(context->free)) (context);
182 void xbt_context_kill(xbt_context_t context)
184 (*(context->kill)) (context);
188 * \param context the context to start
190 * Calling this function prepares \a context to be run. It will
191 however run effectively only when calling #xbt_context_schedule
193 void xbt_context_start(xbt_context_t context)
195 (*(context->start)) (context);
199 * Calling this function makes the current context yield. The context
200 * that scheduled it returns from xbt_context_schedule as if nothing
203 * Only the processes can call this function, giving back the control
206 void xbt_context_yield(void)
208 (*(current_context->yield)) ();
212 * \param context the winner
214 * Calling this function blocks the current context and schedule \a context.
215 * When \a context will call xbt_context_yield, it will return
216 * to this function as if nothing had happened.
218 * Only the maestro can call this function to run a given process.
220 void xbt_context_schedule(xbt_context_t context)
222 (*(context->schedule)) (context);
225 void xbt_context_stop(int exit_code)
228 (*(current_context->stop)) (exit_code);
231 int xbt_context_select_factory(const char *name)
233 /* if a factory is already instantiated (xbt_context_mod_init() was called) */
234 if (NULL != context_factory) {
235 /* if the desired factory is different of the current factory, call xbt_context_mod_exit() */
236 if (strcmp(context_factory->name, name))
237 xbt_context_mod_exit();
239 /* the same context factory is requested return directly */
243 /* get the desired factory */
244 xbt_context_init_factory_by_name(&context_factory, name);
246 /* maestro context specialisation */
247 (*(context_factory->create_maestro_context)) (&maestro_context);
249 /* the current context is the context of the maestro */
250 current_context = maestro_context;
252 /* the current context doesn't want to die */
253 current_context->iwannadie = 0;
255 /* intantiation of the lists containing the contexts to destroy and the contexts in use */
257 xbt_swag_new(xbt_swag_offset(*current_context, hookup));
258 context_living = xbt_swag_new(xbt_swag_offset(*current_context, hookup));
260 /* insert the current context in the list of the contexts in use */
261 xbt_swag_insert(current_context, context_living);
267 xbt_context_init_factory_by_name(xbt_context_factory_t * factory,
270 if (!strcmp(name, "java"))
271 xbt_ctx_java_factory_init(factory);
272 #ifdef CONTEXT_THREADS
273 else if (!strcmp(name, "thread"))
274 xbt_ctx_thread_factory_init(factory);
275 #elif !defined(WIN32)
276 else if (!strcmp(name, "sysv"))
277 xbt_ctx_sysv_factory_init(factory);
280 THROW1(not_found_error, 0, "Factory '%s' does not exist", name);
283 /** Garbage collection
285 * Should be called some time to time to free the memory allocated for contexts
286 * that have finished executing their main functions.
288 void xbt_context_empty_trash(void)
290 xbt_context_t context = NULL;
292 while ((context = xbt_swag_extract(context_to_destroy)))
293 (*(context->free)) (context);