- if (max_duration >= 0) {
- 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);
+ /* at least one of the semaphore unsuspended us -- great, let's search the first one (and get out of the others) */
+ xbt_dynar_foreach(sems, counter, sem) {
+ if (!xbt_fifo_remove(sem->sleeping, self) && result == -1) {
+ if (sem->capacity != SMX_SEM_NOLIMIT) {
+ /* Take the released token */
+ sem->capacity--;
+ }
+ result = counter;