+ smx_process_t self = SIMIX_process_self();
+ xbt_assert0((cond != NULL), "Invalid parameters");
+
+ /* process status */
+
+ self->cond = cond;
+ xbt_swag_insert(self, cond->sleeping);
+ SIMIX_process_yield();
+ self->cond = NULL;
+ while (self->suspended) {
+ SIMIX_process_yield();
+ }
+ return;
+}
+
+/**
+ * \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)
+{
+ smx_action_t act_sleep;
+
+ DEBUG1("Timed wait condition %p", cond);
+
+ /* If there is a mutex unlock it */
+ if (mutex != NULL) {
+ cond->mutex = mutex;
+ SIMIX_mutex_unlock(mutex);
+ }
+
+ if (max_duration >= 0) {
+ act_sleep = SIMIX_action_sleep(SIMIX_host_self(), max_duration);
+ SIMIX_action_set_name(act_sleep,
+ bprintf
+ ("Timed wait condition %p (max_duration:%f)",
+ cond, max_duration));
+ SIMIX_register_action_to_condition(act_sleep, cond);
+ SIMIX_process_self()->waiting_action = act_sleep;
+ __SIMIX_cond_wait(cond);
+ SIMIX_process_self()->waiting_action = NULL;
+ SIMIX_unregister_action_to_condition(act_sleep, cond);
+ if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
+ SIMIX_action_destroy(act_sleep);
+ THROW1(timeout_error, 0, "Condition timeout after %f", max_duration);
+ } else {
+ SIMIX_action_destroy(act_sleep);
+ }
+
+ } else
+ SIMIX_cond_wait(cond, NULL);
+
+ /* get the mutex again if necessary */
+ if (mutex != NULL)
+ SIMIX_mutex_lock(cond->mutex);
+}
+
+/**
+ * \brief Broadcasts a condition.
+ *
+ * Signalizes a condition and wakes up ALL sleping process. If there are no process sleeping, no action is done.
+ * \param cond A condition
+ */
+void SIMIX_cond_broadcast(smx_cond_t cond)
+{
+ smx_process_t proc = NULL;
+ smx_process_t proc_next = NULL;
+
+ xbt_assert0((cond != NULL), "Invalid parameters");
+
+ DEBUG1("Broadcast condition %p", cond);
+ xbt_swag_foreach_safe(proc, proc_next, cond->sleeping) {
+ xbt_swag_remove(proc, cond->sleeping);
+ xbt_swag_insert(proc, simix_global->process_to_run);
+ }
+}
+
+/**
+ * \brief Destroys a contidion.
+ *
+ * Destroys and frees the condition's memory.
+ * \param cond A condition
+ */
+void SIMIX_cond_destroy(smx_cond_t cond)
+{
+ DEBUG1("Destroy condition %p", cond);
+ if (cond == NULL)
+ return;
+ else {
+ xbt_fifo_item_t item = NULL;
+ smx_action_t action = NULL;
+
+ xbt_assert0(xbt_swag_size(cond->sleeping) == 0,
+ "Cannot destroy conditional since someone is still using it");
+ xbt_swag_free(cond->sleeping);
+
+ DEBUG1("%d actions registered", xbt_fifo_size(cond->actions));
+ __SIMIX_cond_display_actions(cond);
+ xbt_fifo_foreach(cond->actions, item, action, smx_action_t) {
+ SIMIX_unregister_action_to_condition(action, cond);
+ }
+ __SIMIX_cond_display_actions(cond);
+
+ xbt_fifo_free(cond->actions);
+ xbt_free(cond);
+ return;
+ }
+}