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 XBT_INLINE 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 XBT_INLINE void SIMIX_mutex_destroy(smx_mutex_t mutex)
123 xbt_swag_free(mutex->sleeping);
129 /******************************** Conditional *********************************/
132 * \brief Initialize a condition.
134 * Allocates and creates the data for the condition.
135 * It have to be called before the use of the condition.
136 * \return A condition
138 smx_cond_t SIMIX_cond_init()
140 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
143 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
144 cond->actions = xbt_fifo_new();
150 * \brief Signalizes a condition.
152 * Signalizes a condition and wakes up a sleeping process. If there are no process sleeping, no action is done.
153 * \param cond A condition
155 void SIMIX_cond_signal(smx_cond_t cond)
157 smx_process_t proc = NULL;
158 DEBUG1("Signal condition %p", cond);
159 xbt_assert0((cond != NULL), "Invalid parameters");
162 if (xbt_swag_size(cond->sleeping) >= 1) {
163 proc = xbt_swag_extract(cond->sleeping);
164 xbt_swag_insert(proc, simix_global->process_to_run);
171 * \brief Waits on a condition.
173 * Blocks a process until the signal is called. This functions frees the mutex associated and locks it after its execution.
174 * \param cond A condition
175 * \param mutex A mutex
177 void SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
179 smx_action_t act_sleep;
181 DEBUG1("Wait condition %p", cond);
183 /* If there is a mutex unlock it */
186 SIMIX_mutex_unlock(mutex);
189 /* Always create an action null in case there is a host failure */
190 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), -1);
191 SIMIX_action_set_name(act_sleep,bprintf("Wait condition %p", cond));
192 SIMIX_process_self()->waiting_action = act_sleep;
193 SIMIX_register_action_to_condition(act_sleep, cond);
194 __SIMIX_cond_wait(cond);
195 SIMIX_process_self()->waiting_action = NULL;
196 SIMIX_unregister_action_to_condition(act_sleep, cond);
197 SIMIX_action_destroy(act_sleep);
199 /* get the mutex again if necessary */
201 SIMIX_mutex_lock(cond->mutex);
206 XBT_INLINE xbt_fifo_t SIMIX_cond_get_actions(smx_cond_t cond)
208 xbt_assert0((cond != NULL), "Invalid parameters");
209 return cond->actions;
212 void __SIMIX_cond_wait(smx_cond_t cond)
214 smx_process_t self = SIMIX_process_self();
215 xbt_assert0((cond != NULL), "Invalid parameters");
220 xbt_swag_insert(self, cond->sleeping);
221 SIMIX_process_yield();
223 while (self->suspended) {
224 SIMIX_process_yield();
230 * \brief Waits on a condition with timeout.
232 * Same behavior of #SIMIX_cond_wait, but waits a maximum time and throws an timeout_error if it happens.
233 * \param cond A condition
234 * \param mutex A mutex
235 * \param max_duration Timeout time
237 void SIMIX_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex,
240 smx_action_t act_sleep;
242 DEBUG1("Timed wait condition %p", cond);
244 /* If there is a mutex unlock it */
247 SIMIX_mutex_unlock(mutex);
250 if (max_duration >= 0) {
251 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), max_duration);
252 SIMIX_action_set_name(act_sleep,bprintf("Timed wait condition %p (max_duration:%f)", cond,max_duration));
253 SIMIX_register_action_to_condition(act_sleep, cond);
254 SIMIX_process_self()->waiting_action = act_sleep;
255 __SIMIX_cond_wait(cond);
256 SIMIX_process_self()->waiting_action = NULL;
257 SIMIX_unregister_action_to_condition(act_sleep, cond);
258 if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
259 SIMIX_action_destroy(act_sleep);
260 THROW1(timeout_error, 0, "Condition timeout after %f",max_duration);
262 SIMIX_action_destroy(act_sleep);
266 SIMIX_cond_wait(cond,NULL);
268 /* get the mutex again if necessary */
270 SIMIX_mutex_lock(cond->mutex);
274 * \brief Broadcasts a condition.
276 * Signalizes a condition and wakes up ALL sleping process. If there are no process sleeping, no action is done.
277 * \param cond A condition
279 void SIMIX_cond_broadcast(smx_cond_t cond)
281 smx_process_t proc = NULL;
282 smx_process_t proc_next = NULL;
284 xbt_assert0((cond != NULL), "Invalid parameters");
286 DEBUG1("Broadcast condition %p", cond);
287 xbt_swag_foreach_safe(proc, proc_next, cond->sleeping) {
288 xbt_swag_remove(proc, cond->sleeping);
289 xbt_swag_insert(proc, simix_global->process_to_run);
294 * \brief Destroys a contidion.
296 * Destroys and frees the condition's memory.
297 * \param cond A condition
299 void SIMIX_cond_destroy(smx_cond_t cond)
301 DEBUG1("Destroy condition %p", cond);
305 xbt_fifo_item_t item = NULL;
306 smx_action_t action = NULL;
308 xbt_assert0(xbt_swag_size(cond->sleeping) == 0,
309 "Cannot destroy conditional since someone is still using it");
310 xbt_swag_free(cond->sleeping);
312 DEBUG1("%d actions registered", xbt_fifo_size(cond->actions));
313 __SIMIX_cond_display_actions(cond);
314 xbt_fifo_foreach(cond->actions, item, action, smx_action_t) {
315 SIMIX_unregister_action_to_condition(action, cond);
317 __SIMIX_cond_display_actions(cond);
319 xbt_fifo_free(cond->actions);
325 void SIMIX_cond_display_info(smx_cond_t cond)
330 smx_process_t process = NULL;
332 INFO0("Blocked process on this condition:");
333 xbt_swag_foreach(process, cond->sleeping) {
334 INFO2("\t %s running on host %s", process->name,
335 process->smx_host->name);
340 /* ************************** Semaphores ************************************** */
341 #define SMX_SEM_NOLIMIT 99999
342 /** @brief Initialize a semaphore */
343 smx_sem_t SIMIX_sem_init(int capacity) {
344 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
347 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
348 sem->actions = xbt_fifo_new();
349 sem->capacity = capacity;
352 /** @brief Destroys a semaphore */
353 void SIMIX_sem_destroy(smx_sem_t sem) {
354 DEBUG1("Destroy semaphore %p", sem);
358 xbt_fifo_item_t item = NULL;
359 smx_action_t action = NULL;
361 xbt_assert0(xbt_swag_size(sem->sleeping) == 0,
362 "Cannot destroy semaphore since someone is still using it");
363 xbt_swag_free(sem->sleeping);
365 DEBUG1("%d actions registered", xbt_fifo_size(sem->actions));
366 xbt_fifo_foreach(sem->actions, item, action, smx_action_t) {
367 SIMIX_unregister_action_to_semaphore(action, sem);
370 xbt_fifo_free(sem->actions);
374 /** @brief release the semaphore
376 * The first locked process on this semaphore is unlocked.
377 * If no one was blocked, the semaphore capacity is increased by 1.
379 void SIMIX_sem_release(smx_sem_t sem) {
380 if (xbt_swag_size(sem->sleeping) >= 1) {
381 smx_process_t proc = xbt_swag_extract(sem->sleeping);
382 xbt_swag_insert(proc, simix_global->process_to_run);
383 } else if (sem->capacity != SMX_SEM_NOLIMIT) {
387 /** @brief make sure the semaphore will never be blocking again
389 * This function is not really in the semaphore spirit. It makes
390 * sure that the semaphore will never be blocking anymore.
392 * Releasing and acquiring the semaphore after calling this
393 * function is a noop. Such "broken" semaphores are useful to
394 * implement something between condition variables (with broadcast)
395 * and semaphore (with memory). It's like a semaphore signaled for ever.
397 * There is no way to reset the semaphore to a more regular state afterward.
399 void SIMIX_sem_release_forever(smx_sem_t sem) {
400 smx_process_t proc = NULL;
401 smx_process_t proc_next = NULL;
403 DEBUG1("Broadcast semaphore %p", sem);
404 xbt_swag_foreach_safe(proc, proc_next, sem->sleeping) {
405 xbt_swag_remove(proc, sem->sleeping);
406 xbt_swag_insert(proc, simix_global->process_to_run);
410 static inline void SIMIX_sem_block_onto(smx_sem_t sem) {
411 smx_process_t self = SIMIX_process_self();
415 xbt_swag_insert(self, sem->sleeping);
416 SIMIX_process_yield();
418 while (self->suspended)
419 SIMIX_process_yield();
422 /** @brief Returns true if acquiring this semaphore would block */
423 XBT_INLINE int SIMIX_sem_would_block(smx_sem_t sem) {
424 return (sem->capacity>0);
428 * \brief Waits on a semaphore
430 * If the capacity>0, decrease the capacity.
432 * If capacity==0, locks the current process
433 * until someone call SIMIX_sem_release() on this semaphore
435 void SIMIX_sem_acquire(smx_sem_t sem) {
436 smx_action_t act_sleep;
438 DEBUG1("Wait semaphore %p", sem);
440 if (sem->capacity == SMX_SEM_NOLIMIT)
441 return; /* don't even decrease it if wide open */
443 /* If capacity sufficient, decrease it */
444 if (sem->capacity>0) {
449 /* Always create an action null in case there is a host failure */
450 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), -1);
451 SIMIX_action_set_name(act_sleep,bprintf("Locked in semaphore %p", sem));
452 SIMIX_process_self()->waiting_action = act_sleep;
453 SIMIX_register_action_to_semaphore(act_sleep, sem);
454 SIMIX_sem_block_onto(sem);
455 SIMIX_process_self()->waiting_action = NULL;
456 SIMIX_unregister_action_to_semaphore(act_sleep, sem);
457 SIMIX_action_destroy(act_sleep);
460 * \brief Tries to acquire a semaphore before a timeout
462 * Same behavior of #SIMIX_sem_acquire, but waits a maximum time and throws an timeout_error if it happens.
464 void SIMIX_sem_acquire_timeout(smx_sem_t sem, double max_duration) {
465 smx_action_t act_sleep;
467 DEBUG2("Timed wait semaphore %p (timeout:%f)", sem,max_duration);
469 if (sem->capacity == SMX_SEM_NOLIMIT)
470 return; /* don't even decrease it if wide open */
472 /* If capacity sufficient, decrease it */
473 if (sem->capacity>0) {
478 if (max_duration >= 0) {
479 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), max_duration);
480 SIMIX_action_set_name(act_sleep,bprintf("Timed wait semaphore %p (max_duration:%f)", sem,max_duration));
481 SIMIX_register_action_to_semaphore(act_sleep, sem);
482 SIMIX_process_self()->waiting_action = act_sleep;
483 SIMIX_sem_block_onto(sem);
484 SIMIX_process_self()->waiting_action = NULL;
485 SIMIX_unregister_action_to_semaphore(act_sleep, sem);
486 if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
487 SIMIX_action_destroy(act_sleep);
488 THROW1(timeout_error, 0, "Semaphore acquire timeouted after %f",max_duration);
490 SIMIX_action_destroy(act_sleep);
494 SIMIX_sem_acquire(sem);
497 * \brief Blocks on a set of semaphore
499 * If any of the semaphores has some more capacity, it gets decreased.
500 * If not, blocks until the capacity of one of the semaphores becomes more friendly.
502 * \return the rank in the dynar of the semaphore which just got locked from the set
504 unsigned int SIMIX_sem_acquire_any(xbt_dynar_t sems) {
506 unsigned int counter,result=-1;
507 smx_action_t act_sleep;
508 smx_process_t self = SIMIX_process_self();
510 xbt_assert0(xbt_dynar_length(sems),
511 "I refuse to commit sucide by locking on an **empty** set of semaphores!!");
512 DEBUG1("Wait on semaphore set %p", sems);
514 xbt_dynar_foreach(sems,counter,sem) {
515 if (!SIMIX_sem_would_block(sem))
516 SIMIX_sem_acquire(sem);
520 /* Always create an action null in case there is a host failure */
521 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), -1);
522 SIMIX_action_set_name(act_sleep,bprintf("Locked in semaphore %p", sem));
523 self->waiting_action = act_sleep;
524 SIMIX_register_action_to_semaphore(act_sleep, xbt_dynar_get_as(sems,0,smx_sem_t));
526 /* Get listed as member of all the provided semaphores */
527 self->sem = (smx_sem_t)sems; /* FIXME: we pass a pointer to dynar where a pointer to sem is expected...*/
528 xbt_dynar_foreach(sems,counter,sem) {
529 xbt_swag_insert(self, sem->sleeping);
531 SIMIX_process_yield();
532 while (self->suspended)
533 SIMIX_process_yield();
535 /* one of the semaphore unsuspended us -- great, let's search which one (and get out of the others) */
537 xbt_dynar_foreach(sems,counter,sem) {
538 if (xbt_swag_belongs(self,sem->sleeping))
539 xbt_swag_remove(self,sem->sleeping);
541 xbt_assert0(result==-1,"More than one semaphore unlocked us. Dunno what to do");
545 xbt_assert0(counter!=-1,"Cannot find which semaphore unlocked me!");
547 /* Destroy the waiting action */
548 self->waiting_action = NULL;
549 SIMIX_unregister_action_to_semaphore(act_sleep, xbt_dynar_get_as(sems,0,smx_sem_t));
550 SIMIX_action_destroy(act_sleep);