1 // Distributed under the Boost Software License, Version 1.0. (See
2 // accompanying file LICENSE_1_0.txt or copy at
3 // http://www.boost.org/LICENSE_1_0.txt)
4 // (C) Copyright 2013 Vicente J. Botet Escriba
6 #ifndef BOOST_THREAD_LATCH_HPP
7 #define BOOST_THREAD_LATCH_HPP
9 #include <boost/thread/detail/config.hpp>
10 #include <boost/thread/detail/delete.hpp>
11 #include <boost/thread/detail/counter.hpp>
13 #include <boost/thread/mutex.hpp>
14 #include <boost/thread/lock_types.hpp>
15 #include <boost/thread/condition_variable.hpp>
16 #include <boost/chrono/duration.hpp>
17 #include <boost/chrono/time_point.hpp>
18 #include <boost/assert.hpp>
20 #include <boost/config/abi_prefix.hpp>
26 /// @Requires: count_ must be greater than 0
27 /// Effect: Decrement the count. Unlocks the lock and notify anyone waiting if we reached zero.
28 /// Returns: true if count_ reached the value 0.
29 /// @ThreadSafe ensured by the @c lk parameter
30 bool count_down(unique_lock<mutex> &)
31 /// pre_condition (count_ > 0)
33 BOOST_ASSERT(count_ > 0);
43 /// Effect: Decrement the count is > 0. Unlocks the lock notify anyone waiting if we reached zero.
44 /// Returns: true if count_ is 0.
45 /// @ThreadSafe ensured by the @c lk parameter
46 bool try_count_down(unique_lock<mutex> &lk)
50 return count_down(lk);
55 BOOST_THREAD_NO_COPYABLE( latch)
57 /// Constructs a latch with a given count.
58 latch(std::size_t count) :
59 count_(count), generation_(0)
64 /// Precondition: No threads are waiting or invoking count_down on @c *this.
71 /// Blocks until the latch has counted down to zero.
74 boost::unique_lock<boost::mutex> lk(mutex_);
75 if (count_ == 0) return;
76 std::size_t generation(generation_);
77 cond_.wait(lk, detail::not_equal(generation, generation_));
80 /// @return true if the internal counter is already 0, false otherwise
83 boost::unique_lock<boost::mutex> lk(mutex_);
87 /// try to wait for a specified amount of time is elapsed.
88 /// @return whether there is a timeout or not.
89 template <class Rep, class Period>
90 cv_status wait_for(const chrono::duration<Rep, Period>& rel_time)
92 boost::unique_lock<boost::mutex> lk(mutex_);
93 if (count_ == 0) return cv_status::no_timeout;
94 std::size_t generation(generation_);
95 return cond_.wait_for(lk, rel_time, detail::not_equal(generation, generation_))
96 ? cv_status::no_timeout
100 /// try to wait until the specified time_point is reached
101 /// @return whether there were a timeout or not.
102 template <class Clock, class Duration>
103 cv_status wait_until(const chrono::time_point<Clock, Duration>& abs_time)
105 boost::unique_lock<boost::mutex> lk(mutex_);
106 if (count_ == 0) return cv_status::no_timeout;
107 std::size_t generation(generation_);
108 return cond_.wait_until(lk, abs_time, detail::not_equal(generation, generation_))
109 ? cv_status::no_timeout
110 : cv_status::timeout;
113 /// Decrement the count and notify anyone waiting if we reach zero.
114 /// @Requires count must be greater than 0
117 boost::unique_lock<boost::mutex> lk(mutex_);
120 /// Effect: Decrement the count if it is > 0 and notify anyone waiting if we reached zero.
121 /// Returns: true if count_ was 0 or reached 0.
122 bool try_count_down()
124 boost::unique_lock<boost::mutex> lk(mutex_);
125 return try_count_down(lk);
132 /// Decrement the count and notify anyone waiting if we reach zero.
133 /// Blocks until the latch has counted down to zero.
134 /// @Requires count must be greater than 0
135 void count_down_and_wait()
137 boost::unique_lock<boost::mutex> lk(mutex_);
138 std::size_t generation(generation_);
143 cond_.wait(lk, detail::not_equal(generation, generation_));
147 count_down_and_wait();
150 /// Reset the counter
151 /// #Requires This method may only be invoked when there are no other threads currently inside the count_down_and_wait() method.
152 void reset(std::size_t count)
154 boost::lock_guard<boost::mutex> lk(mutex_);
155 //BOOST_ASSERT(count_ == 0);
161 condition_variable cond_;
163 std::size_t generation_;
168 #include <boost/config/abi_suffix.hpp>