Data-RingBuffer-Shared
view release on metacpan or search on metacpan
/* ================================================================
* Read -- by relative position (0=latest) or absolute sequence
* ================================================================ */
/* Read by absolute sequence number. Returns 1 if data for that seq was
* observed intact, 0 if not-yet-written / overwritten / mid-write. */
static inline int ring_read_seq(RingHandle *h, uint64_t seq, void *out) {
uint64_t head = __atomic_load_n(&h->hdr->head, __ATOMIC_ACQUIRE);
if (seq >= head) return 0; /* not yet written */
uint64_t oldest = (head > h->capacity) ? head - h->capacity : 0;
if (seq < oldest) return 0; /* already overwritten */
uint64_t slot_idx = seq % h->capacity;
uint64_t expected = (seq + 1) << 1; /* even mark: pos=seq committed */
for (int retry = 0; retry < 8; retry++) {
uint64_t s1 = __atomic_load_n(&h->seq[slot_idx], __ATOMIC_ACQUIRE);
if (s1 & 1) continue; /* writer in progress: spin and retry */
if (s1 != expected) return 0; /* stale epoch (overwritten) */
/* The payload acquire orders the epoch re-check after the value load. */
uint64_t bits = __atomic_load_n((uint64_t *)(h->data + slot_idx * h->elem_size), __ATOMIC_ACQUIRE);
uint64_t s2 = __atomic_load_n(&h->seq[slot_idx], __ATOMIC_RELAXED);
if (s1 == s2) {
memcpy(out, &bits, sizeof(bits));
return 1;
}
}
return 0; /* too much contention to get a clean read */
}
/* Read the nth most recent value (0=latest, 1=previous, ...).
* Returns 1 on success, 0 if n >= available entries or slot is unstable. */
static inline int ring_read_latest(RingHandle *h, uint32_t n, void *out) {
uint64_t head = __atomic_load_n(&h->hdr->head, __ATOMIC_ACQUIRE);
if (head == 0) return 0;
uint64_t avail = head < h->capacity ? head : h->capacity;
if (n >= avail) return 0;
return ring_read_seq(h, head - 1 - n, out);
}
/* ================================================================
* Status
* ================================================================ */
static inline uint64_t ring_head(RingHandle *h) {
return __atomic_load_n(&h->hdr->head, __ATOMIC_ACQUIRE);
}
static inline uint64_t ring_size(RingHandle *h) {
uint64_t head = __atomic_load_n(&h->hdr->head, __ATOMIC_ACQUIRE);
return head < h->hdr->capacity ? head : h->hdr->capacity;
}
/* ================================================================
* Wait -- block until new data arrives
* ================================================================ */
static inline void ring_make_deadline(double t, struct timespec *dl) {
clock_gettime(CLOCK_MONOTONIC, dl);
if (!(t < 1e9)) t = 1e9; /* clamp Inf/NaN/huge: avoid UB (time_t) cast -> instant spurious timeout */
dl->tv_sec += (time_t)t;
dl->tv_nsec += (long)((t - (double)(time_t)t) * 1e9);
if (dl->tv_nsec >= 1000000000L) { dl->tv_sec++; dl->tv_nsec -= 1000000000L; }
}
static inline int ring_remaining(const struct timespec *dl, struct timespec *rem) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
rem->tv_sec = dl->tv_sec - now.tv_sec;
rem->tv_nsec = dl->tv_nsec - now.tv_nsec;
if (rem->tv_nsec < 0) { rem->tv_sec--; rem->tv_nsec += 1000000000L; }
return rem->tv_sec >= 0;
}
/* Returns -1 on syscall errors, with waiters balanced before handling signals. */
static inline int ring_wait_until(RingHandle *h, uint64_t expected_count, const struct timespec *dl) {
if (__atomic_load_n(&h->hdr->count, __ATOMIC_ACQUIRE) != expected_count) return 1;
struct timespec rem;
for (;;) {
/* Register before checking count, in the writer's seq-cst order. */
__atomic_add_fetch(&h->hdr->waiters, 1, __ATOMIC_SEQ_CST);
uint32_t seq = __atomic_load_n(&h->hdr->wake_seq, __ATOMIC_ACQUIRE);
uint64_t cur = __atomic_load_n(&h->hdr->count, __ATOMIC_SEQ_CST);
int error = 0;
if (cur == expected_count) {
struct timespec *pts = NULL;
if (dl) {
if (!ring_remaining(dl, &rem)) {
__atomic_sub_fetch(&h->hdr->waiters, 1, __ATOMIC_RELAXED);
return 0;
}
pts = &rem;
}
if (syscall(SYS_futex, &h->hdr->wake_seq, FUTEX_WAIT, seq, pts, NULL, 0) < 0)
error = errno;
}
__atomic_sub_fetch(&h->hdr->waiters, 1, __ATOMIC_RELAXED);
if (error && error != EAGAIN && error != ETIMEDOUT) { errno = error; return -1; }
if (__atomic_load_n(&h->hdr->count, __ATOMIC_ACQUIRE) != expected_count) return 1;
if (dl && !ring_remaining(dl, &rem)) return 0;
}
}
static inline int ring_wait(RingHandle *h, uint64_t expected_count, double timeout) {
if (__atomic_load_n(&h->hdr->count, __ATOMIC_ACQUIRE) != expected_count) return 1;
if (timeout == 0) return 0;
struct timespec dl;
if (timeout > 0) ring_make_deadline(timeout, &dl);
return ring_wait_until(h, expected_count, timeout > 0 ? &dl : NULL);
}
/* ================================================================
* Create / Open / Close
* ================================================================ */
#define RING_ERR(fmt, ...) do { if (errbuf) snprintf(errbuf, RING_ERR_BUFLEN, fmt, ##__VA_ARGS__); } while(0)
/* Layout offsets:
* seq_off = sizeof(RingHeader) (128)
( run in 1.432 second using v1.01-cache-2.11-cpan-60884a8df76 )