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;
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 /* destroy all contexts in the list of contexts to destroy */
90 xbt_context_empty_trash();
92 /* remove the context of the scheduler from the list of the contexts in use */
93 xbt_swag_remove(maestro_context, context_living);
96 * kill all the contexts in use :
97 * the killed contexts are added in the list of the contexts to destroy
99 while ((context = xbt_swag_extract(context_living)))
100 (*(context_factory->kill)) (context);
102 /* destroy all contexts in the list of contexts to destroy */
103 xbt_context_empty_trash();
105 /* finalize the context factory */
106 finalize_factory = context_factory->finalize;
108 (*finalize_factory) (&context_factory);
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_factory->free)) (context);
183 void xbt_context_kill(xbt_context_t context)
185 (*(context_factory->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_factory->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 (*(context_factory->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_factory->schedule)) (context);
226 void xbt_context_stop(int exit_code)
229 (*(context_factory->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"))
273 xbt_ctx_java_factory_init(factory);
275 THROW0(not_found_error, 0, "Factory 'Java' does not exist: Java support was not compiled in the SimGrid library");
276 #endif /* HAVE_JAVA */
278 else if (!strcmp(name, "thread"))
279 #ifdef CONTEXT_THREADS
280 xbt_ctx_thread_factory_init(factory);
282 THROW0(not_found_error, 0, "Factory 'thread' does not exist: thread support was not compiled in the SimGrid library");
283 #endif /* CONTEXT_THREADS */
285 else if (!strcmp(name, "sysv"))
286 #if !defined(WIN32) && !defined(CONTEXT_THREADS)
287 xbt_ctx_sysv_factory_init(factory);
289 THROW0(not_found_error, 0, "Factory 'sysv' does not exist: no System V thread support under Windows");
292 THROW1(not_found_error, 0, "Factory '%s' does not exist", name);
295 /** Garbage collection
297 * Should be called some time to time to free the memory allocated for contexts
298 * that have finished executing their main functions.
300 void xbt_context_empty_trash(void)
302 xbt_context_t context = NULL;
304 while ((context = xbt_swag_extract(context_to_destroy)))
305 (*(context_factory->free)) (context);