#ifndef SIMGRID_S4U_COND_VARIABLE_HPP
#define SIMGRID_S4U_COND_VARIABLE_HPP
+#include <condition_variable>
#include <mutex>
#include <utility> // std::swap
class Mutex;
+/** A condition variable
+ *
+ * This is based on std::condition_variable and should respect the same
+ * semantic. But we currently use (only) double for both durations and
+ * timestamp timeouts.
+ */
XBT_PUBLIC_CLASS ConditionVariable {
public:
return cond_ != nullptr;
}
- /**
- * Wait functions
- */
+ // Wait functions:
+
void wait(std::unique_lock<Mutex>& lock);
- void wait_for(std::unique_lock<Mutex>& lock, double time);
+ std::cv_status wait_until(std::unique_lock<Mutex>& lock, double timeout_time);
+ std::cv_status wait_for(std::unique_lock<Mutex>& lock, double duration);
+
+ // Variants which takes a predicate:
+
+ template<class P>
+ void wait(std::unique_lock<Mutex>& lock, P pred)
+ {
+ while (!pred())
+ wait(lock);
+ }
+ template<class P>
+ bool wait_until(std::unique_lock<Mutex>& lock, double timeout_time, P pred)
+ {
+ while (!pred())
+ if (this->wait_until(lock, timeout_time) == std::cv_status::timeout)
+ return pred();
+ return true;
+ }
+ template<class P>
+ bool wait_for(std::unique_lock<Mutex>& lock, double duration, P pred)
+ {
+ return this->wait_until(lock, SIMIX_get_clock() + duration, std::move(pred));
+ }
+
+ // Notify functions
- /**
- * Notify functions
- */
void notify();
void notify_all();