-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocking_queue.h
211 lines (172 loc) · 3.83 KB
/
blocking_queue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#ifndef blocking_queue_h
#define blocking_queue_h
#include <mutex>
#include <deque>
namespace estd
{
class semaphore
{
public:
semaphore(int count = 0)
: count_(count)
{ }
void signal();
void wait();
private:
std::mutex mtx_;
std::mutex sig_;
int count_;
};
void semaphore::signal()
{
std::unique_lock<std::mutex> lck(mtx_);
++count_;
sig_.unlock();
}
void semaphore::wait()
{
std::unique_lock<std::mutex> lck(mtx_);
while (count_ == 0)
{
sig_.lock();
}
--count_;
}
template<typename T>
class blocking_queue
{
public:
class consumer_iterator
{
public:
consumer_iterator(blocking_queue& bq, bool advance, bool end);
public:
consumer_iterator& operator++();
consumer_iterator& operator--();
T& operator*() { return value_; }
bool operator==(const consumer_iterator& it) const { return end_ == it.end_; }
bool operator!=(const consumer_iterator& it) const { return !(*this == it); }
void detach(bool enable = true) { detach_ = enable; }
bool is_detached() const { return detach_; }
private:
blocking_queue& bq_;
T value_;
bool detach_ = false;
bool end_;
};
public:
void push_back(const T&);
T pop_back();
void push_front(const T&);
T pop_front();
size_t size() const;
consumer_iterator begin() { return consumer_iterator { *this, true, false }; }
consumer_iterator end() { return consumer_iterator { *this, true, true }; }
consumer_iterator rbegin() { return consumer_iterator { *this, false, false }; }
consumer_iterator rend() { return end(); }
consumer_iterator begin_detached();
consumer_iterator rbegin_detached();
private:
std::deque<T> data_;
mutable std::mutex m_;
semaphore e_;
};
template<typename T>
inline void blocking_queue<T>::push_back(const T& t)
{
m_.lock();
data_.push_back(t);
m_.unlock();
e_.signal();
}
template<typename T>
inline void blocking_queue<T>::push_front(const T& t)
{
m_.lock();
data_.push_front(t);
m_.unlock();
e_.signal();
}
template<typename T>
inline T blocking_queue<T>::pop_back()
{
e_.wait();
m_.lock();
T t = data_.back();
data_.pop_back();
m_.unlock();
return t;
}
template<typename T>
inline T blocking_queue<T>::pop_front()
{
e_.wait();
m_.lock();
T t = data_.front();
data_.pop_front();
m_.unlock();
return t;
}
template<typename T>
inline size_t blocking_queue<T>::size() const
{
m_.lock();
size_t sz = data_.size();
m_.unlock();
return sz;
}
template<typename T>
inline typename blocking_queue<T>::consumer_iterator blocking_queue<T>::begin_detached()
{
auto bgn = begin();
bgn.detach();
return bgn;
}
template<typename T>
inline typename blocking_queue<T>::consumer_iterator blocking_queue<T>::rbegin_detached()
{
auto rbgn = rbegin();
rbgn.detach();
return rbgn;
}
template<typename T>
inline blocking_queue<T>::consumer_iterator::consumer_iterator(blocking_queue& bq, bool advance, bool end)
: bq_(bq), end_(end)
{
if (end)
{
return;
}
if (advance)
{
++*this;
}
else
{
--*this;
}
}
template<typename T>
inline typename blocking_queue<T>::consumer_iterator& blocking_queue<T>::consumer_iterator::operator++()
{
if (is_detached() && bq_.size() == 0)
{
end_ = true;
return *this;
}
value_ = bq_.pop_front();
return *this;
}
template<typename T>
inline typename blocking_queue<T>::consumer_iterator& blocking_queue<T>::consumer_iterator::operator--()
{
if (is_detached() && bq_.size() == 0)
{
end_ = true;
return *this;
}
value_ = bq_.pop_back();
return *this;
}
} // namespace estd
#endif