-/**
- * \brief Tries to acquire a semaphore before a timeout
- *
- * Same behavior of #SIMIX_sem_acquire, but waits a maximum time and throws an timeout_error if it happens.
- */
-void SIMIX_sem_acquire_timeout(smx_sem_t sem, double max_duration) {
- smx_action_t act_sleep;
-
- DEBUG2("Timed wait semaphore %p (timeout:%f)", sem,max_duration);
-
- if (sem->capacity == SMX_SEM_NOLIMIT)
- return; /* don't even decrease it if wide open */
-
- /* If capacity sufficient, decrease it */
- if (sem->capacity>0) {
- sem->capacity--;
- return;
- }
-
- if (max_duration >= 0) {
- sem->capacity--;
- act_sleep = SIMIX_action_sleep(SIMIX_host_self(), max_duration);
- SIMIX_action_set_name(act_sleep,bprintf("Timed wait semaphore %p (max_duration:%f)", sem,max_duration));
- SIMIX_register_action_to_semaphore(act_sleep, sem);
- SIMIX_process_self()->waiting_action = act_sleep;
- SIMIX_sem_block_onto(sem);
- SIMIX_process_self()->waiting_action = NULL;
- SIMIX_unregister_action_to_semaphore(act_sleep, sem);
- if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
- SIMIX_action_destroy(act_sleep);
- THROW1(timeout_error, 0, "Semaphore acquire timeouted after %f",max_duration);
- } else {
- SIMIX_action_destroy(act_sleep);
- }
- sem->capacity++;