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. */
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
14 "Logging specific to SIMIX (synchronization)");
17 /****************************** Synchronization *******************************/
19 /*********************************** Mutex ************************************/
22 * \brief Initialize a mutex.
24 * Allocs and creates the data for the mutex. It have to be called before the utilisation of the mutex.
27 smx_mutex_t SIMIX_mutex_init()
29 smx_mutex_t m = xbt_new0(s_smx_mutex_t,1);
30 s_smx_process_t p; /* useful to initialize sleeping swag */
31 /* structures initialization */
33 m->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
38 * \brief Locks a mutex.
40 * Tries to lock a mutex, if the mutex isn't used yet, the process can continue its execution, else it'll be blocked here. You have to call #SIMIX_mutex_unlock to free the mutex.
41 * \param mutex The mutex
43 void SIMIX_mutex_lock(smx_mutex_t mutex)
45 smx_process_t self = SIMIX_process_self();
46 xbt_assert0((mutex != NULL), "Invalid parameters");
50 /* somebody using the mutex, block */
51 xbt_swag_insert(self, mutex->sleeping);
52 self->simdata->mutex = mutex;
53 /* wait for some process make the unlock and wake up me from mutex->sleeping */
55 self->simdata->mutex = NULL;
57 /* verify if the process was suspended */
58 while (self->simdata->suspended) {
72 * \brief Tries to lock a mutex.
74 * Tries to lock a mutex, return 1 if the mutex is free, 0 else. This function does not block the process if the mutex is used.
75 * \param mutex The mutex
76 * \return 1 - mutex free, 0 - mutex used
78 int SIMIX_mutex_trylock(smx_mutex_t mutex)
80 xbt_assert0((mutex != NULL), "Invalid parameters");
91 * \brief Unlocks a mutex.
93 * Unlocks the mutex and wakes up a process blocked on it. If there are no process sleeping, only sets the mutex as free.
94 * \param mutex The mutex
96 void SIMIX_mutex_unlock(smx_mutex_t mutex)
98 smx_process_t p; /*process to wake up */
100 xbt_assert0((mutex != NULL), "Invalid parameters");
102 if (xbt_swag_size(mutex->sleeping) > 0) {
103 p = xbt_swag_extract(mutex->sleeping);
105 xbt_swag_insert(p, simix_global->process_to_run);
108 /* nobody to wake up */
115 * \brief Destroys a mutex.
117 * Destroys and frees the mutex's memory.
118 * \param mutex A mutex
120 void SIMIX_mutex_destroy(smx_mutex_t mutex)
125 xbt_swag_free(mutex->sleeping);
131 /******************************** Conditional *********************************/
134 * \brief Initialize a condition.
136 * Allocs and creates the data for the condition. It have to be called before the utilisation of the condition.
137 * \return A condition
139 smx_cond_t SIMIX_cond_init()
141 smx_cond_t cond = xbt_new0(s_smx_cond_t,1);
144 cond->sleeping = xbt_swag_new(xbt_swag_offset(p,synchro_hookup));
145 cond->actions = xbt_fifo_new();
151 * \brief Signalizes a condition.
153 * Signalizes a condition and wakes up a sleping process. If there are no process sleeping, no action is done.
154 * \param cond A condition
156 void SIMIX_cond_signal(smx_cond_t cond)
158 xbt_assert0((cond != NULL), "Invalid parameters");
159 smx_process_t proc = NULL;
161 if (xbt_swag_size(cond->sleeping) >= 1) {
162 proc = xbt_swag_extract(cond->sleeping);
163 xbt_swag_insert(proc, simix_global->process_to_run);
170 * \brief Waits on a condition.
172 * Blocks a process until the signal is called. This functions frees the mutex associated and locks it after its execution.
173 * \param cond A condition
174 * \param mutex A mutex
176 void SIMIX_cond_wait(smx_cond_t cond,smx_mutex_t mutex)
178 smx_action_t act_sleep;
179 xbt_assert0((mutex != NULL), "Invalid parameters");
183 SIMIX_mutex_unlock(mutex);
184 /* create an action null only if there are no actions already on the condition, usefull if the host crashs */
185 if (xbt_fifo_size(cond->actions) ==0 ) {
186 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), -1);
187 SIMIX_register_action_to_condition(act_sleep,cond);
188 SIMIX_register_condition_to_action(act_sleep,cond);
189 __SIMIX_cond_wait(cond);
190 xbt_fifo_pop(act_sleep->cond_list);
191 SIMIX_action_destroy(act_sleep);
194 __SIMIX_cond_wait(cond);
196 /* get the mutex again */
197 SIMIX_mutex_lock(cond->mutex);
202 xbt_fifo_t SIMIX_cond_get_actions(smx_cond_t cond)
204 xbt_assert0((cond != NULL), "Invalid parameters");
205 return cond->actions;
208 void __SIMIX_cond_wait(smx_cond_t cond)
210 smx_process_t self = SIMIX_process_self();
211 xbt_assert0((cond != NULL), "Invalid parameters");
215 self->simdata->cond = cond;
216 xbt_swag_insert(self, cond->sleeping);
218 self->simdata->cond = NULL;
219 while (self->simdata->suspended) {
227 * \brief Waits on a condition with timeout.
229 * Same behavior of #SIMIX_cond_wait, but waits a maximum time and throws an timeout_error if it happens.
230 * \param cond A condition
231 * \param mutex A mutex
232 * \param max_duration Timeout time
234 void SIMIX_cond_wait_timeout(smx_cond_t cond,smx_mutex_t mutex, double max_duration)
236 xbt_assert0((mutex != NULL), "Invalid parameters");
237 smx_action_t act_sleep;
241 SIMIX_mutex_unlock(mutex);
242 if (max_duration >=0) {
243 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), max_duration);
244 SIMIX_register_action_to_condition(act_sleep,cond);
245 SIMIX_register_condition_to_action(act_sleep,cond);
246 __SIMIX_cond_wait(cond);
247 xbt_fifo_remove(act_sleep->cond_list,cond);
248 if ( SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
249 SIMIX_action_destroy(act_sleep);
250 THROW0(timeout_error,0,"Condition timeout");
253 SIMIX_action_destroy(act_sleep);
258 __SIMIX_cond_wait(cond);
260 /* get the mutex again */
261 SIMIX_mutex_lock(cond->mutex);
267 * \brief Broadcasts a condition.
269 * Signalizes a condition and wakes up ALL sleping process. If there are no process sleeping, no action is done.
270 * \param cond A condition
272 void SIMIX_cond_broadcast(smx_cond_t cond)
274 xbt_assert0((cond != NULL), "Invalid parameters");
275 smx_process_t proc = NULL;
276 smx_process_t proc_next = NULL;
278 xbt_swag_foreach_safe(proc,proc_next,cond->sleeping) {
279 xbt_swag_remove(proc,cond->sleeping);
280 xbt_swag_insert(proc, simix_global->process_to_run);
287 * \brief Destroys a contidion.
289 * Destroys and frees the condition's memory.
290 * \param cond A condition
292 void SIMIX_cond_destroy(smx_cond_t cond)
297 xbt_assert0( xbt_swag_size(cond->sleeping) == 0 , "Cannot destroy conditional");
298 xbt_swag_free(cond->sleeping);
299 xbt_fifo_free(cond->actions);
306 * \brief Set a condition to an action
308 * Creates the "link" between an action and a condition. You have to call this function when you create an action and want to wait its ending.
309 * \param action SIMIX action
310 * \param cond SIMIX cond
312 void SIMIX_register_condition_to_action(smx_action_t action, smx_cond_t cond)
314 xbt_assert0( (action != NULL) && (cond != NULL), "Invalid parameters");
316 xbt_fifo_push(action->cond_list,cond);