Data-Deque-Shared

 view release on metacpan or  search on metacpan

deque.h  view on Meta::CPAN

#define DEQ_SLOT_STATE_MASK  3u
#define DEQ_SLOT_STATE(c)    ((uint32_t)((c) & DEQ_SLOT_STATE_MASK))
#define DEQ_SLOT_GEN(c)      ((c) >> 2)

/* Combined cursor: upper 32 bits = head, lower 32 bits = tail. A single
 * 64-bit CAS atomically updates both ends, so push_front vs push_back (or
 * pop_front vs pop_back) cannot both succeed when they share a boundary
 * slot. Capacity is bounded by 2^31 elements. Head and tail wrap mod 2^32
 * after 4B ops; size = (tail - head) treated as uint32.
 */
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 */
    uint32_t push_wait_hint;   /* registration generation + active bit */
    uint32_t pop_wait_hint;
    uint8_t  _pad0[8];

    uint64_t cursor;           /* 64: (head<<32)|tail */
    uint32_t waiters_push;     /* 72 */
    uint32_t waiters_pop;      /* 76 */
    uint64_t stat_pushes;      /* 80 */
    uint64_t stat_pops;        /* 88 */
    uint64_t stat_waits;       /* 96 */
    uint64_t stat_timeouts;    /* 104 */
    uint32_t push_wake_seq;    /* 112: futex word for pushers */
    uint32_t pop_wake_seq;     /* 116: futex word for poppers */
    uint64_t stat_recoveries;  /* 120: recovery of abandoned/stuck slots (WRITING, READING, or EMPTY) */
} DeqHeader;

#define DEQ_CURSOR(head, tail)  (((uint64_t)(head) << 32) | (uint32_t)(tail))
#define DEQ_CURSOR_HEAD(c)      ((uint32_t)((c) >> 32))
#define DEQ_CURSOR_TAIL(c)      ((uint32_t)(c))
#define DEQ_CURSOR_SIZE(c)      ((uint32_t)(DEQ_CURSOR_TAIL(c) - DEQ_CURSOR_HEAD(c)))

#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
_Static_assert(sizeof(DeqHeader) == 128, "DeqHeader must be 128 bytes");
#endif

typedef struct {
    DeqHeader *hdr;
    uint8_t   *data;
    uint64_t  *ctl;            /* per-slot state+generation word */
    size_t     mmap_size;
    uint32_t   elem_size;
    uint32_t   capacity;       /* cached-at-attach ring size (fixed geometry) */
    char      *path;
    int        notify_fd;
    int        backing_fd;
} DeqHandle;

/* ================================================================ */

static inline void deq_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 deq_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 uint64_t *deq_ctl_at(DeqHandle *h, uint32_t idx) {
    return &h->ctl[idx];
}

static inline void deq_copy(void *dst, const void *src, uint32_t size) {
    if (size == sizeof(uint64_t)) memcpy(dst, src, sizeof(uint64_t));
    else memcpy(dst, src, size);
}

static inline uint32_t deq_size(DeqHandle *h) {
    uint64_t c = __atomic_load_n(&h->hdr->cursor, __ATOMIC_ACQUIRE);
    return DEQ_CURSOR_SIZE(c);
}

static inline void deq_spin_pause(void) {
#if defined(__x86_64__) || defined(__i386__)
    __asm__ volatile("pause" ::: "memory");
#elif defined(__aarch64__)
    __asm__ volatile("yield" ::: "memory");
#endif
}

static inline void deq_after_rmw_fence(void) {
#if defined(__x86_64__) || defined(__i386__)
    /* The preceding locked RMW already supplies the CPU fence. */
    __asm__ volatile("" ::: "memory");
#else
    __atomic_thread_fence(__ATOMIC_SEQ_CST);
#endif
}

static inline void deq_waiter_enter(uint32_t *count, uint32_t *hint) {
    __atomic_add_fetch(count, 1, __ATOMIC_RELEASE);
    uint32_t c = __atomic_load_n(hint, __ATOMIC_RELAXED);
    while (!__atomic_compare_exchange_n(hint, &c, (c + 2u) | 1u,
            1, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) {}
}

static inline void deq_waiter_leave(uint32_t *count, uint32_t *hint) {
    uint32_t c = __atomic_load_n(hint, __ATOMIC_RELAXED);
    if (__atomic_sub_fetch(count, 1, __ATOMIC_RELAXED) == 0)
        __atomic_compare_exchange_n(hint, &c, c & ~1u,
                0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
}

static void deq_wake_slow(DeqHeader *hdr, int push, int n, uint32_t c) {



( run in 0.879 second using v1.01-cache-2.11-cpan-60884a8df76 )