-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathts_queue.h
400 lines (358 loc) · 10.8 KB
/
ts_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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#pragma once
#include "config.h"
#include "util.h"
#include "spinlock.h"
namespace co
{
// 渚靛叆寮忔暟鎹粨鏋凥ook鍩虹被
struct TSQueueHook
{
TSQueueHook *prev = nullptr;
TSQueueHook *next = nullptr;
void *check_ = nullptr;
};
// 渚靛叆寮忓弻鍚戦摼琛�
// moveable, noncopyable, foreachable
// erase, size, empty: O(1)
template <typename T>
class SList
{
static_assert((std::is_base_of<TSQueueHook, T>::value), "T must be baseof TSQueueHook");
public:
struct iterator
{
T* ptr;
iterator() : ptr(nullptr) {}
explicit iterator(T* p) : ptr(p) {}
friend bool operator==(iterator const& lhs, iterator const& rhs)
{ return lhs.ptr == rhs.ptr; }
friend bool operator!=(iterator const& lhs, iterator const& rhs)
{ return !(lhs.ptr == rhs.ptr); }
iterator& operator++() { ptr = (T*)ptr->next; return *this; }
iterator operator++(int) { iterator ret = *this; ++(*this); return ret; }
iterator& operator--() { ptr = (T*)ptr->prev; return *this; }
iterator operator--(int) { iterator ret = *this; --(*this); return ret; }
T& operator*() { return *(T*)ptr; }
T* operator->() { return (T*)ptr; }
};
T* head_;
T* tail_;
void *check_;
std::size_t count_;
public:
SList() : head_(nullptr), tail_(nullptr), check_(nullptr), count_(0) {}
SList(TSQueueHook* h, TSQueueHook* t, std::size_t count, void *c)
: head_((T*)h), tail_((T*)t), check_(c), count_(count) {}
SList(SList const&) = delete;
SList& operator=(SList const&) = delete;
SList(SList<T> && other)
{
head_ = other.head_;
tail_ = other.tail_;
check_ = other.check_;
count_ = other.count_;
other.stealed();
}
SList& operator=(SList<T> && other)
{
clear();
head_ = other.head_;
tail_ = other.tail_;
check_ = other.check_;
count_ = other.count_;
other.stealed();
return *this;
}
~SList()
{
clear();
}
iterator begin() { return iterator{head_}; }
iterator end() { return iterator(); }
ALWAYS_INLINE bool empty() const { return head_ == nullptr; }
iterator erase(iterator it)
{
T* ptr = (it++).ptr;
if (ptr->prev) ptr->prev->next = ptr->next;
else head_ = (T*)head_->next;
if (ptr->next) ptr->next->prev = ptr->prev;
else tail_ = (T*)tail_->prev;
ptr->prev = ptr->next = nullptr;
ptr->check_ = nullptr;
-- count_;
DecrementRef(ptr);
return it;
}
std::size_t size() const
{
return count_;
}
void clear()
{
auto it = begin();
while (it != end())
it = erase(it);
stealed();
}
void stealed()
{
head_ = tail_ = nullptr;
check_ = nullptr;
count_ = 0;
}
ALWAYS_INLINE TSQueueHook* head() { return head_; }
ALWAYS_INLINE TSQueueHook* tail() { return tail_; }
ALWAYS_INLINE bool check(void *c) { return check_ == c; }
};
// 绾跨▼瀹夊叏鐨勯槦鍒�(鏀寔闅忔満鍒犻櫎)
template <typename T, bool ThreadSafe = true>
class TSQueue
{
static_assert((std::is_base_of<TSQueueHook, T>::value), "T must be baseof TSQueueHook");
//private:
public:
LFLock lck;
typedef typename std::conditional<ThreadSafe,
std::lock_guard<LFLock>,
fake_lock_guard>::type LockGuard;
TSQueueHook* head_;
TSQueueHook* tail_;
std::size_t count_;
void *check_ = nullptr;
public:
TSQueue()
{
head_ = tail_ = new TSQueueHook;
count_ = 0;
check_ = this;
}
~TSQueue()
{
LockGuard lock(lck);
while (head_ != tail_) {
TSQueueHook *prev = tail_->prev;
DecrementRef((T*)tail_);
tail_ = prev;
}
delete head_;
head_ = tail_ = 0;
}
ALWAYS_INLINE bool empty()
{
LockGuard lock(lck);
return head_ == tail_;
}
ALWAYS_INLINE std::size_t size()
{
LockGuard lock(lck);
return count_;
}
ALWAYS_INLINE void push(T* element)
{
LockGuard lock(lck);
TSQueueHook *hook = static_cast<TSQueueHook*>(element);
tail_->next = hook;
hook->prev = tail_;
hook->next = nullptr;
hook->check_ = check_;
tail_ = hook;
++ count_;
IncrementRef(element);
}
ALWAYS_INLINE T* pop()
{
if (head_ == tail_) return nullptr;
LockGuard lock(lck);
if (head_ == tail_) return nullptr;
TSQueueHook* ptr = head_->next;
if (ptr == tail_) tail_ = head_;
head_->next = ptr->next;
if (ptr->next) ptr->next->prev = head_;
ptr->prev = ptr->next = nullptr;
ptr->check_ = nullptr;
-- count_;
DecrementRef((T*)ptr);
return (T*)ptr;
}
ALWAYS_INLINE void push(SList<T> && elements)
{
if (elements.empty()) return ; // empty鐨凷List涓嶈兘check, 鍥犱负stealed鐨勬椂鍊欏凡缁忔竻闄heck_.
assert(elements.check(check_));
LockGuard lock(lck);
count_ += elements.size();
tail_->next = elements.head();
elements.head()->prev = tail_;
elements.tail()->next = nullptr;
tail_ = elements.tail();
elements.stealed();
}
// O(n), 鎱庣敤.
ALWAYS_INLINE SList<T> pop_front(uint32_t n)
{
if (head_ == tail_) return SList<T>();
LockGuard lock(lck);
if (head_ == tail_) return SList<T>();
TSQueueHook* first = head_->next;
TSQueueHook* last = first;
uint32_t c = 1;
for (; c < n && last->next; ++c)
last = last->next;
if (last == tail_) tail_ = head_;
head_->next = last->next;
if (last->next) last->next->prev = head_;
first->prev = last->next = nullptr;
count_ -= c;
return SList<T>(first, last, c, check_);
}
// O(n), 鎱庣敤.
ALWAYS_INLINE SList<T> pop_back(uint32_t n)
{
if (head_ == tail_) return SList<T>();
LockGuard lock(lck);
if (head_ == tail_) return SList<T>();
TSQueueHook* last = tail_;
TSQueueHook* first = last;
uint32_t c = 1;
for (; c < n && first->prev && first->prev != head_; ++c)
first = first->prev;
tail_ = first->prev;
first->prev = tail_->next = nullptr;
count_ -= c;
return SList<T>(first, last, c, check_);
}
ALWAYS_INLINE SList<T> pop_all()
{
if (head_ == tail_) return SList<T>();
LockGuard lock(lck);
if (head_ == tail_) return SList<T>();
TSQueueHook* first = head_->next;
TSQueueHook* last = tail_;
tail_ = head_;
head_->next = nullptr;
first->prev = last->next = nullptr;
std::size_t c = count_;
count_ = 0;
return SList<T>(first, last, c, check_);
}
ALWAYS_INLINE bool erase(T* hook)
{
LockGuard lock(lck);
if (hook->check_ != (void*)check_) return false;
if (hook->prev) hook->prev->next = hook->next;
if (hook->next) hook->next->prev = hook->prev;
else if (hook == tail_) tail_ = tail_->prev;
hook->prev = hook->next = nullptr;
hook->check_ = nullptr;
-- count_;
DecrementRef((T*)hook);
return true;
}
};
// 绾跨▼瀹夊叏鐨勮烦琛ㄩ槦鍒�(鏀寔蹇�焢op鍑簄涓厓绱�)
template <typename T,
bool ThreadSafe = true,
std::size_t SkipDistance = 64 // 蹇呴』鏄�2鐨刵娆℃柟
>
class TSSkipQueue
{
static_assert((std::is_base_of<TSQueueHook, T>::value), "T must be baseof TSQueueHook");
private:
LFLock lck;
typedef typename std::conditional<ThreadSafe,
std::lock_guard<LFLock>,
fake_lock_guard>::type LockGuard;
TSQueueHook* head_;
TSQueueHook* tail_;
std::size_t count_;
struct SkipLayer
{
std::deque<TSQueueHook*> indexs_;
std::size_t head_offset_ = 0; // begin璺濈head鐨勮窛绂�
std::size_t tail_offset_ = 0; // end璺濈tail鐨勮窛绂�
};
SkipLayer skip_layer_;
public:
TSSkipQueue()
{
head_ = tail_ = new TSQueueHook;
count_ = 0;
}
~TSSkipQueue()
{
LockGuard lock(lck);
while (head_ != tail_) {
TSQueueHook *prev = tail_->prev;
DecrementRef((T*)tail_);
tail_ = prev;
}
delete head_;
head_ = tail_ = 0;
}
bool empty()
{
LockGuard lock(lck);
return head_ == tail_;
}
std::size_t size()
{
LockGuard lock(lck);
return count_;
}
void push(T* element)
{
LockGuard lock(lck);
// 鎻掑叆鍒板熬绔�
TSQueueHook *hook = static_cast<TSQueueHook*>(element);
tail_->next = hook;
hook->prev = tail_;
hook->next = nullptr;
hook->check_ = this;
tail_ = hook;
++ count_;
IncrementRef(element);
// 鍒锋柊璺宠〃
if (++skip_layer_.tail_offset_ == SkipDistance) {
skip_layer_.tail_offset_ = 0;
skip_layer_.indexs_.push_back(tail_);
}
}
SList<T> pop_front(std::size_t n)
{
if (head_ == tail_ || !n) return SList<T>();
LockGuard lock(lck);
if (head_ == tail_) return SList<T>();
TSQueueHook* first = head_->next;
TSQueueHook* last = first;
n = (std::min)(n, count_);
std::size_t next_step = n - 1;
// 浠庤烦琛ㄤ笂鏌ユ壘
if ((SkipDistance - skip_layer_.head_offset_) > n) {
skip_layer_.head_offset_ += n;
} else if (skip_layer_.indexs_.empty()) {
skip_layer_.tail_offset_ = count_ - n;
} else {
// 寰�鍓嶆壘
std::size_t forward = (n + skip_layer_.head_offset_ - SkipDistance) / SkipDistance;
next_step = (n + skip_layer_.head_offset_ - SkipDistance) & (SkipDistance - 1);
auto it = skip_layer_.indexs_.begin();
std::advance(it, forward);
last = *it;
skip_layer_.indexs_.erase(skip_layer_.indexs_.begin(), ++it);
if (skip_layer_.indexs_.empty()) {
skip_layer_.tail_offset_ = count_ - n;
skip_layer_.head_offset_ = 0;
} else {
skip_layer_.head_offset_ = next_step;
}
}
for (std::size_t i = 0; i < next_step; ++i)
last = last->next;
if (last == tail_) tail_ = head_;
head_->next = last->next;
if (last->next) last->next->prev = head_;
first->prev = last->next = nullptr;
count_ -= n;
return SList<T>(first, last, n, this);
}
};
} //namespace co