Chris@16
|
1 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
2 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
3 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
4 // (C) Copyright 2013 Vicente J. Botet Escriba
|
Chris@16
|
5
|
Chris@16
|
6 #ifndef BOOST_THREAD_LATCH_HPP
|
Chris@16
|
7 #define BOOST_THREAD_LATCH_HPP
|
Chris@16
|
8
|
Chris@16
|
9 #include <boost/thread/detail/config.hpp>
|
Chris@16
|
10 #include <boost/thread/detail/delete.hpp>
|
Chris@16
|
11 #include <boost/thread/detail/counter.hpp>
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/thread/mutex.hpp>
|
Chris@16
|
14 #include <boost/thread/lock_types.hpp>
|
Chris@16
|
15 #include <boost/thread/condition_variable.hpp>
|
Chris@16
|
16 #include <boost/chrono/duration.hpp>
|
Chris@16
|
17 #include <boost/chrono/time_point.hpp>
|
Chris@16
|
18 #include <boost/assert.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #include <boost/config/abi_prefix.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 namespace boost
|
Chris@16
|
23 {
|
Chris@16
|
24 class latch
|
Chris@16
|
25 {
|
Chris@16
|
26 /// @Requires: count_ must be greater than 0
|
Chris@16
|
27 /// Effect: Decrement the count. Unlocks the lock and notify anyone waiting if we reached zero.
|
Chris@16
|
28 /// Returns: true if count_ reached the value 0.
|
Chris@16
|
29 /// @ThreadSafe ensured by the @c lk parameter
|
Chris@101
|
30 bool count_down(unique_lock<mutex> &)
|
Chris@16
|
31 /// pre_condition (count_ > 0)
|
Chris@16
|
32 {
|
Chris@16
|
33 BOOST_ASSERT(count_ > 0);
|
Chris@16
|
34 if (--count_ == 0)
|
Chris@16
|
35 {
|
Chris@16
|
36 ++generation_;
|
Chris@101
|
37 //lk.unlock();
|
Chris@16
|
38 cond_.notify_all();
|
Chris@16
|
39 return true;
|
Chris@16
|
40 }
|
Chris@16
|
41 return false;
|
Chris@16
|
42 }
|
Chris@16
|
43 /// Effect: Decrement the count is > 0. Unlocks the lock notify anyone waiting if we reached zero.
|
Chris@16
|
44 /// Returns: true if count_ is 0.
|
Chris@16
|
45 /// @ThreadSafe ensured by the @c lk parameter
|
Chris@16
|
46 bool try_count_down(unique_lock<mutex> &lk)
|
Chris@16
|
47 {
|
Chris@16
|
48 if (count_ > 0)
|
Chris@16
|
49 {
|
Chris@16
|
50 return count_down(lk);
|
Chris@16
|
51 }
|
Chris@16
|
52 return true;
|
Chris@16
|
53 }
|
Chris@16
|
54 public:
|
Chris@16
|
55 BOOST_THREAD_NO_COPYABLE( latch)
|
Chris@16
|
56
|
Chris@16
|
57 /// Constructs a latch with a given count.
|
Chris@16
|
58 latch(std::size_t count) :
|
Chris@16
|
59 count_(count), generation_(0)
|
Chris@16
|
60 {
|
Chris@16
|
61 }
|
Chris@16
|
62
|
Chris@16
|
63 /// Destructor
|
Chris@16
|
64 /// Precondition: No threads are waiting or invoking count_down on @c *this.
|
Chris@16
|
65
|
Chris@16
|
66 ~latch()
|
Chris@16
|
67 {
|
Chris@16
|
68
|
Chris@16
|
69 }
|
Chris@16
|
70
|
Chris@16
|
71 /// Blocks until the latch has counted down to zero.
|
Chris@16
|
72 void wait()
|
Chris@16
|
73 {
|
Chris@16
|
74 boost::unique_lock<boost::mutex> lk(mutex_);
|
Chris@16
|
75 std::size_t generation(generation_);
|
Chris@16
|
76 cond_.wait(lk, detail::not_equal(generation, generation_));
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 /// @return true if the internal counter is already 0, false otherwise
|
Chris@16
|
80 bool try_wait()
|
Chris@16
|
81 {
|
Chris@16
|
82 boost::unique_lock<boost::mutex> lk(mutex_);
|
Chris@16
|
83 return (count_ == 0);
|
Chris@16
|
84 }
|
Chris@16
|
85
|
Chris@16
|
86 /// try to wait for a specified amount of time is elapsed.
|
Chris@16
|
87 /// @return whether there is a timeout or not.
|
Chris@16
|
88 template <class Rep, class Period>
|
Chris@16
|
89 cv_status wait_for(const chrono::duration<Rep, Period>& rel_time)
|
Chris@16
|
90 {
|
Chris@16
|
91 boost::unique_lock<boost::mutex> lk(mutex_);
|
Chris@16
|
92 std::size_t generation(generation_);
|
Chris@16
|
93 return cond_.wait_for(lk, rel_time, detail::not_equal(generation, generation_))
|
Chris@16
|
94 ? cv_status::no_timeout
|
Chris@16
|
95 : cv_status::timeout;
|
Chris@16
|
96 }
|
Chris@16
|
97
|
Chris@16
|
98 /// try to wait until the specified time_point is reached
|
Chris@16
|
99 /// @return whether there were a timeout or not.
|
Chris@16
|
100 template <class Clock, class Duration>
|
Chris@16
|
101 cv_status wait_until(const chrono::time_point<Clock, Duration>& abs_time)
|
Chris@16
|
102 {
|
Chris@16
|
103 boost::unique_lock<boost::mutex> lk(mutex_);
|
Chris@16
|
104 std::size_t generation(generation_);
|
Chris@16
|
105 return cond_.wait_until(lk, abs_time, detail::not_equal(generation, generation_))
|
Chris@16
|
106 ? cv_status::no_timeout
|
Chris@16
|
107 : cv_status::timeout;
|
Chris@16
|
108 }
|
Chris@16
|
109
|
Chris@16
|
110 /// Decrement the count and notify anyone waiting if we reach zero.
|
Chris@16
|
111 /// @Requires count must be greater than 0
|
Chris@16
|
112 void count_down()
|
Chris@16
|
113 {
|
Chris@16
|
114 boost::unique_lock<boost::mutex> lk(mutex_);
|
Chris@16
|
115 count_down(lk);
|
Chris@16
|
116 }
|
Chris@16
|
117 /// Effect: Decrement the count if it is > 0 and notify anyone waiting if we reached zero.
|
Chris@16
|
118 /// Returns: true if count_ was 0 or reached 0.
|
Chris@16
|
119 bool try_count_down()
|
Chris@16
|
120 {
|
Chris@16
|
121 boost::unique_lock<boost::mutex> lk(mutex_);
|
Chris@16
|
122 return try_count_down(lk);
|
Chris@16
|
123 }
|
Chris@16
|
124 void signal()
|
Chris@16
|
125 {
|
Chris@16
|
126 count_down();
|
Chris@16
|
127 }
|
Chris@16
|
128
|
Chris@16
|
129 /// Decrement the count and notify anyone waiting if we reach zero.
|
Chris@16
|
130 /// Blocks until the latch has counted down to zero.
|
Chris@16
|
131 /// @Requires count must be greater than 0
|
Chris@16
|
132 void count_down_and_wait()
|
Chris@16
|
133 {
|
Chris@16
|
134 boost::unique_lock<boost::mutex> lk(mutex_);
|
Chris@16
|
135 std::size_t generation(generation_);
|
Chris@16
|
136 if (count_down(lk))
|
Chris@16
|
137 {
|
Chris@16
|
138 return;
|
Chris@16
|
139 }
|
Chris@16
|
140 cond_.wait(lk, detail::not_equal(generation, generation_));
|
Chris@16
|
141 }
|
Chris@16
|
142 void sync()
|
Chris@16
|
143 {
|
Chris@16
|
144 count_down_and_wait();
|
Chris@16
|
145 }
|
Chris@16
|
146
|
Chris@16
|
147 /// Reset the counter
|
Chris@16
|
148 /// #Requires This method may only be invoked when there are no other threads currently inside the count_down_and_wait() method.
|
Chris@16
|
149 void reset(std::size_t count)
|
Chris@16
|
150 {
|
Chris@16
|
151 boost::lock_guard<boost::mutex> lk(mutex_);
|
Chris@16
|
152 //BOOST_ASSERT(count_ == 0);
|
Chris@16
|
153 count_ = count;
|
Chris@16
|
154 }
|
Chris@16
|
155
|
Chris@16
|
156 private:
|
Chris@16
|
157 mutex mutex_;
|
Chris@16
|
158 condition_variable cond_;
|
Chris@16
|
159 std::size_t count_;
|
Chris@16
|
160 std::size_t generation_;
|
Chris@16
|
161 };
|
Chris@16
|
162
|
Chris@16
|
163 } // namespace boost
|
Chris@16
|
164
|
Chris@16
|
165 #include <boost/config/abi_suffix.hpp>
|
Chris@16
|
166
|
Chris@16
|
167 #endif
|