Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4fd9db7783adf33ae10000e948d030ae682641f3
[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 /** @brief Process datatype 
58     @ingroup m_datatypes_management_details @{ */
59      typedef struct s_smx_process {
60        s_xbt_swag_hookup_t process_hookup;
61        s_xbt_swag_hookup_t synchro_hookup;
62        s_xbt_swag_hookup_t host_proc_hookup;
63        s_xbt_swag_hookup_t destroy_hookup;
64
65        char *name;              /**< @brief process name if any */
66        smx_host_t smx_host;     /* the host on which the process is running */
67        smx_context_t context;   /* the context that executes the scheduler function */
68        int blocked : 1;
69        int suspended : 1;
70        int iwannadie : 1;
71        smx_mutex_t mutex;       /* mutex on which the process is blocked  */
72        smx_cond_t cond;         /* cond on which the process is blocked  */
73        xbt_dict_t properties;
74        void *data;              /* kept for compatibility, it should be replaced with moddata */
75
76      } s_smx_process_t;
77 /** @} */
78
79 typedef struct s_smx_process_arg {
80   const char *name;
81   xbt_main_func_t code;
82   void *data;
83   char *hostname;
84   int argc;
85   char **argv;
86   double kill_time;
87   xbt_dict_t properties;
88 } s_smx_process_arg_t, *smx_process_arg_t;
89
90 void SIMIX_process_empty_trash(void);
91 void __SIMIX_process_schedule(smx_process_t process);
92 void __SIMIX_process_yield(void);
93
94 /*************************** Mutex and Conditional ****************************/
95
96 typedef struct s_smx_mutex {
97
98   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_mutex */
99   xbt_swag_t sleeping;          /* list of sleeping process */
100   int refcount;
101   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_mutex */
102
103 } s_smx_mutex_t;
104
105 typedef struct s_smx_cond {
106
107   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_cond */
108   xbt_swag_t sleeping;          /* list of sleeping process */
109   smx_mutex_t mutex;
110   xbt_fifo_t actions;           /* list of actions */
111   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_cond */
112
113 } s_smx_cond_t;
114
115 /********************************* Action *************************************/
116
117 /** @brief Action datatype
118     @ingroup m_datatypes_management_details */
119 typedef struct s_smx_action {
120   char *name;              /**< @brief action name if any */
121   xbt_fifo_t cond_list;    /*< conditional variables that must be signaled when the action finish. */
122   void *data;              /**< @brief user data */
123   int refcount;            /**< @brief reference counter */
124   surf_action_t surf_action;    /* SURF modeling of computation  */
125   smx_host_t source;
126 } s_smx_action_t;
127
128 /************************** Configuration support *****************************/
129
130 extern int _simix_init_status;  /* 0: beginning of time; FIXME: KILLME ?
131                                    1: pre-inited (cfg_set created);
132                                    2: inited (running) */
133
134 #define SIMIX_CHECK_HOST()  xbt_assert0(surf_workstation_model->extension.workstation. \
135                                   get_state(SIMIX_host_self()->host)==SURF_RESOURCE_ON,\
136                                   "Host failed, you cannot call this function.")
137
138 smx_host_t __SIMIX_host_create(const char *name, void *workstation, void *data);
139 void __SIMIX_host_destroy(void *host);
140 void __SIMIX_cond_wait(smx_cond_t cond);
141 void __SIMIX_cond_display_actions(smx_cond_t cond);
142 void __SIMIX_action_display_conditions(smx_action_t action);
143 void __SIMIX_create_maestro_process(void);
144
145 /******************************** Context *************************************/
146
147 void SIMIX_context_mod_init(void);
148
149 void SIMIX_context_mod_exit(void);
150
151 /* *********************** */
152 /* Context type definition */
153 /* *********************** */
154 /* the following function pointers types describe the interface that all context
155    concepts must implement */
156
157 /* each context type must contain this macro at its begining -- OOP in C :/ */
158 #define SMX_CTX_BASE_T \
159   s_xbt_swag_hookup_t hookup; \
160   ex_ctx_t *exception; \
161   xbt_main_func_t code; \
162   int argc; \
163   char **argv; \
164   void_f_pvoid_t cleanup_func; \
165   void *cleanup_arg; \
166
167 /* all other context types derive from this structure */
168 typedef struct s_smx_context {
169   SMX_CTX_BASE_T;
170 } s_smx_context_t;
171
172 /* *********************** */
173 /* factory type definition */
174 /* *********************** */
175
176 /* Each context implementation define its own context factory
177  * A context factory is responsable of the creation and manipulation of the 
178  * execution context of all the simulated processes (and maestro) using the
179  * selected implementation.
180  *
181  * For example, the context switch based on java thread use the
182  * java implementation of the context and the java factory to build and control
183  * the contexts depending on this implementation.
184
185  * The following function pointer types describe the interface that any context 
186  * factory should implement.
187  */
188
189 /* function used to create a new context */
190 typedef smx_context_t (*smx_pfn_context_factory_create_context_t) 
191                       (xbt_main_func_t, int, char**, void_f_pvoid_t, void*);
192
193 /* function used to create the context for the maestro process */
194 typedef smx_context_t (*smx_pfn_context_factory_create_maestro_context_t) (void);
195
196 /* this function finalize the specified context factory */
197 typedef int (*smx_pfn_context_factory_finalize_t) (smx_context_factory_t*);
198
199 /* function used to destroy the specified context */
200 typedef void (*smx_pfn_context_free_t) (smx_context_t);
201
202 /* function used to start the specified context */
203 typedef void (*smx_pfn_context_start_t) (smx_context_t);
204
205 /* function used to stop the current context */
206 typedef void (*smx_pfn_context_stop_t) (smx_context_t);
207
208 /* function used to suspend the current context */
209 typedef void (*smx_pfn_context_suspend_t) (smx_context_t context);
210
211 /* function used to resume the current context */
212 typedef void (*smx_pfn_context_resume_t) (smx_context_t old_context, 
213                                           smx_context_t new_context);
214
215 /* interface of the context factories */
216 typedef struct s_smx_context_factory {
217   smx_pfn_context_factory_create_maestro_context_t create_maestro_context;
218   smx_pfn_context_factory_create_context_t create_context;
219   smx_pfn_context_factory_finalize_t finalize;
220   smx_pfn_context_free_t free;
221   smx_pfn_context_start_t start;
222   smx_pfn_context_stop_t stop;
223   smx_pfn_context_suspend_t suspend;
224   smx_pfn_context_resume_t resume;
225   const char *name;
226 } s_smx_context_factory_t;
227
228 /* Selects a context factory associated with the name specified by the parameter name.
229  * If successful the function returns 0. Otherwise the function returns the error code.
230  */
231 int SIMIX_context_select_factory(const char *name);
232
233 /* Initializes a context factory from the name specified by the parameter name.
234  * If the factory cannot be found, an exception is raised.
235  */
236 void SIMIX_context_init_factory_by_name(smx_context_factory_t * factory, const char *name);
237
238 /* All factories init */
239 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory);
240
241 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t * factory);
242
243 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory);
244
245 /* ****************************** */
246 /* context manipulation functions */
247 /* ****************************** */
248
249 /* Scenario for the end of a context:
250  *
251  * CASE 1: death after end of the main function
252  *   the context_wrapper, called internally by the context module, calls 
253  *   SIMIX_context_stop after user code stops, smx_context_stop calls user 
254  *   cleanup_func if any (in context settings), add current process to trashbin
255  *   and yields back to maestro.
256  *   From time to time, maestro calls SIMIX_context_empty_trash, which destroy
257  *   all the process and context data structures, and frees the memory 
258  *
259  * CASE 2: brutal death
260  *   SIMIX_process_kill (from any process) set process->iwannadie = 1 and then
261  *   schedules the process. Then the process is awaken in the middle of the
262  *   SIMIX_process_yield function, and at the end of it, it checks that
263  *   iwannadie == 1, and call SIMIX_context_stop(same than first case afterward)
264  */
265
266 /**
267  * \brief creates the context for the maestro process
268  */
269 static inline smx_context_t SIMIX_context_create_maestro()
270 {
271   return (*(simix_global->context_factory->create_maestro_context)) ();
272 }
273
274 /**
275  * \brief creates a new context for a user level process
276  * \param code a main function
277  * \param argc the number of arguments of the main function
278  * \param argv the vector of arguments of the main function
279  * \param cleanup_func the function to call when the context stops
280  * \param cleanup_arg the argument of the cleanup_func function
281  */
282 static inline smx_context_t SIMIX_context_new(xbt_main_func_t code, int argc,
283                                               char** argv,
284                                               void_f_pvoid_t cleanup_func,
285                                               void* cleanup_arg)
286 {
287   return (*(simix_global->context_factory->create_context))
288            (code, argc, argv, cleanup_func, cleanup_arg);
289 }
290
291 /**
292  * \brief destroy a context 
293  * \param context the context to destroy
294  * Argument must be stopped first -- runs in maestro context
295  */
296 static inline void SIMIX_context_free(smx_context_t context)
297 {
298   (*(simix_global->context_factory->free)) (context);
299 }
300
301 /**
302  * \brief prepares aa context to be run
303  * \param context the context to start
304  * It will however run effectively only when calling #SIMIX_process_schedule
305  */
306 static inline void SIMIX_context_start(smx_context_t context)
307 {
308   (*(simix_global->context_factory->start)) (context);
309 }
310
311 /**
312  * \brief stops the execution of a context
313  * \param context to stop
314  */
315 static inline void SIMIX_context_stop(smx_context_t context)
316 {
317   (*(simix_global->context_factory->stop)) (context);
318 }
319
320 /**
321  \brief resumes the execution of a context
322  \param old_context the actual context from which is resuming
323  \param new_context the context to resume
324  */
325 static inline void SIMIX_context_resume(smx_context_t old_context,
326                                         smx_context_t new_context)
327 {
328   (*(simix_global->context_factory->resume)) (old_context, new_context);
329 }
330
331 /**
332  \brief suspends a context and return the control back to the one which
333         scheduled it
334  \param context the context to be suspended (it must be the running one)
335  */
336 static inline void SIMIX_context_suspend(smx_context_t context)
337 {
338   (*(simix_global->context_factory->suspend)) (context);
339 }
340
341 #endif