-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathringbuf.h
76 lines (63 loc) · 2.21 KB
/
ringbuf.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
#include <iostream>
#include <vector>
#include <mutex>
#include <stdexcept>
#include <condition_variable>
class RingBuffer {
public:
explicit RingBuffer(size_t size) : buffer(size), capacity(size), head(0), tail(0), is_full(false) {}
// blocking push: Will wait if there's not enough space
bool push( uint8_t*data, size_t size) {
std::unique_lock<std::mutex> lock(mutex);
size_t data_size = size;
if (data_size > available_space()) {
cond_var.wait(lock, [=] (){ return (size > occupied_space()); }); // Not enough space to push data
}
for (size_t i = 0; i < data_size; ++i) {
buffer[tail] = data[i];
tail = (tail + 1) % capacity;
if (head == tail) {
is_full = true; // Buffer is now full
}
}
return true; // Data successfully pushed
}
// Non-blocking pop: Will return all available data if requested size is too large
std::vector<uint8_t> pop(size_t size) {
std::lock_guard<std::mutex> lock(mutex);
size_t data_size = occupied_space();
if (data_size == 0) {
return {}; // Buffer is empty, return an empty vector
}
// If the requested size is larger than available data, adjust the size
size_t pop_size = std::min(size, data_size);
std::vector<uint8_t> data(pop_size);
for (size_t i = 0; i < pop_size; ++i) {
data[i] = buffer[head];
head = (head + 1) % capacity;
is_full = false; // Buffer is no longer full
}
cond_var.notify_one(); // Notify one waiting thread
return data; // Return the popped data
}
size_t occupied_space() const {
if (is_full) {
return capacity; // If full, return the capacity
}
if (tail >= head) {
return tail - head;
}
return capacity - head + tail;
}
size_t available_space() const {
return capacity - occupied_space(); // Available space in the buffer
}
private:
std::vector<uint8_t> buffer;
size_t capacity;
size_t head;
size_t tail;
mutable std::mutex mutex;
bool is_full;
std::condition_variable cond_var;
};