$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r84421 - in trunk/boost/thread: . detail
From: vicente.botet_at_[hidden]
Date: 2013-05-22 13:18:24
Author: viboes
Date: 2013-05-22 13:18:23 EDT (Wed, 22 May 2013)
New Revision: 84421
URL: http://svn.boost.org/trac/boost/changeset/84421
Log:
Thread: update latch to use generation to prevent spurious wake-ups.
Text files modified: 
   trunk/boost/thread/detail/counter.hpp |    21 +++++++++++++---                        
   trunk/boost/thread/latch.hpp          |    51 +++++++++++++++++++++++++++++---------- 
   2 files changed, 55 insertions(+), 17 deletions(-)
Modified: trunk/boost/thread/detail/counter.hpp
==============================================================================
--- trunk/boost/thread/detail/counter.hpp	(original)
+++ trunk/boost/thread/detail/counter.hpp	2013-05-22 13:18:23 EDT (Wed, 22 May 2013)
@@ -75,15 +75,28 @@
     };
     struct counter_is_not_zero
     {
-      counter_is_not_zero(const counter& count) : count_(count) {}
+      counter_is_not_zero(counter const& count) : count_(count) {}
       bool operator()() const { return count_ != 0; }
-      const counter& count_;
+      counter const& count_;
     };
     struct counter_is_zero
     {
-      counter_is_zero(const counter& count) : count_(count) {}
+      counter_is_zero(counter const& count) : count_(count) {}
       bool operator()() const { return count_ == 0; }
-      const counter& count_;
+      counter const& count_;
+    };
+    struct is_zero
+    {
+      is_zero(std::size_t& count) : count_(count) {}
+      bool operator()() const { return count_ == 0; }
+      std::size_t& count_;
+    };
+    struct not_equal
+    {
+      not_equal(std::size_t& x, std::size_t& y) : x_(x), y_(y) {}
+      bool operator()() const { return x_ != y_; }
+      std::size_t& x_;
+      std::size_t& y_;
     };
   }
 } // namespace boost
Modified: trunk/boost/thread/latch.hpp
==============================================================================
--- trunk/boost/thread/latch.hpp	(original)
+++ trunk/boost/thread/latch.hpp	2013-05-22 13:18:23 EDT (Wed, 22 May 2013)
@@ -23,28 +23,40 @@
 {
   class latch
   {
-    /// @Requires: count_.value_ must be greater than 0
-    /// Effect: Decrement the count. Unlocks the lock notify anyone waiting if we reached zero.
-    /// Returns: true if count_.value_ reached the value 0.
+    /// @Requires: count_ must be greater than 0
+    /// Effect: Decrement the count. Unlocks the lock and notify anyone waiting if we reached zero.
+    /// Returns: true if count_ reached the value 0.
     /// @ThreadSafe ensured by the @c lk parameter
     bool count_down(unique_lock<mutex> &lk)
-    /// pre_condition (count_.value_ > 0)
+    /// pre_condition (count_ > 0)
     {
-      BOOST_ASSERT(count_.value_ > 0);
-      if (--count_.value_ == 0)
+      BOOST_ASSERT(count_ > 0);
+      if (--count_ == 0)
       {
-        count_.cond_.notify_all();
+        ++generation_;
         lk.unlock();
+        cond_.notify_all();
         return true;
       }
       return false;
     }
+    /// Effect: Decrement the count is > 0. Unlocks the lock notify anyone waiting if we reached zero.
+    /// Returns: true if count_ is 0.
+    /// @ThreadSafe ensured by the @c lk parameter
+    bool try_count_down(unique_lock<mutex> &lk)
+    {
+      if (count_ > 0)
+      {
+        return count_down(lk);
+      }
+      return true;
+    }
   public:
     BOOST_THREAD_NO_COPYABLE( latch)
 
     /// Constructs a latch with a given count.
     latch(std::size_t count) :
-      count_(count)
+      count_(count), generation_(0)
     {
     }
 
@@ -60,7 +72,8 @@
     void wait()
     {
       boost::unique_lock<boost::mutex> lk(mutex_);
-      count_.cond_.wait(lk, detail::counter_is_zero(count_));
+      std::size_t generation(generation_);
+      cond_.wait(lk, detail::not_equal(generation, generation_));
     }
 
     /// @return true if the internal counter is already 0, false otherwise
@@ -76,7 +89,8 @@
     cv_status wait_for(const chrono::duration<Rep, Period>& rel_time)
     {
       boost::unique_lock<boost::mutex> lk(mutex_);
-      return count_.cond_.wait_for(lk, rel_time, detail::counter_is_zero(count_))
+      std::size_t generation(generation_);
+      return cond_.wait_for(lk, rel_time, detail::not_equal(generation, generation_))
               ? cv_status::no_timeout
               : cv_status::timeout;
     }
@@ -87,7 +101,8 @@
     cv_status wait_until(const chrono::time_point<Clock, Duration>& abs_time)
     {
       boost::unique_lock<boost::mutex> lk(mutex_);
-      return count_.cond_.wait_until(lk, abs_time, detail::counter_is_zero(count_))
+      std::size_t generation(generation_);
+      return cond_.wait_until(lk, abs_time, detail::not_equal(generation, generation_))
           ? cv_status::no_timeout
           : cv_status::timeout;
     }
@@ -99,6 +114,13 @@
       boost::unique_lock<boost::mutex> lk(mutex_);
       count_down(lk);
     }
+    /// Effect: Decrement the count if it is > 0 and notify anyone waiting if we reached zero.
+    /// Returns: true if count_ was 0 or reached 0.
+    bool try_count_down()
+    {
+      boost::unique_lock<boost::mutex> lk(mutex_);
+      return try_count_down(lk);
+    }
     void signal()
     {
       count_down();
@@ -110,11 +132,12 @@
     void count_down_and_wait()
     {
       boost::unique_lock<boost::mutex> lk(mutex_);
+      std::size_t generation(generation_);
       if (count_down(lk))
       {
         return;
       }
-      count_.cond_.wait(lk, detail::counter_is_zero(count_));
+      cond_.wait(lk, detail::not_equal(generation, generation_));
     }
     void sync()
     {
@@ -132,7 +155,9 @@
 
   private:
     mutex mutex_;
-    detail::counter count_;
+    condition_variable cond_;
+    std::size_t count_;
+    std::size_t generation_;
   };
 
 } // namespace boost