Data-Heap-Shared
view release on metacpan or search on metacpan
if (!rp || rp + 2 >= buf + n) return 0; /* need ") X" within the bytes read */
return rp[1] == ' ' && rp[2] == 'Z';
}
static inline int heap_pid_alive(uint32_t pid) {
if (pid == 0) return 0; /* no owner recorded, treat as dead */
if (kill((pid_t)pid, 0) == -1 && errno == ESRCH) return 0; /* definitely dead */
return !heap_pid_is_zombie(pid); /* kill() also succeeds for a zombie -> treat as dead */
}
static inline void heap_mutex_lock(HeapHandle *h) {
HeapHeader *hdr = h->hdr;
uint32_t mypid = HEAP_MUTEX_BIT | ((uint32_t)getpid() & HEAP_MUTEX_PID);
for (int spin = 0; ; spin++) {
uint32_t expected = 0;
if (__atomic_compare_exchange_n(&hdr->mutex, &expected, mypid,
1, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
return;
if (spin < HEAP_SPIN_LIMIT) {
#if defined(__x86_64__) || defined(__i386__)
__asm__ volatile("pause" ::: "memory");
#elif defined(__aarch64__)
__asm__ volatile("yield" ::: "memory");
#endif
continue;
}
__atomic_add_fetch(&hdr->mutex_waiters, 1, __ATOMIC_RELAXED);
uint32_t cur = __atomic_load_n(&hdr->mutex, __ATOMIC_RELAXED);
if (cur != 0) {
long rc = syscall(SYS_futex, &hdr->mutex, FUTEX_WAIT, cur,
&heap_lock_timeout, NULL, 0);
if (rc == -1 && errno == ETIMEDOUT && cur >= HEAP_MUTEX_BIT) {
uint32_t pid = cur & HEAP_MUTEX_PID;
if (!heap_pid_alive(pid)) {
if (__atomic_compare_exchange_n(&hdr->mutex, &cur, mypid,
0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED)) {
__atomic_sub_fetch(&hdr->mutex_waiters, 1, __ATOMIC_RELAXED);
__atomic_add_fetch(&hdr->stat_recoveries, 1, __ATOMIC_RELAXED);
heap_journal_replay(hdr, h->data, h->capacity);
return;
}
}
}
}
__atomic_sub_fetch(&hdr->mutex_waiters, 1, __ATOMIC_RELAXED);
spin = 0;
}
}
static inline void heap_mutex_unlock(HeapHeader *hdr) {
__atomic_store_n(&hdr->mutex, 0, __ATOMIC_RELEASE);
if (__atomic_load_n(&hdr->mutex_waiters, __ATOMIC_RELAXED) > 0)
syscall(SYS_futex, &hdr->mutex, FUTEX_WAKE, 1, NULL, NULL, 0);
}
/* ================================================================
* Public API
* ================================================================ */
static inline void heap_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 heap_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;
}
static inline int heap_push(HeapHandle *h, int64_t priority, int64_t value) {
HeapHeader *hdr = h->hdr;
heap_mutex_lock(h);
uint32_t cur = hdr->size;
if (cur >= h->capacity) {
heap_mutex_unlock(hdr);
return 0;
}
HeapEntry e = { priority, value };
if (cur > 0 && h->data[(cur - 1) / 2].priority > priority) {
heap_journal_begin(hdr, e, HEAP_J_UP, cur + 1, cur);
hdr->size = cur + 1;
__atomic_signal_fence(__ATOMIC_SEQ_CST);
heap_sift_up(hdr, h->data, cur, e);
} else {
h->data[cur] = e;
/* The size publishes the entry: a pusher killed before it must leave nothing to pop. */
__atomic_signal_fence(__ATOMIC_SEQ_CST);
hdr->size = cur + 1;
}
__atomic_add_fetch(&hdr->stat_pushes, 1, __ATOMIC_RELAXED);
heap_mutex_unlock(hdr);
/* Wake pop-waiters. Full barrier so our size store (under the lock) is
* globally ordered before this waiters_pop load; pairs with the matching
* fence in heap_pop_wait so a concurrent waiter either observes our push
* (via the futex value re-check) or we observe its waiters_pop increment
* -- Dekker, no lost wakeup on weakly-ordered CPUs. */
__atomic_thread_fence(__ATOMIC_SEQ_CST);
if (__atomic_load_n(&hdr->waiters_pop, __ATOMIC_SEQ_CST) > 0)
syscall(SYS_futex, &hdr->size, FUTEX_WAKE, 1, NULL, NULL, 0);
return 1;
}
static inline int heap_pop(HeapHandle *h, int64_t *out_priority, int64_t *out_value) {
HeapHeader *hdr = h->hdr;
heap_mutex_lock(h);
uint32_t cur = hdr->size;
if (cur == 0) {
heap_mutex_unlock(hdr);
return 0;
}
/* Layer B: size is read from the shared segment and used just below as an
* array index (h->data[size]) and as the sift-down bound. A local peer
* with write access to the backing file can corrupt it past capacity,
* which would drive an out-of-bounds read/write. A valid heap always
* keeps size <= capacity (push enforces it), so this never fires for
( run in 0.679 second using v1.01-cache-2.11-cpan-60884a8df76 )