Logo AND Algorithmique Numérique Distribuée

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