Stop the Oboe recording stream when recording is stopped
[openal-soft.git] / common / ringbuffer.h
blobfa8fce10d297af6294b3846e925e80a0f691b247
1 #ifndef RINGBUFFER_H
2 #define RINGBUFFER_H
4 #include <atomic>
5 #include <memory>
6 #include <stddef.h>
7 #include <utility>
9 #include "albyte.h"
10 #include "almalloc.h"
13 /* NOTE: This lockless ringbuffer implementation is copied from JACK, extended
14 * to include an element size. Consequently, parameters and return values for a
15 * size or count is in 'elements', not bytes. Additionally, it only supports
16 * single-consumer/single-provider operation.
19 struct ll_ringbuffer_data {
20 al::byte *buf;
21 size_t len;
23 using ll_ringbuffer_data_pair = std::pair<ll_ringbuffer_data,ll_ringbuffer_data>;
26 struct RingBuffer {
27 private:
28 std::atomic<size_t> mWritePtr{0u};
29 std::atomic<size_t> mReadPtr{0u};
30 size_t mWriteSize{0u};
31 size_t mSizeMask{0u};
32 size_t mElemSize{0u};
34 al::FlexArray<al::byte, 16> mBuffer;
36 public:
37 RingBuffer(const size_t count) : mBuffer{count} { }
39 /** Reset the read and write pointers to zero. This is not thread safe. */
40 void reset() noexcept;
42 /**
43 * The non-copying data reader. Returns two ringbuffer data pointers that
44 * hold the current readable data. If the readable data is in one segment
45 * the second segment has zero length.
47 ll_ringbuffer_data_pair getReadVector() const noexcept;
48 /**
49 * The non-copying data writer. Returns two ringbuffer data pointers that
50 * hold the current writeable data. If the writeable data is in one segment
51 * the second segment has zero length.
53 ll_ringbuffer_data_pair getWriteVector() const noexcept;
55 /**
56 * Return the number of elements available for reading. This is the number
57 * of elements in front of the read pointer and behind the write pointer.
59 size_t readSpace() const noexcept
61 const size_t w{mWritePtr.load(std::memory_order_acquire)};
62 const size_t r{mReadPtr.load(std::memory_order_acquire)};
63 return (w-r) & mSizeMask;
66 /**
67 * The copying data reader. Copy at most `cnt' elements into `dest'.
68 * Returns the actual number of elements copied.
70 size_t read(void *dest, size_t cnt) noexcept;
71 /**
72 * The copying data reader w/o read pointer advance. Copy at most `cnt'
73 * elements into `dest'. Returns the actual number of elements copied.
75 size_t peek(void *dest, size_t cnt) const noexcept;
76 /** Advance the read pointer `cnt' places. */
77 void readAdvance(size_t cnt) noexcept
78 { mReadPtr.fetch_add(cnt, std::memory_order_acq_rel); }
81 /**
82 * Return the number of elements available for writing. This is the number
83 * of elements in front of the write pointer and behind the read pointer.
85 size_t writeSpace() const noexcept
87 const size_t w{mWritePtr.load(std::memory_order_acquire)};
88 const size_t r{mReadPtr.load(std::memory_order_acquire) + mWriteSize - mSizeMask};
89 return (r-w-1) & mSizeMask;
92 /**
93 * The copying data writer. Copy at most `cnt' elements from `src'. Returns
94 * the actual number of elements copied.
96 size_t write(const void *src, size_t cnt) noexcept;
97 /** Advance the write pointer `cnt' places. */
98 void writeAdvance(size_t cnt) noexcept
99 { mWritePtr.fetch_add(cnt, std::memory_order_acq_rel); }
102 * Create a new ringbuffer to hold at least `sz' elements of `elem_sz'
103 * bytes. The number of elements is rounded up to the next power of two
104 * (even if it is already a power of two, to ensure the requested amount
105 * can be written).
107 static std::unique_ptr<RingBuffer> Create(size_t sz, size_t elem_sz, int limit_writes);
109 DEF_FAM_NEWDEL(RingBuffer, mBuffer)
111 using RingBufferPtr = std::unique_ptr<RingBuffer>;
113 #endif /* RINGBUFFER_H */