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. */
13 #include <lua5.1/lauxlib.h>
15 #include "smx_context_ruby.c"
17 #define HAVE_RUBY /* HACK HACK */
21 extern void SIMIX_ctx_ruby_factory_init(smx_context_factory_t *factory);
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_context, simix, "Context switching mecanism");
26 const char *xbt_ctx_factory_to_use = NULL;
29 * This function is call by SIMIX_global_init() to initialize the context module.
32 void SIMIX_context_mod_init(void)
34 if (!simix_global->context_factory) {
35 /* select context factory to use to create the context(depends of the macro definitions) */
36 if (xbt_ctx_factory_to_use) {
37 SIMIX_context_select_factory(xbt_ctx_factory_to_use);
39 #ifdef CONTEXT_THREADS
40 /* context switch based os thread */
41 SIMIX_ctx_thread_factory_init(&simix_global->context_factory);
43 /* context switch based ucontext */
44 SIMIX_ctx_sysv_factory_init(&simix_global->context_factory);
46 /* context switch is not allowed on Windows */
47 #error ERROR [__FILE__, line __LINE__]: no context based implementation specified.
54 * This function is call by SIMIX_clean() to finalize the context module.
56 void SIMIX_context_mod_exit(void)
58 if (simix_global->context_factory) {
59 smx_pfn_context_factory_finalize_t finalize_factory;
61 /* finalize the context factory */
62 finalize_factory = simix_global->context_factory->finalize;
63 (*finalize_factory) (&simix_global->context_factory);
68 * This function is used to change the context factory.
69 * Warning: it destroy all the existing processes (even for maestro), and it
70 * will create a new maestro process using the new context factory.
72 int SIMIX_context_select_factory(const char *name)
74 /* if a context factory is already instantiated and it is different from the
75 newly selected one, then kill all the processes, exit the context module
76 and initialize the new factory.
80 if (simix_global->context_factory != NULL) {
81 if (strcmp(simix_global->context_factory->name, name)){
83 SIMIX_process_killall();
85 /* kill maestro process */
86 SIMIX_context_free(simix_global->maestro_process->context);
87 free(simix_global->maestro_process);
88 simix_global->maestro_process = NULL;
90 SIMIX_context_mod_exit();
93 /* the same context factory is requested return directly */
97 /* init the desired factory */
98 SIMIX_context_init_factory_by_name(&simix_global->context_factory, name);
100 SIMIX_create_maestro_process ();
108 * Initializes a context factory given by its name
110 void SIMIX_context_init_factory_by_name(smx_context_factory_t * factory,
114 if (!strcmp(name, "java"))
116 SIMIX_ctx_java_factory_init(factory);
118 THROW0(not_found_error, 0, "Factory 'Java' does not exist: Java support was not compiled in the SimGrid library");
119 #endif /* HAVE_JAVA */
121 else if (!strcmp(name, "thread"))
122 #ifdef CONTEXT_THREADS
123 SIMIX_ctx_thread_factory_init(factory);
125 THROW0(not_found_error, 0, "Factory 'thread' does not exist: thread support was not compiled in the SimGrid library");
126 #endif /* CONTEXT_THREADS */
128 else if (!strcmp(name, "sysv"))
129 #if !defined(WIN32) && !defined(CONTEXT_THREADS)
130 SIMIX_ctx_sysv_factory_init(factory);
132 THROW0(not_found_error, 0, "Factory 'sysv' does not exist: no System V thread support under Windows");
134 else if (!strcmp(name, "lua"))
136 SIMIX_ctx_lua_factory_init(factory);
139 THROW0(not_found_error, 0, "Factory 'lua' does not exist: Lua support was not compiled in the SimGrid library");
140 #endif /* HAVE_LUA */
142 else if (!strcmp(name,"ruby"))
144 SIMIX_ctx_ruby_factory_init(factory);
146 THROW0(not_found_error, 0, "Factory 'ruby' does not exist: Ruby support was not compiled in the SimGrid library");
149 THROW1(not_found_error, 0, "Factory '%s' does not exist", name);