Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reduce the coupling between main lib and java binding
[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 /* Use os threads (either pthreads or windows ones) */
32       SIMIX_ctx_thread_factory_init(&simix_global->context_factory);
33 #elif defined(CONTEXT_UCONTEXT) /* use ucontext */
34       SIMIX_ctx_sysv_factory_init(&simix_global->context_factory);
35 #else
36 #error ERROR [__FILE__, line __LINE__]: no context implementation specified.
37 #endif
38     }
39   }
40 }
41
42 /**
43  * This function is call by SIMIX_clean() to finalize the context module.
44  */
45 void SIMIX_context_mod_exit(void)
46 {
47   if (simix_global->context_factory) {
48     smx_pfn_context_factory_finalize_t finalize_factory;
49
50     /* finalize the context factory */
51     finalize_factory = simix_global->context_factory->finalize;
52     (*finalize_factory) (&simix_global->context_factory);
53   }
54 }
55
56 /**
57  * This function is used to change the context factory.
58  * Warning: it destroy all the existing processes (even for maestro), and it
59  * will create a new maestro process using the new context factory.
60  */
61 int SIMIX_context_select_factory(const char *name)
62 {
63   /* if a context factory is already instantiated and it is different from the
64      newly selected one, then kill all the processes, exit the context module
65      and initialize the new factory.
66    */
67
68
69   if (simix_global->context_factory != NULL) {
70     if (strcmp(simix_global->context_factory->name, name)) {
71
72       SIMIX_process_killall();
73
74       /* kill maestro process */
75       SIMIX_context_free(simix_global->maestro_process->context);
76       free(simix_global->maestro_process);
77       simix_global->maestro_process = NULL;
78
79       SIMIX_context_mod_exit();
80     } else
81       /* the same context factory is requested return directly */
82       return 0;
83   }
84
85   /* init the desired factory */
86   smx_context_factory_t * factory = &simix_global->context_factory;
87   if (!strcmp(name, "thread"))
88 #ifdef CONTEXT_THREADS
89     SIMIX_ctx_thread_factory_init(factory);
90 #else
91     THROW0(not_found_error, 0,
92            "Factory 'thread' does not exist: thread support was not compiled in the SimGrid library");
93 #endif                          /* CONTEXT_THREADS */
94
95   else if (!strcmp(name, "sysv"))
96 #if !defined(_XBT_WIN32) && !defined(CONTEXT_THREADS)
97     SIMIX_ctx_sysv_factory_init(factory);
98 #else
99     THROW0(not_found_error, 0,
100            "Factory 'sysv' does not exist: no System V thread support under Windows");
101 #endif
102
103   else
104     THROW1(not_found_error, 0, "Factory '%s' does not exist", name);
105
106
107
108
109   SIMIX_create_maestro_process();
110
111   return 0;
112 }
113