Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] ConditionVariable, implement missing wait methods
[simgrid.git] / include / simgrid / s4u / conditionVariable.hpp
index ce06ef0..2858244 100644 (file)
@@ -6,6 +6,7 @@
 #ifndef SIMGRID_S4U_COND_VARIABLE_HPP
 #define SIMGRID_S4U_COND_VARIABLE_HPP
 
+#include <condition_variable>
 #include <mutex>
 #include <utility> // std::swap
 
@@ -47,33 +48,36 @@ public:
     return cond_ != nullptr;
   }
   
-  /**
-  * Wait functions
-  */
+  //  Wait functions:
+
   void wait(std::unique_lock<Mutex>& lock);
-  // TODO, return std::cv_status
-  void wait_for(std::unique_lock<Mutex>& lock, double duration);
-  // TODO, wait_until
+  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:
 
-  /** Variant which takes a predice */
   template<class P>
   void wait(std::unique_lock<Mutex>& lock, P pred)
   {
     while (!pred())
       wait(lock);
   }
-  // TODO, return std::cv_status
   template<class P>
-  void wait_for(std::unique_lock<Mutex>& lock, double duration, P pred)
+  bool wait_until(std::unique_lock<Mutex>& lock, double timeout_time, P pred)
   {
     while (!pred())
-      wait_for(lock, duration);
+      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));
   }
-  // TODO,wait_until
 
-  /**
-  * Notify functions
-  */
+  // Notify functions
+
   void notify();
   void notify_all();