Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
48fd01069a38dd8f37e3538c4269c8de147b3ab8
[simgrid.git] / src / simix / private.h
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donnassolo.
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 #ifndef SIMIX_PRIVATE_H
10 #define SIMIX_PRIVATE_H
11
12 #include <stdio.h>
13 #include "simix/simix.h"
14 #include "surf/surf.h"
15 #include "xbt/fifo.h"
16 #include "xbt/swag.h"
17 #include "xbt/dict.h"
18 #include "xbt/config.h"
19 #include "xbt/function_types.h"
20
21 /******************************** Datatypes ***********************************/
22
23
24 /*********************************** Host *************************************/
25
26 /** @brief Host datatype
27     @ingroup m_datatypes_management_details */
28 typedef struct s_smx_host {
29   char *name;              /**< @brief host name if any */
30   void *host;              /* SURF modeling */
31   xbt_swag_t process_list;
32   void *data;              /**< @brief user data */
33 } s_smx_host_t;
34
35 /********************************** Simix Global ******************************/
36
37 typedef struct s_smx_context_factory *smx_context_factory_t;
38
39 typedef struct SIMIX_Global {
40   smx_context_factory_t context_factory;
41   xbt_dict_t host;
42   xbt_swag_t process_to_run;
43   xbt_swag_t process_list;
44   xbt_swag_t process_to_destroy;
45   smx_process_t current_process;
46   smx_process_t maestro_process;
47   xbt_dict_t registered_functions;
48   smx_creation_func_t create_process_function;
49   void_f_pvoid_t kill_process_function;
50   void_f_pvoid_t cleanup_process_function;
51 } s_SIMIX_Global_t, *SIMIX_Global_t;
52
53 extern SIMIX_Global_t simix_global;
54
55 /******************************** Process *************************************/
56
57 typedef struct s_smx_context *smx_context_t;
58
59 /** @brief Process datatype
60     @ingroup m_datatypes_management_details @{ */
61      typedef struct s_smx_process {
62        s_xbt_swag_hookup_t process_hookup;
63        s_xbt_swag_hookup_t synchro_hookup;
64        s_xbt_swag_hookup_t host_proc_hookup;
65        s_xbt_swag_hookup_t destroy_hookup;
66
67        char *name;              /**< @brief process name if any */
68        smx_host_t smx_host;     /* the host on which the process is running */
69        smx_context_t context;   /* the context that executes the scheduler function */
70        int argc;                /* arguments number if any */
71        char **argv;             /* arguments table if any */
72        int blocked : 1;
73        int suspended : 1;
74        int iwannadie : 1;
75        smx_mutex_t mutex;       /* mutex on which the process is blocked  */
76        smx_cond_t cond;         /* cond on which the process is blocked  */
77        xbt_dict_t properties;
78        void *data;              /* kept for compatibility, it should be replaced with moddata */
79        void_f_pvoid_t cleanup_func;
80        void *cleanup_arg;
81
82      } s_smx_process_t;
83 /** @} */
84
85 typedef struct s_smx_process_arg {
86   const char *name;
87   xbt_main_func_t code;
88   void *data;
89   char *hostname;
90   int argc;
91   char **argv;
92   double kill_time;
93   xbt_dict_t properties;
94 } s_smx_process_arg_t, *smx_process_arg_t;
95
96 void SIMIX_process_empty_trash(void);
97 void __SIMIX_process_schedule(smx_process_t process);
98 void __SIMIX_process_yield(void);
99
100 /*************************** Mutex and Conditional ****************************/
101
102 typedef struct s_smx_mutex {
103
104   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_mutex */
105   xbt_swag_t sleeping;          /* list of sleeping process */
106   int refcount;
107   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_mutex */
108
109 } s_smx_mutex_t;
110
111 typedef struct s_smx_cond {
112
113   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_cond */
114   xbt_swag_t sleeping;          /* list of sleeping process */
115   smx_mutex_t mutex;
116   xbt_fifo_t actions;           /* list of actions */
117   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_cond */
118
119 } s_smx_cond_t;
120
121 /********************************* Action *************************************/
122
123 /** @brief Action datatype
124     @ingroup m_datatypes_management_details */
125 typedef struct s_smx_action {
126   char *name;              /**< @brief action name if any */
127   xbt_fifo_t cond_list;    /*< conditional variables that must be signaled when the action finish. */
128   void *data;              /**< @brief user data */
129   int refcount;            /**< @brief reference counter */
130   surf_action_t surf_action;    /* SURF modeling of computation  */
131   smx_host_t source;
132 } s_smx_action_t;
133
134 /************************** Configuration support *****************************/
135
136 extern int _simix_init_status;  /* 0: beginning of time; FIXME: KILLME ?
137                                    1: pre-inited (cfg_set created);
138                                    2: inited (running) */
139
140 #define SIMIX_CHECK_HOST()  xbt_assert0(surf_workstation_model->extension.workstation. \
141                                   get_state(SIMIX_host_self()->host)==SURF_RESOURCE_ON,\
142                                   "Host failed, you cannot call this function.")
143
144 smx_host_t __SIMIX_host_create(const char *name, void *workstation, void *data);
145 void __SIMIX_host_destroy(void *host);
146 void __SIMIX_cond_wait(smx_cond_t cond);
147 void __SIMIX_cond_display_actions(smx_cond_t cond);
148 void __SIMIX_action_display_conditions(smx_action_t action);
149 void __SIMIX_create_maestro_process(void);
150
151 /******************************** Context *************************************/
152
153 void SIMIX_context_mod_init(void);
154
155 void SIMIX_context_mod_exit(void);
156
157 /* *********************** */
158 /* Context type definition */
159 /* *********************** */
160 /* the following function pointers types describe the interface that all context
161    concepts must implement */
162
163 /* each context type must contain this macro at its begining -- OOP in C :/ */
164 #define SMX_CTX_BASE_T \
165   s_xbt_swag_hookup_t hookup; \
166   ex_ctx_t *exception; \
167   xbt_main_func_t code; \
168
169 /* all other context types derive from this structure */
170 typedef struct s_smx_context {
171   SMX_CTX_BASE_T;
172 } s_smx_context_t;
173
174 /* *********************** */
175 /* factory type definition */
176 /* *********************** */
177
178 /* Each context implementation define its own context factory
179  * A context factory is responsable of the creation and manipulation of the 
180  * execution context of all the simulated processes (and maestro) using the
181  * selected implementation.
182  *
183  * For example, the context switch based on java thread use the
184  * java implementation of the context and the java factory to build and control
185  * the contexts depending on this implementation.
186
187  * The following function pointer types describe the interface that any context 
188  * factory should implement.
189  */
190
191 /* function used to create a new context */
192 typedef int (*smx_pfn_context_factory_create_context_t) (smx_process_t *, xbt_main_func_t);
193
194 /* function used to create the context for the maestro process */
195 typedef int (*smx_pfn_context_factory_create_maestro_context_t) (smx_process_t*);
196
197 /* this function finalize the specified context factory */
198 typedef int (*smx_pfn_context_factory_finalize_t) (smx_context_factory_t*);
199
200 /* function used to destroy the specified context */
201 typedef void (*smx_pfn_context_free_t) (smx_process_t);
202
203 /* function used to kill the specified context */
204 typedef void (*smx_pfn_context_kill_t) (smx_process_t);
205
206 /* function used to resume the specified context */
207 typedef void (*smx_pfn_context_schedule_t) (smx_process_t);
208
209 /* function used to yield the specified context */
210 typedef void (*smx_pfn_context_yield_t) (void);
211
212 /* function used to start the specified context */
213 typedef void (*smx_pfn_context_start_t) (smx_process_t);
214
215 /* function used to stop the current context */
216 typedef void (*smx_pfn_context_stop_t) (int);
217
218 /* interface of the context factories */
219 typedef struct s_smx_context_factory {
220   smx_pfn_context_factory_create_maestro_context_t create_maestro_context;
221   smx_pfn_context_factory_create_context_t create_context;
222   smx_pfn_context_factory_finalize_t finalize;
223   smx_pfn_context_free_t free;
224   smx_pfn_context_kill_t kill;
225   smx_pfn_context_schedule_t schedule;
226   smx_pfn_context_yield_t yield;
227   smx_pfn_context_start_t start;
228   smx_pfn_context_stop_t stop;
229   const char *name;
230 } s_smx_context_factory_t;
231
232 /* Selects a context factory associated with the name specified by the parameter name.
233  * If successful the function returns 0. Otherwise the function returns the error code.
234  */
235 int SIMIX_context_select_factory(const char *name);
236
237 /* Initializes a context factory from the name specified by the parameter name.
238  * If the factory cannot be found, an exception is raised.
239  */
240 void SIMIX_context_init_factory_by_name(smx_context_factory_t * factory, const char *name);
241
242 /* All factories init */
243 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory);
244
245 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t * factory);
246
247 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory);
248
249 /* ******************************* */
250 /* contexts manipulation functions */
251 /* ******************************* */
252
253 /**
254  * \param smx_process the simix maestro process that contains this context
255  */
256 static inline int SIMIX_context_create_maestro(smx_process_t *process)
257 {
258   return (*(simix_global->context_factory->create_maestro_context)) (process);
259 }
260
261 /**
262  * \param smx_process the simix process that contains this context
263  * \param code a main function
264  */
265 static inline int SIMIX_context_new(smx_process_t *process, xbt_main_func_t code)
266 {
267     return (*(simix_global->context_factory->create_context)) (process, code);
268 }
269
270 /* Scenario for the end of a context:
271  *
272  * CASE 1: death after end of function
273  *   __context_wrapper, called by os thread, calls smx_context_stop after user code stops
274  *   smx_context_stop calls user cleanup_func if any (in context settings),
275  *                    add current to trashbin
276  *                    yields back to maestro (destroy os thread on need)
277  *   From time to time, maestro calls smx_context_empty_trash,
278  *       which maps smx_context_free on the content
279  *   smx_context_free frees some more memory,
280  *                    joins os thread
281  *
282  * CASE 2: brutal death
283  *   smx_context_kill (from any context)
284  *                    set context->wannadie to 1
285  *                    yields to the context
286  *   the context is awaken in the middle of __yield.
287  *   At the end of it, it checks that wannadie == 1, and call smx_context_stop
288  *   (same than first case afterward)
289  */
290 static inline void SIMIX_context_kill(smx_process_t process)
291 {
292   (*(simix_global->context_factory->kill)) (process);
293 }
294
295 /* Argument must be stopped first -- runs in maestro context */
296 static inline void SIMIX_context_free(smx_process_t process)
297 {
298   (*(simix_global->context_factory->free)) (process);
299 }
300
301 /**
302  * \param context the context to start
303  *
304  * Calling this function prepares \a process to be run. It will
305    however run effectively only when calling #SIMIX_context_schedule
306  */
307 static inline void SIMIX_context_start(smx_process_t process)
308 {
309   (*(simix_global->context_factory->start)) (process);
310 }
311
312 /**
313  * Calling this function makes the current process yield. The process
314  * that scheduled it returns from SIMIX_context_schedule as if nothing
315  * had happened.
316  *
317  * Only the processes can call this function, giving back the control
318  * to the maestro
319  */
320 static inline void SIMIX_context_yield(void)
321 {
322   (*(simix_global->context_factory->yield)) ();
323 }
324
325 /**
326  * \param process to be scheduled
327  *
328  * Calling this function blocks the current process and schedule \a process.
329  * When \a process would call SIMIX_context_yield, it will return
330  * to this function as if nothing had happened.
331  *
332  * Only the maestro can call this function to run a given process.
333  */
334 static inline void SIMIX_context_schedule(smx_process_t process)
335 {
336   (*(simix_global->context_factory->schedule)) (process);
337 }
338
339 static inline void SIMIX_context_stop(int exit_code)
340 {
341   (*(simix_global->context_factory->stop)) (exit_code);
342 }
343
344 #endif