Logo AND Algorithmique Numérique Distribuée

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