Logo AND Algorithmique Numérique Distribuée

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