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. */
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smx_context, simix, "Context switching mecanism");
18 * This function is call by SIMIX_global_init() to initialize the context module.
20 void SIMIX_context_mod_init(void)
22 if (!simix_global->context_factory) {
23 /* select context factory to use to create the context(depends of the macro definitions) */
25 #ifdef CONTEXT_THREADS
26 /* context switch based os thread */
27 SIMIX_ctx_thread_factory_init(&simix_global->context_factory);
29 /* context switch based ucontext */
30 SIMIX_ctx_sysv_factory_init(&simix_global->context_factory);
32 /* context switch is not allowed on Windows */
33 #error ERROR [__FILE__, line __LINE__]: no context based implementation specified.
39 * This function is call by SIMIX_clean() to finalize the context module.
41 void SIMIX_context_mod_exit(void)
43 if (simix_global->context_factory) {
44 smx_pfn_context_factory_finalize_t finalize_factory;
46 /* if there are living processes then kill them (except maestro) */
47 if(simix_global->process_list != NULL)
48 SIMIX_process_killall();
50 /* finalize the context factory */
51 finalize_factory = simix_global->context_factory->finalize;
52 (*finalize_factory) (&simix_global->context_factory);
57 * This function is used to change the context factory.
58 * Warning: it destroy all the existing contexts
60 int SIMIX_context_select_factory(const char *name)
62 /* if a factory is already instantiated (SIMIX_context_mod_init() was called) */
63 if (simix_global->context_factory != NULL) {
64 /* if the desired factory is different of the current factory, call SIMIX_context_mod_exit() */
65 if (strcmp(simix_global->context_factory->name, name))
66 SIMIX_context_mod_exit();
68 /* the same context factory is requested return directly */
72 /* get the desired factory */
73 SIMIX_context_init_factory_by_name(&simix_global->context_factory, name);
75 /* maestro process specialisation */
76 simix_global->maestro_process->context = SIMIX_context_create_maestro ();
78 /* the current process is the process of the maestro */
79 simix_global->current_process = simix_global->maestro_process;
81 /* the current context doesn't want to die */
82 simix_global->current_process->iwannadie = 0;
84 /* insert the current context in the list of the contexts in use */
85 xbt_swag_insert(simix_global->current_process, simix_global->process_list);
91 * Initializes a context factory given by its name
93 void SIMIX_context_init_factory_by_name(smx_context_factory_t * factory,
96 if (!strcmp(name, "java"))
98 SIMIX_ctx_java_factory_init(factory);
100 THROW0(not_found_error, 0, "Factory 'Java' does not exist: Java support was not compiled in the SimGrid library");
101 #endif /* HAVE_JAVA */
103 else if (!strcmp(name, "thread"))
104 #ifdef CONTEXT_THREADS
105 SIMIX_ctx_thread_factory_init(factory);
107 THROW0(not_found_error, 0, "Factory 'thread' does not exist: thread support was not compiled in the SimGrid library");
108 #endif /* CONTEXT_THREADS */
110 else if (!strcmp(name, "sysv"))
111 #if !defined(WIN32) && !defined(CONTEXT_THREADS)
112 SIMIX_ctx_sysv_factory_init(factory);
114 THROW0(not_found_error, 0, "Factory 'sysv' does not exist: no System V thread support under Windows");
117 THROW1(not_found_error, 0, "Factory '%s' does not exist", name);