-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathringque.h
209 lines (174 loc) · 4.08 KB
/
ringque.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
/*
* ringque.h
* Created on: 2012-1-13
* Author: qianqians
* ringque
*/
#ifndef _RINGQUE_H
#define _RINGQUE_H
#include <atomic>
#include <shared_mutex>
namespace concurrent{
template<typename T, typename _Allocator = std::allocator<T>, unsigned detailsize = 1024 >
class ringque{
private:
using _Allque = typename std::allocator_traits<_Allocator>::template rebind_alloc<std::atomic<T*> >;
typedef std::atomic<T *> * que;
public:
ringque(){
_que = get_que(detailsize);
_push_slide.store(0);
_que_max = detailsize;
_pop_slide.store(_que_max);
_size.store(0);
}
virtual ~ringque(){
put_que(_que, _que_max);
}
/*
* que is empty
*/
bool empty(){
return (size() == 0);
}
/*
* get que size
*/
std::size_t size(){
return _size.load();
}
/*
* clear this que
*/
void clear(){
std::unique_lock<std::shared_mutex> lock(_mu);
std::atomic<T *> * _tmp = _que;
std::uint32_t _tmp_push_slide = _push_slide.load();
std::uint32_t _tmp_pop_slide = _pop_slide.load();
_que = get_que(_que_max);
_push_slide.store(0);
_pop_slide.store(_que_max);
_size.store(0);
lock.unlock();
while(1){
if (_tmp_pop_slide == _que_max){
_tmp_pop_slide = 0;
}
if (_tmp_pop_slide != _tmp_push_slide){
break;
}
delete _tmp[_tmp_pop_slide].load();
_tmp_pop_slide++;
}
put_que(_tmp, _que_max);
}
/*
* push a element to que
*/
void push(T data){
std::shared_lock<std::shared_mutex> lock(_mu);
unsigned int slide = _push_slide.load();
unsigned int newslide = 0;
while(1){
newslide = slide+1;
while (newslide == _pop_slide.load()){
lock.unlock();
{
std::unique_lock<std::shared_mutex> uniquelock(_mu, std::try_to_lock);
if (uniquelock.owns_lock()){
if (newslide == _pop_slide.load()){
resize();
}
}
}
lock.lock();
}
if (newslide == _que_max){
newslide = 0;
}
if (_push_slide.compare_exchange_strong(slide, newslide)){
T * _null = 0;
T * _node = new T(data);
while(!_que[slide].compare_exchange_weak(_null, _node)){
_null = 0;
}
_size++;
break;
}
_mm_pause();
}
}
/*
* pop a element form que if empty return false
*/
bool pop(T & data){
std::shared_lock<std::shared_mutex> lock(_mu);
T * _tmp = 0;
unsigned int slide = _pop_slide.load(), slidetail = ((slide == _que_max) ? 0 : slide);
unsigned int newslide = 0;
while(1){
newslide = slidetail + 1;
if (slidetail == _push_slide.load()){
break;
}
if (_pop_slide.compare_exchange_strong(slide, newslide)){
while((_tmp = _que[slidetail].exchange(0)) == 0);
data = *_tmp;
delete _tmp;
_size--;
return true;
}
_mm_pause();
}
return false;
}
private:
void resize(){
size_t size = 0;
unsigned int pushslide = _push_slide.load();
unsigned int popslide = _pop_slide.load();
unsigned int slidetail = ((popslide == _que_max) ? 0 : popslide);
std::atomic<T *> * _tmp = 0;
size = _que_max*2;
_tmp = get_que((uint32_t)size);
if (popslide >= pushslide){
for(size_t i = 0; i < pushslide; i++){
_tmp[i].store(_que[i].load(std::memory_order_relaxed), std::memory_order_relaxed);
}
size_t size1 = _que_max - popslide;
for(size_t i = 0; i < size1; i++){
_tmp[i+size-size1].store(_que[popslide+i].load(std::memory_order_relaxed), std::memory_order_relaxed);
}
_pop_slide += _que_max;
}else{
for(size_t i = popslide; i < pushslide; i++){
_tmp[i].store(_que[i].load(std::memory_order_relaxed), std::memory_order_relaxed);
}
}
put_que(_que, _que_max);
_que_max *= 2;
_que = _tmp;
}
private:
que get_que(uint32_t size){
que _que = _que_alloc.allocate(size);
while(_que == 0){
_que = _que_alloc.allocate(size);
}
for(uint32_t i = 0; i < size; i++){
_que[i].store(0, std::memory_order_relaxed);
}
return _que;
}
void put_que(que _que, uint32_t size){
_que_alloc.deallocate(_que, size);
}
private:
std::shared_mutex _mu;
que _que;
std::atomic_uint _push_slide, _pop_slide, _size;
unsigned int _que_max;
_Allque _que_alloc;
};
}//concurrent
#endif //_RINGQUE_H