Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add SIMIX_rdv_{set,get}_data; tiny fixups
[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 ex_ctx_t *SIMIX_process_get_exception(void);
97 void SIMIX_process_exception_terminate(xbt_ex_t * e);
98
99 /*************************** Mutex and Conditional ****************************/
100
101 typedef struct s_smx_mutex {
102
103   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_mutex */
104   xbt_swag_t sleeping;          /* list of sleeping process */
105   int refcount;
106   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_mutex */
107
108 } s_smx_mutex_t;
109
110 typedef struct s_smx_cond {
111
112   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_cond */
113   xbt_swag_t sleeping;          /* list of sleeping process */
114   smx_mutex_t mutex;
115   xbt_fifo_t actions;           /* list of actions */
116   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_cond */
117
118 } s_smx_cond_t;
119
120 /******************************* Networking ***********************************/
121
122 /** @brief Rendez-vous point datatype */
123 typedef struct s_smx_rvpoint {
124   char *name;
125   smx_mutex_t read;
126   smx_mutex_t write;
127   xbt_fifo_t comm_fifo;  
128   void *data;
129 } s_smx_rvpoint_t;
130
131 typedef struct s_smx_comm {
132
133
134   smx_comm_type_t type;   /* Type of the communication (comm_send,comm_recv) */
135   smx_rdv_t rdv;          /* Rendez-vous where the comm is queued */
136   smx_cond_t cond;        /* Condition associated to the surf simulation */
137   int refcount;           /* Number of processes involved in the cond */
138
139   /* Surf action data */
140   smx_process_t src_proc;
141   smx_process_t dst_proc;
142   smx_action_t act;
143   double rate;
144   double task_size;
145
146   /* Data to be transfered */
147   void *src_buff;
148   size_t src_buff_size;
149   void *dst_buff;
150   size_t *dst_buff_size;
151   void *data;             /* User data associated to communication */
152 } s_smx_comm_t;
153
154 void SIMIX_network_copy_data(smx_comm_t comm);
155 smx_comm_t SIMIX_communication_new(smx_comm_type_t type);
156 void SIMIX_communication_destroy(smx_comm_t comm);
157 static inline void SIMIX_communication_use(smx_comm_t comm);
158 static inline void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout);
159 static inline void SIMIX_rdv_push(smx_rdv_t rdv, smx_comm_t comm);
160 static inline void SIMIX_rdv_remove(smx_rdv_t rdv, smx_comm_t comm);
161
162 /********************************* Action *************************************/
163
164 typedef enum {ready, ongoing, done, failed} smx_action_state_t;
165
166 /** @brief Action datatype
167     @ingroup m_datatypes_management_details */
168 typedef struct s_smx_action {
169   char *name;              /**< @brief action name if any */
170   xbt_fifo_t cond_list;    /*< conditional variables that must be signaled when the action finish. */
171   void *data;              /**< @brief user data */
172   int refcount;            /**< @brief reference counter */
173   surf_action_t surf_action;    /* SURF modeling of computation  */
174   smx_host_t source;
175 } s_smx_action_t;
176
177 /************************** Configuration support *****************************/
178
179 extern int _simix_init_status;  /* 0: beginning of time; FIXME: KILLME ?
180                                    1: pre-inited (cfg_set created);
181                                    2: inited (running) */
182
183 #define SIMIX_CHECK_HOST()  xbt_assert0(surf_workstation_model->extension.workstation. \
184                                   get_state(SIMIX_host_self()->host)==SURF_RESOURCE_ON,\
185                                   "Host failed, you cannot call this function.")
186
187 smx_host_t __SIMIX_host_create(const char *name, void *workstation, void *data);
188 void __SIMIX_host_destroy(void *host);
189 void __SIMIX_cond_wait(smx_cond_t cond);
190 void __SIMIX_cond_display_actions(smx_cond_t cond);
191 void __SIMIX_action_display_conditions(smx_action_t action);
192
193 /******************************** Context *************************************/
194
195 void SIMIX_context_mod_init(void);
196
197 void SIMIX_context_mod_exit(void);
198
199 /* *********************** */
200 /* Context type definition */
201 /* *********************** */
202 /* the following function pointers types describe the interface that all context
203    concepts must implement */
204
205 /* each context type must contain this macro at its begining -- OOP in C :/ */
206 #define SMX_CTX_BASE_T \
207   s_xbt_swag_hookup_t hookup; \
208   xbt_main_func_t code; \
209   int argc; \
210   char **argv; \
211   void_f_pvoid_t cleanup_func; \
212   void *cleanup_arg; \
213
214 /* all other context types derive from this structure */
215 typedef struct s_smx_context {
216   SMX_CTX_BASE_T;
217 } s_smx_context_t;
218
219 /* *********************** */
220 /* factory type definition */
221 /* *********************** */
222
223 /* Each context implementation define its own context factory
224  * A context factory is responsable of the creation and manipulation of the 
225  * execution context of all the simulated processes (and maestro) using the
226  * selected implementation.
227  *
228  * For example, the context switch based on java thread use the
229  * java implementation of the context and the java factory to build and control
230  * the contexts depending on this implementation.
231
232  * The following function pointer types describe the interface that any context 
233  * factory should implement.
234  */
235
236 /* function used to create a new context */
237 typedef smx_context_t (*smx_pfn_context_factory_create_context_t) 
238                       (xbt_main_func_t, int, char**, void_f_pvoid_t, void*);
239
240 /* this function finalize the specified context factory */
241 typedef int (*smx_pfn_context_factory_finalize_t) (smx_context_factory_t*);
242
243 /* function used to destroy the specified context */
244 typedef void (*smx_pfn_context_free_t) (smx_context_t);
245
246 /* function used to start the specified context */
247 typedef void (*smx_pfn_context_start_t) (smx_context_t);
248
249 /* function used to stop the current context */
250 typedef void (*smx_pfn_context_stop_t) (smx_context_t);
251
252 /* function used to suspend the current context */
253 typedef void (*smx_pfn_context_suspend_t) (smx_context_t context);
254
255 /* function used to resume the current context */
256 typedef void (*smx_pfn_context_resume_t) (smx_context_t old_context, 
257                                           smx_context_t new_context);
258
259 /* interface of the context factories */
260 typedef struct s_smx_context_factory {
261   smx_pfn_context_factory_create_context_t create_context;
262   smx_pfn_context_factory_finalize_t finalize;
263   smx_pfn_context_free_t free;
264   smx_pfn_context_start_t start;
265   smx_pfn_context_stop_t stop;
266   smx_pfn_context_suspend_t suspend;
267   smx_pfn_context_resume_t resume;
268   const char *name;
269 } s_smx_context_factory_t;
270
271 /* Selects a context factory associated with the name specified by the parameter name.
272  * If successful the function returns 0. Otherwise the function returns the error code.
273  */
274 int SIMIX_context_select_factory(const char *name);
275
276 /* Initializes a context factory from the name specified by the parameter name.
277  * If the factory cannot be found, an exception is raised.
278  */
279 void SIMIX_context_init_factory_by_name(smx_context_factory_t * factory, const char *name);
280
281 /* All factories init */
282 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory);
283
284 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t * factory);
285
286 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory);
287
288 /* ****************************** */
289 /* context manipulation functions */
290 /* ****************************** */
291
292 /* Scenario for the end of a context:
293  *
294  * CASE 1: death after end of the main function
295  *   the context_wrapper, called internally by the context module, calls 
296  *   SIMIX_context_stop after user code stops, smx_context_stop calls user 
297  *   cleanup_func if any (in context settings), add current process to trashbin
298  *   and yields back to maestro.
299  *   From time to time, maestro calls SIMIX_context_empty_trash, which destroy
300  *   all the process and context data structures, and frees the memory 
301  *
302  * CASE 2: brutal death
303  *   SIMIX_process_kill (from any process) set process->iwannadie = 1 and then
304  *   schedules the process. Then the process is awaken in the middle of the
305  *   SIMIX_process_yield function, and at the end of it, it checks that
306  *   iwannadie == 1, and call SIMIX_context_stop(same than first case afterward)
307  */
308
309 /**
310  * \brief creates a new context for a user level process
311  * \param code a main function
312  * \param argc the number of arguments of the main function
313  * \param argv the vector of arguments of the main function
314  * \param cleanup_func the function to call when the context stops
315  * \param cleanup_arg the argument of the cleanup_func function
316  */
317 static inline smx_context_t SIMIX_context_new(xbt_main_func_t code, int argc,
318                                               char** argv,
319                                               void_f_pvoid_t cleanup_func,
320                                               void* cleanup_arg)
321 {
322   return (*(simix_global->context_factory->create_context))
323            (code, argc, argv, cleanup_func, cleanup_arg);
324 }
325
326 /**
327  * \brief destroy a context 
328  * \param context the context to destroy
329  * Argument must be stopped first -- runs in maestro context
330  */
331 static inline void SIMIX_context_free(smx_context_t context)
332 {
333   (*(simix_global->context_factory->free)) (context);
334 }
335
336 /**
337  * \brief prepares aa context to be run
338  * \param context the context to start
339  * It will however run effectively only when calling #SIMIX_process_schedule
340  */
341 static inline void SIMIX_context_start(smx_context_t context)
342 {
343   (*(simix_global->context_factory->start)) (context);
344 }
345
346 /**
347  * \brief stops the execution of a context
348  * \param context to stop
349  */
350 static inline void SIMIX_context_stop(smx_context_t context)
351 {
352   (*(simix_global->context_factory->stop)) (context);
353 }
354
355 /**
356  \brief resumes the execution of a context
357  \param old_context the actual context from which is resuming
358  \param new_context the context to resume
359  */
360 static inline void SIMIX_context_resume(smx_context_t old_context,
361                                         smx_context_t new_context)
362 {
363   (*(simix_global->context_factory->resume)) (old_context, new_context);
364 }
365
366 /**
367  \brief suspends a context and return the control back to the one which
368         scheduled it
369  \param context the context to be suspended (it must be the running one)
370  */
371 static inline void SIMIX_context_suspend(smx_context_t context)
372 {
373   (*(simix_global->context_factory->suspend)) (context);
374 }
375
376 #endif