mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-07-13 15:44:36 +00:00
update openal
This commit is contained in:
parent
62f3b93ff9
commit
6721a6b021
287 changed files with 33851 additions and 27325 deletions
|
|
@ -2,111 +2,133 @@
|
|||
#define RINGBUFFER_H
|
||||
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <stddef.h>
|
||||
#include <new>
|
||||
#include <utility>
|
||||
|
||||
#include "albyte.h"
|
||||
#include "almalloc.h"
|
||||
#include "flexarray.h"
|
||||
|
||||
|
||||
/* NOTE: This lockless ringbuffer implementation is copied from JACK, extended
|
||||
* to include an element size. Consequently, parameters and return values for a
|
||||
* size or count is in 'elements', not bytes. Additionally, it only supports
|
||||
* size or count are in 'elements', not bytes. Additionally, it only supports
|
||||
* single-consumer/single-provider operation.
|
||||
*/
|
||||
|
||||
struct RingBuffer {
|
||||
private:
|
||||
std::atomic<size_t> mWritePtr{0u};
|
||||
std::atomic<size_t> mReadPtr{0u};
|
||||
size_t mWriteSize{0u};
|
||||
size_t mSizeMask{0u};
|
||||
size_t mElemSize{0u};
|
||||
#if defined(__cpp_lib_hardware_interference_size) && !defined(_LIBCPP_VERSION)
|
||||
static constexpr std::size_t sCacheAlignment{std::hardware_destructive_interference_size};
|
||||
#else
|
||||
/* Assume a 64-byte cache line, the most common/likely value. */
|
||||
static constexpr std::size_t sCacheAlignment{64};
|
||||
#endif
|
||||
alignas(sCacheAlignment) std::atomic<std::size_t> mWriteCount{0u};
|
||||
alignas(sCacheAlignment) std::atomic<std::size_t> mReadCount{0u};
|
||||
|
||||
al::FlexArray<al::byte, 16> mBuffer;
|
||||
alignas(sCacheAlignment) const std::size_t mWriteSize;
|
||||
const std::size_t mSizeMask;
|
||||
const std::size_t mElemSize;
|
||||
|
||||
al::FlexArray<std::byte, 16> mBuffer;
|
||||
|
||||
public:
|
||||
struct Data {
|
||||
al::byte *buf;
|
||||
size_t len;
|
||||
std::byte *buf;
|
||||
std::size_t len;
|
||||
};
|
||||
using DataPair = std::pair<Data,Data>;
|
||||
|
||||
|
||||
RingBuffer(const size_t count) : mBuffer{count} { }
|
||||
RingBuffer(const std::size_t writesize, const std::size_t mask, const std::size_t elemsize,
|
||||
const std::size_t numbytes)
|
||||
: mWriteSize{writesize}, mSizeMask{mask}, mElemSize{elemsize}, mBuffer{numbytes}
|
||||
{ }
|
||||
|
||||
/** Reset the read and write pointers to zero. This is not thread safe. */
|
||||
void reset() noexcept;
|
||||
auto reset() noexcept -> void;
|
||||
|
||||
/**
|
||||
* Return the number of elements available for reading. This is the number
|
||||
* of elements in front of the read pointer and behind the write pointer.
|
||||
*/
|
||||
[[nodiscard]] auto readSpace() const noexcept -> std::size_t
|
||||
{
|
||||
const std::size_t w{mWriteCount.load(std::memory_order_acquire)};
|
||||
const std::size_t r{mReadCount.load(std::memory_order_acquire)};
|
||||
/* mWriteCount is never more than mWriteSize greater than mReadCount. */
|
||||
return w - r;
|
||||
}
|
||||
|
||||
/**
|
||||
* The copying data reader. Copy at most `count' elements into `dest'.
|
||||
* Returns the actual number of elements copied.
|
||||
*/
|
||||
[[nodiscard]] auto read(void *dest, std::size_t count) noexcept -> std::size_t;
|
||||
/**
|
||||
* The copying data reader w/o read pointer advance. Copy at most `count'
|
||||
* elements into `dest'. Returns the actual number of elements copied.
|
||||
*/
|
||||
[[nodiscard]] auto peek(void *dest, std::size_t count) const noexcept -> std::size_t;
|
||||
|
||||
/**
|
||||
* The non-copying data reader. Returns two ringbuffer data pointers that
|
||||
* hold the current readable data. If the readable data is in one segment
|
||||
* the second segment has zero length.
|
||||
*/
|
||||
DataPair getReadVector() const noexcept;
|
||||
[[nodiscard]] auto getReadVector() noexcept -> DataPair;
|
||||
/** Advance the read pointer `count' places. */
|
||||
auto readAdvance(std::size_t count) noexcept -> void
|
||||
{
|
||||
const std::size_t w{mWriteCount.load(std::memory_order_acquire)};
|
||||
const std::size_t r{mReadCount.load(std::memory_order_relaxed)};
|
||||
[[maybe_unused]] const std::size_t readable{w - r};
|
||||
assert(readable >= count);
|
||||
mReadCount.store(r+count, std::memory_order_release);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the number of elements available for writing. This is the total
|
||||
* number of writable elements excluding what's readable (already written).
|
||||
*/
|
||||
[[nodiscard]] auto writeSpace() const noexcept -> std::size_t
|
||||
{ return mWriteSize - readSpace(); }
|
||||
|
||||
/**
|
||||
* The copying data writer. Copy at most `count' elements from `src'. Returns
|
||||
* the actual number of elements copied.
|
||||
*/
|
||||
[[nodiscard]] auto write(const void *src, std::size_t count) noexcept -> std::size_t;
|
||||
|
||||
/**
|
||||
* The non-copying data writer. Returns two ringbuffer data pointers that
|
||||
* hold the current writeable data. If the writeable data is in one segment
|
||||
* the second segment has zero length.
|
||||
*/
|
||||
DataPair getWriteVector() const noexcept;
|
||||
|
||||
/**
|
||||
* Return the number of elements available for reading. This is the number
|
||||
* of elements in front of the read pointer and behind the write pointer.
|
||||
*/
|
||||
size_t readSpace() const noexcept
|
||||
[[nodiscard]] auto getWriteVector() noexcept -> DataPair;
|
||||
/** Advance the write pointer `count' places. */
|
||||
auto writeAdvance(std::size_t count) noexcept -> void
|
||||
{
|
||||
const size_t w{mWritePtr.load(std::memory_order_acquire)};
|
||||
const size_t r{mReadPtr.load(std::memory_order_acquire)};
|
||||
return (w-r) & mSizeMask;
|
||||
const std::size_t w{mWriteCount.load(std::memory_order_relaxed)};
|
||||
const std::size_t r{mReadCount.load(std::memory_order_acquire)};
|
||||
[[maybe_unused]] const std::size_t writable{mWriteSize - (w - r)};
|
||||
assert(writable >= count);
|
||||
mWriteCount.store(w+count, std::memory_order_release);
|
||||
}
|
||||
|
||||
/**
|
||||
* The copying data reader. Copy at most `cnt' elements into `dest'.
|
||||
* Returns the actual number of elements copied.
|
||||
*/
|
||||
size_t read(void *dest, size_t cnt) noexcept;
|
||||
/**
|
||||
* The copying data reader w/o read pointer advance. Copy at most `cnt'
|
||||
* elements into `dest'. Returns the actual number of elements copied.
|
||||
*/
|
||||
size_t peek(void *dest, size_t cnt) const noexcept;
|
||||
/** Advance the read pointer `cnt' places. */
|
||||
void readAdvance(size_t cnt) noexcept
|
||||
{ mReadPtr.fetch_add(cnt, std::memory_order_acq_rel); }
|
||||
|
||||
|
||||
/**
|
||||
* Return the number of elements available for writing. This is the number
|
||||
* of elements in front of the write pointer and behind the read pointer.
|
||||
*/
|
||||
size_t writeSpace() const noexcept
|
||||
{
|
||||
const size_t w{mWritePtr.load(std::memory_order_acquire)};
|
||||
const size_t r{mReadPtr.load(std::memory_order_acquire) + mWriteSize - mSizeMask};
|
||||
return (r-w-1) & mSizeMask;
|
||||
}
|
||||
|
||||
/**
|
||||
* The copying data writer. Copy at most `cnt' elements from `src'. Returns
|
||||
* the actual number of elements copied.
|
||||
*/
|
||||
size_t write(const void *src, size_t cnt) noexcept;
|
||||
/** Advance the write pointer `cnt' places. */
|
||||
void writeAdvance(size_t cnt) noexcept
|
||||
{ mWritePtr.fetch_add(cnt, std::memory_order_acq_rel); }
|
||||
|
||||
size_t getElemSize() const noexcept { return mElemSize; }
|
||||
[[nodiscard]] auto getElemSize() const noexcept -> std::size_t { return mElemSize; }
|
||||
|
||||
/**
|
||||
* Create a new ringbuffer to hold at least `sz' elements of `elem_sz'
|
||||
* bytes. The number of elements is rounded up to the next power of two
|
||||
* (even if it is already a power of two, to ensure the requested amount
|
||||
* can be written).
|
||||
* bytes. The number of elements is rounded up to a power of two. If
|
||||
* `limit_writes' is true, the writable space will be limited to `sz'
|
||||
* elements regardless of the rounded size.
|
||||
*/
|
||||
static std::unique_ptr<RingBuffer> Create(size_t sz, size_t elem_sz, int limit_writes);
|
||||
[[nodiscard]] static
|
||||
auto Create(std::size_t sz, std::size_t elem_sz, bool limit_writes) -> std::unique_ptr<RingBuffer>;
|
||||
|
||||
DEF_FAM_NEWDEL(RingBuffer, mBuffer)
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue