Data-ReqRep-Shared
view release on metacpan or search on metacpan
* start is what tells the dead process apart from the live one, which may be about to register. */
static inline int reqrep_record_dead(ReqRepProc r) {
if (reqrep_pid_gone(r.pid)) return 1;
uint32_t now;
return reqrep_proc_stat(r.pid, &now) == 0;
}
static const struct timespec reqrep_lock_timeout = { REQREP_LOCK_TIMEOUT_SEC, 0 };
/* SYS_futex takes the kernel's long-based timespec, narrower than time_t on 32-bit time64 ABIs. */
static inline long reqrep_futex_wait(uint32_t *addr, uint32_t val, const struct timespec *ts) {
#if !(defined(__x86_64__) && defined(__ILP32__))
if (ts && sizeof(ts->tv_sec) > sizeof(long)) {
struct { long tv_sec, tv_nsec; } k = { (long)ts->tv_sec, ts->tv_nsec };
return syscall(SYS_futex, addr, FUTEX_WAIT, val, &k, NULL, 0);
}
#endif
return syscall(SYS_futex, addr, FUTEX_WAIT, val, ts, NULL, 0);
}
/* fork() resets these in the child. */
static uint32_t reqrep_start_cache;
static uint32_t reqrep_pid_cache;
static void reqrep_pid_forget(void) { __atomic_store_n(&reqrep_pid_cache, 0, __ATOMIC_RELAXED); __atomic_store_n(&reqrep_start_cache, 0, __ATOMIC_RELAXED); }
static void reqrep_pid_watch_forks(void) { pthread_atfork(NULL, NULL, reqrep_pid_forget); }
static inline uint32_t reqrep_self_pid(void) {
uint32_t pid = __atomic_load_n(&reqrep_pid_cache, __ATOMIC_RELAXED);
if (!pid) {
static pthread_once_t once = PTHREAD_ONCE_INIT;
pthread_once(&once, reqrep_pid_watch_forks);
pid = (uint32_t)getpid();
__atomic_store_n(&reqrep_pid_cache, pid, __ATOMIC_RELAXED);
}
return pid;
}
static inline int reqrep_remaining_time(const struct timespec *deadline,
struct timespec *remaining) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
remaining->tv_sec = deadline->tv_sec - now.tv_sec;
remaining->tv_nsec = deadline->tv_nsec - now.tv_nsec;
if (remaining->tv_nsec < 0) {
remaining->tv_sec--;
remaining->tv_nsec += 1000000000L;
}
return remaining->tv_sec >= 0;
}
static inline uint64_t reqrep_now_ns(void) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (uint64_t)t.tv_sec * 1000000000ULL + (uint64_t)t.tv_nsec;
}
static inline void reqrep_make_deadline(double timeout, struct timespec *deadline) {
clock_gettime(CLOCK_MONOTONIC, deadline);
if (!(timeout < 1e9)) timeout = 1e9; /* Inf/NaN/huge: the time_t cast would be UB */
deadline->tv_sec += (time_t)timeout;
deadline->tv_nsec += (long)((timeout - (double)(time_t)timeout) * 1e9);
if (deadline->tv_nsec >= 1000000000L) {
deadline->tv_sec++;
deadline->tv_nsec -= 1000000000L;
}
}
static inline double reqrep_time_left(const struct timespec *deadline) {
struct timespec left;
if (!reqrep_remaining_time(deadline, &left)) return 1e-9;
return (double)left.tv_sec + (double)left.tv_nsec / 1e9;
}
/* A waiter count: [epoch:32][count:32]. A waiter takes back its registration only in the epoch
* it registered in, so a new epoch forgets the registrations of waiters that died parked. */
#define REQREP_WAITERS(w) ((uint32_t)(w))
/* Wakes in a row that find nobody parked, with the count unchanged, before a waker starts a new
* epoch. Live waiters keep changing the count; one left by the dead stays put. */
#define REQREP_STALE_WAKES 8
static inline uint32_t reqrep_waiters_add(uint64_t *word) {
return (uint32_t)(__atomic_add_fetch(word, 1, __ATOMIC_ACQ_REL) >> 32);
}
static inline void reqrep_waiters_sub(uint64_t *word, uint32_t epoch) {
uint64_t w = __atomic_load_n(word, __ATOMIC_ACQUIRE);
while ((uint32_t)(w >> 32) == epoch && REQREP_WAITERS(w)
&& !__atomic_compare_exchange_n(word, &w, w - 1, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) {}
}
/* Start a new epoch unless the count moved since `seen`. A live waiter of the old epoch either
* parked before this wake-all or parks on a futex value already bumped, so none sleeps uncounted. */
static inline void reqrep_waiters_forget(uint32_t *futex_word, uint64_t *waiters, uint64_t seen) {
if (!__atomic_compare_exchange_n(waiters, &seen, (uint64_t)((uint32_t)(seen >> 32) + 1) << 32,
0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED))
return;
__atomic_add_fetch(futex_word, 1, __ATOMIC_RELEASE);
syscall(SYS_futex, futex_word, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
}
/* Called after the caller changed shared state: the fence publishes that change before the count
* is read, so a waiter registering concurrently is not missed. */
static inline void reqrep_wake(uint32_t *futex_word, uint64_t *waiters, int n, ReqRepWakeMemo *memo) {
__atomic_thread_fence(__ATOMIC_SEQ_CST);
uint64_t w = __atomic_load_n(waiters, __ATOMIC_RELAXED);
if (!REQREP_WAITERS(w)) return;
__atomic_add_fetch(futex_word, 1, __ATOMIC_RELEASE);
if (syscall(SYS_futex, futex_word, FUTEX_WAKE, n, NULL, NULL, 0) != 0) { memo->misses = 0; return; }
if (w != memo->seen) { memo->seen = w; memo->misses = 1; return; }
if (++memo->misses < REQREP_STALE_WAKES) return;
memo->misses = 0;
reqrep_waiters_forget(futex_word, waiters, w);
}
static inline void reqrep_wake_consumers(ReqRepHandle *h) {
reqrep_wake(&h->hdr->recv_futex, &h->hdr->recv_waiters, 1, &h->recv_wake);
}
static inline void reqrep_wake_producers(ReqRepHandle *h, uint32_t n) {
( run in 0.808 second using v1.01-cache-2.11-cpan-60884a8df76 )