+/**
+ * \brief Waits on a condition with timeout.
+ *
+ * Same behavior of #SIMIX_cond_wait, but waits a maximum time and throws an timeout_error if it happens.
+ * \param cond A condition
+ * \param mutex A mutex
+ * \param max_duration Timeout time
+ */
+void SIMIX_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex,
+ double max_duration)
+{
+ xbt_assert0((mutex != NULL), "Invalid parameters");
+ smx_action_t act_sleep;
+
+ DEBUG1("Timed wait condition %p", cond);
+ cond->mutex = mutex;
+
+ SIMIX_mutex_unlock(mutex);
+ if (max_duration >= 0) {
+ act_sleep = SIMIX_action_sleep(SIMIX_host_self(), max_duration);
+ SIMIX_register_action_to_condition(act_sleep, cond);
+ __SIMIX_cond_wait(cond);
+ SIMIX_unregister_action_to_condition(act_sleep, cond);
+ if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
+ SIMIX_action_destroy(act_sleep);
+ THROW0(timeout_error, 0, "Condition timeout");
+ } else {
+ SIMIX_action_destroy(act_sleep);
+ }
+
+ } else
+ __SIMIX_cond_wait(cond);
+
+ /* get the mutex again */
+ SIMIX_mutex_lock(cond->mutex);
+
+ return;