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");
49 if (mutex->refcount) {
50 /* somebody using the mutex, block */
51 xbt_swag_insert(self, mutex->sleeping);
53 /* wait for some process make the unlock and wake up me from mutex->sleeping */
54 SIMIX_process_yield();
57 /* verify if the process was suspended */
58 while (self->suspended) {
59 SIMIX_process_yield();
71 * \brief Tries to lock a mutex.
73 * 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.
74 * \param mutex The mutex
75 * \return 1 - mutex free, 0 - mutex used
77 int SIMIX_mutex_trylock(smx_mutex_t mutex)
79 xbt_assert0((mutex != NULL), "Invalid parameters");
90 * \brief Unlocks a mutex.
92 * Unlocks the mutex and wakes up a process blocked on it. If there are no process sleeping, only sets the mutex as free.
93 * \param mutex The mutex
95 void SIMIX_mutex_unlock(smx_mutex_t mutex)
97 smx_process_t p; /*process to wake up */
99 xbt_assert0((mutex != NULL), "Invalid parameters");
101 if (xbt_swag_size(mutex->sleeping) > 0) {
102 p = xbt_swag_extract(mutex->sleeping);
104 xbt_swag_insert(p, simix_global->process_to_run);
106 /* nobody to wake up */
113 * \brief Destroys a mutex.
115 * Destroys and frees the mutex's memory.
116 * \param mutex A mutex
118 void SIMIX_mutex_destroy(smx_mutex_t mutex)
123 xbt_swag_free(mutex->sleeping);
129 /******************************** Conditional *********************************/
132 * \brief Initialize a condition.
134 * Allocs and creates the data for the condition. It have to be called before the utilisation of the condition.
135 * \return A condition
137 smx_cond_t SIMIX_cond_init()
139 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
142 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
143 cond->actions = xbt_fifo_new();
149 * \brief Signalizes a condition.
151 * Signalizes a condition and wakes up a sleeping process. If there are no process sleeping, no action is done.
152 * \param cond A condition
154 void SIMIX_cond_signal(smx_cond_t cond)
156 smx_process_t proc = NULL;
157 DEBUG1("Signal condition %p", cond);
158 xbt_assert0((cond != NULL), "Invalid parameters");
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;
180 DEBUG1("Wait condition %p", cond);
182 /* If there is a mutex unlock it */
185 SIMIX_mutex_unlock(mutex);
188 /* Always create an action null in case there is a host failure */
189 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), -1);
190 SIMIX_process_self()->waiting_action = act_sleep;
191 SIMIX_register_action_to_condition(act_sleep, cond);
192 __SIMIX_cond_wait(cond);
193 SIMIX_process_self()->waiting_action = NULL;
194 SIMIX_unregister_action_to_condition(act_sleep, cond);
195 SIMIX_action_destroy(act_sleep);
197 /* get the mutex again if necessary */
199 SIMIX_mutex_lock(cond->mutex);
204 xbt_fifo_t SIMIX_cond_get_actions(smx_cond_t cond)
206 xbt_assert0((cond != NULL), "Invalid parameters");
207 return cond->actions;
210 void __SIMIX_cond_wait(smx_cond_t cond)
212 smx_process_t self = SIMIX_process_self();
213 xbt_assert0((cond != NULL), "Invalid parameters");
218 xbt_swag_insert(self, cond->sleeping);
219 SIMIX_process_yield();
221 while (self->suspended) {
222 SIMIX_process_yield();
228 * \brief Waits on a condition with timeout.
230 * Same behavior of #SIMIX_cond_wait, but waits a maximum time and throws an timeout_error if it happens.
231 * \param cond A condition
232 * \param mutex A mutex
233 * \param max_duration Timeout time
235 void SIMIX_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex,
238 smx_action_t act_sleep;
240 DEBUG1("Timed wait condition %p", cond);
242 /* If there is a mutex unlock it */
245 SIMIX_mutex_unlock(mutex);
248 if (max_duration >= 0) {
249 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), max_duration);
250 SIMIX_register_action_to_condition(act_sleep, cond);
251 SIMIX_process_self()->waiting_action = act_sleep;
252 __SIMIX_cond_wait(cond);
253 SIMIX_process_self()->waiting_action = NULL;
254 SIMIX_unregister_action_to_condition(act_sleep, cond);
255 if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
256 SIMIX_action_destroy(act_sleep);
257 THROW0(timeout_error, 0, "Condition timeout");
259 SIMIX_action_destroy(act_sleep);
263 __SIMIX_cond_wait(cond);
265 /* get the mutex again if necessary */
267 SIMIX_mutex_lock(cond->mutex);
273 * \brief Broadcasts a condition.
275 * Signalizes a condition and wakes up ALL sleping process. If there are no process sleeping, no action is done.
276 * \param cond A condition
278 void SIMIX_cond_broadcast(smx_cond_t cond)
280 smx_process_t proc = NULL;
281 smx_process_t proc_next = NULL;
283 xbt_assert0((cond != NULL), "Invalid parameters");
285 DEBUG1("Broadcast condition %p", cond);
286 xbt_swag_foreach_safe(proc, proc_next, cond->sleeping) {
287 xbt_swag_remove(proc, cond->sleeping);
288 xbt_swag_insert(proc, simix_global->process_to_run);
295 * \brief Destroys a contidion.
297 * Destroys and frees the condition's memory.
298 * \param cond A condition
300 void SIMIX_cond_destroy(smx_cond_t cond)
302 DEBUG1("Destroy condition %p", cond);
306 xbt_fifo_item_t item = NULL;
307 smx_action_t action = NULL;
309 xbt_assert0(xbt_swag_size(cond->sleeping) == 0,
310 "Cannot destroy conditional since someone is still using it");
311 xbt_swag_free(cond->sleeping);
313 DEBUG1("%d actions registered", xbt_fifo_size(cond->actions));
314 __SIMIX_cond_display_actions(cond);
315 xbt_fifo_foreach(cond->actions, item, action, smx_action_t) {
316 SIMIX_unregister_action_to_condition(action, cond);
318 __SIMIX_cond_display_actions(cond);
320 xbt_fifo_free(cond->actions);
326 void SIMIX_cond_display_info(smx_cond_t cond)
331 smx_process_t process = NULL;
333 INFO0("Blocked process on this condition:");
334 xbt_swag_foreach(process, cond->sleeping) {
335 INFO2("\t %s running on host %s", process->name,
336 process->smx_host->name);