aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/cubeb_triple_buffer.h
blob: 759b92e62b85f8240933b2b23693cb4ef4db2575 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * 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;
  }
  // Reset state and indices to initial values.
  void invalidate()
  {
    shared_state.store(0, std::memory_order_release);
    input_idx = 1;
    output_idx = 2;
  }

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