Data-Stack-Shared
view release on metacpan or search on metacpan
/* Slot state (low 2 bits of ctl word). Upper 62 bits = generation. */
#define STK_SLOT_EMPTY 0u
#define STK_SLOT_WRITING 1u
#define STK_SLOT_FILLED 2u
#define STK_SLOT_READING 3u
#define STK_SLOT_STATE_MASK 3u
#define STK_SLOT_STATE(c) ((uint32_t)((c) & STK_SLOT_STATE_MASK))
#define STK_SLOT_GEN(c) ((c) >> 2)
/* ================================================================
* Header (128 bytes)
* ================================================================ */
typedef struct {
uint32_t magic;
uint32_t version;
uint32_t elem_size;
uint32_t variant_id;
uint64_t capacity;
uint64_t total_size;
uint64_t data_off;
uint64_t ctl_off; /* offset to per-slot ctl array */
uint8_t _pad0[16];
uint32_t top; /* 64: next free index (0=empty, capacity=full) */
uint32_t waiters_push; /* 68 */
uint32_t waiters_pop; /* 72 */
uint32_t push_wake_seq; /* 76: bumped by every pop, futex word for pushers */
uint32_t pop_wake_seq; /* 80: bumped by every push, futex word for poppers */
uint32_t _pad1; /* 84 */
uint64_t stat_pushes; /* 88 */
uint64_t stat_pops; /* 96 */
uint64_t stat_waits; /* 104 */
uint64_t stat_timeouts; /* 112 */
uint8_t _pad2[8]; /* 120-127 */
} StkHeader;
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
_Static_assert(sizeof(StkHeader) == 128, "StkHeader must be 128 bytes");
#endif
typedef struct {
StkHeader *hdr;
uint8_t *data;
uint64_t *ctl; /* per-slot state+generation word */
size_t mmap_size;
uint32_t elem_size; /* cached from header at open time */
uint64_t capacity; /* cached from header at open time -- trusted bound */
char *path;
int notify_fd;
int backing_fd;
} StkHandle;
/* ================================================================
* Utility
* ================================================================ */
static inline void stk_make_deadline(double timeout, struct timespec *dl) {
clock_gettime(CLOCK_MONOTONIC, dl);
if (!(timeout < 1e9)) timeout = 1e9; /* clamp Inf/NaN/huge: avoid UB (time_t) cast -> instant spurious timeout */
dl->tv_sec += (time_t)timeout;
dl->tv_nsec += (long)((timeout - (double)(time_t)timeout) * 1e9);
if (dl->tv_nsec >= 1000000000L) { dl->tv_sec++; dl->tv_nsec -= 1000000000L; }
}
static inline int stk_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 void stk_futex_wait(uint32_t *word, uint32_t seq, struct timespec *timeout) {
#ifdef STK_WAIT_SLICE_NS
/* Poll deferred signals even if one arrived just before the syscall. */
struct timespec slice = { 0, STK_WAIT_SLICE_NS };
if (!timeout || timeout->tv_sec > 0 || timeout->tv_nsec > slice.tv_nsec)
timeout = &slice;
#endif
syscall(SYS_futex, word, FUTEX_WAIT, seq, timeout, NULL, 0);
}
static inline uint8_t *stk_slot(StkHandle *h, uint32_t idx) {
return h->data + (uint64_t)idx * h->elem_size;
}
static inline void stk_spin_pause(void) {
#if defined(__x86_64__) || defined(__i386__)
__asm__ volatile("pause" ::: "memory");
#elif defined(__aarch64__)
__asm__ volatile("yield" ::: "memory");
#endif
}
/* Slot state machine -- same pattern as Data::Deque::Shared.
*
* Bounded wait for `want`, with abandoned-slot recovery.
*
* A peer that died between claiming a slot and publishing/releasing it leaves
* the slot stuck in WRITING (crashed pusher) or READING (crashed popper). An
* unbounded spin here wedges EVERY process using the stack, forever, at 100%
* CPU -- stk_drain already bounds its own wait for exactly this reason (see the
* comment there); push/pop must do the same or a single SIGKILL is a permanent
* cluster-wide DoS. We hot-spin briefly, then sleep, and on a ~2s deadline force
* the slot back to EMPTY with the generation bumped.
*
* Returns 1 with *out_gen set when the slot was claimed in state `want`.
* Returns 0 when the slot was abandoned and force-reclaimed.
*
* Same false-positive caveat as stk_drain: ctl encodes no PID, so a live peer
* stalled >2s is indistinguishable from a dead one. Its later publish/release is
* a CAS against the old generation (stk_slot_publish), so it no-ops instead of
* resurrecting a phantom slot; the value is dropped exactly as for a real crash.
* The claim->publish window is a sub-microsecond memcpy, so the threshold is
* many orders of magnitude above normal latency. */
static inline int stk_slot_wait_state(uint64_t *ctl_word, uint32_t want,
uint64_t *out_gen) {
struct timespec dl;
( run in 0.977 second using v1.01-cache-2.11-cpan-60884a8df76 )