Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
First prototype of the networking API for SIMIX. It compiles but it is
[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 #include "xbt/ex_interface.h"
21
22 /******************************** Datatypes ***********************************/
23
24
25 /*********************************** Host *************************************/
26
27 /** @brief Host datatype
28     @ingroup m_datatypes_management_details */
29 typedef struct s_smx_host {
30   char *name;              /**< @brief host name if any */
31   void *host;              /* SURF modeling */
32   xbt_swag_t process_list;
33   void *data;              /**< @brief user data */
34 } s_smx_host_t;
35
36 /********************************** Simix Global ******************************/
37
38 typedef struct s_smx_context_factory *smx_context_factory_t;
39
40 typedef struct SIMIX_Global {
41   smx_context_factory_t context_factory;
42   xbt_dict_t host;
43   xbt_swag_t process_to_run;
44   xbt_swag_t process_list;
45   xbt_swag_t process_to_destroy;
46   smx_process_t current_process;
47   smx_process_t maestro_process;
48   xbt_dict_t registered_functions;
49   smx_creation_func_t create_process_function;
50   void_f_pvoid_t kill_process_function;
51   void_f_pvoid_t cleanup_process_function;
52 } s_SIMIX_Global_t, *SIMIX_Global_t;
53
54 extern SIMIX_Global_t simix_global;
55
56 /******************************** Process *************************************/
57
58 /** @brief Process datatype 
59     @ingroup m_datatypes_management_details @{ */
60      typedef struct s_smx_process {
61        s_xbt_swag_hookup_t process_hookup;
62        s_xbt_swag_hookup_t synchro_hookup;
63        s_xbt_swag_hookup_t host_proc_hookup;
64        s_xbt_swag_hookup_t destroy_hookup;
65
66        char *name;              /**< @brief process name if any */
67        smx_host_t smx_host;     /* the host on which the process is running */
68        smx_context_t context;   /* the context that executes the scheduler function */
69        ex_ctx_t *exception;
70        int blocked : 1;
71        int suspended : 1;
72        int iwannadie : 1;
73        smx_mutex_t mutex;       /* mutex on which the process is blocked  */
74        smx_cond_t cond;         /* cond on which the process is blocked  */
75        smx_action_t waiting_action;
76        xbt_dict_t properties;
77        void *data;              /* kept for compatibility, it should be replaced with moddata */
78
79      } s_smx_process_t;
80 /** @} */
81
82 typedef struct s_smx_process_arg {
83   const char *name;
84   xbt_main_func_t code;
85   void *data;
86   char *hostname;
87   int argc;
88   char **argv;
89   double kill_time;
90   xbt_dict_t properties;
91 } s_smx_process_arg_t, *smx_process_arg_t;
92
93 void SIMIX_create_maestro_process(void);
94 void SIMIX_process_empty_trash(void);
95 void SIMIX_process_schedule(smx_process_t process);
96 void SIMIX_process_yield(void);
97 ex_ctx_t *SIMIX_process_get_exception(void);
98 void SIMIX_process_exception_terminate(xbt_ex_t * e);
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 /******************************* Networking ***********************************/
122
123 /** @brief Rendez-vous point datatype */
124 typedef struct s_smx_rvpoint {
125   char *name;
126   smx_mutex_t read;
127   smx_mutex_t write;
128   smx_mutex_t comm_mutex;
129   xbt_fifo_t comm_fifo;  
130 } s_smx_rvpoint_t;
131
132 typedef struct s_smx_comm {
133   smx_host_t src_host;
134   smx_host_t dst_host;
135   smx_rvpoint_t rdv;
136   smx_cond_t cond;
137   smx_action_t act;
138   void *data;
139   size_t data_size;
140   void **dest_buff;
141   size_t dest_buff_size;
142   double rate;
143   int refcount;
144 } s_smx_comm_t;
145
146 /********************************* Action *************************************/
147
148 typedef enum {ready, ongoing, done, failed} smx_action_state_t;
149
150 /** @brief Action datatype
151     @ingroup m_datatypes_management_details */
152 typedef struct s_smx_action {
153   char *name;              /**< @brief action name if any */
154   xbt_fifo_t cond_list;    /*< conditional variables that must be signaled when the action finish. */
155   void *data;              /**< @brief user data */
156   int refcount;            /**< @brief reference counter */
157   surf_action_t surf_action;    /* SURF modeling of computation  */
158   smx_host_t source;
159 } s_smx_action_t;
160
161 /************************** Configuration support *****************************/
162
163 extern int _simix_init_status;  /* 0: beginning of time; FIXME: KILLME ?
164                                    1: pre-inited (cfg_set created);
165                                    2: inited (running) */
166
167 #define SIMIX_CHECK_HOST()  xbt_assert0(surf_workstation_model->extension.workstation. \
168                                   get_state(SIMIX_host_self()->host)==SURF_RESOURCE_ON,\
169                                   "Host failed, you cannot call this function.")
170
171 smx_host_t __SIMIX_host_create(const char *name, void *workstation, void *data);
172 void __SIMIX_host_destroy(void *host);
173 void __SIMIX_cond_wait(smx_cond_t cond);
174 void __SIMIX_cond_display_actions(smx_cond_t cond);
175 void __SIMIX_action_display_conditions(smx_action_t action);
176
177 /******************************** Context *************************************/
178
179 void SIMIX_context_mod_init(void);
180
181 void SIMIX_context_mod_exit(void);
182
183 /* *********************** */
184 /* Context type definition */
185 /* *********************** */
186 /* the following function pointers types describe the interface that all context
187    concepts must implement */
188
189 /* each context type must contain this macro at its begining -- OOP in C :/ */
190 #define SMX_CTX_BASE_T \
191   s_xbt_swag_hookup_t hookup; \
192   xbt_main_func_t code; \
193   int argc; \
194   char **argv; \
195   void_f_pvoid_t cleanup_func; \
196   void *cleanup_arg; \
197
198 /* all other context types derive from this structure */
199 typedef struct s_smx_context {
200   SMX_CTX_BASE_T;
201 } s_smx_context_t;
202
203 /* *********************** */
204 /* factory type definition */
205 /* *********************** */
206
207 /* Each context implementation define its own context factory
208  * A context factory is responsable of the creation and manipulation of the 
209  * execution context of all the simulated processes (and maestro) using the
210  * selected implementation.
211  *
212  * For example, the context switch based on java thread use the
213  * java implementation of the context and the java factory to build and control
214  * the contexts depending on this implementation.
215
216  * The following function pointer types describe the interface that any context 
217  * factory should implement.
218  */
219
220 /* function used to create a new context */
221 typedef smx_context_t (*smx_pfn_context_factory_create_context_t) 
222                       (xbt_main_func_t, int, char**, void_f_pvoid_t, void*);
223
224 /* this function finalize the specified context factory */
225 typedef int (*smx_pfn_context_factory_finalize_t) (smx_context_factory_t*);
226
227 /* function used to destroy the specified context */
228 typedef void (*smx_pfn_context_free_t) (smx_context_t);
229
230 /* function used to start the specified context */
231 typedef void (*smx_pfn_context_start_t) (smx_context_t);
232
233 /* function used to stop the current context */
234 typedef void (*smx_pfn_context_stop_t) (smx_context_t);
235
236 /* function used to suspend the current context */
237 typedef void (*smx_pfn_context_suspend_t) (smx_context_t context);
238
239 /* function used to resume the current context */
240 typedef void (*smx_pfn_context_resume_t) (smx_context_t old_context, 
241                                           smx_context_t new_context);
242
243 /* interface of the context factories */
244 typedef struct s_smx_context_factory {
245   smx_pfn_context_factory_create_context_t create_context;
246   smx_pfn_context_factory_finalize_t finalize;
247   smx_pfn_context_free_t free;
248   smx_pfn_context_start_t start;
249   smx_pfn_context_stop_t stop;
250   smx_pfn_context_suspend_t suspend;
251   smx_pfn_context_resume_t resume;
252   const char *name;
253 } s_smx_context_factory_t;
254
255 /* Selects a context factory associated with the name specified by the parameter name.
256  * If successful the function returns 0. Otherwise the function returns the error code.
257  */
258 int SIMIX_context_select_factory(const char *name);
259
260 /* Initializes a context factory from the name specified by the parameter name.
261  * If the factory cannot be found, an exception is raised.
262  */
263 void SIMIX_context_init_factory_by_name(smx_context_factory_t * factory, const char *name);
264
265 /* All factories init */
266 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory);
267
268 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t * factory);
269
270 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory);
271
272 /* ****************************** */
273 /* context manipulation functions */
274 /* ****************************** */
275
276 /* Scenario for the end of a context:
277  *
278  * CASE 1: death after end of the main function
279  *   the context_wrapper, called internally by the context module, calls 
280  *   SIMIX_context_stop after user code stops, smx_context_stop calls user 
281  *   cleanup_func if any (in context settings), add current process to trashbin
282  *   and yields back to maestro.
283  *   From time to time, maestro calls SIMIX_context_empty_trash, which destroy
284  *   all the process and context data structures, and frees the memory 
285  *
286  * CASE 2: brutal death
287  *   SIMIX_process_kill (from any process) set process->iwannadie = 1 and then
288  *   schedules the process. Then the process is awaken in the middle of the
289  *   SIMIX_process_yield function, and at the end of it, it checks that
290  *   iwannadie == 1, and call SIMIX_context_stop(same than first case afterward)
291  */
292
293 /**
294  * \brief creates a new context for a user level process
295  * \param code a main function
296  * \param argc the number of arguments of the main function
297  * \param argv the vector of arguments of the main function
298  * \param cleanup_func the function to call when the context stops
299  * \param cleanup_arg the argument of the cleanup_func function
300  */
301 static inline smx_context_t SIMIX_context_new(xbt_main_func_t code, int argc,
302                                               char** argv,
303                                               void_f_pvoid_t cleanup_func,
304                                               void* cleanup_arg)
305 {
306   return (*(simix_global->context_factory->create_context))
307            (code, argc, argv, cleanup_func, cleanup_arg);
308 }
309
310 /**
311  * \brief destroy a context 
312  * \param context the context to destroy
313  * Argument must be stopped first -- runs in maestro context
314  */
315 static inline void SIMIX_context_free(smx_context_t context)
316 {
317   (*(simix_global->context_factory->free)) (context);
318 }
319
320 /**
321  * \brief prepares aa context to be run
322  * \param context the context to start
323  * It will however run effectively only when calling #SIMIX_process_schedule
324  */
325 static inline void SIMIX_context_start(smx_context_t context)
326 {
327   (*(simix_global->context_factory->start)) (context);
328 }
329
330 /**
331  * \brief stops the execution of a context
332  * \param context to stop
333  */
334 static inline void SIMIX_context_stop(smx_context_t context)
335 {
336   (*(simix_global->context_factory->stop)) (context);
337 }
338
339 /**
340  \brief resumes the execution of a context
341  \param old_context the actual context from which is resuming
342  \param new_context the context to resume
343  */
344 static inline void SIMIX_context_resume(smx_context_t old_context,
345                                         smx_context_t new_context)
346 {
347   (*(simix_global->context_factory->resume)) (old_context, new_context);
348 }
349
350 /**
351  \brief suspends a context and return the control back to the one which
352         scheduled it
353  \param context the context to be suspended (it must be the running one)
354  */
355 static inline void SIMIX_context_suspend(smx_context_t context)
356 {
357   (*(simix_global->context_factory->suspend)) (context);
358 }
359
360 #endif