Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill the (unused) lua context factory. Lua does fine with regular contextes. That...
[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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_context, simix,
15                                 "Context switching mecanism");
16
17 const char *xbt_ctx_factory_to_use = NULL;
18 typedef void (*SIMIX_ctx_factory_initializer_t)(smx_context_factory_t *);
19 SIMIX_ctx_factory_initializer_t factory_initializer_to_use = NULL;
20
21 /** 
22  * This function is called by SIMIX_global_init() to initialize the context module.
23  */
24 void SIMIX_context_mod_init(void)
25 {
26   if (!simix_global->context_factory) {
27     /* select context factory to use to create the context(depends of the macro definitions) */
28     if (factory_initializer_to_use) {
29       (*factory_initializer_to_use)(&(simix_global->context_factory));
30     } else {
31 #ifdef CONTEXT_THREADS
32       /* context switch based os thread */
33       SIMIX_ctx_thread_factory_init(&simix_global->context_factory);
34 #elif defined(CONTEXT_UCONTEXT)
35       /* context switch based ucontext */
36       SIMIX_ctx_sysv_factory_init(&simix_global->context_factory);
37 #elif defined(_XBT_WIN32)
38       /* context switch based windows */
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
73
74   if (simix_global->context_factory != NULL) {
75     if (strcmp(simix_global->context_factory->name, name)) {
76
77       SIMIX_process_killall();
78
79       /* kill maestro process */
80       SIMIX_context_free(simix_global->maestro_process->context);
81       free(simix_global->maestro_process);
82       simix_global->maestro_process = NULL;
83
84       SIMIX_context_mod_exit();
85     } else
86       /* the same context factory is requested return directly */
87       return 0;
88   }
89
90   /* init the desired factory */
91   smx_context_factory_t * factory = &simix_global->context_factory;
92   if (!strcmp(name, "java"))
93 #ifdef HAVE_JAVA
94     SIMIX_ctx_java_factory_init(factory);
95 #else
96     THROW0(not_found_error, 0,
97            "Factory 'Java' does not exist: Java support was not compiled in the SimGrid library");
98 #endif                          /* HAVE_JAVA */
99
100   else if (!strcmp(name, "thread"))
101 #ifdef CONTEXT_THREADS
102     SIMIX_ctx_thread_factory_init(factory);
103 #else
104     THROW0(not_found_error, 0,
105            "Factory 'thread' does not exist: thread support was not compiled in the SimGrid library");
106 #endif                          /* CONTEXT_THREADS */
107
108   else if (!strcmp(name, "sysv"))
109 #if !defined(_XBT_WIN32) && !defined(CONTEXT_THREADS)
110     SIMIX_ctx_sysv_factory_init(factory);
111 #else
112     THROW0(not_found_error, 0,
113            "Factory 'sysv' does not exist: no System V thread support under Windows");
114 #endif
115
116   else
117     THROW1(not_found_error, 0, "Factory '%s' does not exist", name);
118
119
120
121
122   SIMIX_create_maestro_process();
123
124   return 0;
125 }
126