Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add some patches for delete some warnings with cmake.
[simgrid.git] / buildtools / CPACK / Patch / 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 #include <lua5.1/lauxlib.h>
14 #ifndef RUBY_H
15         #include <ruby.h>
16 #endif
17 #include "smx_context_ruby.c"
18
19 #define HAVE_RUBY /* HACK HACK */
20 // #define DEBUG 
21
22 #ifdef HAVE_RUBY
23  extern void SIMIX_ctx_ruby_factory_init(smx_context_factory_t *factory);
24 #endif 
25  
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_context, simix, "Context switching mecanism");
27
28 const char *xbt_ctx_factory_to_use = NULL;
29
30 /** 
31  * This function is call by SIMIX_global_init() to initialize the context module.
32  */
33
34 void SIMIX_context_mod_init(void)
35 {
36   if (!simix_global->context_factory) {
37   /* select context factory to use to create the context(depends of the macro definitions) */
38     if (xbt_ctx_factory_to_use) {
39       SIMIX_context_select_factory(xbt_ctx_factory_to_use);
40     } else {
41 #ifdef CONTEXT_THREADS
42     /* context switch based os thread */
43     SIMIX_ctx_thread_factory_init(&simix_global->context_factory);
44 #elif !defined(WIN32)
45     /* context switch based ucontext */
46     SIMIX_ctx_sysv_factory_init(&simix_global->context_factory);
47 #else
48     /* context switch is not allowed on Windows */
49 #error ERROR [__FILE__, line __LINE__]: no context based implementation specified.
50 #endif
51     }
52   }
53 }
54
55 /**
56  * This function is call by SIMIX_clean() to finalize the context module.
57  */
58 void SIMIX_context_mod_exit(void)
59 {
60   if (simix_global->context_factory) {
61     smx_pfn_context_factory_finalize_t finalize_factory;
62     
63     /* finalize the context factory */
64     finalize_factory = simix_global->context_factory->finalize;
65     (*finalize_factory) (&simix_global->context_factory);
66   }
67 }
68
69 /**
70  * This function is used to change the context factory.
71  * Warning: it destroy all the existing processes (even for maestro), and it
72  * will create a new maestro process using the new context factory.
73  */
74 int SIMIX_context_select_factory(const char *name)
75 {
76   /* if a context factory is already instantiated and it is different from the
77      newly selected one, then kill all the processes, exit the context module
78      and initialize the new factory.
79   */
80   
81
82   if (simix_global->context_factory != NULL) {
83     if (strcmp(simix_global->context_factory->name, name)){
84
85       SIMIX_process_killall();
86       
87       /* kill maestro process */
88       SIMIX_context_free(simix_global->maestro_process->context);
89       free(simix_global->maestro_process);  
90       simix_global->maestro_process = NULL;
91
92       SIMIX_context_mod_exit();
93     }
94     else
95       /* the same context factory is requested return directly */
96       return 0;
97   }
98   
99   /* init the desired factory */
100   SIMIX_context_init_factory_by_name(&simix_global->context_factory, name);
101
102   SIMIX_create_maestro_process ();
103
104
105   
106   return 0;
107 }
108
109 /**
110  * Initializes a context factory given by its name
111  */
112 void SIMIX_context_init_factory_by_name(smx_context_factory_t * factory,
113                                         const char *name)
114 {
115   
116   if (!strcmp(name, "java"))
117 #ifdef HAVE_JAVA     
118     SIMIX_ctx_java_factory_init(factory);
119 #else
120     THROW0(not_found_error, 0, "Factory 'Java' does not exist: Java support was not compiled in the SimGrid library");
121 #endif /* HAVE_JAVA */
122    
123   else if (!strcmp(name, "thread"))
124 #ifdef CONTEXT_THREADS
125     SIMIX_ctx_thread_factory_init(factory);
126 #else
127     THROW0(not_found_error, 0, "Factory 'thread' does not exist: thread support was not compiled in the SimGrid library");
128 #endif /* CONTEXT_THREADS */
129    
130     else if (!strcmp(name, "sysv"))
131   #if !defined(WIN32) && !defined(CONTEXT_THREADS)
132       SIMIX_ctx_sysv_factory_init(factory);
133   #else
134       THROW0(not_found_error, 0, "Factory 'sysv' does not exist: no System V thread support under Windows");
135   #endif
136     else if (!strcmp(name, "lua"))
137 #ifdef HAVE_LUA
138       SIMIX_ctx_lua_factory_init(factory);
139 #else
140
141     THROW0(not_found_error, 0, "Factory 'lua' does not exist: Lua support was not compiled in the SimGrid library");
142 #endif /* HAVE_LUA */
143
144     else if (!strcmp(name,"ruby"))
145 #ifdef HAVE_RUBY
146     SIMIX_ctx_ruby_factory_init(factory);
147 #else
148      THROW0(not_found_error, 0, "Factory 'ruby' does not exist: Ruby support was not compiled in the SimGrid library");
149 #endif
150   else
151     THROW1(not_found_error, 0, "Factory '%s' does not exist", name);
152 }