-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocklessQueue.cpp
More file actions
149 lines (119 loc) · 3.54 KB
/
LocklessQueue.cpp
File metadata and controls
149 lines (119 loc) · 3.54 KB
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
//#include "LocklessQueue.hpp"
#include <iostream>
#include <algorithm>
#include "ShutdownException.hpp"
template <class T>
LocklessQueue<T>::LocklessQueue(size_t n) :
LocklessQueue(n, 0) {}
template <class T>
LocklessQueue<T>::LocklessQueue(size_t n, size_t max)
:items(new std::vector<std::atomic<T>>(n)), capacity(n), head(0), tail(0),
count_sem(0), empty_sem(n), is_shutdown(false),
dequeue_atom(true), enqueue_atom(true), max_size(max) {}
template <class T>
LocklessQueue<T>::~LocklessQueue() {
shutdown();
}
template <class T>
void LocklessQueue<T>::shutdown() {
count_sem.shutdown();
empty_sem.shutdown();
is_shutdown.store(true);
delete items;
}
template <class T>
void LocklessQueue<T>::enqueue(T x){
if(is_shutdown.load()) {
throw ShutdownException();
}
bool expected;
do {
expected = true;
} while(!std::atomic_compare_exchange_strong(&enqueue_atom, &expected, false));
if((capacity >= max_size && (empty_sem.wait(),1))
|| empty_sem.try_wait()) {
(*items)[head].store(x);
head = ((head + 1) % capacity);
enqueue_atom = true;
count_sem.post();
}
else {
expand();
enqueue_atom = true;
enqueue(x);
}
}
template <class T>
T LocklessQueue<T>::dequeue() {
if(is_shutdown.load()) {
throw ShutdownException();
}
bool expected;
do {
expected = true;
} while(!std::atomic_compare_exchange_strong(&dequeue_atom, &expected, false));
count_sem.wait();
T ret = (*items)[tail].load();
(*items)[tail].store(0);
tail = ((tail + 1) % capacity);
empty_sem.post();
dequeue_atom = true;
return ret;
}
template <class T>
std::pair<long, long> LocklessQueue<T>::drain_semaphores() {
long count = 0;
long empty = 0;
while(count + empty < capacity) {
while(count_sem.try_wait()) count++;
while(empty_sem.try_wait()) empty++;
}
return std::pair<long, long>(count, empty);
}
template <class T>
void LocklessQueue<T>::fill_semaphores(std::pair<long, long> &p) {
long count = p.first;
long empty = p.second;
for(int i = 0; i < count; i++) {
count_sem.post();
}
for(int i = 0; i < empty; i++) {
empty_sem.post();
}
}
template <class T>
void LocklessQueue<T>::expand() {
std::unique_lock<std::mutex> lock(expand_lock);
size_t quarter_cap = capacity/4;
size_t e = empty_sem.current();
if(e < std::max(quarter_cap, (size_t)1)) {
long count = 0;
long empty = 0;
auto pair = drain_semaphores();
count = pair.first;
empty = pair.second;
size_t newcapacity = capacity * 2;
if(empty > std::max(capacity/4, (size_t)1)) {
fill_semaphores(pair);
return;
}
auto new_items = new std::vector<std::atomic<T>>(newcapacity);
for(size_t i = 0; i < capacity; i++) {
(*new_items)[i].store((*items)[i].load());
}
empty += capacity;
if(head <= tail) {
for(size_t i = tail; i < capacity; i++) {
size_t dist_from_end = (capacity - i);
(*new_items)[newcapacity - dist_from_end].store((*new_items)[i]);
(*new_items)[i].store(T());
}
tail = newcapacity - (capacity - tail);
}
capacity = newcapacity;
delete items;
items = new_items;
pair.second = empty;
fill_semaphores(pair);
}
}