@@ -34,19 +34,14 @@ template <typename T> class Queue {
34
34
size_t amount () const ; // amount
35
35
void push (T element);
36
36
optional<T> pop ();
37
- optional<T> tryPop ();
38
37
optional<T> peek ();
39
38
optional<T> exchange (T element);
40
- bool wait (const optional<std::chrono::milliseconds> &duration = nullopt);
41
39
42
40
private:
43
- void pushImpl (T element);
44
- optional<T> popImpl ();
45
-
46
41
const size_t mLimit ;
47
42
size_t mAmount ;
48
43
std::queue<T> mQueue ;
49
- std::condition_variable mPopCondition , mPushCondition ;
44
+ std::condition_variable mPushCondition ;
50
45
amount_function mAmountFunction ;
51
46
bool mStopping = false ;
52
47
@@ -66,7 +61,6 @@ template <typename T> Queue<T>::~Queue() { stop(); }
66
61
template <typename T> void Queue<T>::stop() {
67
62
std::lock_guard lock (mMutex );
68
63
mStopping = true ;
69
- mPopCondition .notify_all ();
70
64
mPushCondition .notify_all ();
71
65
}
72
66
@@ -98,18 +92,22 @@ template <typename T> size_t Queue<T>::amount() const {
98
92
template <typename T> void Queue<T>::push(T element) {
99
93
std::unique_lock lock (mMutex );
100
94
mPushCondition .wait (lock, [this ]() { return !mLimit || mQueue .size () < mLimit || mStopping ; });
101
- pushImpl (std::move (element));
95
+ if (mStopping )
96
+ return ;
97
+
98
+ mAmount += mAmountFunction (element);
99
+ mQueue .emplace (std::move (element));
102
100
}
103
101
104
102
template <typename T> optional<T> Queue<T>::pop() {
105
103
std::unique_lock lock (mMutex );
106
- mPopCondition .wait (lock, [this ]() { return !mQueue .empty () || mStopping ; });
107
- return popImpl ();
108
- }
104
+ if (mQueue .empty ())
105
+ return nullopt;
109
106
110
- template <typename T> optional<T> Queue<T>::tryPop() {
111
- std::unique_lock lock (mMutex );
112
- return popImpl ();
107
+ mAmount -= mAmountFunction (mQueue .front ());
108
+ optional<T> element{std::move (mQueue .front ())};
109
+ mQueue .pop ();
110
+ return element;
113
111
}
114
112
115
113
template <typename T> optional<T> Queue<T>::peek() {
@@ -126,36 +124,6 @@ template <typename T> optional<T> Queue<T>::exchange(T element) {
126
124
return std::make_optional (std::move (element));
127
125
}
128
126
129
- template <typename T> bool Queue<T>::wait(const optional<std::chrono::milliseconds> &duration) {
130
- std::unique_lock lock (mMutex );
131
- if (duration) {
132
- return mPopCondition .wait_for (lock, *duration,
133
- [this ]() { return !mQueue .empty () || mStopping ; });
134
- } else {
135
- mPopCondition .wait (lock, [this ]() { return !mQueue .empty () || mStopping ; });
136
- return true ;
137
- }
138
- }
139
-
140
- template <typename T> void Queue<T>::pushImpl(T element) {
141
- if (mStopping )
142
- return ;
143
-
144
- mAmount += mAmountFunction (element);
145
- mQueue .emplace (std::move (element));
146
- mPopCondition .notify_one ();
147
- }
148
-
149
- template <typename T> optional<T> Queue<T>::popImpl() {
150
- if (mQueue .empty ())
151
- return nullopt;
152
-
153
- mAmount -= mAmountFunction (mQueue .front ());
154
- optional<T> element{std::move (mQueue .front ())};
155
- mQueue .pop ();
156
- return element;
157
- }
158
-
159
127
} // namespace rtc::impl
160
128
161
129
#endif
0 commit comments