-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLockFreeFIFO.cpp
88 lines (83 loc) · 2.63 KB
/
LockFreeFIFO.cpp
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
#include "LockFreeFIFO.h"
template<typename T, int MAX_LEN>
LockFreeFIFO<T, MAX_LEN>::LockFreeFIFO() {
ABAIndex head;
head.head = -1;
head.aba = 0;
fifo.combined = head.combined;
for (int i = 0; i < MAX_LEN; ++i) {
all_links[i].next = i - 1;
}
head.head = MAX_LEN - 1;
((std::atomic_uint64_t*)&free.combined)->store(head.combined, std::memory_order_seq_cst);
}
template<typename T, int MAX_LEN>
void LockFreeFIFO<T, MAX_LEN>::push_fifo(int index)
{
int32_t head;
head = ((std::atomic_uint32_t*)&fifo.head)->load(std::memory_order_seq_cst);
bool success = false;
do {
all_links[index].next = head.head;
success = std::atomic_compare_exchange_weak(((std::atomic_uint32_t*)&fifo.head), &head, index);
} while (!success);
}
template<typename T, int MAX_LEN>
int LockFreeFIFO<T, MAX_LEN>::pop_fifo()
{
int ret;
ABAIndex head;
head.combined = ((std::atomic_uint64_t*)&fifo.combined)->load(std::memory_order_seq_cst);
bool success = false;
do {
if (head.head == -1) return -1;
ret = head.head;
ABAIndex to;
to.head = all_links[ret].next;
to.aba = head.aba + 1;
success = std::atomic_compare_exchange_weak(((std::atomic_uint64_t*)&fifo.combined), &head.combined, to);
} while (!success);
return ret;
}
template<typename T, int MAX_LEN>
int LockFreeFIFO<T, MAX_LEN>::steal_fifo()
{
int32_t head = -1;
head = std::atomic_exchange(((std::atomic_uint32_t*)&fifo.head), head);
return head;
}
template<typename T, int MAX_LEN>
void LockFreeFIFO<T, MAX_LEN>::push_free(int index)
{
int32_t head;
head = ((std::atomic_uint32_t*)&free.head)->load(std::memory_order_seq_cst);
bool success = false;
do {
all_links[index].next = head.head;
success = std::atomic_compare_exchange_weak(((std::atomic_uint32_t*)&free.head), &head, index);
} while (!success);
}
template<typename T, int MAX_LEN>
int LockFreeFIFO<T, MAX_LEN>::pop_free()
{
int ret;
ABAIndex head;
head.combined = ((std::atomic_uint64_t*)&free.combined)->load(std::memory_order_seq_cst);
bool success = false;
do {
if (head.head == -1) return -1;
ret = head.head;
ABAIndex to;
to.head = all_links[ret].next;
to.aba = head.aba + 1;
success = std::atomic_compare_exchange_weak(((std::atomic_uint64_t*)&free.combined), &head.combined, to);
} while (!success);
return ret;
}
template<typename T, int MAX_LEN>
int LockFreeFIFO<T, MAX_LEN>::steal_free()
{
int32_t head = -1;
head = std::atomic_exchange(((std::atomic_uint32_t*)&free.head), head);
return head;
}