Use one PulseAudio mainloop per device
[openal-soft.git] / alc / ringbuffer.h
blob3151fdcbbb5d4e06a1e4bdd093657d58398dcb16
1 #ifndef RINGBUFFER_H
2 #define RINGBUFFER_H
4 #include <stddef.h>
6 #include <atomic>
7 #include <memory>
8 #include <utility>
10 #include "albyte.h"
11 #include "almalloc.h"
14 /* NOTE: This lockless ringbuffer implementation is copied from JACK, extended
15 * to include an element size. Consequently, parameters and return values for a
16 * size or count is in 'elements', not bytes. Additionally, it only supports
17 * single-consumer/single-provider operation.
20 struct ll_ringbuffer_data {
21 al::byte *buf;
22 size_t len;
24 using ll_ringbuffer_data_pair = std::pair<ll_ringbuffer_data,ll_ringbuffer_data>;
27 struct RingBuffer {
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 RingBuffer(const size_t count) : mBuffer{count} { }
38 /** Reset the read and write pointers to zero. This is not thread safe. */
39 void reset() noexcept;
41 /**
42 * The non-copying data reader. Returns two ringbuffer data pointers that
43 * hold the current readable data. If the readable data is in one segment
44 * the second segment has zero length.
46 ll_ringbuffer_data_pair getReadVector() const noexcept;
47 /**
48 * The non-copying data writer. Returns two ringbuffer data pointers that
49 * hold the current writeable data. If the writeable data is in one segment
50 * the second segment has zero length.
52 ll_ringbuffer_data_pair getWriteVector() const noexcept;
54 /**
55 * Return the number of elements available for reading. This is the number
56 * of elements in front of the read pointer and behind the write pointer.
58 size_t readSpace() const noexcept;
59 /**
60 * The copying data reader. Copy at most `cnt' elements into `dest'.
61 * Returns the actual number of elements copied.
63 size_t read(void *dest, size_t cnt) noexcept;
64 /**
65 * The copying data reader w/o read pointer advance. Copy at most `cnt'
66 * elements into `dest'. Returns the actual number of elements copied.
68 size_t peek(void *dest, size_t cnt) const noexcept;
69 /** Advance the read pointer `cnt' places. */
70 void readAdvance(size_t cnt) noexcept;
72 /**
73 * Return the number of elements available for writing. This is the number
74 * of elements in front of the write pointer and behind the read pointer.
76 size_t writeSpace() const noexcept;
77 /**
78 * The copying data writer. Copy at most `cnt' elements from `src'. Returns
79 * the actual number of elements copied.
81 size_t write(const void *src, size_t cnt) noexcept;
82 /** Advance the write pointer `cnt' places. */
83 void writeAdvance(size_t cnt) noexcept;
85 DEF_FAM_NEWDEL(RingBuffer, mBuffer)
87 using RingBufferPtr = std::unique_ptr<RingBuffer>;
90 /**
91 * Create a new ringbuffer to hold at least `sz' elements of `elem_sz' bytes.
92 * The number of elements is rounded up to the next power of two (even if it is
93 * already a power of two, to ensure the requested amount can be written).
95 RingBufferPtr CreateRingBuffer(size_t sz, size_t elem_sz, int limit_writes);
97 #endif /* RINGBUFFER_H */