53 int capacity() const noexcept {
return capacity_; }
57 const int w = write_pos_.load(std::memory_order_acquire);
58 const int r = read_pos_.load(std::memory_order_acquire);
59 const int diff = w - r;
60 return diff >= 0 ? diff : diff + capacity_;
72 read_pos_.store(0, std::memory_order_relaxed);
73 write_pos_.store(0, std::memory_order_relaxed);
83 int& start_1,
int& size_1,
84 int& start_2,
int& size_2)
const noexcept {
85 const int w = write_pos_.load(std::memory_order_relaxed);
86 const int r = read_pos_.load(std::memory_order_acquire);
87 const int free = (r > w ? r - w : capacity_ - w + r) - 1;
88 const int can_write = std::min(num_to_write < 0 ? 0 : num_to_write,
100 const int contiguous = capacity_ - w;
101 if (can_write <= contiguous) {
108 size_2 = can_write - contiguous;
117 if (num_written <= 0)
return;
118 const int w = write_pos_.load(std::memory_order_relaxed);
123 int next = (w + num_written) % capacity_;
124 if (next < 0) next += capacity_;
125 write_pos_.store(next, std::memory_order_release);
132 int& start_1,
int& size_1,
133 int& start_2,
int& size_2)
const noexcept {
134 const int r = read_pos_.load(std::memory_order_relaxed);
135 const int w = write_pos_.load(std::memory_order_acquire);
136 const int available = (w >= r ? w - r : capacity_ - r + w);
137 const int can_read = std::min(num_to_read < 0 ? 0 : num_to_read,
149 const int contiguous = capacity_ - r;
150 if (can_read <= contiguous) {
157 size_2 = can_read - contiguous;
163 if (num_read <= 0)
return;
164 const int r = read_pos_.load(std::memory_order_relaxed);
167 int next = (r + num_read) % capacity_;
168 if (next < 0) next += capacity_;
169 read_pos_.store(next, std::memory_order_release);
179 std::atomic<int> read_pos_{0};
180 std::atomic<int> write_pos_{0};
SpscRingIndex(SpscRingIndex &&)=delete
void finish_write(int num_written) noexcept
Advance the write cursor by num_written items.
Definition spsc_ring_index.hpp:116
int num_ready() const noexcept
Number of items currently available to read.
Definition spsc_ring_index.hpp:56
int free_space() const noexcept
Free space (in items) available for new writes.
Definition spsc_ring_index.hpp:65
void prepare_to_read(int num_to_read, int &start_1, int &size_1, int &start_2, int &size_2) const noexcept
Plan a read of up to num_to_read items.
Definition spsc_ring_index.hpp:131
SpscRingIndex(const SpscRingIndex &)=delete
SpscRingIndex & operator=(const SpscRingIndex &)=delete
int capacity() const noexcept
Total size of the managed buffer in items.
Definition spsc_ring_index.hpp:53
void reset() noexcept
Reset the cursors back to empty.
Definition spsc_ring_index.hpp:71
SpscRingIndex(int capacity) noexcept
Construct an SpscRingIndex that manages indices in [0, capacity).
Definition spsc_ring_index.hpp:49
SpscRingIndex & operator=(SpscRingIndex &&)=delete
void finish_read(int num_read) noexcept
Advance the read cursor by num_read items.
Definition spsc_ring_index.hpp:162
void prepare_to_write(int num_to_write, int &start_1, int &size_1, int &start_2, int &size_2) const noexcept
Plan a write of up to num_to_write items.
Definition spsc_ring_index.hpp:82
Definition generated_event_source.hpp:33