Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fd3bc2beb31122c418e5b72ffa47eea977f5cd61
[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 "smx_context_private.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smx_context, simix, "Context switching mecanism");
15
16 /* the context factory used to create the appropriate context
17  * each context implementation define its own context factory
18  * a context factory is responsable of the creation of the context
19  * associated with the maestro and of all the context based on
20  * the selected implementation.
21  *
22  * for example, the context switch based on java thread use the
23  * java implementation of the context and the java factory build
24  * the context depending of this implementation.
25  */
26
27 /**
28  * This function is call by SIMIX_global_init() to initialize the context module.
29  */
30 void SIMIX_context_mod_init(void)
31 {
32   if (!simix_global->context_factory) {
33   /* select context factory to use to create the context(depends of the macro definitions) */
34
35 #ifdef CONTEXT_THREADS
36     /* context switch based os thread */
37     SIMIX_ctx_thread_factory_init(&simix_global->context_factory);
38 #elif !defined(WIN32)
39     /* context switch based ucontext */
40     SIMIX_ctx_sysv_factory_init(&simix_global->context_factory);
41 #else
42     /* context switch is not allowed on Windows */
43 #error ERROR [__FILE__, line __LINE__]: no context based implementation specified.
44 #endif
45   }
46 }
47
48 /**
49  * This function is call by SIMIX_clean() to finalize the context module.
50  */
51 void SIMIX_context_mod_exit(void)
52 {
53   if (simix_global->context_factory) {
54     smx_pfn_context_factory_finalize_t finalize_factory;
55
56     /* if there are living processes then kill them (except maestro) */
57     if(simix_global->process_list != NULL)
58       SIMIX_process_killall();
59     
60     /* finalize the context factory */
61     finalize_factory = simix_global->context_factory->finalize;
62     (*finalize_factory) (&simix_global->context_factory);
63   }
64 }
65
66 /*******************************/
67 /* Object creation/destruction */
68 /*******************************/
69 /**
70  * \param smx_process the simix process that contains this context
71  * \param code a main function
72  */
73 int SIMIX_context_new(smx_process_t *process, xbt_main_func_t code)
74 {
75   /* use the appropriate context factory to create the appropriate context */
76     return (*(simix_global->context_factory->create_context)) (process, code);
77 }
78
79
80 int SIMIX_context_create_maestro(smx_process_t *process)
81 {
82   return (*(simix_global->context_factory->create_maestro_context)) (process);
83 }
84
85 /* Scenario for the end of a context:
86  *
87  * CASE 1: death after end of function
88  *   __context_wrapper, called by os thread, calls smx_context_stop after user code stops
89  *   smx_context_stop calls user cleanup_func if any (in context settings),
90  *                    add current to trashbin
91  *                    yields back to maestro (destroy os thread on need)
92  *   From time to time, maestro calls smx_context_empty_trash,
93  *       which maps smx_context_free on the content
94  *   smx_context_free frees some more memory,
95  *                    joins os thread
96  *
97  * CASE 2: brutal death
98  *   smx_context_kill (from any context)
99  *                    set context->wannadie to 1
100  *                    yields to the context
101  *   the context is awaken in the middle of __yield.
102  *   At the end of it, it checks that wannadie == 1, and call smx_context_stop
103  *   (same than first case afterward)
104  */
105
106
107 /* Argument must be stopped first -- runs in maestro context */
108 void SIMIX_context_free(smx_process_t process)
109 {
110   (*(simix_global->context_factory->free)) (process);
111 }
112
113 void SIMIX_context_kill(smx_process_t process)
114 {
115   (*(simix_global->context_factory->kill)) (process);
116 }
117
118 /**
119  * \param context the context to start
120  *
121  * Calling this function prepares \a process to be run. It will
122    however run effectively only when calling #SIMIX_context_schedule
123  */
124 void SIMIX_context_start(smx_process_t process)
125 {
126   (*(simix_global->context_factory->start)) (process);
127 }
128
129 /**
130  * Calling this function makes the current process yield. The process
131  * that scheduled it returns from SIMIX_context_schedule as if nothing
132  * had happened.
133  *
134  * Only the processes can call this function, giving back the control
135  * to the maestro
136  */
137 void SIMIX_context_yield(void)
138 {
139   (*(simix_global->context_factory->yield)) ();
140 }
141
142 /**
143  * \param process to be scheduled
144  *
145  * Calling this function blocks the current process and schedule \a process.
146  * When \a process would call SIMIX_context_yield, it will return
147  * to this function as if nothing had happened.
148  *
149  * Only the maestro can call this function to run a given process.
150  */
151 void SIMIX_context_schedule(smx_process_t process)
152 {
153   (*(simix_global->context_factory->schedule)) (process);
154 }
155
156 void SIMIX_context_stop(int exit_code)
157 {
158   (*(simix_global->context_factory->stop)) (exit_code);
159 }
160
161 int SIMIX_context_select_factory(const char *name)
162 {
163   /* if a factory is already instantiated (SIMIX_context_mod_init() was called) */
164   if (simix_global->context_factory != NULL) {
165     /* if the desired factory is different of the current factory, call SIMIX_context_mod_exit() */
166     if (strcmp(simix_global->context_factory->name, name))
167       SIMIX_context_mod_exit();
168     else
169       /* the same context factory is requested return directly */
170       return 0;
171   }
172
173   /* get the desired factory */
174   SIMIX_context_init_factory_by_name(&simix_global->context_factory, name);
175
176   /* maestro process specialisation */
177   (*(simix_global->context_factory->create_maestro_context)) (&simix_global->maestro_process);
178
179   /* the current process is the process of the maestro */
180   simix_global->current_process = simix_global->maestro_process;
181
182   /* the current context doesn't want to die */
183   simix_global->current_process->iwannadie = 0;
184
185   /* insert the current context in the list of the contexts in use */
186   xbt_swag_insert(simix_global->current_process, simix_global->process_list);
187
188   return 0;
189 }
190
191 void
192 SIMIX_context_init_factory_by_name(smx_context_factory_t * factory,
193                                    const char *name)
194 {
195   if (!strcmp(name, "java"))
196 #ifdef HAVE_JAVA     
197     SIMIX_ctx_java_factory_init(factory);
198 #else
199     THROW0(not_found_error, 0, "Factory 'Java' does not exist: Java support was not compiled in the SimGrid library");
200 #endif /* HAVE_JAVA */
201    
202   else if (!strcmp(name, "thread"))
203 #ifdef CONTEXT_THREADS
204     SIMIX_ctx_thread_factory_init(factory);
205 #else
206     THROW0(not_found_error, 0, "Factory 'thread' does not exist: thread support was not compiled in the SimGrid library");
207 #endif /* CONTEXT_THREADS */
208    
209   else if (!strcmp(name, "sysv"))
210 #if !defined(WIN32) && !defined(CONTEXT_THREADS)
211     SIMIX_ctx_sysv_factory_init(factory);
212 #else
213     THROW0(not_found_error, 0, "Factory 'sysv' does not exist: no System V thread support under Windows");
214 #endif   
215   else
216     THROW1(not_found_error, 0, "Factory '%s' does not exist", name);
217 }
218
219 /** Garbage collection
220  *
221  * Should be called some time to time to free the memory allocated for processes
222  * that have finished (or killed).
223  */
224 void SIMIX_context_empty_trash(void)
225
226   smx_process_t process = NULL;
227   int i;  
228
229   while ((process = xbt_swag_extract(simix_global->process_to_destroy))){
230     free(process->name);
231     process->name = NULL;
232   
233     if (process->argv) {
234       for (i = 0; i < process->argc; i++)
235         if (process->argv[i])
236           free(process->argv[i]);
237
238       free(process->argv);
239     }
240   
241     free(process);
242   }
243 }