-
Notifications
You must be signed in to change notification settings - Fork 1
/
SharedQueue.h
109 lines (87 loc) · 2.35 KB
/
SharedQueue.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
#ifndef SharedQueue_H
#define SharedQueue_H
#include<vector>
#include <thread>
#include <functional>
#include <algorithm>
#include<cmath>
#include<iostream>
// convert gcc to c0xx
#define thread_local __thread;
typedef std::thread Thread;
typedef std::vector<std::thread> ThreadGroup;
typedef std::mutex Mutex;
typedef std::unique_lock<std::mutex> Guard;
typedef std::condition_variable Condition;
#define CACHE_LINE_SIZE 64 // 64 byte cache line on x86 and x86-64
/*
typedef<typename T>
struct Padded {
Padded(T& const i) : t(i){}
Padded(T&&i) : t(std::move(i)){}
T t;
char pad[CACHE_LINE_SIZE-sizeof(T)%CACHE_LINE_SIZE];
};
*/
template<typename T>
class Queue {
public:
explicit Queue(size_t maxsize) : head(maxsize-1), tail(head), last(maxsize-1),
container(maxsize), drained(false) {}
typedef T AT __attribute__ ((__aligned__(CACHE_LINE_SIZE)));
void waitFull() const{
// spinlock
while(full()) {
nanosleep(0,0);
// std::this_thread::yield();
// std::this_thread::sleep_for(0);
}
}
bool waitEmpty() const{
// spinlock
while(empty()&&(!drained))
{
nanosleep(0,0);
//std::this_thread::sleep_for(0);
// std::this_thread::yield();
}
return empty()&&drained;
}
// only one thread can push
void push(T const & t) {
while (true) {
waitFull();
volatile size_t cur=head;
container[cur] = t; // shall be done first to avoid popping wrong value
// does not work: if fails state of head is unknown....
if (__sync_bool_compare_and_swap(&head,cur,cur==0 ? last : cur-1 )) {
// container[cur] = t; // too late pop already occured!
break;
}
}
}
// N threads can pop
bool pop(T&t) {
while (true) {
if(waitEmpty()) return false; // include a signal to drain and terminate
volatile size_t cur=tail;
if (cur==head) continue;
t = container[cur];
if (__sync_bool_compare_and_swap(&tail,cur,cur==0 ?last : cur-1)) break;
}
return true;
}
bool full() const { return (head==0 && tail==last)
|| (tail==head-1);
}
bool empty() const { return head==tail;}
void drain() { drained=true;}
void reset() { drained=false; head=tail=last;}
// circular buffer
volatile size_t head;
volatile size_t tail;
size_t last;
std::vector<AT> container;
bool drained;
};
#endif