1 /* a fast and simple context switching library */
3 /* Copyright (c) 2009, 2010. 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. */
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_context, simix,
19 "Context switching mecanism");
21 const char *xbt_ctx_factory_to_use = NULL;
22 typedef void (*SIMIX_ctx_factory_initializer_t)(smx_context_factory_t *);
23 SIMIX_ctx_factory_initializer_t factory_initializer_to_use = NULL;
26 * This function is called by SIMIX_global_init() to initialize the context module.
28 void SIMIX_context_mod_init(void)
30 if (!simix_global->context_factory) {
31 /* select context factory to use to create the context(depends of the macro definitions) */
32 if (factory_initializer_to_use) {
33 (*factory_initializer_to_use)(&(simix_global->context_factory));
35 #ifdef CONTEXT_THREADS
36 /* context switch based os thread */
37 SIMIX_ctx_thread_factory_init(&simix_global->context_factory);
38 #elif defined(CONTEXT_UCONTEXT)
39 /* context switch based ucontext */
40 SIMIX_ctx_sysv_factory_init(&simix_global->context_factory);
41 #elif defined(_XBT_WIN32)
42 /* context switch based windows */
44 /* context switch is not allowed on Windows */
45 #error ERROR [__FILE__, line __LINE__]: no context based implementation specified.
52 * This function is call by SIMIX_clean() to finalize the context module.
54 void SIMIX_context_mod_exit(void)
56 if (simix_global->context_factory) {
57 smx_pfn_context_factory_finalize_t finalize_factory;
59 /* finalize the context factory */
60 finalize_factory = simix_global->context_factory->finalize;
61 (*finalize_factory) (&simix_global->context_factory);
66 * This function is used to change the context factory.
67 * Warning: it destroy all the existing processes (even for maestro), and it
68 * will create a new maestro process using the new context factory.
70 int SIMIX_context_select_factory(const char *name)
72 /* if a context factory is already instantiated and it is different from the
73 newly selected one, then kill all the processes, exit the context module
74 and initialize the new factory.
78 if (simix_global->context_factory != NULL) {
79 if (strcmp(simix_global->context_factory->name, name)) {
81 SIMIX_process_killall();
83 /* kill maestro process */
84 SIMIX_context_free(simix_global->maestro_process->context);
85 free(simix_global->maestro_process);
86 simix_global->maestro_process = NULL;
88 SIMIX_context_mod_exit();
90 /* the same context factory is requested return directly */
94 /* init the desired factory */
95 smx_context_factory_t * factory = &simix_global->context_factory;
96 if (!strcmp(name, "java"))
98 SIMIX_ctx_java_factory_init(factory);
100 THROW0(not_found_error, 0,
101 "Factory 'Java' does not exist: Java support was not compiled in the SimGrid library");
102 #endif /* HAVE_JAVA */
104 else if (!strcmp(name, "thread"))
105 #ifdef CONTEXT_THREADS
106 SIMIX_ctx_thread_factory_init(factory);
108 THROW0(not_found_error, 0,
109 "Factory 'thread' does not exist: thread support was not compiled in the SimGrid library");
110 #endif /* CONTEXT_THREADS */
112 else if (!strcmp(name, "sysv"))
113 #if !defined(_XBT_WIN32) && !defined(CONTEXT_THREADS)
114 SIMIX_ctx_sysv_factory_init(factory);
116 THROW0(not_found_error, 0,
117 "Factory 'sysv' does not exist: no System V thread support under Windows");
119 else if (!strcmp(name, "lua"))
121 SIMIX_ctx_lua_factory_init(factory);
124 THROW0(not_found_error, 0,
125 "Factory 'lua' does not exist: Lua support was not compiled in the SimGrid library");
126 #endif /* HAVE_LUA */
129 THROW1(not_found_error, 0, "Factory '%s' does not exist", name);
134 SIMIX_create_maestro_process();