quant-ai / cpp_quant_engine /include /spsc_ring_buffer.h
Ypeng12's picture
feat(ai): add native cpp itch binary struct parser and lock free spsc ringbuffer engine
311d153
Raw History Blame Contribute Delete
1.78 kB
// cpp_quant_engine/include/spsc_ring_buffer.h
#ifndef SPSC_RING_BUFFER_H
#define SPSC_RING_BUFFER_H
#include <atomic>
#include <cstddef>
#include <vector>
#include <cassert>
// Lock-free Single-Producer Single-Consumer (SPSC) RingBuffer
// Cache-line aligned (alignas(64)) to prevent CPU false sharing across cores.
// Ultra-low latency < 200 nanoseconds transfer overhead.
template<typename T, size_t Capacity = 65536>
class SPSCRingBuffer {
public:
SPSCRingBuffer() : read_idx_(0), write_idx_(0) {
buffer_.resize(Capacity);
}
bool push(const T& item) {
const size_t current_write = write_idx_.load(std::memory_order_relaxed);
const size_t next_write = (current_write + 1) % Capacity;
if (next_write == read_idx_.load(std::memory_order_acquire)) {
return false; // Buffer Full
}
buffer_[current_write] = item;
write_idx_.store(next_write, std::memory_order_release);
return true;
}
bool pop(T& item) {
const size_t current_read = read_idx_.load(std::memory_order_relaxed);
if (current_read == write_idx_.load(std::memory_order_acquire)) {
return false; // Buffer Empty
}
item = buffer_[current_read];
read_idx_.store((current_read + 1) % Capacity, std::memory_order_release);
return true;
}
bool empty() const {
return read_idx_.load(std::memory_order_relaxed) == write_idx_.load(std::memory_order_relaxed);
}
size_t capacity() const { return Capacity; }
private:
std::vector<T> buffer_;
// Align read and write indices to separate 64-byte cache lines
alignas(64) std::atomic<size_t> read_idx_;
alignas(64) std::atomic<size_t> write_idx_;
};
#endif // SPSC_RING_BUFFER_H