-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathglobal_lock_impl.hpp
216 lines (186 loc) · 3.79 KB
/
global_lock_impl.hpp
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
212
213
214
215
216
#pragma once
#include <cassert>
#include <memory>
#include <mutex>
#include <iterator>
#include "macros.hpp"
/**
* Standard singly-linked list with a global lock for protection, and
* standard reference counting
*
* References returned by this implementation are guaranteed to be valid
* until the element is removed from the list
*/
template <typename T>
class global_lock_impl {
private:
struct node;
typedef std::unique_lock<std::mutex> unique_lock;
typedef std::shared_ptr<unique_lock> unique_lock_ptr;
typedef std::shared_ptr<node> node_ptr;
struct node {
// non-copyable
node(const node &) = delete;
node(node &&) = delete;
node &operator=(const node &) = delete;
node() : value_(), next_() {}
node(const T &value, const node_ptr &next)
: value_(value), next_(next) {}
T value_;
node_ptr next_;
};
mutable std::mutex mutex_;
node_ptr head_;
node_ptr tail_;
struct iterator_ : public std::iterator<std::forward_iterator_tag, T> {
iterator_() : lock_(), node_() {}
iterator_(const unique_lock_ptr &lock, const node_ptr &node)
: lock_(lock), node_(node) {}
iterator_(unique_lock_ptr &&lock, const node_ptr &node)
: lock_(std::move(lock)), node_(node) {}
T &
operator*() const
{
return node_->value_;
}
T *
operator->() const
{
return &node_->value_;
}
bool
operator==(const iterator_ &o) const
{
return node_ == o.node_;
}
bool
operator!=(const iterator_ &o) const
{
return !operator==(o);
}
iterator_ &
operator++()
{
node_ = node_->next_;
return *this;
}
iterator_
operator++(int)
{
iterator_ cur = *this;
++(*this);
return cur;
}
unique_lock_ptr lock_;
node_ptr node_;
};
public:
typedef iterator_ iterator;
global_lock_impl() : mutex_(), head_(), tail_() {}
size_t
size() const
{
unique_lock l(mutex_);
size_t ret = 0;
node_ptr cur = head_;
while (cur) {
ret++;
cur = cur->next_;
}
return ret;
}
inline T &
front()
{
unique_lock l(mutex_);
assert(head_);
return head_->value_;
}
inline const T &
front() const
{
return const_cast<global_lock_impl *>(this)->front();
}
inline T &
back()
{
unique_lock l(mutex_);
assert(tail_);
assert(!tail_->next_);
return tail_->value_;
}
inline const T &
back() const
{
return const_cast<global_lock_impl *>(this)->back();
}
void
pop_front()
{
unique_lock l(mutex_);
assert(head_);
node_ptr next = head_->next_;
head_ = next;
if (!head_)
tail_.reset();
}
void
push_back(const T &val)
{
unique_lock l(mutex_);
node_ptr n(std::make_shared<node>(val, nullptr));
if (!tail_) {
assert(!head_);
head_ = tail_ = n;
} else {
tail_->next_ = n;
tail_ = n;
}
}
inline void
remove(const T &val)
{
unique_lock l(mutex_);
node_ptr prev;
node_ptr p = head_, *pp = &head_;
while (p) {
if (p->value_ == val) {
// unlink
*pp = p->next_;
p = *pp;
if (!*pp)
// removed last value
tail_ = prev;
} else {
prev = p;
pp = &p->next_;
p = p->next_;
}
}
}
std::pair<bool, T>
try_pop_front()
{
unique_lock l(mutex_);
if (unlikely(!head_)) {
assert(!tail_);
return std::make_pair(false, T());
}
T t = head_->value_;
node_ptr next = head_->next_;
head_ = next;
if (!head_)
tail_.reset();
return std::make_pair(true, t);
}
iterator
begin()
{
return iterator_(std::move(std::make_shared<unique_lock>(mutex_)), head_);
}
iterator
end()
{
return iterator_(unique_lock_ptr(), node_ptr());
}
};