-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_tm.hpp
143 lines (115 loc) · 3.41 KB
/
queue_tm.hpp
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
#pragma once
#include "trade_v1/trade.hpp"
#include "polyfill_v1/memory.hpp"
#include <optional>
#include <vector>
#include "testing/config.hpp"
namespace testing {
/// A transactional queue for testing purposes.
template <class Value> class queue_tm {
struct node_t {
trade::atom<std::shared_ptr<node_t>> m_next;
Value m_value;
#ifndef NDEBUG
~node_t() { --s_live_nodes; }
#endif
template <class ForwardableValue>
node_t(ForwardableValue &&value)
: m_value(std::forward<ForwardableValue>(value)) {
#ifndef NDEBUG
++s_live_nodes;
#endif
}
};
trade::atom<std::shared_ptr<node_t>> m_first;
trade::atom<std::shared_ptr<node_t>> m_last;
public:
using value_t = Value;
queue_tm() = default;
queue_tm(const queue_tm &) = delete;
queue_tm &operator=(const queue_tm &) = delete;
size_t size() const;
bool empty() const;
void clear();
template <class ForwardableValue> void push_back(ForwardableValue &&value);
template <class ForwardableValue> void push_front(ForwardableValue &&value);
std::optional<Value> try_pop_front();
Value pop_front();
operator std::vector<Value>() const;
#ifndef NDEBUG
static std::atomic<size_t> s_live_nodes;
#endif
};
template <class Value> size_t queue_tm<Value>::size() const {
return trade::atomically(trade::assume_readonly, [&]() {
size_t n = 0;
for (auto node = m_first.load(); node; node = node->m_next)
n += 1;
return n;
});
}
template <class Value> bool queue_tm<Value>::empty() const {
return trade::atomically(trade::assume_readonly,
[&]() { return !m_last.load(); });
}
template <class Value> void queue_tm<Value>::clear() {
trade::atomically([&]() { m_first = m_last = nullptr; });
}
template <class Value>
template <class ForwardableValue>
void queue_tm<Value>::push_back(ForwardableValue &&value) {
std::shared_ptr<node_t> node(
new node_t(std::forward<ForwardableValue>(value)));
trade::atomically([&]() {
if (auto prev = m_last.load())
prev->m_next = m_last = node;
else
m_first = m_last = node;
});
}
template <class Value>
template <class ForwardableValue>
void queue_tm<Value>::push_front(ForwardableValue &&value) {
std::shared_ptr<node_t> node(
new node_t(std::forward<ForwardableValue>(value)));
trade::atomically([&]() {
if (auto next = m_first.load()) {
m_first = node;
node->m_next = next;
} else {
m_last = m_first = node;
}
});
}
template <class Value> std::optional<Value> queue_tm<Value>::try_pop_front() {
return trade::atomically([&]() -> std::optional<Value> {
if (auto first = m_first.load()) {
if (auto next = first->m_next.load())
m_first = next;
else
m_last = m_first = nullptr;
return first->m_value;
}
return std::nullopt;
});
}
template <class Value> Value queue_tm<Value>::pop_front() {
return trade::atomically([&]() {
if (auto opt_value = try_pop_front())
return opt_value.value();
trade::retry();
});
}
template <class Value> queue_tm<Value>::operator std::vector<Value>() const {
std::vector<Value> values;
trade::atomically(trade::assume_readonly, [&]() {
values.clear();
for (auto node = m_first.load(); node; node = node->m_next)
values.push_back(node->m_value);
});
return values;
}
#ifndef NDEBUG
template <class Value> std::atomic<size_t> queue_tm<Value>::s_live_nodes = 0;
#endif
} // namespace testing