3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donnassolo.
4 All rights reserved. */
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. */
9 #ifndef SIMIX_PRIVATE_H
10 #define SIMIX_PRIVATE_H
13 #include "simix/simix.h"
14 #include "surf/surf.h"
18 #include "xbt/config.h"
19 #include "xbt/function_types.h"
21 /******************************** Datatypes ***********************************/
24 /*********************************** Host *************************************/
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 */
35 /********************************** Simix Global ******************************/
37 typedef struct s_smx_context_factory *smx_context_factory_t;
39 typedef struct SIMIX_Global {
40 smx_context_factory_t context_factory;
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;
53 extern SIMIX_Global_t simix_global;
55 /******************************** Process *************************************/
57 typedef struct s_smx_context *smx_context_t;
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;
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 */
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 xbt_dict_t properties;
76 void *data; /* kept for compatibility, it should be replaced with moddata */
81 typedef struct s_smx_process_arg {
89 xbt_dict_t properties;
90 } s_smx_process_arg_t, *smx_process_arg_t;
92 void SIMIX_process_empty_trash(void);
93 void __SIMIX_process_schedule(smx_process_t process);
94 void __SIMIX_process_yield(void);
96 /*************************** Mutex and Conditional ****************************/
98 typedef struct s_smx_mutex {
100 /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_mutex */
101 xbt_swag_t sleeping; /* list of sleeping process */
103 /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_mutex */
107 typedef struct s_smx_cond {
109 /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_cond */
110 xbt_swag_t sleeping; /* list of sleeping process */
112 xbt_fifo_t actions; /* list of actions */
113 /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_cond */
117 /********************************* Action *************************************/
119 /** @brief Action datatype
120 @ingroup m_datatypes_management_details */
121 typedef struct s_smx_action {
122 char *name; /**< @brief action name if any */
123 xbt_fifo_t cond_list; /*< conditional variables that must be signaled when the action finish. */
124 void *data; /**< @brief user data */
125 int refcount; /**< @brief reference counter */
126 surf_action_t surf_action; /* SURF modeling of computation */
130 /************************** Configuration support *****************************/
132 extern int _simix_init_status; /* 0: beginning of time; FIXME: KILLME ?
133 1: pre-inited (cfg_set created);
134 2: inited (running) */
136 #define SIMIX_CHECK_HOST() xbt_assert0(surf_workstation_model->extension.workstation. \
137 get_state(SIMIX_host_self()->host)==SURF_RESOURCE_ON,\
138 "Host failed, you cannot call this function.")
140 smx_host_t __SIMIX_host_create(const char *name, void *workstation, void *data);
141 void __SIMIX_host_destroy(void *host);
142 void __SIMIX_cond_wait(smx_cond_t cond);
143 void __SIMIX_cond_display_actions(smx_cond_t cond);
144 void __SIMIX_action_display_conditions(smx_action_t action);
145 void __SIMIX_create_maestro_process(void);
147 /******************************** Context *************************************/
149 void SIMIX_context_mod_init(void);
151 void SIMIX_context_mod_exit(void);
153 /* *********************** */
154 /* Context type definition */
155 /* *********************** */
156 /* the following function pointers types describe the interface that all context
157 concepts must implement */
159 /* each context type must contain this macro at its begining -- OOP in C :/ */
160 #define SMX_CTX_BASE_T \
161 s_xbt_swag_hookup_t hookup; \
162 ex_ctx_t *exception; \
163 xbt_main_func_t code; \
166 void_f_pvoid_t cleanup_func; \
169 /* all other context types derive from this structure */
170 typedef struct s_smx_context {
174 /* *********************** */
175 /* factory type definition */
176 /* *********************** */
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.
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.
187 * The following function pointer types describe the interface that any context
188 * factory should implement.
191 /* function used to create a new context */
192 typedef smx_context_t (*smx_pfn_context_factory_create_context_t)
193 (xbt_main_func_t, int, char**, void_f_pvoid_t, void*);
195 /* function used to create the context for the maestro process */
196 typedef smx_context_t (*smx_pfn_context_factory_create_maestro_context_t) (void);
198 /* this function finalize the specified context factory */
199 typedef int (*smx_pfn_context_factory_finalize_t) (smx_context_factory_t*);
201 /* function used to destroy the specified context */
202 typedef void (*smx_pfn_context_free_t) (smx_context_t);
204 /* function used to start the specified context */
205 typedef void (*smx_pfn_context_start_t) (smx_context_t);
207 /* function used to stop the current context */
208 typedef void (*smx_pfn_context_stop_t) (smx_context_t);
210 /* function used to suspend the current context */
211 typedef void (*smx_pfn_context_suspend_t) (smx_context_t context);
213 /* function used to resume the current context */
214 typedef void (*smx_pfn_context_resume_t) (smx_context_t old_context,
215 smx_context_t new_context);
217 /* interface of the context factories */
218 typedef struct s_smx_context_factory {
219 smx_pfn_context_factory_create_maestro_context_t create_maestro_context;
220 smx_pfn_context_factory_create_context_t create_context;
221 smx_pfn_context_factory_finalize_t finalize;
222 smx_pfn_context_free_t free;
223 smx_pfn_context_start_t start;
224 smx_pfn_context_stop_t stop;
225 smx_pfn_context_suspend_t suspend;
226 smx_pfn_context_resume_t resume;
228 } s_smx_context_factory_t;
230 /* Selects a context factory associated with the name specified by the parameter name.
231 * If successful the function returns 0. Otherwise the function returns the error code.
233 int SIMIX_context_select_factory(const char *name);
235 /* Initializes a context factory from the name specified by the parameter name.
236 * If the factory cannot be found, an exception is raised.
238 void SIMIX_context_init_factory_by_name(smx_context_factory_t * factory, const char *name);
240 /* All factories init */
241 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory);
243 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t * factory);
245 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory);
247 /* ****************************** */
248 /* context manipulation functions */
249 /* ****************************** */
251 /* Scenario for the end of a context:
253 * CASE 1: death after end of the main function
254 * the context_wrapper, called internally by the context module, calls
255 * SIMIX_context_stop after user code stops, smx_context_stop calls user
256 * cleanup_func if any (in context settings), add current process to trashbin
257 * and yields back to maestro.
258 * From time to time, maestro calls SIMIX_context_empty_trash, which destroy
259 * all the process and context data structures, and frees the memory
261 * CASE 2: brutal death
262 * SIMIX_process_kill (from any process) set process->iwannadie = 1 and then
263 * schedules the process. Then the process is awaken in the middle of the
264 * SIMIX_process_yield function, and at the end of it, it checks that
265 * iwannadie == 1, and call SIMIX_context_stop(same than first case afterward)
269 * \brief creates the context for the maestro process
271 static inline smx_context_t SIMIX_context_create_maestro()
273 return (*(simix_global->context_factory->create_maestro_context)) ();
277 * \brief creates a new context for a user level process
278 * \param code a main function
279 * \param argc the number of arguments of the main function
280 * \param argv the vector of arguments of the main function
281 * \param cleanup_func the function to call when the context stops
282 * \param cleanup_arg the argument of the cleanup_func function
284 static inline smx_context_t SIMIX_context_new(xbt_main_func_t code, int argc,
286 void_f_pvoid_t cleanup_func,
289 return (*(simix_global->context_factory->create_context))
290 (code, argc, argv, cleanup_func, cleanup_arg);
294 * \brief destroy a context
295 * \param context the context to destroy
296 * Argument must be stopped first -- runs in maestro context
298 static inline void SIMIX_context_free(smx_context_t context)
300 (*(simix_global->context_factory->free)) (context);
304 * \brief prepares aa context to be run
305 * \param context the context to start
306 * It will however run effectively only when calling #SIMIX_process_schedule
308 static inline void SIMIX_context_start(smx_context_t context)
310 (*(simix_global->context_factory->start)) (context);
314 * \brief stops the execution of a context
315 * \param context to stop
317 static inline void SIMIX_context_stop(smx_context_t context)
319 (*(simix_global->context_factory->stop)) (context);
323 \brief resumes the execution of a context
324 \param old_context the actual context from which is resuming
325 \param new_context the context to resume
327 static inline void SIMIX_context_resume(smx_context_t old_context,
328 smx_context_t new_context)
330 (*(simix_global->context_factory->resume)) (old_context, new_context);
334 \brief suspends a context and return the control back to the one which
336 \param context the context to be suspended (it must be the running one)
338 static inline void SIMIX_context_suspend(smx_context_t context)
340 (*(simix_global->context_factory->suspend)) (context);