-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspsc_ring_buffer.cpp
More file actions
34 lines (24 loc) · 954 Bytes
/
spsc_ring_buffer.cpp
File metadata and controls
34 lines (24 loc) · 954 Bytes
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
#include "spsc_ring_buffer.h"
bool SPSCQueue::put(int item) {
int write = m_write_index.load(std::memory_order_relaxed);
int next = (write + 1) & (m_size - 1);
if (next == m_read_index.load(std::memory_order_acquire)) return false; // full
m_buffer[write] = item;
m_write_index.store(next, std::memory_order_release);
return true;
}
bool SPSCQueue::get(int& item) {
int read = m_read_index.load(std::memory_order_relaxed);
if (read == m_write_index.load(std::memory_order_acquire)) return false; //empty
item = m_buffer[read];
m_read_index.store((read + 1) & (m_size - 1), std::memory_order_release);
return true;
}
bool SPSCQueue::debugIsEmpty() const {
return m_read_index.load(std::memory_order_acquire) ==
m_write_index.load(std::memory_order_acquire);
}
bool SPSCQueue::debugIsFull() const {
return ((m_write_index.load(std::memory_order_acquire) + 1) & (m_size - 1)) ==
m_read_index.load(std::memory_order_acquire);
}