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,
15 "Context switching mecanism");
17 /* the context associated with the current process */
18 xbt_context_t current_context = NULL;
20 /* the context associated with the maestro */
21 xbt_context_t maestro_context = NULL;
24 /* this list contains the contexts to destroy */
25 xbt_swag_t context_to_destroy = NULL;
27 /* this list contains the contexts in use */
28 xbt_swag_t context_living = NULL;
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.
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.
40 static xbt_context_factory_t context_factory = NULL;
43 * This function is call by the xbt_init() function to initialize the context module.
45 void xbt_context_mod_init(void)
47 if (!context_factory) {
48 /* select context factory to use to create the context(depends of the macro definitions) */
50 #ifdef CONTEXT_THREADS
51 /* context switch based os thread */
52 xbt_ctx_thread_factory_init(&context_factory);
54 /* context switch based ucontext */
55 xbt_ctx_sysv_factory_init(&context_factory);
57 /* context switch is not allowed on Windows */
58 #error ERROR [__FILE__, line __LINE__]: no context based implementation specified.
61 /* maestro context specialisation (this create the maestro with the good implementation */
62 (*(context_factory->create_maestro_context)) (&maestro_context);
64 /* the current context is the context of the maestro */
65 current_context = maestro_context;
67 /* the current context doesn't want to die */
68 current_context->iwannadie = 0;
70 /* intantiation of the lists containing the contexts to destroy and the contexts in use */
72 xbt_swag_new(xbt_swag_offset(*current_context, hookup));
73 context_living = xbt_swag_new(xbt_swag_offset(*current_context, hookup));
75 /* insert the current context in the list of the contexts in use */
76 xbt_swag_insert(current_context, context_living);
82 * This function is call by the xbt_exit() function to finalize the context module.
84 void xbt_context_mod_exit(void)
86 if (context_factory) {
87 xbt_context_t context = NULL;
88 xbt_pfn_context_factory_finalize_t finalize_factory;
90 /* finalize the context factory */
91 finalize_factory = context_factory->finalize;
93 (*finalize_factory) (&context_factory);
95 /* destroy all contexts in the list of contexts to destroy */
96 xbt_context_empty_trash();
98 /* remove the context of the scheduler from the list of the contexts in use */
99 xbt_swag_remove(maestro_context, context_living);
102 * kill all the contexts in use :
103 * the killed contexts are added in the list of the contexts to destroy
105 while ((context = xbt_swag_extract(context_living)))
106 (*(context->kill)) (context);
109 /* destroy all contexts in the list of contexts to destroy */
110 xbt_context_empty_trash();
112 free(maestro_context);
113 maestro_context = current_context = NULL;
115 /* destroy the lists */
116 xbt_swag_free(context_to_destroy);
117 xbt_swag_free(context_living);
121 /*******************************/
122 /* Object creation/destruction */
123 /*******************************/
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
136 xbt_context_new(const char *name,
137 xbt_main_func_t code,
138 void_f_pvoid_t startup_func,
140 void_f_pvoid_t cleanup_func,
141 void *cleanup_arg, int argc, char *argv[]
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);
150 /* add the context in the list of the contexts in use */
151 xbt_swag_insert(context, context_living);
156 /* Scenario for the end of a context:
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,
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)
178 /* Argument must be stopped first -- runs in maestro context */
179 void xbt_context_free(xbt_context_t context)
181 (*(context->free)) (context);
185 void xbt_context_kill(xbt_context_t context)
187 (*(context->kill)) (context);
191 * \param context the context to start
193 * Calling this function prepares \a context to be run. It will
194 however run effectively only when calling #xbt_context_schedule
196 void xbt_context_start(xbt_context_t context)
198 (*(context->start)) (context);
202 * Calling this function makes the current context yield. The context
203 * that scheduled it returns from xbt_context_schedule as if nothing
206 * Only the processes can call this function, giving back the control
209 void xbt_context_yield(void)
211 (*(current_context->yield)) ();
215 * \param context the winner
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.
221 * Only the maestro can call this function to run a given process.
223 void xbt_context_schedule(xbt_context_t context)
225 (*(context->schedule)) (context);
228 void xbt_context_stop(int exit_code)
231 (*(current_context->stop)) (exit_code);
234 int xbt_context_select_factory(const char *name)
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();
242 /* the same context factory is requested return directly */
246 /* get the desired factory */
247 xbt_context_init_factory_by_name(&context_factory, name);
249 /* maestro context specialisation */
250 (*(context_factory->create_maestro_context)) (&maestro_context);
252 /* the current context is the context of the maestro */
253 current_context = maestro_context;
255 /* the current context doesn't want to die */
256 current_context->iwannadie = 0;
258 /* intantiation of the lists containing the contexts to destroy and the contexts in use */
260 xbt_swag_new(xbt_swag_offset(*current_context, hookup));
261 context_living = xbt_swag_new(xbt_swag_offset(*current_context, hookup));
263 /* insert the current context in the list of the contexts in use */
264 xbt_swag_insert(current_context, context_living);
270 xbt_context_init_factory_by_name(xbt_context_factory_t * factory,
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);
283 THROW1(not_found_error, 0, "Factory '%s' does not exist", name);
286 /** Garbage collection
288 * Should be called some time to time to free the memory allocated for contexts
289 * that have finished executing their main functions.
291 void xbt_context_empty_trash(void)
293 xbt_context_t context = NULL;
295 while ((context = xbt_swag_extract(context_to_destroy)))
296 (*(context->free)) (context);