Logo AND Algorithmique Numérique Distribuée

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