3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donnassolo.
\r
4 All rights reserved. */
\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
9 #ifndef SIMIX_PRIVATE_H
\r
10 #define SIMIX_PRIVATE_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
20 /******************************** Datatypes ***********************************/
\r
23 /*********************************** Host *************************************/
\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
34 /********************************** Simix Global ******************************/
\r
36 typedef struct s_smx_context_factory *smx_context_factory_t;
\r
38 typedef struct SIMIX_Global {
\r
39 smx_context_factory_t context_factory;
\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
52 extern SIMIX_Global_t simix_global;
\r
54 /******************************** Process *************************************/
\r
56 typedef struct s_smx_context *smx_context_t;
\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
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
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
84 typedef struct s_smx_process_arg {
\r
86 xbt_main_func_t code;
\r
92 xbt_dict_t properties;
\r
93 } s_smx_process_arg_t, *smx_process_arg_t;
\r
95 void SIMIX_process_empty_trash(void);
\r
97 /*************************** Mutex and Conditional ****************************/
\r
99 typedef struct s_smx_mutex {
\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
104 /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_mutex */
\r
108 typedef struct s_smx_cond {
\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
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
118 /********************************* Action *************************************/
\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
131 /************************** Configuration support *****************************/
\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
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
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
148 /******************************** Context *************************************/
\r
150 void SIMIX_context_mod_init(void);
\r
152 void SIMIX_context_mod_exit(void);
\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
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
166 /* all other context types derive from this structure */
\r
167 typedef struct s_smx_context {
\r
171 /* *********************** */
\r
172 /* factory type definition */
\r
173 /* *********************** */
\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
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
184 * The following function pointer types describe the interface that any context
\r
185 * factory should implement.
\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
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
194 /* this function finalize the specified context factory */
\r
195 typedef int (*smx_pfn_context_factory_finalize_t) (smx_context_factory_t*);
\r
197 /* function used to destroy the specified context */
\r
198 typedef void (*smx_pfn_context_free_t) (smx_process_t);
\r
200 /* function used to kill the specified context */
\r
201 typedef void (*smx_pfn_context_kill_t) (smx_process_t);
\r
203 /* function used to resume the specified context */
\r
204 typedef void (*smx_pfn_context_schedule_t) (smx_process_t);
\r
206 /* function used to yield the specified context */
\r
207 typedef void (*smx_pfn_context_yield_t) (void);
\r
209 /* function used to start the specified context */
\r
210 typedef void (*smx_pfn_context_start_t) (smx_process_t);
\r
212 /* function used to stop the current context */
\r
213 typedef void (*smx_pfn_context_stop_t) (int);
\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
227 } s_smx_context_factory_t;
\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
232 int SIMIX_context_select_factory(const char *name);
\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
237 void SIMIX_context_init_factory_by_name(smx_context_factory_t * factory, const char *name);
\r
239 /* All factories init */
\r
240 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory);
\r
242 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t * factory);
\r
244 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory);
\r
246 /* ******************************* */
\r
247 /* contexts manipulation functions */
\r
248 /* ******************************* */
\r
251 * \param smx_process the simix maestro process that contains this context
\r
253 static inline int SIMIX_context_create_maestro(smx_process_t *process)
\r
255 return (*(simix_global->context_factory->create_maestro_context)) (process);
\r
259 * \param smx_process the simix process that contains this context
\r
260 * \param code a main function
\r
262 static inline int SIMIX_context_new(smx_process_t *process, xbt_main_func_t code)
\r
264 return (*(simix_global->context_factory->create_context)) (process, code);
\r
267 /* Scenario for the end of a context:
\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
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
287 static inline void SIMIX_context_kill(smx_process_t process)
\r
289 (*(simix_global->context_factory->kill)) (process);
\r
292 /* Argument must be stopped first -- runs in maestro context */
\r
293 static inline void SIMIX_context_free(smx_process_t process)
\r
295 (*(simix_global->context_factory->free)) (process);
\r
299 * \param context the context to start
\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
304 static inline void SIMIX_context_start(smx_process_t process)
\r
306 (*(simix_global->context_factory->start)) (process);
\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
314 * Only the processes can call this function, giving back the control
\r
317 static inline void SIMIX_context_yield(void)
\r
319 (*(simix_global->context_factory->yield)) ();
\r
323 * \param process to be scheduled
\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
329 * Only the maestro can call this function to run a given process.
\r
331 static inline void SIMIX_context_schedule(smx_process_t process)
\r
333 (*(simix_global->context_factory->schedule)) (process);
\r
336 static inline void SIMIX_context_stop(int exit_code)
\r
338 (*(simix_global->context_factory->stop)) (exit_code);
\r