Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] Headers cleanup
[simgrid.git] / include / simgrid / s4u / conditionVariable.hpp
index d5b9244..0530406 100644 (file)
@@ -6,9 +6,15 @@
 #ifndef SIMGRID_S4U_COND_VARIABLE_HPP
 #define SIMGRID_S4U_COND_VARIABLE_HPP
 
+#include <condition_variable>
+#include <future>
 #include <mutex>
 #include <utility> // std::swap
 
+#include <boost/intrusive_ptr.hpp>
+
+#include <xbt/base.h>
+
 #include <simgrid/simix.h>
 #include <simgrid/s4u/mutex.hpp>
 
@@ -17,6 +23,12 @@ namespace s4u {
 
 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:
@@ -47,36 +59,42 @@ 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(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
-  */
-  void notify();
+  // Notify functions
+
+  void notify_one();
   void notify_all();
 
+  XBT_ATTRIB_DEPRECATED("Use notify_one() instead")
+  void notify() { notify_one(); }
+
 private:
   smx_cond_t cond_;