-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathring_buffer.h
39 lines (33 loc) · 898 Bytes
/
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
#pragma once
#include <atomic>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <mutex>
class RingBuffer {
protected:
const size_t size_;
const std::unique_ptr<uint8_t[]> buffer_;
size_t begin_{0};
size_t end_{0};
std::atomic<bool> finished_{false};
public:
RingBuffer(size_t size);
virtual bool push(const uint8_t* input, size_t input_size);
virtual bool pop(uint8_t* output, size_t output_size);
void finished() { finished_ = true; }
size_t space_left() const;
size_t space_used() const;
protected:
};
class RingBufferLock : public RingBuffer {
private:
std::mutex m_;
std::condition_variable empty_;
std::condition_variable full_;
public:
RingBufferLock(size_t size) : RingBuffer(size) {}
virtual bool push(const uint8_t* input, size_t input_size) override;
virtual bool pop(uint8_t* output, size_t output_size) override;
};