Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
"new ruby host method"
[simgrid.git] / src / simix / smx_context.c
1 /* a fast and simple context switching library                              */
2
3 /* Copyright (c) 2004-2008 the SimGrid team.                                */
4 /* All rights reserved.                                                     */
5
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. */
8
9 #include "portable.h"
10 #include "xbt/log.h"
11 #include "xbt/swag.h"
12 #include "private.h"
13
14 #ifdef HAVE_LUA
15 #include <lua5.1/lauxlib.h>
16 #endif
17
18 #ifdef HAVE_RUBY
19  void SIMIX_ctx_ruby_factory_init(smx_context_factory_t *factory);
20 #endif 
21  
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_context, simix, "Context switching mecanism");
23
24 const char *xbt_ctx_factory_to_use = NULL;
25
26 /** 
27  * This function is call by SIMIX_global_init() to initialize the context module.
28  */
29
30 void SIMIX_context_mod_init(void)
31 {
32   if (!simix_global->context_factory) {
33   /* select context factory to use to create the context(depends of the macro definitions) */
34     if (xbt_ctx_factory_to_use) {
35       SIMIX_context_select_factory(xbt_ctx_factory_to_use);
36     } else {
37 #ifdef CONTEXT_THREADS
38     /* context switch based os thread */
39     SIMIX_ctx_thread_factory_init(&simix_global->context_factory);
40 #elif !defined(WIN32)
41     /* context switch based ucontext */
42     SIMIX_ctx_sysv_factory_init(&simix_global->context_factory);
43 #else
44     /* context switch is not allowed on Windows */
45 #error ERROR [__FILE__, line __LINE__]: no context based implementation specified.
46 #endif
47     }
48   }
49 }
50
51 /**
52  * This function is call by SIMIX_clean() to finalize the context module.
53  */
54 void SIMIX_context_mod_exit(void)
55 {
56   if (simix_global->context_factory) {
57     smx_pfn_context_factory_finalize_t finalize_factory;
58     
59     /* finalize the context factory */
60     finalize_factory = simix_global->context_factory->finalize;
61     (*finalize_factory) (&simix_global->context_factory);
62   }
63 }
64
65 /**
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.
69  */
70 int SIMIX_context_select_factory(const char *name)
71 {
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.
75   */
76   
77
78   if (simix_global->context_factory != NULL) {
79     if (strcmp(simix_global->context_factory->name, name)){
80
81       SIMIX_process_killall();
82       
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;
87
88       SIMIX_context_mod_exit();
89     }
90     else
91       /* the same context factory is requested return directly */
92       return 0;
93   }
94   
95   /* init the desired factory */
96   SIMIX_context_init_factory_by_name(&simix_global->context_factory, name);
97
98   SIMIX_create_maestro_process ();
99
100
101   
102   return 0;
103 }
104
105 /**
106  * Initializes a context factory given by its name
107  */
108 void SIMIX_context_init_factory_by_name(smx_context_factory_t * factory,
109                                         const char *name)
110 {
111   
112   if (!strcmp(name, "java"))
113 #ifdef HAVE_JAVA     
114     SIMIX_ctx_java_factory_init(factory);
115 #else
116     THROW0(not_found_error, 0, "Factory 'Java' does not exist: Java support was not compiled in the SimGrid library");
117 #endif /* HAVE_JAVA */
118    
119   else if (!strcmp(name, "thread"))
120 #ifdef CONTEXT_THREADS
121     SIMIX_ctx_thread_factory_init(factory);
122 #else
123     THROW0(not_found_error, 0, "Factory 'thread' does not exist: thread support was not compiled in the SimGrid library");
124 #endif /* CONTEXT_THREADS */
125    
126     else if (!strcmp(name, "sysv"))
127   #if !defined(WIN32) && !defined(CONTEXT_THREADS)
128       SIMIX_ctx_sysv_factory_init(factory);
129   #else
130       THROW0(not_found_error, 0, "Factory 'sysv' does not exist: no System V thread support under Windows");
131   #endif
132     else if (!strcmp(name, "lua"))
133 #ifdef HAVE_LUA
134       SIMIX_ctx_lua_factory_init(factory);
135 #else
136
137     THROW0(not_found_error, 0, "Factory 'lua' does not exist: Lua support was not compiled in the SimGrid library");
138 #endif /* HAVE_LUA */
139
140     else if (!strcmp(name,"ruby"))
141 #ifdef HAVE_RUBY
142     SIMIX_ctx_ruby_factory_init(factory);
143 #else
144      THROW0(not_found_error, 0, "Factory 'ruby' does not exist: Ruby support was not compiled in the SimGrid library");
145 #endif
146   else
147     THROW1(not_found_error, 0, "Factory '%s' does not exist", name);
148 }