| 1 |
/* |
| 2 |
* Copyright © 2022 Mozilla Foundation |
| 3 |
* |
| 4 |
* This program is made available under an ISC-style license. See the |
| 5 |
* accompanying file LICENSE for details. |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Adapted and ported to C++ from https://crates.io/crates/triple_buffer |
| 10 |
*/ |
| 11 |
|
| 12 |
#ifndef CUBEB_TRIPLE_BUFFER |
| 13 |
#define CUBEB_TRIPLE_BUFFER |
| 14 |
|
| 15 |
#include <atomic> |
| 16 |
|
| 17 |
// Single producer / single consumer wait-free triple buffering |
| 18 |
// implementation, for when a producer wants to publish data to a consumer |
| 19 |
// without blocking, but when a queue is wastefull, because it's OK for the |
| 20 |
// consumer to miss data updates. |
| 21 |
template <typename T> class triple_buffer { |
| 22 |
public: |
| 23 |
// Write a new value into the triple buffer. Returns true if a value was |
| 24 |
// overwritten. |
| 25 |
// Producer-side only. |
| 26 |
bool write(T & input) |
| 27 |
{ |
| 28 |
storage[input_idx] = input; |
| 29 |
return publish(); |
| 30 |
} |
| 31 |
// Get the latest value from the triple buffer. |
| 32 |
// Consumer-side only. |
| 33 |
T & read() |
| 34 |
{ |
| 35 |
update(); |
| 36 |
return storage[output_idx]; |
| 37 |
} |
| 38 |
// Returns true if a new value has been published by the consumer without |
| 39 |
// having been consumed yet. |
| 40 |
// Consumer-side only. |
| 41 |
bool updated() |
| 42 |
{ |
| 43 |
return (shared_state.load(std::memory_order_relaxed) & BACK_DIRTY_BIT) != 0; |
| 44 |
} |
| 45 |
|
| 46 |
private: |
| 47 |
// Publish a value to the consumer. Returns true if the data was overwritten |
| 48 |
// without having been read. |
| 49 |
bool publish() |
| 50 |
{ |
| 51 |
auto former_back_idx = shared_state.exchange(input_idx | BACK_DIRTY_BIT, |
| 52 |
std::memory_order_acq_rel); |
| 53 |
input_idx = former_back_idx & BACK_INDEX_MASK; |
| 54 |
return (former_back_idx & BACK_DIRTY_BIT) != 0; |
| 55 |
} |
| 56 |
// Get a new value from the producer, if a new value has been produced. |
| 57 |
bool update() |
| 58 |
{ |
| 59 |
bool was_updated = updated(); |
| 60 |
if (was_updated) { |
| 61 |
auto former_back_idx = |
| 62 |
shared_state.exchange(output_idx, std::memory_order_acq_rel); |
| 63 |
output_idx = former_back_idx & BACK_INDEX_MASK; |
| 64 |
} |
| 65 |
return was_updated; |
| 66 |
} |
| 67 |
T storage[3]; |
| 68 |
// Mask used to extract back-buffer index |
| 69 |
const uint8_t BACK_INDEX_MASK = 0b11; |
| 70 |
// Bit set by producer to signal updates |
| 71 |
const uint8_t BACK_DIRTY_BIT = 0b100; |
| 72 |
// Shared state: a dirty bit, and an index. |
| 73 |
std::atomic<uint8_t> shared_state = {0}; |
| 74 |
// Output index, private to the consumer. |
| 75 |
uint8_t output_idx = 1; |
| 76 |
// Input index, private to the producer. |
| 77 |
uint8_t input_idx = 2; |
| 78 |
}; |
| 79 |
|
| 80 |
#endif // CUBEB_TRIPLE_BUFFER |