diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /media/libcubeb/src/cubeb_triple_buffer.h | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'media/libcubeb/src/cubeb_triple_buffer.h')
-rw-r--r-- | media/libcubeb/src/cubeb_triple_buffer.h | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/media/libcubeb/src/cubeb_triple_buffer.h b/media/libcubeb/src/cubeb_triple_buffer.h new file mode 100644 index 0000000000..a5a5978fb4 --- /dev/null +++ b/media/libcubeb/src/cubeb_triple_buffer.h @@ -0,0 +1,80 @@ +/* + * Copyright © 2022 Mozilla Foundation + * + * This program is made available under an ISC-style license. See the + * accompanying file LICENSE for details. + */ + +/** + * Adapted and ported to C++ from https://crates.io/crates/triple_buffer + */ + +#ifndef CUBEB_TRIPLE_BUFFER +#define CUBEB_TRIPLE_BUFFER + +#include <atomic> + +// Single producer / single consumer wait-free triple buffering +// implementation, for when a producer wants to publish data to a consumer +// without blocking, but when a queue is wastefull, because it's OK for the +// consumer to miss data updates. +template <typename T> class triple_buffer { +public: + // Write a new value into the triple buffer. Returns true if a value was + // overwritten. + // Producer-side only. + bool write(T & input) + { + storage[input_idx] = input; + return publish(); + } + // Get the latest value from the triple buffer. + // Consumer-side only. + T & read() + { + update(); + return storage[output_idx]; + } + // Returns true if a new value has been published by the consumer without + // having been consumed yet. + // Consumer-side only. + bool updated() + { + return (shared_state.load(std::memory_order_relaxed) & BACK_DIRTY_BIT) != 0; + } + +private: + // Publish a value to the consumer. Returns true if the data was overwritten + // without having been read. + bool publish() + { + auto former_back_idx = shared_state.exchange(input_idx | BACK_DIRTY_BIT, + std::memory_order_acq_rel); + input_idx = former_back_idx & BACK_INDEX_MASK; + return (former_back_idx & BACK_DIRTY_BIT) != 0; + } + // Get a new value from the producer, if a new value has been produced. + bool update() + { + bool was_updated = updated(); + if (was_updated) { + auto former_back_idx = + shared_state.exchange(output_idx, std::memory_order_acq_rel); + output_idx = former_back_idx & BACK_INDEX_MASK; + } + return was_updated; + } + T storage[3]; + // Mask used to extract back-buffer index + const uint8_t BACK_INDEX_MASK = 0b11; + // Bit set by producer to signal updates + const uint8_t BACK_DIRTY_BIT = 0b100; + // Shared state: a dirty bit, and an index. + std::atomic<uint8_t> shared_state = {0}; + // Output index, private to the consumer. + uint8_t output_idx = 1; + // Input index, private to the producer. + uint8_t input_idx = 2; +}; + +#endif // CUBEB_TRIPLE_BUFFER |