-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring_buffer.h
146 lines (120 loc) · 3.12 KB
/
ring_buffer.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
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <mutex>
template <typename T>
class ring_buffer
{
private:
T *buffer;
size_t buffer_size;
size_t write_ptr, read_ptr;
std::mutex mtx;
std::condition_variable cv;
bool is_empty_without_lock(void);
bool is_full_without_lock(void);
bool enqueue_signle_without_lock(const T *data);
bool dequeue_signle_without_lock(T *data);
public:
ring_buffer(const size_t size);
~ring_buffer();
bool is_empty(void);
size_t get_buffer_size(void);
size_t get_count(void);
size_t enqueue(const T *data, size_t length);
size_t dequeue(T *data, size_t max_length);
bool wait(const std::chrono::steady_clock::time_point &timeout_at);
void notify_one(void);
};
template <typename T>
ring_buffer<T>::ring_buffer(const size_t size)
{
buffer = new T[size];
buffer_size = size;
write_ptr = 0;
read_ptr = 0;
}
template <typename T>
ring_buffer<T>::~ring_buffer()
{
delete buffer;
}
template <typename T>
bool ring_buffer<T>::is_empty_without_lock(void)
{
return write_ptr == read_ptr;
}
template <typename T>
bool ring_buffer<T>::is_full_without_lock(void)
{
const auto next_write_ptr = (write_ptr + 1) % buffer_size;
return next_write_ptr == read_ptr;
}
template <typename T>
bool ring_buffer<T>::is_empty(void)
{
std::lock_guard<std::mutex> lock(mtx);
return is_empty_without_lock();
}
template <typename T>
size_t ring_buffer<T>::get_buffer_size(void)
{
return buffer_size - 1;
}
template <typename T>
size_t ring_buffer<T>::get_count(void)
{
std::lock_guard<std::mutex> lock(mtx);
return (write_ptr - read_ptr + buffer_size) % buffer_size;
}
template <typename T>
bool ring_buffer<T>::enqueue_signle_without_lock(const T *data)
{
if (is_full_without_lock()) {
return false;
}
buffer[write_ptr] = *data;
write_ptr = (write_ptr + 1) % buffer_size;
return true;
}
template <typename T>
size_t ring_buffer<T>::enqueue(const T *data, size_t length)
{
std::lock_guard<std::mutex> lock(mtx);
size_t ptr = 0;
while(ptr < length && enqueue_signle_without_lock(&data[ptr])) {ptr++;}
return ptr;
}
template <typename T>
bool ring_buffer<T>::dequeue_signle_without_lock(T *data)
{
if (is_empty_without_lock()) {
return false;
}
*data = buffer[read_ptr];
read_ptr = (read_ptr + 1) % buffer_size;
return true;
}
template <typename T>
size_t ring_buffer<T>::dequeue(T *data, size_t max_length)
{
std::lock_guard<std::mutex> lock(mtx);
size_t ptr = 0;
while(ptr < max_length && dequeue_signle_without_lock(&data[ptr])) {ptr++;}
return ptr;
}
template <typename T>
bool ring_buffer<T>::wait(const std::chrono::steady_clock::time_point &timeout_at)
{
std::unique_lock<std::mutex> lock(mtx);
if (!is_empty_without_lock()) {
return false;
}
return cv.wait_until(lock, timeout_at, [&]{return !is_empty_without_lock();});
}
template <typename T>
void ring_buffer<T>::notify_one(void)
{
cv.notify_one();
return;
}