File size: 1,781 Bytes
311d153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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