Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] ConditionVariable, implement missing wait methods
authorGabriel Corona <gabriel.corona@loria.fr>
Tue, 21 Jun 2016 09:30:24 +0000 (11:30 +0200)
committerGabriel Corona <gabriel.corona@loria.fr>
Tue, 21 Jun 2016 09:35:24 +0000 (11:35 +0200)
include/simgrid/s4u/conditionVariable.hpp
src/s4u/s4u_conditionVariable.cpp

index 252a4f1..2858244 100644 (file)
@@ -48,28 +48,36 @@ public:
     return cond_ != nullptr;
   }
   
     return cond_ != nullptr;
   }
   
-  /**
-  * Wait functions
-  */
+  //  Wait functions:
+
   void wait(std::unique_lock<Mutex>& lock);
   void wait(std::unique_lock<Mutex>& lock);
-  // TODO, return std::cv_status
+  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);
   std::cv_status wait_for(std::unique_lock<Mutex>& lock, double duration);
-  // TODO, wait_until
 
 
-  /** Variant which takes a predice */
+  // Variants which takes a predicate:
+
   template<class P>
   void wait(std::unique_lock<Mutex>& lock, P pred)
   {
     while (!pred())
       wait(lock);
   }
   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));
+  }
 
 
-  // TODO, return std::cv_status
-  // TODO,wait_until
+  // Notify functions
 
 
-  /**
-  * Notify functions
-  */
   void notify();
   void notify_all();
 
   void notify();
   void notify_all();
 
index bb367af..28c9fc0 100644 (file)
@@ -22,29 +22,43 @@ s4u::ConditionVariable::~ConditionVariable() {
 void s4u::ConditionVariable::wait(std::unique_lock<Mutex>& lock) {
   simcall_cond_wait(cond_, lock.mutex()->mutex_);
 }
 void s4u::ConditionVariable::wait(std::unique_lock<Mutex>& lock) {
   simcall_cond_wait(cond_, lock.mutex()->mutex_);
 }
-  
+
 std::cv_status s4u::ConditionVariable::wait_for(std::unique_lock<Mutex>& lock, double timeout) {
   try {
     simcall_cond_wait_timeout(cond_, lock.mutex()->mutex_, timeout);
     return std::cv_status::timeout;
   }
   catch (xbt_ex& e) {
 std::cv_status s4u::ConditionVariable::wait_for(std::unique_lock<Mutex>& lock, double timeout) {
   try {
     simcall_cond_wait_timeout(cond_, lock.mutex()->mutex_, timeout);
     return std::cv_status::timeout;
   }
   catch (xbt_ex& e) {
+
+    // If the exception was a timeout, we have to take the lock again:
     if (e.category == timeout_error) {
     if (e.category == timeout_error) {
-      // We have to take the lock:
       try {
         lock.mutex()->lock();
       try {
         lock.mutex()->lock();
+        return std::cv_status::timeout;
       }
       catch (...) {
         std::terminate();
       }
       }
       catch (...) {
         std::terminate();
       }
-      return std::cv_status::timeout;
     }
     }
+
+    // Another exception: should we reaquire the lock?
     std::terminate();
   }
   catch (...) {
     std::terminate();
   }
 }
     std::terminate();
   }
   catch (...) {
     std::terminate();
   }
 }
+
+std::cv_status s4u::ConditionVariable::wait_until(std::unique_lock<Mutex>& lock, double timeout_time)
+{
+  double now = SIMIX_get_clock();
+  double timeout;
+  if (timeout_time < now)
+    timeout = 0.0;
+  else
+    timeout = timeout_time - now;
+  return this->wait_for(lock, timeout);
+}
   
 /**
  * Notify functions
   
 /**
  * Notify functions