| 1 |
|
| /* |
| 2 |
|
| * queue.h -- Shared-memory MPMC queue for Linux |
| 3 |
|
| * |
| 4 |
|
| * Two variants: |
| 5 |
|
| * Int -- lock-free Vyukov bounded MPMC queue (int64 values) |
| 6 |
|
| * Str -- futex-mutex protected queue with circular arena (byte strings) |
| 7 |
|
| * |
| 8 |
|
| * Both use file-backed mmap(MAP_SHARED) for cross-process sharing, |
| 9 |
|
| * futex for blocking wait, and PID-based stale lock recovery. |
| 10 |
|
| */ |
| 11 |
|
| |
| 12 |
|
| #ifndef QUEUE_H |
| 13 |
|
| #define QUEUE_H |
| 14 |
|
| |
| 15 |
|
| #include <stddef.h> |
| 16 |
|
| #include <stdint.h> |
| 17 |
|
| #include <stdbool.h> |
| 18 |
|
| #include <stdlib.h> |
| 19 |
|
| #include <string.h> |
| 20 |
|
| #include <unistd.h> |
| 21 |
|
| #include <fcntl.h> |
| 22 |
|
| #include <errno.h> |
| 23 |
|
| #include <time.h> |
| 24 |
|
| #include <limits.h> |
| 25 |
|
| #include <signal.h> |
| 26 |
|
| #include <sys/mman.h> |
| 27 |
|
| #include <sys/stat.h> |
| 28 |
|
| #include <sys/file.h> |
| 29 |
|
| #include <sys/syscall.h> |
| 30 |
|
| #include <linux/futex.h> |
| 31 |
|
| #include <sys/eventfd.h> |
| 32 |
|
| |
| 33 |
|
| /* ================================================================ |
| 34 |
|
| * Constants |
| 35 |
|
| * ================================================================ */ |
| 36 |
|
| |
| 37 |
|
| #define QUEUE_MAGIC 0x51554531U /* "QUE1" */ |
| 38 |
|
| #define QUEUE_VERSION 2 |
| 39 |
|
| #define QUEUE_MODE_INT 0 |
| 40 |
|
| #define QUEUE_MODE_STR 1 |
| 41 |
|
| #define QUEUE_MODE_INT32 2 |
| 42 |
|
| #define QUEUE_MODE_INT16 3 |
| 43 |
|
| #define QUEUE_ERR_BUFLEN (PATH_MAX + 256) |
| 44 |
|
| #define QUEUE_SPIN_LIMIT 32 |
| 45 |
|
| /* Period of the safety retries in the wait loops, and the longest wait before a lock holder is checked. */ |
| 46 |
|
| #define QUEUE_RECHECK_SEC 2 |
| 47 |
|
| #define QUEUE_RECHECK_NS (QUEUE_RECHECK_SEC * 1000000000L) |
| 48 |
|
| /* A lock waiter first checks the holder is alive after this, doubling up to QUEUE_RECHECK_NS. */ |
| 49 |
|
| #define QUEUE_LOCK_CHECK_NS 1000000L |
| 50 |
|
| /* Arena bytes populated per step, under the mutex: small, as every producer waits for it. */ |
| 51 |
|
| #define QUEUE_POPULATE_CHUNK (16u << 10) |
| 52 |
|
| |
| 53 |
|
| /* ================================================================ |
| 54 |
|
| * Header (256 bytes = 4 cache lines, lives at start of mmap) |
| 55 |
|
| * ================================================================ */ |
| 56 |
|
| |
| 57 |
|
| typedef struct { |
| 58 |
|
| /* ---- Cache line 0 (0-63): immutable after create ---- */ |
| 59 |
|
| uint32_t magic; /* 0 */ |
| 60 |
|
| uint32_t version; /* 4 */ |
| 61 |
|
| uint32_t mode; /* 8: QUEUE_MODE_INT or QUEUE_MODE_STR */ |
| 62 |
|
| uint32_t capacity; /* 12: max elements (power of 2) */ |
| 63 |
|
| uint64_t total_size; /* 16: mmap size */ |
| 64 |
|
| uint64_t slots_off; /* 24: offset to slot array */ |
| 65 |
|
| uint64_t arena_off; /* 32: str mode: offset to arena; int: 0 */ |
| 66 |
|
| uint64_t arena_cap; /* 40: str mode: arena byte capacity; int: 0 */ |
| 67 |
|
| uint64_t pidns; /* 48: creator's PID namespace inode, 0 if unknown */ |
| 68 |
|
| uint32_t pidns_mixed; /* 56: set once a process of another PID namespace attaches */ |
| 69 |
|
| uint8_t _pad0[4]; /* 60-63 */ |
| 70 |
|
| |
| 71 |
|
| /* ---- Cache line 1 (64-127): head / consumer hot ---- */ |
| 72 |
|
| uint64_t head; /* 64: consumer position */ |
| 73 |
|
| uint32_t pop_waiters; /* 72: count of blocked consumers */ |
| 74 |
|
| uint32_t pop_futex; /* 76: futex word for consumer wakeup */ |
| 75 |
|
| uint8_t _pad1[48]; /* 80-127 */ |
| 76 |
|
| |
| 77 |
|
| /* ---- Cache line 2 (128-191): tail / producer hot ---- */ |
| 78 |
|
| uint64_t tail; /* 128: producer position */ |
| 79 |
|
| uint32_t push_waiters; /* 136: count of blocked producers */ |
| 80 |
|
| uint32_t push_futex; /* 140: futex word for producer wakeup */ |
| 81 |
|
| uint64_t stat_lock_waits; /* 144: Str lock acquisitions that slept */ |
| 82 |
|
| uint64_t stat_lock_wait_ns; /* 152 */ |
| 83 |
|
| uint64_t stat_lock_wait_max_ns;/* 160 */ |
| 84 |
|
| uint64_t stat_lock_timeouts; /* 168 */ |
| 85 |
|
| uint64_t stat_last_recovery; /* 176: CLOCK_REALTIME seconds */ |
| 86 |
|
| uint8_t _pad2[8]; /* 184-191 */ |
| 87 |
|
| |
| 88 |
|
| /* ---- Cache line 3 (192-255): mutex + arena state + stats ---- */ |
| 89 |
|
| uint32_t mutex; /* 192: futex-based mutex (0 or PID|0x80000000) */ |
| 90 |
|
| uint32_t mutex_waiters; /* 196 */ |
| 91 |
|
| union { |
| 92 |
|
| struct { |
| 93 |
|
| uint32_t arena_wpos; /* 200 */ |
| 94 |
|
| uint32_t arena_used; /* 204 */ |
| 95 |
|
| }; |
| 96 |
|
| uint64_t arena_state; |
| 97 |
|
| }; |
| 98 |
|
| uint64_t stat_push_ok; /* 208 */ |
| 99 |
|
| uint64_t stat_pop_ok; /* 216 */ |
| 100 |
|
| uint64_t stat_push_full; /* 224 */ |
| 101 |
|
| uint64_t stat_pop_empty; /* 232 */ |
| 102 |
|
| uint64_t stat_recoveries;/* 240 */ |
| 103 |
|
| uint32_t str_arena_wait; /* 248: a producer has run out of arena bytes */ |
| 104 |
|
| uint32_t arena_populated;/* 252: arena bytes below this are allocated */ |
| 105 |
|
| } QueueHeader; |
| 106 |
|
| |
| 107 |
|
| #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L |
| 108 |
|
| _Static_assert(sizeof(QueueHeader) == 256, "QueueHeader must be 256 bytes"); |
| 109 |
|
| #endif |
| 110 |
|
| |
| 111 |
|
| /* ================================================================ |
| 112 |
|
| * Slot types |
| 113 |
|
| * ================================================================ */ |
| 114 |
|
| |
| 115 |
|
| /* Int slot: Vyukov MPMC sequence + value */ |
| 116 |
|
| typedef struct { |
| 117 |
|
| uint64_t sequence; |
| 118 |
|
| int64_t value; |
| 119 |
|
| } QueueIntSlot; /* 16 bytes */ |
| 120 |
|
| |
| 121 |
|
| /* Compact int slots: 32-bit sequence + value = 8 bytes (2x cache density) */ |
| 122 |
|
| typedef struct { |
| 123 |
|
| uint32_t sequence; |
| 124 |
|
| int32_t value; |
| 125 |
|
| } QueueInt32Slot; /* 8 bytes */ |
| 126 |
|
| |
| 127 |
|
| typedef struct { |
| 128 |
|
| uint32_t sequence; |
| 129 |
|
| int16_t value; |
| 130 |
|
| int16_t _pad; |
| 131 |
|
| } QueueInt16Slot; /* 8 bytes */ |
| 132 |
|
| |
| 133 |
|
| /* Str slot: arena pointer + length + skip (for FIFO arena free) */ |
| 134 |
|
| typedef struct { |
| 135 |
|
| uint32_t arena_off; |
| 136 |
|
| uint32_t packed_len; /* bit 31 = UTF-8, bits 0-30 = byte length */ |
| 137 |
|
| uint32_t arena_skip; /* bytes to release from arena on pop (includes wrap waste) */ |
| 138 |
|
| uint32_t prev_wpos; /* arena_wpos before this push (for pop_back rollback) */ |
| 139 |
|
| } QueueStrSlot; /* 16 bytes */ |
| 140 |
|
| |
| 141 |
|
| #define QUEUE_STR_UTF8_FLAG 0x80000000U |
| 142 |
|
| #define QUEUE_STR_LEN_MASK 0x7FFFFFFFU |
| 143 |
|
| |
| 144 |
|
| /* ================================================================ |
| 145 |
|
| * Process-local handle |
| 146 |
|
| * ================================================================ */ |
| 147 |
|
| |
| 148 |
|
| typedef struct { |
| 149 |
|
| QueueHeader *hdr; |
| 150 |
|
| void *slots; /* QueueIntSlot* or QueueStrSlot* */ |
| 151 |
|
| char *arena; /* NULL for int mode */ |
| 152 |
|
| size_t mmap_size; |
| 153 |
|
| uint32_t capacity; |
| 154 |
|
| uint32_t cap_mask; /* capacity - 1 */ |
| 155 |
|
| uint64_t arena_cap; |
| 156 |
|
| char *copy_buf; /* for str pop: buffer to copy string before unlock */ |
| 157 |
|
| uint32_t copy_buf_cap; |
| 158 |
|
| char *path; |
| 159 |
|
| int notify_fd; /* eventfd for event-loop integration, -1 if disabled */ |
| 160 |
|
| int backing_fd; /* memfd fd, -1 for file-backed/anonymous */ |
| 161 |
|
| int notify_fd_owned; /* 1 if we created notify_fd and must close it */ |
| 162 |
|
| double lock_timeout; /* Str: seconds to wait for the mutex, < 0 for no limit */ |
| 163 |
|
| int fail_errno; |
| 164 |
|
| int same_pidns; /* this process sees the creator's PIDs */ |
| 165 |
|
| } QueueHandle; |
| 166 |
|
| |
| 167 |
|
| /* An operation that did nothing for a reason in h->fail_errno. */ |
| 168 |
|
| #define QUEUE_FAILED (-3) |
| 169 |
|
| |
| 170 |
|
| /* ================================================================ |
| 171 |
|
| * Utility |
| 172 |
|
| * ================================================================ */ |
| 173 |
|
| |
| 174 |
167 |
| static inline uint32_t queue_next_pow2(uint32_t v) { |
| 175 |
167 |
▶ | if (v < 2) return 2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v < 2) return 2; |
2 |
165 |
167 |
|
| 176 |
165 |
▶ | if (v > 0x80000000U) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v > 0x80000000U) return 0; |
0 |
165 |
165 |
|
| 177 |
165 |
| v--; |
| 178 |
165 |
| v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; |
| 179 |
165 |
| return v + 1; |
| 180 |
|
| } |
| 181 |
|
| |
| 182 |
3398 |
| static inline void queue_spin_pause(void) { |
| 183 |
|
| #if defined(__x86_64__) || defined(__i386__) |
| 184 |
3398 |
| __asm__ volatile("pause" ::: "memory"); |
| 185 |
|
| #elif defined(__aarch64__) |
| 186 |
|
| __asm__ volatile("yield" ::: "memory"); |
| 187 |
|
| #else |
| 188 |
|
| __asm__ volatile("" ::: "memory"); |
| 189 |
|
| #endif |
| 190 |
3398 |
| } |
| 191 |
|
| |
| 192 |
17835 |
| static inline int queue_ensure_copy_buf(QueueHandle *h, uint32_t needed) { |
| 193 |
17835 |
▶ | if (needed <= h->copy_buf_cap) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (needed <= h->copy_buf_cap) return 1; |
17783 |
52 |
17835 |
|
| 194 |
52 |
▶ | uint32_t ns = h->copy_buf_cap ? h->copy_buf_cap : 64; |
Branch: 1 branch
| Branch | True | False | Total |
| uint32_t ns = h->copy_buf_cap ? h->copy_buf_cap : 64; |
3 |
49 |
52 |
|
| 195 |
128 |
▶ | while (ns < needed) { uint32_t n2 = ns * 2; if (n2 <= ns) { ns = needed; break; } ns = n2; } |
Branch: 2 branches
| Branch | True | False | Total |
| while (ns < needed) { uint32_t n2 = ns * 2; if (n2 <= ns) { ns = needed; break; } ns = n2; } |
0 |
76 |
76 |
| while (ns < needed) { uint32_t n2 = ns * 2; if (n2 <= ns) { ns = needed; break; } ns = n2; } |
76 |
52 |
128 |
|
| 196 |
52 |
| char *nb = (char *)realloc(h->copy_buf, ns); |
| 197 |
52 |
▶ | if (!nb) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nb) return 0; |
0 |
52 |
52 |
|
| 198 |
52 |
| h->copy_buf = nb; |
| 199 |
52 |
| h->copy_buf_cap = ns; |
| 200 |
52 |
| return 1; |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
|
| /* Keep the arena front consistent if a process dies between metadata writes. */ |
| 204 |
21597 |
| static inline void queue_set_arena(QueueHeader *hdr, uint32_t wpos, uint32_t used) { |
| 205 |
|
| union { uint32_t parts[2]; uint64_t word; } state; |
| 206 |
21597 |
| state.parts[0] = wpos; |
| 207 |
21597 |
| state.parts[1] = used; |
| 208 |
21597 |
| __atomic_store_n(&hdr->arena_state, state.word, __ATOMIC_RELEASE); |
| 209 |
21597 |
| } |
| 210 |
|
| |
| 211 |
|
| /* Compute remaining timespec from absolute deadline. Returns 0 if deadline passed. */ |
| 212 |
2152 |
| static inline int queue_remaining_time(const struct timespec *deadline, |
| 213 |
|
| struct timespec *remaining) { |
| 214 |
|
| struct timespec now; |
| 215 |
2152 |
| clock_gettime(CLOCK_MONOTONIC, &now); |
| 216 |
2152 |
| remaining->tv_sec = deadline->tv_sec - now.tv_sec; |
| 217 |
2152 |
| remaining->tv_nsec = deadline->tv_nsec - now.tv_nsec; |
| 218 |
2152 |
▶ | if (remaining->tv_nsec < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (remaining->tv_nsec < 0) { |
2056 |
96 |
2152 |
|
| 219 |
2056 |
| remaining->tv_sec--; |
| 220 |
2056 |
| remaining->tv_nsec += 1000000000L; |
| 221 |
|
| } |
| 222 |
2152 |
| return remaining->tv_sec >= 0; |
| 223 |
|
| } |
| 224 |
|
| |
| 225 |
|
| /* Convert timeout in seconds (double) to absolute deadline */ |
| 226 |
50 |
| static inline void queue_make_deadline(double timeout, struct timespec *deadline) { |
| 227 |
50 |
| clock_gettime(CLOCK_MONOTONIC, deadline); |
| 228 |
50 |
▶ | if (!(timeout < 1e9)) timeout = 1e9; /* clamp Inf/NaN/huge: avoid UB (time_t) cast -> instant spurious timeout */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(timeout < 1e9)) timeout = 1e9; /* clamp Inf/NaN/huge: avoid UB (time_t) cast -> instant spurious timeout */ |
0 |
50 |
50 |
|
| 229 |
50 |
| deadline->tv_sec += (time_t)timeout; |
| 230 |
50 |
| deadline->tv_nsec += (long)((timeout - (double)(time_t)timeout) * 1e9); |
| 231 |
50 |
▶ | if (deadline->tv_nsec >= 1000000000L) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (deadline->tv_nsec >= 1000000000L) { |
4 |
46 |
50 |
|
| 232 |
4 |
| deadline->tv_sec++; |
| 233 |
4 |
| deadline->tv_nsec -= 1000000000L; |
| 234 |
|
| } |
| 235 |
50 |
| } |
| 236 |
|
| |
| 237 |
|
| /* ================================================================ |
| 238 |
|
| * Futex helpers |
| 239 |
|
| * ================================================================ */ |
| 240 |
|
| |
| 241 |
|
| #define QUEUE_MUTEX_WRITER_BIT 0x80000000U |
| 242 |
|
| #define QUEUE_MUTEX_PID_MASK 0x7FFFFFFFU |
| 243 |
|
| #define QUEUE_MUTEX_VAL(pid) (QUEUE_MUTEX_WRITER_BIT | ((uint32_t)(pid) & QUEUE_MUTEX_PID_MASK)) |
| 244 |
|
| |
| 245 |
|
| /* A zombie (dead but not yet reaped) still answers kill(pid,0) as alive, so a |
| 246 |
|
| * process that crashed while holding the lock and lingers unreaped would never |
| 247 |
|
| * be recovered. Treat /proc/<pid>/stat state 'Z' as dead. Linux-only (as is |
| 248 |
|
| * this module); if /proc is unreadable we fall back to "alive" (safe: we never |
| 249 |
|
| * force-recover a possibly-live holder). */ |
| 250 |
92 |
| static inline int queue_pid_is_zombie(uint32_t pid) { |
| 251 |
|
| char path[32], buf[256]; |
| 252 |
92 |
| snprintf(path, sizeof(path), "/proc/%u/stat", (unsigned)pid); |
| 253 |
92 |
| int fd = open(path, O_RDONLY | O_CLOEXEC); |
| 254 |
92 |
▶ | if (fd < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) return 0; |
0 |
92 |
92 |
|
| 255 |
92 |
| ssize_t n = read(fd, buf, sizeof(buf) - 1); |
| 256 |
92 |
| close(fd); |
| 257 |
92 |
▶ | if (n <= 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) return 0; |
0 |
92 |
92 |
|
| 258 |
92 |
| buf[n] = '\0'; |
| 259 |
|
| /* "pid (comm) state ..."; comm may contain ')', so scan to the last one. */ |
| 260 |
92 |
| char *rp = strrchr(buf, ')'); |
| 261 |
92 |
▶ | if (!rp || rp + 2 >= buf + n) return 0; /* need ") X" within the bytes read */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!rp || rp + 2 >= buf + n) return 0; /* need ") X" within the bytes read */ |
92 |
0 |
92 |
| if (!rp || rp + 2 >= buf + n) return 0; /* need ") X" within the bytes read */ |
0 |
92 |
92 |
|
| 262 |
92 |
▶ | return rp[1] == ' ' && rp[2] == 'Z'; |
Branch: 2 branches
| Branch | True | False | Total |
| return rp[1] == ' ' && rp[2] == 'Z'; |
92 |
0 |
92 |
| return rp[1] == ' ' && rp[2] == 'Z'; |
1 |
91 |
92 |
|
| 263 |
|
| } |
| 264 |
94 |
| static inline int queue_pid_alive(uint32_t pid) { |
| 265 |
94 |
▶ | if (pid == 0) return 0; /* no owner recorded, treat as dead */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (pid == 0) return 0; /* no owner recorded, treat as dead */ |
0 |
94 |
94 |
|
| 266 |
94 |
▶ | if (kill((pid_t)pid, 0) == -1 && errno == ESRCH) return 0; /* definitely dead */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (kill((pid_t)pid, 0) == -1 && errno == ESRCH) return 0; /* definitely dead */ |
2 |
92 |
94 |
| if (kill((pid_t)pid, 0) == -1 && errno == ESRCH) return 0; /* definitely dead */ |
2 |
0 |
2 |
|
| 267 |
92 |
▶ | if (queue_pid_is_zombie(pid)) return 0; /* zombie -> treat as dead */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (queue_pid_is_zombie(pid)) return 0; /* zombie -> treat as dead */ |
1 |
91 |
92 |
|
| 268 |
|
| /* Re-check kill(): if process was reaped during proc scan, ESRCH avoids 2s stall */ |
| 269 |
91 |
▶ | if (kill((pid_t)pid, 0) == -1 && errno == ESRCH) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (kill((pid_t)pid, 0) == -1 && errno == ESRCH) return 0; |
0 |
91 |
91 |
| if (kill((pid_t)pid, 0) == -1 && errno == ESRCH) return 0; |
0 |
0 |
0 |
|
| 270 |
91 |
| return 1; |
| 271 |
|
| } |
| 272 |
|
| |
| 273 |
|
| static const struct timespec queue_recheck = { QUEUE_RECHECK_SEC, 0 }; |
| 274 |
|
| |
| 275 |
|
| #ifndef MADV_WIPEONFORK |
| 276 |
|
| #define MADV_WIPEONFORK 18 |
| 277 |
|
| #endif |
| 278 |
|
| |
| 279 |
|
| /* getpid() costs a syscall per lock. The cache sits in a page MADV_WIPEONFORK zeroes in a forked |
| 280 |
|
| * child, so a child never takes the lock under its parent's PID. */ |
| 281 |
41633 |
| static uint32_t queue_self_pid(void) { |
| 282 |
|
| static uint32_t *cache; |
| 283 |
|
| static int uncached; |
| 284 |
41633 |
| uint32_t *c = __atomic_load_n(&cache, __ATOMIC_ACQUIRE); |
| 285 |
41633 |
▶ | if (c && *c) return *c; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c && *c) return *c; |
41617 |
16 |
41633 |
| if (c && *c) return *c; |
41617 |
0 |
41617 |
|
| 286 |
16 |
| uint32_t pid = (uint32_t)getpid(); |
| 287 |
16 |
▶ | if (!c && !uncached) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!c && !uncached) { |
16 |
0 |
16 |
| if (!c && !uncached) { |
16 |
0 |
16 |
|
| 288 |
16 |
| size_t page = (size_t)sysconf(_SC_PAGESIZE); |
| 289 |
16 |
| void *p = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 290 |
16 |
▶ | if (p == MAP_FAILED) { uncached = 1; return pid; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == MAP_FAILED) { uncached = 1; return pid; } |
0 |
16 |
16 |
|
| 291 |
16 |
▶ | if (madvise(p, page, MADV_WIPEONFORK) < 0) { munmap(p, page); uncached = 1; return pid; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (madvise(p, page, MADV_WIPEONFORK) < 0) { munmap(p, page); uncached = 1; return pid; } |
0 |
16 |
16 |
|
| 292 |
16 |
| uint32_t *expected = NULL; |
| 293 |
16 |
▶ | if (__atomic_compare_exchange_n(&cache, &expected, (uint32_t *)p, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (__atomic_compare_exchange_n(&cache, &expected, (uint32_t *)p, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) |
16 |
0 |
16 |
|
| 294 |
16 |
| c = (uint32_t *)p; |
| 295 |
0 |
| else { munmap(p, page); c = expected; } |
| 296 |
|
| } |
| 297 |
16 |
▶ | if (c) *c = pid; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c) *c = pid; |
16 |
0 |
16 |
|
| 298 |
16 |
| return pid; |
| 299 |
|
| } |
| 300 |
|
| |
| 301 |
305 |
| static uint64_t queue_pidns(void) { |
| 302 |
|
| struct stat st; |
| 303 |
305 |
▶ | return stat("/proc/self/ns/pid", &st) == 0 ? (uint64_t)st.st_ino : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return stat("/proc/self/ns/pid", &st) == 0 ? (uint64_t)st.st_ino : 0; |
305 |
0 |
305 |
|
| 304 |
|
| } |
| 305 |
|
| |
| 306 |
3 |
| static inline int queue_recover_stale_mutex(QueueHeader *hdr, uint32_t observed) { |
| 307 |
3 |
▶ | if (!__atomic_compare_exchange_n(&hdr->mutex, &observed, 0, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!__atomic_compare_exchange_n(&hdr->mutex, &observed, 0, |
0 |
3 |
3 |
|
| 308 |
|
| 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED)) |
| 309 |
0 |
| return 0; |
| 310 |
3 |
| __atomic_add_fetch(&hdr->stat_recoveries, 1, __ATOMIC_RELAXED); |
| 311 |
3 |
| __atomic_store_n(&hdr->stat_last_recovery, (uint64_t)time(NULL), __ATOMIC_RELAXED); |
| 312 |
3 |
▶ | if (__atomic_load_n(&hdr->mutex_waiters, __ATOMIC_RELAXED) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (__atomic_load_n(&hdr->mutex_waiters, __ATOMIC_RELAXED) > 0) |
0 |
3 |
3 |
|
| 313 |
0 |
| syscall(SYS_futex, &hdr->mutex, FUTEX_WAKE, 1, NULL, NULL, 0); |
| 314 |
3 |
| return 1; |
| 315 |
|
| } |
| 316 |
|
| |
| 317 |
94 |
| static inline int queue_recover_dead_holder(QueueHeader *hdr, uint32_t val) { |
| 318 |
94 |
| return val >= QUEUE_MUTEX_WRITER_BIT |
| 319 |
94 |
▶ | && !queue_pid_alive(val & QUEUE_MUTEX_PID_MASK) |
Branch: 1 branch
| Branch | True | False | Total |
| && !queue_pid_alive(val & QUEUE_MUTEX_PID_MASK) |
3 |
91 |
94 |
|
| 320 |
188 |
▶ | && queue_recover_stale_mutex(hdr, val); |
Branch: 2 branches
| Branch | True | False | Total |
| && queue_recover_stale_mutex(hdr, val); |
94 |
0 |
94 |
| && queue_recover_stale_mutex(hdr, val); |
3 |
0 |
3 |
|
| 321 |
|
| } |
| 322 |
|
| |
| 323 |
|
| /* Under the mutex, after a wait that started at *since. */ |
| 324 |
2 |
| static void queue_lock_wait_stat(QueueHeader *hdr, const struct timespec *since) { |
| 325 |
|
| struct timespec now; |
| 326 |
2 |
| clock_gettime(CLOCK_MONOTONIC, &now); |
| 327 |
2 |
| uint64_t ns = (uint64_t)(now.tv_sec - since->tv_sec) * 1000000000u + now.tv_nsec - since->tv_nsec; |
| 328 |
2 |
| __atomic_store_n(&hdr->stat_lock_waits, hdr->stat_lock_waits + 1, __ATOMIC_RELAXED); |
| 329 |
2 |
| __atomic_store_n(&hdr->stat_lock_wait_ns, hdr->stat_lock_wait_ns + ns, __ATOMIC_RELAXED); |
| 330 |
2 |
▶ | if (ns > hdr->stat_lock_wait_max_ns) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ns > hdr->stat_lock_wait_max_ns) |
2 |
0 |
2 |
|
| 331 |
2 |
| __atomic_store_n(&hdr->stat_lock_wait_max_ns, ns, __ATOMIC_RELAXED); |
| 332 |
2 |
| } |
| 333 |
|
| |
| 334 |
|
| /* Takes the Str mutex, or returns 0 once h->lock_timeout runs out. PIDs of another namespace look |
| 335 |
|
| * dead, so a queue shared across PID namespaces checks a holder only after QUEUE_RECHECK_NS. */ |
| 336 |
41633 |
| static int queue_str_lock(QueueHandle *h) { |
| 337 |
41633 |
| QueueHeader *hdr = h->hdr; |
| 338 |
41633 |
| uint32_t mypid = QUEUE_MUTEX_VAL(queue_self_pid()); |
| 339 |
41633 |
| uint32_t watched = 0; |
| 340 |
41633 |
| long check_ns = 0; |
| 341 |
|
| struct timespec deadline, slept_at; |
| 342 |
41633 |
| int has_deadline = 0, slept = 0; |
| 343 |
45124 |
| for (int spin = 0; ; spin++) { |
| 344 |
45124 |
| uint32_t expected = 0; |
| 345 |
45124 |
▶ | if (__atomic_compare_exchange_n(&hdr->mutex, &expected, mypid, |
Branch: 1 branch
| Branch | True | False | Total |
| if (__atomic_compare_exchange_n(&hdr->mutex, &expected, mypid, |
41617 |
3507 |
45124 |
|
| 346 |
|
| 1, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) { |
| 347 |
41617 |
▶ | if (slept) queue_lock_wait_stat(hdr, &slept_at); |
Branch: 1 branch
| Branch | True | False | Total |
| if (slept) queue_lock_wait_stat(hdr, &slept_at); |
2 |
41615 |
41617 |
|
| 348 |
41633 |
| return 1; |
| 349 |
|
| } |
| 350 |
3507 |
▶ | if (__builtin_expect(spin < QUEUE_SPIN_LIMIT, 1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (__builtin_expect(spin < QUEUE_SPIN_LIMIT, 1)) { |
3398 |
109 |
3507 |
|
| 351 |
3398 |
| queue_spin_pause(); |
| 352 |
3399 |
| continue; |
| 353 |
|
| } |
| 354 |
109 |
| spin = 0; |
| 355 |
109 |
▶ | int fast = h->same_pidns && !__atomic_load_n(&hdr->pidns_mixed, __ATOMIC_SEQ_CST); |
Branch: 2 branches
| Branch | True | False | Total |
| int fast = h->same_pidns && !__atomic_load_n(&hdr->pidns_mixed, __ATOMIC_SEQ_CST); |
109 |
0 |
109 |
| int fast = h->same_pidns && !__atomic_load_n(&hdr->pidns_mixed, __ATOMIC_SEQ_CST); |
109 |
0 |
109 |
|
| 356 |
109 |
| __atomic_add_fetch(&hdr->mutex_waiters, 1, __ATOMIC_RELAXED); |
| 357 |
109 |
| uint32_t cur = __atomic_load_n(&hdr->mutex, __ATOMIC_RELAXED); |
| 358 |
109 |
▶ | if (cur == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cur == 0) { |
0 |
109 |
109 |
|
| 359 |
0 |
| __atomic_sub_fetch(&hdr->mutex_waiters, 1, __ATOMIC_RELAXED); |
| 360 |
0 |
| continue; |
| 361 |
|
| } |
| 362 |
109 |
▶ | if (cur != watched) { watched = cur; check_ns = fast ? QUEUE_LOCK_CHECK_NS : QUEUE_RECHECK_NS; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (cur != watched) { watched = cur; check_ns = fast ? QUEUE_LOCK_CHECK_NS : QUEUE_RECHECK_NS; } |
19 |
90 |
109 |
| if (cur != watched) { watched = cur; check_ns = fast ? QUEUE_LOCK_CHECK_NS : QUEUE_RECHECK_NS; } |
19 |
0 |
19 |
|
| 363 |
109 |
| struct timespec ts = { check_ns / 1000000000L, check_ns % 1000000000L }; |
| 364 |
109 |
| int full_wait = 1; |
| 365 |
109 |
▶ | if (h->lock_timeout >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->lock_timeout >= 0) { |
107 |
2 |
109 |
|
| 366 |
|
| struct timespec rem; |
| 367 |
107 |
▶ | if (!has_deadline) { queue_make_deadline(h->lock_timeout, &deadline); has_deadline = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!has_deadline) { queue_make_deadline(h->lock_timeout, &deadline); has_deadline = 1; } |
17 |
90 |
107 |
|
| 368 |
107 |
▶ | if (!queue_remaining_time(&deadline, &rem)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_remaining_time(&deadline, &rem)) { |
17 |
90 |
107 |
|
| 369 |
17 |
| __atomic_sub_fetch(&hdr->mutex_waiters, 1, __ATOMIC_RELAXED); |
| 370 |
17 |
▶ | if (fast && queue_recover_dead_holder(hdr, cur)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (fast && queue_recover_dead_holder(hdr, cur)) continue; |
17 |
0 |
17 |
| if (fast && queue_recover_dead_holder(hdr, cur)) continue; |
1 |
16 |
17 |
|
| 371 |
16 |
| __atomic_add_fetch(&hdr->stat_lock_timeouts, 1, __ATOMIC_RELAXED); |
| 372 |
16 |
| h->fail_errno = EAGAIN; |
| 373 |
16 |
| return 0; |
| 374 |
|
| } |
| 375 |
90 |
▶ | if (rem.tv_sec < ts.tv_sec || (rem.tv_sec == ts.tv_sec && rem.tv_nsec < ts.tv_nsec)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (rem.tv_sec < ts.tv_sec || (rem.tv_sec == ts.tv_sec && rem.tv_nsec < ts.tv_nsec)) |
90 |
0 |
90 |
| if (rem.tv_sec < ts.tv_sec || (rem.tv_sec == ts.tv_sec && rem.tv_nsec < ts.tv_nsec)) |
90 |
0 |
90 |
| if (rem.tv_sec < ts.tv_sec || (rem.tv_sec == ts.tv_sec && rem.tv_nsec < ts.tv_nsec)) |
15 |
75 |
90 |
|
| 376 |
15 |
| ts = rem, full_wait = 0; |
| 377 |
|
| } |
| 378 |
92 |
▶ | if (!slept) { clock_gettime(CLOCK_MONOTONIC, &slept_at); slept = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!slept) { clock_gettime(CLOCK_MONOTONIC, &slept_at); slept = 1; } |
17 |
75 |
92 |
|
| 379 |
92 |
| long rc = syscall(SYS_futex, &hdr->mutex, FUTEX_WAIT, cur, &ts, NULL, 0); |
| 380 |
92 |
▶ | int timed_out = rc == -1 && errno == ETIMEDOUT; |
Branch: 2 branches
| Branch | True | False | Total |
| int timed_out = rc == -1 && errno == ETIMEDOUT; |
92 |
0 |
92 |
| int timed_out = rc == -1 && errno == ETIMEDOUT; |
92 |
0 |
92 |
|
| 381 |
92 |
| __atomic_sub_fetch(&hdr->mutex_waiters, 1, __ATOMIC_RELAXED); |
| 382 |
92 |
▶ | if (!timed_out || __atomic_load_n(&hdr->mutex, __ATOMIC_RELAXED) != cur) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!timed_out || __atomic_load_n(&hdr->mutex, __ATOMIC_RELAXED) != cur) |
92 |
0 |
92 |
| if (!timed_out || __atomic_load_n(&hdr->mutex, __ATOMIC_RELAXED) != cur) |
0 |
92 |
92 |
|
| 383 |
0 |
| watched = 0; /* the lock moved on: check the next hold afresh */ |
| 384 |
92 |
▶ | else if (full_wait && !queue_recover_dead_holder(hdr, cur)) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (full_wait && !queue_recover_dead_holder(hdr, cur)) |
77 |
15 |
92 |
| else if (full_wait && !queue_recover_dead_holder(hdr, cur)) |
75 |
2 |
77 |
|
| 385 |
75 |
▶ | check_ns = check_ns < QUEUE_RECHECK_NS / 2 ? check_ns * 2 : QUEUE_RECHECK_NS; |
Branch: 1 branch
| Branch | True | False | Total |
| check_ns = check_ns < QUEUE_RECHECK_NS / 2 ? check_ns * 2 : QUEUE_RECHECK_NS; |
75 |
0 |
75 |
|
| 386 |
|
| } |
| 387 |
|
| } |
| 388 |
|
| |
| 389 |
41617 |
| static inline void queue_mutex_unlock(QueueHeader *hdr) { |
| 390 |
41617 |
| __atomic_store_n(&hdr->mutex, 0, __ATOMIC_RELEASE); |
| 391 |
41617 |
▶ | if (__atomic_load_n(&hdr->mutex_waiters, __ATOMIC_RELAXED) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (__atomic_load_n(&hdr->mutex_waiters, __ATOMIC_RELAXED) > 0) |
1 |
41616 |
41617 |
|
| 392 |
1 |
| syscall(SYS_futex, &hdr->mutex, FUTEX_WAKE, 1, NULL, NULL, 0); |
| 393 |
41617 |
| } |
| 394 |
|
| |
| 395 |
|
| /* Wake up to `n` blocked consumers (after batch push). Each woken |
| 396 |
|
| * consumer pops at most one item, so batch publishers must wake `n` |
| 397 |
|
| * (not 1) to drain a multi-item commit without leaving consumers |
| 398 |
|
| * sleeping on still-available items. */ |
| 399 |
20162 |
| static inline void queue_wake_consumers_n(QueueHeader *hdr, uint32_t n) { |
| 400 |
20162 |
▶ | if (n == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return; |
0 |
20162 |
20162 |
|
| 401 |
20162 |
▶ | if (__atomic_load_n(&hdr->pop_waiters, __ATOMIC_RELAXED) > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (__atomic_load_n(&hdr->pop_waiters, __ATOMIC_RELAXED) > 0) { |
1 |
20161 |
20162 |
|
| 402 |
1 |
| __atomic_add_fetch(&hdr->pop_futex, 1, __ATOMIC_RELEASE); |
| 403 |
1 |
| syscall(SYS_futex, &hdr->pop_futex, FUTEX_WAKE, |
| 404 |
1 |
▶ | n > (uint32_t)INT_MAX ? INT_MAX : (int)n, NULL, NULL, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| n > (uint32_t)INT_MAX ? INT_MAX : (int)n, NULL, NULL, 0); |
1 |
0 |
1 |
|
| 405 |
|
| } |
| 406 |
|
| } |
| 407 |
|
| |
| 408 |
|
| /* Wake blocked consumers (after single push) */ |
| 409 |
20158 |
| static inline void queue_wake_consumers(QueueHeader *hdr) { |
| 410 |
20158 |
| queue_wake_consumers_n(hdr, 1); |
| 411 |
20158 |
| } |
| 412 |
|
| |
| 413 |
|
| /* Wake up to `n` blocked producers (after batch pop). See |
| 414 |
|
| * queue_wake_consumers_n for the batching rationale. */ |
| 415 |
18277 |
| static inline void queue_wake_producers_n(QueueHeader *hdr, uint32_t n) { |
| 416 |
18277 |
▶ | if (n == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return; |
0 |
18277 |
18277 |
|
| 417 |
18277 |
▶ | if (__atomic_load_n(&hdr->push_waiters, __ATOMIC_RELAXED) > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (__atomic_load_n(&hdr->push_waiters, __ATOMIC_RELAXED) > 0) { |
13 |
18264 |
18277 |
|
| 418 |
13 |
| __atomic_add_fetch(&hdr->push_futex, 1, __ATOMIC_RELEASE); |
| 419 |
13 |
| syscall(SYS_futex, &hdr->push_futex, FUTEX_WAKE, |
| 420 |
13 |
▶ | n > (uint32_t)INT_MAX ? INT_MAX : (int)n, NULL, NULL, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| n > (uint32_t)INT_MAX ? INT_MAX : (int)n, NULL, NULL, 0); |
13 |
0 |
13 |
|
| 421 |
|
| } |
| 422 |
|
| } |
| 423 |
|
| |
| 424 |
|
| /* Wake blocked producers (after single pop) */ |
| 425 |
485 |
| static inline void queue_wake_producers(QueueHeader *hdr) { |
| 426 |
485 |
| queue_wake_producers_n(hdr, 1); |
| 427 |
485 |
| } |
| 428 |
|
| |
| 429 |
|
| /* ================================================================ |
| 430 |
|
| * Create / Open / Close |
| 431 |
|
| * ================================================================ */ |
| 432 |
|
| |
| 433 |
|
| #define QUEUE_ERR(fmt, ...) do { if (errbuf) snprintf(errbuf, QUEUE_ERR_BUFLEN, fmt, ##__VA_ARGS__); } while(0) |
| 434 |
|
| |
| 435 |
|
| #define QUEUE_FORMAT_ERR "queue file format v%u, this module uses v%u; recreate the queue" |
| 436 |
|
| |
| 437 |
|
| typedef struct { uint32_t cap; uint64_t arena_off, arena_cap, total_size; } QueueGeometry; |
| 438 |
|
| |
| 439 |
190 |
| static uint64_t queue_slot_size(uint32_t mode) { |
| 440 |
|
| return mode == QUEUE_MODE_INT ? sizeof(QueueIntSlot) |
| 441 |
190 |
▶ | : mode == QUEUE_MODE_INT32 ? sizeof(QueueInt32Slot) |
Branch: 3 branches
| Branch | True | False | Total |
| : mode == QUEUE_MODE_INT32 ? sizeof(QueueInt32Slot) |
107 |
83 |
190 |
| : mode == QUEUE_MODE_INT32 ? sizeof(QueueInt32Slot) |
91 |
16 |
107 |
| : mode == QUEUE_MODE_INT32 ? sizeof(QueueInt32Slot) |
11 |
80 |
91 |
|
| 442 |
|
| : mode == QUEUE_MODE_INT16 ? sizeof(QueueInt16Slot) |
| 443 |
|
| : sizeof(QueueStrSlot); |
| 444 |
|
| } |
| 445 |
|
| |
| 446 |
167 |
| static int queue_geometry(uint32_t capacity, uint32_t mode, uint64_t arena_cap_hint, |
| 447 |
|
| QueueGeometry *g, char *errbuf) { |
| 448 |
167 |
| g->cap = queue_next_pow2(capacity); |
| 449 |
167 |
▶ | if (g->cap == 0) { QUEUE_ERR("invalid capacity"); return 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (g->cap == 0) { QUEUE_ERR("invalid capacity"); return 0; } |
0 |
167 |
167 |
| if (g->cap == 0) { QUEUE_ERR("invalid capacity"); return 0; } |
0 |
0 |
0 |
|
| 450 |
167 |
▶ | if (mode > QUEUE_MODE_INT16) { QUEUE_ERR("unknown mode %u", mode); return 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (mode > QUEUE_MODE_INT16) { QUEUE_ERR("unknown mode %u", mode); return 0; } |
0 |
167 |
167 |
| if (mode > QUEUE_MODE_INT16) { QUEUE_ERR("unknown mode %u", mode); return 0; } |
0 |
0 |
0 |
|
| 451 |
167 |
| uint64_t slots_end = sizeof(QueueHeader) + (uint64_t)g->cap * queue_slot_size(mode); |
| 452 |
167 |
| g->arena_off = g->arena_cap = 0; |
| 453 |
167 |
| g->total_size = slots_end; |
| 454 |
167 |
▶ | if (mode == QUEUE_MODE_STR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (mode == QUEUE_MODE_STR) { |
73 |
94 |
167 |
|
| 455 |
73 |
| g->arena_off = (slots_end + 7) & ~(uint64_t)7; |
| 456 |
73 |
▶ | g->arena_cap = arena_cap_hint < 4096 ? 4096 : arena_cap_hint > UINT32_MAX ? UINT32_MAX : arena_cap_hint; |
Branch: 1 branch
| Branch | True | False | Total |
| g->arena_cap = arena_cap_hint < 4096 ? 4096 : arena_cap_hint > UINT32_MAX ? UINT32_MAX : arena_cap_hint; |
58 |
15 |
73 |
|
| 457 |
73 |
| g->total_size = g->arena_off + g->arena_cap; |
| 458 |
|
| } |
| 459 |
167 |
| return 1; |
| 460 |
|
| } |
| 461 |
|
| |
| 462 |
148 |
| static void queue_init_new_header(void *base, const QueueGeometry *g, uint32_t mode, uint64_t reserved) { |
| 463 |
148 |
| QueueHeader *hdr = (QueueHeader *)base; |
| 464 |
148 |
| memset(hdr, 0, sizeof(QueueHeader)); |
| 465 |
148 |
| hdr->version = QUEUE_VERSION; |
| 466 |
148 |
| hdr->mode = mode; |
| 467 |
148 |
| hdr->capacity = g->cap; |
| 468 |
148 |
| hdr->total_size = g->total_size; |
| 469 |
148 |
| hdr->slots_off = sizeof(QueueHeader); |
| 470 |
148 |
| hdr->arena_off = g->arena_off; |
| 471 |
148 |
| hdr->arena_cap = g->arena_cap; |
| 472 |
148 |
| hdr->pidns = queue_pidns(); |
| 473 |
148 |
▶ | hdr->arena_populated = reserved >= g->total_size ? (uint32_t)g->arena_cap : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| hdr->arena_populated = reserved >= g->total_size ? (uint32_t)g->arena_cap : 0; |
48 |
100 |
148 |
|
| 474 |
|
| #define INIT_SEQ(STYPE) do { \ |
| 475 |
|
| STYPE *s = (STYPE *)((char *)base + sizeof(QueueHeader)); \ |
| 476 |
|
| for (uint32_t _i = 0; _i < g->cap; _i++) s[_i].sequence = _i; \ |
| 477 |
|
| } while(0) |
| 478 |
3578 |
▶ | if (mode == QUEUE_MODE_INT) INIT_SEQ(QueueIntSlot); |
Branch: 2 branches
| Branch | True | False | Total |
| if (mode == QUEUE_MODE_INT) INIT_SEQ(QueueIntSlot); |
57 |
91 |
148 |
| if (mode == QUEUE_MODE_INT) INIT_SEQ(QueueIntSlot); |
3430 |
57 |
3487 |
|
| 479 |
1579 |
▶ | else if (mode == QUEUE_MODE_INT32) INIT_SEQ(QueueInt32Slot); |
Branch: 2 branches
| Branch | True | False | Total |
| else if (mode == QUEUE_MODE_INT32) INIT_SEQ(QueueInt32Slot); |
14 |
77 |
91 |
| else if (mode == QUEUE_MODE_INT32) INIT_SEQ(QueueInt32Slot); |
1488 |
14 |
1502 |
|
| 480 |
1189 |
▶ | else if (mode == QUEUE_MODE_INT16) INIT_SEQ(QueueInt16Slot); |
Branch: 2 branches
| Branch | True | False | Total |
| else if (mode == QUEUE_MODE_INT16) INIT_SEQ(QueueInt16Slot); |
9 |
68 |
77 |
| else if (mode == QUEUE_MODE_INT16) INIT_SEQ(QueueInt16Slot); |
1112 |
9 |
1121 |
|
| 481 |
|
| #undef INIT_SEQ |
| 482 |
|
| /* Publish magic LAST, as a release store: it is the commit point, so a |
| 483 |
|
| creator killed before it leaves magic==0 and never a file mistaken for |
| 484 |
|
| a valid one. A kill during the field stores leaves one to remove by |
| 485 |
|
| hand. */ |
| 486 |
148 |
| __atomic_store_n(&hdr->magic, QUEUE_MAGIC, __ATOMIC_RELEASE); |
| 487 |
148 |
| __atomic_thread_fence(__ATOMIC_SEQ_CST); |
| 488 |
148 |
| } |
| 489 |
|
| |
| 490 |
23 |
| static int queue_header_valid(const QueueHeader *hdr, uint32_t mode, uint64_t size) { |
| 491 |
23 |
| uint64_t slot_size = queue_slot_size(mode); |
| 492 |
23 |
▶ | if (!(hdr->magic == QUEUE_MAGIC && hdr->version == QUEUE_VERSION && hdr->mode == mode |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(hdr->magic == QUEUE_MAGIC && hdr->version == QUEUE_VERSION && hdr->mode == mode |
19 |
4 |
23 |
| if (!(hdr->magic == QUEUE_MAGIC && hdr->version == QUEUE_VERSION && hdr->mode == mode |
15 |
4 |
19 |
| if (!(hdr->magic == QUEUE_MAGIC && hdr->version == QUEUE_VERSION && hdr->mode == mode |
11 |
4 |
15 |
|
| 493 |
11 |
▶ | && hdr->capacity >= 2 && (hdr->capacity & (hdr->capacity - 1)) == 0 |
Branch: 2 branches
| Branch | True | False | Total |
| && hdr->capacity >= 2 && (hdr->capacity & (hdr->capacity - 1)) == 0 |
10 |
1 |
11 |
| && hdr->capacity >= 2 && (hdr->capacity & (hdr->capacity - 1)) == 0 |
10 |
0 |
10 |
|
| 494 |
10 |
▶ | && hdr->total_size == size && hdr->slots_off == sizeof(QueueHeader) |
Branch: 2 branches
| Branch | True | False | Total |
| && hdr->total_size == size && hdr->slots_off == sizeof(QueueHeader) |
10 |
0 |
10 |
| && hdr->total_size == size && hdr->slots_off == sizeof(QueueHeader) |
10 |
0 |
10 |
|
| 495 |
10 |
▶ | && hdr->capacity <= (hdr->total_size - hdr->slots_off) / slot_size)) |
Branch: 1 branch
| Branch | True | False | Total |
| && hdr->capacity <= (hdr->total_size - hdr->slots_off) / slot_size)) |
0 |
10 |
10 |
|
| 496 |
13 |
| return 0; |
| 497 |
10 |
▶ | if (mode != QUEUE_MODE_STR) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (mode != QUEUE_MODE_STR) return 1; |
6 |
4 |
10 |
|
| 498 |
4 |
| uint64_t slots_end = hdr->slots_off + (uint64_t)hdr->capacity * slot_size; |
| 499 |
3 |
▶ | return hdr->arena_cap >= 4096 && (hdr->arena_off & 7) == 0 && hdr->arena_off >= slots_end |
Branch: 2 branches
| Branch | True | False | Total |
| return hdr->arena_cap >= 4096 && (hdr->arena_off & 7) == 0 && hdr->arena_off >= slots_end |
3 |
0 |
3 |
| return hdr->arena_cap >= 4096 && (hdr->arena_off & 7) == 0 && hdr->arena_off >= slots_end |
3 |
0 |
3 |
|
| 500 |
7 |
▶ | && hdr->arena_off <= hdr->total_size && hdr->arena_cap <= hdr->total_size - hdr->arena_off; |
Branch: 3 branches
| Branch | True | False | Total |
| && hdr->arena_off <= hdr->total_size && hdr->arena_cap <= hdr->total_size - hdr->arena_off; |
3 |
1 |
4 |
| && hdr->arena_off <= hdr->total_size && hdr->arena_cap <= hdr->total_size - hdr->arena_off; |
3 |
0 |
3 |
| && hdr->arena_off <= hdr->total_size && hdr->arena_cap <= hdr->total_size - hdr->arena_off; |
3 |
0 |
3 |
|
| 501 |
|
| } |
| 502 |
|
| |
| 503 |
157 |
| static QueueHandle *queue_handle_new(void *base, size_t map_size, const char *path, int backing_fd) { |
| 504 |
157 |
| QueueHeader *hdr = (QueueHeader *)base; |
| 505 |
157 |
| QueueHandle *h = (QueueHandle *)calloc(1, sizeof(QueueHandle)); |
| 506 |
157 |
▶ | if (!h) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) return NULL; |
0 |
157 |
157 |
|
| 507 |
157 |
| h->hdr = hdr; |
| 508 |
157 |
| h->slots = (char *)base + hdr->slots_off; |
| 509 |
157 |
▶ | h->arena = hdr->mode == QUEUE_MODE_STR ? (char *)base + hdr->arena_off : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| h->arena = hdr->mode == QUEUE_MODE_STR ? (char *)base + hdr->arena_off : NULL; |
71 |
86 |
157 |
|
| 510 |
157 |
| h->mmap_size = map_size; |
| 511 |
157 |
| h->capacity = hdr->capacity; |
| 512 |
157 |
| h->cap_mask = hdr->capacity - 1; |
| 513 |
157 |
| h->arena_cap = hdr->arena_cap; |
| 514 |
157 |
▶ | h->path = path ? strdup(path) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| h->path = path ? strdup(path) : NULL; |
80 |
77 |
157 |
|
| 515 |
157 |
| h->notify_fd = -1; |
| 516 |
157 |
| h->backing_fd = backing_fd; |
| 517 |
157 |
| h->lock_timeout = -1; |
| 518 |
157 |
| uint64_t ns = queue_pidns(); |
| 519 |
157 |
▶ | h->same_pidns = ns && ns == hdr->pidns; |
Branch: 2 branches
| Branch | True | False | Total |
| h->same_pidns = ns && ns == hdr->pidns; |
157 |
0 |
157 |
| h->same_pidns = ns && ns == hdr->pidns; |
157 |
0 |
157 |
|
| 520 |
157 |
▶ | if (!h->same_pidns) __atomic_store_n(&hdr->pidns_mixed, 1, __ATOMIC_SEQ_CST); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h->same_pidns) __atomic_store_n(&hdr->pidns_mixed, 1, __ATOMIC_SEQ_CST); |
0 |
157 |
157 |
|
| 521 |
157 |
| return h; |
| 522 |
|
| } |
| 523 |
|
| |
| 524 |
|
| /* Securely obtain a fd: create exclusively (O_CREAT|O_EXCL|O_NOFOLLOW at |
| 525 |
|
| * file_mode, default 0600 = owner-only), or attach an existing file |
| 526 |
|
| * (O_RDWR|O_NOFOLLOW, no O_CREAT). Blocks a symlink swap or a pre-seeded/ |
| 527 |
|
| * hard-linked backing file; cross-user sharing is opt-in via a wider file_mode. */ |
| 528 |
92 |
| static int queue_secure_open(const char *path, mode_t file_mode, char *errbuf) { |
| 529 |
92 |
▶ | for (int attempt = 0; attempt < 100; attempt++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int attempt = 0; attempt < 100; attempt++) { |
92 |
0 |
92 |
|
| 530 |
92 |
| int fd = open(path, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, file_mode); |
| 531 |
92 |
▶ | if (fd >= 0) { (void)fchmod(fd, file_mode); return fd; } /* exact mode: umask narrowed the O_EXCL create */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd >= 0) { (void)fchmod(fd, file_mode); return fd; } /* exact mode: umask narrowed the O_EXCL create */ |
69 |
23 |
92 |
|
| 532 |
23 |
▶ | if (errno != EEXIST) { QUEUE_ERR("create(%s): %s", path, strerror(errno)); return -1; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (errno != EEXIST) { QUEUE_ERR("create(%s): %s", path, strerror(errno)); return -1; } |
0 |
23 |
23 |
| if (errno != EEXIST) { QUEUE_ERR("create(%s): %s", path, strerror(errno)); return -1; } |
0 |
0 |
0 |
|
| 533 |
23 |
| fd = open(path, O_RDWR|O_NOFOLLOW|O_CLOEXEC); |
| 534 |
23 |
▶ | if (fd >= 0) return fd; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd >= 0) return fd; |
23 |
0 |
23 |
|
| 535 |
0 |
▶ | if (errno == ENOENT) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errno == ENOENT) continue; |
0 |
0 |
0 |
|
| 536 |
0 |
▶ | QUEUE_ERR("open(%s): %s", path, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("open(%s): %s", path, strerror(errno)); |
0 |
0 |
0 |
|
| 537 |
0 |
| return -1; |
| 538 |
|
| } |
| 539 |
0 |
▶ | QUEUE_ERR("open(%s): create/attach kept racing", path); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("open(%s): create/attach kept racing", path); |
0 |
0 |
0 |
|
| 540 |
0 |
| return -1; |
| 541 |
|
| } |
| 542 |
|
| |
| 543 |
|
| /* True iff the whole file is zero -- what an abandoned mid-init |
| 544 |
|
| creator leaves. Lets recovery re-init only a provably-empty file, never |
| 545 |
|
| one that merely starts with a zero word. Cold path, so a byte scan is |
| 546 |
|
| fine. Read, not mapped: a read fault on a tmpfs hole allocates the page. */ |
| 547 |
2 |
| static int queue_file_is_zero(int fd, uint64_t size) { |
| 548 |
|
| char buf[16384]; |
| 549 |
3 |
▶ | for (uint64_t off = 0; off < size; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (uint64_t off = 0; off < size; ) { |
2 |
1 |
3 |
|
| 550 |
2 |
| ssize_t n = pread(fd, buf, size - off < sizeof buf ? (size_t)(size - off) : sizeof buf, (off_t)off); |
| 551 |
2 |
▶ | if (n <= 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) return 0; |
0 |
2 |
2 |
|
| 552 |
1025 |
▶ | for (ssize_t i = 0; i < n; i++) if (buf[i]) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| for (ssize_t i = 0; i < n; i++) if (buf[i]) return 0; |
1 |
1023 |
1024 |
| for (ssize_t i = 0; i < n; i++) if (buf[i]) return 0; |
1024 |
1 |
1025 |
|
| 553 |
1 |
| off += (uint64_t)n; |
| 554 |
|
| } |
| 555 |
1 |
| return 1; |
| 556 |
|
| } |
| 557 |
|
| |
| 558 |
|
| /* A sparse segment would SIGBUS at the first write the filesystem cannot back. */ |
| 559 |
104 |
| static uint64_t queue_reserve_len(uint32_t mode, uint64_t total_size, uint64_t arena_off) { |
| 560 |
104 |
| const char *sp = getenv("DATA_QUEUE_SHARED_SPARSE"); |
| 561 |
104 |
▶ | if (sp && *sp) return strcmp(sp, "0") ? 0 : total_size; |
Branch: 3 branches
| Branch | True | False | Total |
| if (sp && *sp) return strcmp(sp, "0") ? 0 : total_size; |
0 |
104 |
104 |
| if (sp && *sp) return strcmp(sp, "0") ? 0 : total_size; |
0 |
0 |
0 |
| if (sp && *sp) return strcmp(sp, "0") ? 0 : total_size; |
0 |
0 |
0 |
|
| 562 |
104 |
▶ | return mode == QUEUE_MODE_STR ? arena_off : total_size; |
Branch: 1 branch
| Branch | True | False | Total |
| return mode == QUEUE_MODE_STR ? arena_off : total_size; |
42 |
62 |
104 |
|
| 563 |
|
| } |
| 564 |
|
| |
| 565 |
85 |
| static int queue_reserve(int fd, uint64_t size) { |
| 566 |
85 |
▶ | if (!size) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!size) return 0; |
0 |
85 |
85 |
|
| 567 |
|
| /* Before Linux 6.11 tmpfs gives up at any pending signal and undoes the allocation, so a |
| 568 |
|
| * periodic one could keep it from ever completing; SIGSTOP cannot be blocked. */ |
| 569 |
|
| sigset_t all, old; |
| 570 |
85 |
| sigfillset(&all); |
| 571 |
85 |
| sigprocmask(SIG_BLOCK, &all, &old); |
| 572 |
|
| int e; |
| 573 |
85 |
▶ | do e = posix_fallocate(fd, 0, (off_t)size); while (e == EINTR); |
Branch: 1 branch
| Branch | True | False | Total |
| do e = posix_fallocate(fd, 0, (off_t)size); while (e == EINTR); |
0 |
85 |
85 |
|
| 574 |
85 |
| sigprocmask(SIG_SETMASK, &old, NULL); |
| 575 |
85 |
▶ | if (e == 0 || e == EOPNOTSUPP || e == EINVAL) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (e == 0 || e == EOPNOTSUPP || e == EINVAL) return 0; |
0 |
85 |
85 |
| if (e == 0 || e == EOPNOTSUPP || e == EINVAL) return 0; |
0 |
0 |
0 |
| if (e == 0 || e == EOPNOTSUPP || e == EINVAL) return 0; |
0 |
0 |
0 |
|
| 576 |
0 |
| errno = e; |
| 577 |
0 |
| return -1; |
| 578 |
|
| } |
| 579 |
|
| |
| 580 |
155 |
| static QueueHandle *queue_create(const char *path, uint32_t capacity, |
| 581 |
|
| uint32_t mode, uint64_t arena_cap_hint, |
| 582 |
|
| mode_t file_mode, char *errbuf) { |
| 583 |
155 |
▶ | if (errbuf) errbuf[0] = '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errbuf) errbuf[0] = '\0'; |
155 |
0 |
155 |
|
| 584 |
|
| QueueGeometry g; |
| 585 |
155 |
▶ | if (!queue_geometry(capacity, mode, arena_cap_hint, &g, errbuf)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_geometry(capacity, mode, arena_cap_hint, &g, errbuf)) return NULL; |
0 |
155 |
155 |
|
| 586 |
155 |
| uint64_t total_size = g.total_size; |
| 587 |
|
| |
| 588 |
155 |
| int anonymous = (path == NULL); |
| 589 |
|
| size_t map_size; |
| 590 |
|
| void *base; |
| 591 |
|
| |
| 592 |
155 |
▶ | if (anonymous) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (anonymous) { |
63 |
92 |
155 |
|
| 593 |
|
| /* Anonymous shared mmap -- fork-inherited, no filesystem */ |
| 594 |
63 |
| map_size = (size_t)total_size; |
| 595 |
63 |
| base = mmap(NULL, map_size, PROT_READ | PROT_WRITE, |
| 596 |
|
| MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 597 |
63 |
▶ | if (base == MAP_FAILED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base == MAP_FAILED) { |
0 |
63 |
63 |
|
| 598 |
0 |
▶ | QUEUE_ERR("mmap(anonymous): %s", strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("mmap(anonymous): %s", strerror(errno)); |
0 |
0 |
0 |
|
| 599 |
0 |
| return NULL; |
| 600 |
|
| } |
| 601 |
63 |
| queue_init_new_header(base, &g, mode, 0); |
| 602 |
|
| } else { |
| 603 |
|
| /* File-backed shared mmap */ |
| 604 |
92 |
| uint64_t reserved = queue_reserve_len(mode, total_size, g.arena_off); |
| 605 |
92 |
| int fd = queue_secure_open(path, file_mode, errbuf); |
| 606 |
104 |
▶ | if (fd < 0) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) return NULL; |
0 |
92 |
92 |
|
| 607 |
|
| |
| 608 |
92 |
▶ | if (flock(fd, LOCK_EX) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (flock(fd, LOCK_EX) < 0) { |
0 |
92 |
92 |
|
| 609 |
0 |
▶ | QUEUE_ERR("flock(%s): %s", path, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("flock(%s): %s", path, strerror(errno)); |
0 |
0 |
0 |
|
| 610 |
0 |
| close(fd); return NULL; |
| 611 |
|
| } |
| 612 |
|
| |
| 613 |
|
| struct stat st; |
| 614 |
92 |
▶ | if (fstat(fd, &st) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fstat(fd, &st) < 0) { |
0 |
92 |
92 |
|
| 615 |
0 |
▶ | QUEUE_ERR("fstat(%s): %s", path, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("fstat(%s): %s", path, strerror(errno)); |
0 |
0 |
0 |
|
| 616 |
0 |
| flock(fd, LOCK_UN); close(fd); return NULL; |
| 617 |
|
| } |
| 618 |
|
| |
| 619 |
92 |
▶ | if (!S_ISREG(st.st_mode)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!S_ISREG(st.st_mode)) { |
1 |
91 |
92 |
|
| 620 |
1 |
▶ | QUEUE_ERR("%s: invalid (not a regular file)", path); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("%s: invalid (not a regular file)", path); |
1 |
0 |
1 |
|
| 621 |
1 |
| flock(fd, LOCK_UN); close(fd); return NULL; |
| 622 |
|
| } |
| 623 |
|
| |
| 624 |
91 |
| int is_new = (st.st_size == 0); |
| 625 |
|
| |
| 626 |
91 |
▶ | if (!is_new && (uint64_t)st.st_size < sizeof(QueueHeader)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!is_new && (uint64_t)st.st_size < sizeof(QueueHeader)) { |
19 |
72 |
91 |
| if (!is_new && (uint64_t)st.st_size < sizeof(QueueHeader)) { |
0 |
19 |
19 |
|
| 627 |
0 |
▶ | QUEUE_ERR("%s: file too small (%lld)", path, (long long)st.st_size); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("%s: file too small (%lld)", path, (long long)st.st_size); |
0 |
0 |
0 |
|
| 628 |
0 |
| flock(fd, LOCK_UN); close(fd); return NULL; |
| 629 |
|
| } |
| 630 |
|
| |
| 631 |
91 |
▶ | if (is_new && (st.st_uid != geteuid() || fchmod(fd, file_mode) < 0)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (is_new && (st.st_uid != geteuid() || fchmod(fd, file_mode) < 0)) { |
72 |
19 |
91 |
| if (is_new && (st.st_uid != geteuid() || fchmod(fd, file_mode) < 0)) { |
72 |
0 |
72 |
| if (is_new && (st.st_uid != geteuid() || fchmod(fd, file_mode) < 0)) { |
0 |
72 |
72 |
|
| 632 |
0 |
▶ | QUEUE_ERR("%s: refusing to initialize file not owned by us", path); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("%s: refusing to initialize file not owned by us", path); |
0 |
0 |
0 |
|
| 633 |
0 |
| flock(fd, LOCK_UN); close(fd); return NULL; |
| 634 |
|
| } |
| 635 |
91 |
▶ | if (is_new) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_new) { |
72 |
19 |
91 |
|
| 636 |
72 |
▶ | if (ftruncate(fd, (off_t)total_size) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ftruncate(fd, (off_t)total_size) < 0) { |
0 |
72 |
72 |
|
| 637 |
0 |
▶ | QUEUE_ERR("ftruncate(%s): %s", path, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("ftruncate(%s): %s", path, strerror(errno)); |
0 |
0 |
0 |
|
| 638 |
0 |
| flock(fd, LOCK_UN); close(fd); return NULL; |
| 639 |
|
| } |
| 640 |
72 |
▶ | if (queue_reserve(fd, reserved) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (queue_reserve(fd, reserved) < 0) { |
0 |
72 |
72 |
|
| 641 |
0 |
▶ | QUEUE_ERR("%s: cannot reserve %llu bytes: %s", path, (unsigned long long)reserved, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("%s: cannot reserve %llu bytes: %s", path, (unsigned long long)reserved, strerror(errno)); |
0 |
0 |
0 |
|
| 642 |
0 |
| if (ftruncate(fd, 0) < 0) { /* best effort */ } |
| 643 |
0 |
| flock(fd, LOCK_UN); close(fd); return NULL; |
| 644 |
|
| } |
| 645 |
|
| } |
| 646 |
|
| |
| 647 |
91 |
▶ | map_size = is_new ? (size_t)total_size : (size_t)st.st_size; |
Branch: 1 branch
| Branch | True | False | Total |
| map_size = is_new ? (size_t)total_size : (size_t)st.st_size; |
19 |
72 |
91 |
|
| 648 |
91 |
| base = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 649 |
91 |
▶ | if (base == MAP_FAILED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base == MAP_FAILED) { |
0 |
91 |
91 |
|
| 650 |
0 |
▶ | QUEUE_ERR("mmap(%s): %s", path, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("mmap(%s): %s", path, strerror(errno)); |
0 |
0 |
0 |
|
| 651 |
0 |
▶ | if (is_new && ftruncate(fd, 0) < 0) { /* best effort */ } |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_new && ftruncate(fd, 0) < 0) { /* best effort */ } |
0 |
0 |
0 |
|
| 652 |
0 |
| flock(fd, LOCK_UN); close(fd); return NULL; |
| 653 |
|
| } |
| 654 |
|
| |
| 655 |
91 |
| QueueHeader *hdr = (QueueHeader *)base; |
| 656 |
|
| |
| 657 |
91 |
▶ | if (!is_new) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_new) { |
19 |
72 |
91 |
|
| 658 |
19 |
▶ | if (!queue_header_valid(hdr, mode, (uint64_t)st.st_size)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_header_valid(hdr, mode, (uint64_t)st.st_size)) { |
12 |
7 |
19 |
|
| 659 |
|
| /* Recover an abandoned mid-init file: a creator killed |
| 660 |
|
| * between the ftruncate and the header init leaves a |
| 661 |
|
| * full-size, all-zero (magic==0) file that would brick every |
| 662 |
|
| * future open of this path. Re-initialize ONLY when it is |
| 663 |
|
| * exactly our size, still uninitialized, and owned by us; |
| 664 |
|
| * anything else still errors. */ |
| 665 |
12 |
▶ | if (hdr->magic == 0 && (uint64_t)st.st_size == total_size |
Branch: 2 branches
| Branch | True | False | Total |
| if (hdr->magic == 0 && (uint64_t)st.st_size == total_size |
3 |
9 |
12 |
| if (hdr->magic == 0 && (uint64_t)st.st_size == total_size |
2 |
1 |
3 |
|
| 666 |
2 |
▶ | && st.st_uid == geteuid() && queue_file_is_zero(fd, map_size)) { |
Branch: 2 branches
| Branch | True | False | Total |
| && st.st_uid == geteuid() && queue_file_is_zero(fd, map_size)) { |
2 |
0 |
2 |
| && st.st_uid == geteuid() && queue_file_is_zero(fd, map_size)) { |
1 |
1 |
2 |
|
| 667 |
1 |
▶ | if (fchmod(fd, file_mode) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fchmod(fd, file_mode) < 0) { |
0 |
1 |
1 |
|
| 668 |
0 |
▶ | QUEUE_ERR("%s: fchmod: %s", path, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("%s: fchmod: %s", path, strerror(errno)); |
0 |
0 |
0 |
|
| 669 |
0 |
| munmap(base, map_size); flock(fd, LOCK_UN); close(fd); return NULL; |
| 670 |
|
| } |
| 671 |
1 |
▶ | if (queue_reserve(fd, reserved) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (queue_reserve(fd, reserved) < 0) { |
0 |
1 |
1 |
|
| 672 |
0 |
▶ | QUEUE_ERR("%s: cannot reserve %llu bytes: %s", path, (unsigned long long)reserved, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("%s: cannot reserve %llu bytes: %s", path, (unsigned long long)reserved, strerror(errno)); |
0 |
0 |
0 |
|
| 673 |
0 |
| munmap(base, map_size); flock(fd, LOCK_UN); close(fd); return NULL; |
| 674 |
|
| } |
| 675 |
1 |
| queue_init_new_header(base, &g, mode, reserved); |
| 676 |
1 |
| flock(fd, LOCK_UN); close(fd); |
| 677 |
8 |
| goto setup_handle; |
| 678 |
|
| } |
| 679 |
11 |
▶ | if (hdr->magic == 0 && (uint64_t)st.st_size == total_size |
Branch: 2 branches
| Branch | True | False | Total |
| if (hdr->magic == 0 && (uint64_t)st.st_size == total_size |
2 |
9 |
11 |
| if (hdr->magic == 0 && (uint64_t)st.st_size == total_size |
1 |
1 |
2 |
|
| 680 |
1 |
▶ | && st.st_uid == geteuid()) |
Branch: 1 branch
| Branch | True | False | Total |
| && st.st_uid == geteuid()) |
1 |
0 |
1 |
|
| 681 |
1 |
▶ | QUEUE_ERR("%s: incomplete queue file left by an interrupted create; remove it and retry", path); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("%s: incomplete queue file left by an interrupted create; remove it and retry", path); |
1 |
0 |
1 |
|
| 682 |
10 |
▶ | else if (hdr->magic == QUEUE_MAGIC && hdr->version != QUEUE_VERSION) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (hdr->magic == QUEUE_MAGIC && hdr->version != QUEUE_VERSION) |
8 |
2 |
10 |
| else if (hdr->magic == QUEUE_MAGIC && hdr->version != QUEUE_VERSION) |
2 |
6 |
8 |
|
| 683 |
2 |
▶ | QUEUE_ERR("%s: " QUEUE_FORMAT_ERR, path, hdr->version, QUEUE_VERSION); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("%s: " QUEUE_FORMAT_ERR, path, hdr->version, QUEUE_VERSION); |
2 |
0 |
2 |
|
| 684 |
|
| else |
| 685 |
8 |
▶ | QUEUE_ERR("%s: invalid or incompatible queue file", path); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("%s: invalid or incompatible queue file", path); |
8 |
0 |
8 |
|
| 686 |
11 |
| munmap(base, map_size); flock(fd, LOCK_UN); close(fd); return NULL; |
| 687 |
|
| } |
| 688 |
7 |
| flock(fd, LOCK_UN); |
| 689 |
7 |
| close(fd); |
| 690 |
7 |
| goto setup_handle; |
| 691 |
|
| } |
| 692 |
|
| |
| 693 |
72 |
| queue_init_new_header(base, &g, mode, reserved); |
| 694 |
72 |
| flock(fd, LOCK_UN); |
| 695 |
72 |
| close(fd); |
| 696 |
|
| } |
| 697 |
|
| |
| 698 |
143 |
| setup_handle:; |
| 699 |
143 |
| QueueHandle *h = queue_handle_new(base, map_size, path, -1); |
| 700 |
143 |
▶ | if (!h) munmap(base, map_size); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) munmap(base, map_size); |
0 |
143 |
143 |
|
| 701 |
143 |
| return h; |
| 702 |
|
| } |
| 703 |
|
| |
| 704 |
|
| /* Create queue backed by memfd -- shareable via fd passing (SCM_RIGHTS) */ |
| 705 |
12 |
| static QueueHandle *queue_create_memfd(const char *name, uint32_t capacity, |
| 706 |
|
| uint32_t mode, uint64_t arena_cap_hint, |
| 707 |
|
| char *errbuf) { |
| 708 |
12 |
▶ | if (errbuf) errbuf[0] = '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errbuf) errbuf[0] = '\0'; |
12 |
0 |
12 |
|
| 709 |
|
| QueueGeometry g; |
| 710 |
12 |
▶ | if (!queue_geometry(capacity, mode, arena_cap_hint, &g, errbuf)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_geometry(capacity, mode, arena_cap_hint, &g, errbuf)) return NULL; |
0 |
12 |
12 |
|
| 711 |
12 |
| uint64_t total_size = g.total_size; |
| 712 |
|
| |
| 713 |
12 |
▶ | int fd = memfd_create(name ? name : "queue", MFD_CLOEXEC | MFD_ALLOW_SEALING); |
Branch: 1 branch
| Branch | True | False | Total |
| int fd = memfd_create(name ? name : "queue", MFD_CLOEXEC | MFD_ALLOW_SEALING); |
12 |
0 |
12 |
|
| 714 |
12 |
▶ | if (fd < 0) { QUEUE_ERR("memfd_create: %s", strerror(errno)); return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (fd < 0) { QUEUE_ERR("memfd_create: %s", strerror(errno)); return NULL; } |
0 |
12 |
12 |
| if (fd < 0) { QUEUE_ERR("memfd_create: %s", strerror(errno)); return NULL; } |
0 |
0 |
0 |
|
| 715 |
|
| |
| 716 |
12 |
▶ | if (ftruncate(fd, (off_t)total_size) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ftruncate(fd, (off_t)total_size) < 0) { |
0 |
12 |
12 |
|
| 717 |
0 |
▶ | QUEUE_ERR("ftruncate(memfd): %s", strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("ftruncate(memfd): %s", strerror(errno)); |
0 |
0 |
0 |
|
| 718 |
0 |
| close(fd); return NULL; |
| 719 |
|
| } |
| 720 |
12 |
| uint64_t reserved = queue_reserve_len(mode, total_size, g.arena_off); |
| 721 |
12 |
▶ | if (queue_reserve(fd, reserved) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (queue_reserve(fd, reserved) < 0) { |
0 |
12 |
12 |
|
| 722 |
0 |
▶ | QUEUE_ERR("memfd: cannot reserve %llu bytes: %s", (unsigned long long)reserved, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("memfd: cannot reserve %llu bytes: %s", (unsigned long long)reserved, strerror(errno)); |
0 |
0 |
0 |
|
| 723 |
0 |
| close(fd); return NULL; |
| 724 |
|
| } |
| 725 |
|
| |
| 726 |
|
| /* Seal size against ftruncate-based SIGBUS attacks via SCM_RIGHTS peers. */ |
| 727 |
12 |
| (void)fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW); |
| 728 |
|
| |
| 729 |
12 |
| void *base = mmap(NULL, (size_t)total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 730 |
12 |
▶ | if (base == MAP_FAILED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base == MAP_FAILED) { |
0 |
12 |
12 |
|
| 731 |
0 |
▶ | QUEUE_ERR("mmap(memfd): %s", strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("mmap(memfd): %s", strerror(errno)); |
0 |
0 |
0 |
|
| 732 |
0 |
| close(fd); return NULL; |
| 733 |
|
| } |
| 734 |
|
| |
| 735 |
12 |
| queue_init_new_header(base, &g, mode, reserved); |
| 736 |
12 |
| QueueHandle *h = queue_handle_new(base, (size_t)total_size, NULL, fd); |
| 737 |
12 |
▶ | if (!h) { munmap(base, (size_t)total_size); close(fd); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) { munmap(base, (size_t)total_size); close(fd); } |
0 |
12 |
12 |
|
| 738 |
12 |
| return h; |
| 739 |
|
| } |
| 740 |
|
| |
| 741 |
|
| /* Open queue from an existing fd (memfd received via SCM_RIGHTS or dup) */ |
| 742 |
4 |
| static QueueHandle *queue_open_fd(int fd, uint32_t mode, char *errbuf) { |
| 743 |
4 |
▶ | if (errbuf) errbuf[0] = '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errbuf) errbuf[0] = '\0'; |
4 |
0 |
4 |
|
| 744 |
|
| |
| 745 |
|
| struct stat st; |
| 746 |
4 |
▶ | if (fstat(fd, &st) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fstat(fd, &st) < 0) { |
0 |
4 |
4 |
|
| 747 |
0 |
▶ | QUEUE_ERR("fstat(fd=%d): %s", fd, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("fstat(fd=%d): %s", fd, strerror(errno)); |
0 |
0 |
0 |
|
| 748 |
0 |
| return NULL; |
| 749 |
|
| } |
| 750 |
|
| |
| 751 |
4 |
▶ | if (!S_ISREG(st.st_mode)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!S_ISREG(st.st_mode)) { |
0 |
4 |
4 |
|
| 752 |
0 |
▶ | QUEUE_ERR("fd %d: invalid (not a regular file)", fd); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("fd %d: invalid (not a regular file)", fd); |
0 |
0 |
0 |
|
| 753 |
0 |
| return NULL; |
| 754 |
|
| } |
| 755 |
|
| |
| 756 |
4 |
▶ | if ((uint64_t)st.st_size < sizeof(QueueHeader)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint64_t)st.st_size < sizeof(QueueHeader)) { |
0 |
4 |
4 |
|
| 757 |
0 |
▶ | QUEUE_ERR("fd %d: too small (%lld)", fd, (long long)st.st_size); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("fd %d: too small (%lld)", fd, (long long)st.st_size); |
0 |
0 |
0 |
|
| 758 |
0 |
| return NULL; |
| 759 |
|
| } |
| 760 |
|
| |
| 761 |
|
| /* before mmap: a creator may truncate the file until it has set magic */ |
| 762 |
|
| uint32_t magic; |
| 763 |
4 |
▶ | if (pread(fd, &magic, sizeof magic, offsetof(QueueHeader, magic)) != (ssize_t)sizeof magic || magic != QUEUE_MAGIC) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pread(fd, &magic, sizeof magic, offsetof(QueueHeader, magic)) != (ssize_t)sizeof magic || magic != QUEUE_MAGIC) { |
4 |
0 |
4 |
| if (pread(fd, &magic, sizeof magic, offsetof(QueueHeader, magic)) != (ssize_t)sizeof magic || magic != QUEUE_MAGIC) { |
0 |
4 |
4 |
|
| 764 |
0 |
▶ | QUEUE_ERR("fd %d: invalid or incompatible queue", fd); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("fd %d: invalid or incompatible queue", fd); |
0 |
0 |
0 |
|
| 765 |
0 |
| return NULL; |
| 766 |
|
| } |
| 767 |
|
| |
| 768 |
4 |
| size_t map_size = (size_t)st.st_size; |
| 769 |
4 |
| void *base = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 770 |
4 |
▶ | if (base == MAP_FAILED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base == MAP_FAILED) { |
0 |
4 |
4 |
|
| 771 |
0 |
▶ | QUEUE_ERR("mmap(fd=%d): %s", fd, strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("mmap(fd=%d): %s", fd, strerror(errno)); |
0 |
0 |
0 |
|
| 772 |
0 |
| return NULL; |
| 773 |
|
| } |
| 774 |
|
| |
| 775 |
4 |
| QueueHeader *hdr = (QueueHeader *)base; |
| 776 |
4 |
▶ | if (!queue_header_valid(hdr, mode, (uint64_t)st.st_size)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_header_valid(hdr, mode, (uint64_t)st.st_size)) { |
2 |
2 |
4 |
|
| 777 |
2 |
▶ | if (hdr->version != QUEUE_VERSION) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->version != QUEUE_VERSION) |
2 |
0 |
2 |
|
| 778 |
2 |
▶ | QUEUE_ERR("fd %d: " QUEUE_FORMAT_ERR, fd, hdr->version, QUEUE_VERSION); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("fd %d: " QUEUE_FORMAT_ERR, fd, hdr->version, QUEUE_VERSION); |
2 |
0 |
2 |
|
| 779 |
|
| else |
| 780 |
0 |
▶ | QUEUE_ERR("fd %d: invalid or incompatible queue", fd); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("fd %d: invalid or incompatible queue", fd); |
0 |
0 |
0 |
|
| 781 |
2 |
| munmap(base, map_size); |
| 782 |
2 |
| return NULL; |
| 783 |
|
| } |
| 784 |
|
| |
| 785 |
|
| /* Dup the fd so caller retains ownership of the original */ |
| 786 |
2 |
| int myfd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
| 787 |
2 |
▶ | if (myfd < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (myfd < 0) { |
0 |
2 |
2 |
|
| 788 |
0 |
▶ | QUEUE_ERR("fcntl(F_DUPFD_CLOEXEC): %s", strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| QUEUE_ERR("fcntl(F_DUPFD_CLOEXEC): %s", strerror(errno)); |
0 |
0 |
0 |
|
| 789 |
0 |
| munmap(base, map_size); |
| 790 |
0 |
| return NULL; |
| 791 |
|
| } |
| 792 |
|
| |
| 793 |
2 |
| QueueHandle *h = queue_handle_new(base, map_size, NULL, myfd); |
| 794 |
2 |
▶ | if (!h) { munmap(base, map_size); close(myfd); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) { munmap(base, map_size); close(myfd); } |
0 |
2 |
2 |
|
| 795 |
2 |
| return h; |
| 796 |
|
| } |
| 797 |
|
| |
| 798 |
157 |
| static void queue_destroy(QueueHandle *h) { |
| 799 |
157 |
▶ | if (!h) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) return; |
0 |
157 |
157 |
|
| 800 |
|
| /* Only close notify_fd if we own it; caller-supplied fds (via |
| 801 |
|
| * queue_eventfd_set) remain the caller's responsibility. */ |
| 802 |
157 |
▶ | if (h->notify_fd >= 0 && h->notify_fd_owned) close(h->notify_fd); |
Branch: 2 branches
| Branch | True | False | Total |
| if (h->notify_fd >= 0 && h->notify_fd_owned) close(h->notify_fd); |
18 |
139 |
157 |
| if (h->notify_fd >= 0 && h->notify_fd_owned) close(h->notify_fd); |
17 |
1 |
18 |
|
| 803 |
157 |
▶ | if (h->backing_fd >= 0) close(h->backing_fd); |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->backing_fd >= 0) close(h->backing_fd); |
14 |
143 |
157 |
|
| 804 |
157 |
▶ | if (h->hdr) munmap(h->hdr, h->mmap_size); |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->hdr) munmap(h->hdr, h->mmap_size); |
157 |
0 |
157 |
|
| 805 |
157 |
| free(h->copy_buf); |
| 806 |
157 |
| free(h->path); |
| 807 |
157 |
| free(h); |
| 808 |
|
| } |
| 809 |
|
| |
| 810 |
|
| /* ================================================================ |
| 811 |
|
| * Int queue macro template: Vyukov bounded MPMC (lock-free) |
| 812 |
|
| * |
| 813 |
|
| * DEFINE_INT_QUEUE(prefix, SlotType, ValType, SeqType, DiffType) |
| 814 |
|
| * generates: queue_<prefix>_try_push, try_pop, push_wait, pop_wait, |
| 815 |
|
| * peek, size, clear |
| 816 |
|
| * ================================================================ */ |
| 817 |
|
| |
| 818 |
|
| #define DEFINE_INT_QUEUE(PFX, SLOT, VTYPE, STYPE, DTYPE) \ |
| 819 |
|
| \ |
| 820 |
|
| static inline int queue_##PFX##_try_push(QueueHandle *h, VTYPE value) { \ |
| 821 |
|
| QueueHeader *hdr = h->hdr; \ |
| 822 |
|
| SLOT *slots = (SLOT *)h->slots; \ |
| 823 |
|
| uint32_t mask = h->cap_mask; \ |
| 824 |
|
| uint64_t pos = __atomic_load_n(&hdr->tail, __ATOMIC_RELAXED); \ |
| 825 |
|
| int spins = 0; \ |
| 826 |
|
| for (;;) { \ |
| 827 |
|
| SLOT *slot = &slots[pos & mask]; \ |
| 828 |
|
| STYPE seq = __atomic_load_n(&slot->sequence, __ATOMIC_ACQUIRE); \ |
| 829 |
|
| DTYPE diff = (DTYPE)(seq - (STYPE)pos); \ |
| 830 |
|
| if (diff == 0) { \ |
| 831 |
|
| if (__atomic_compare_exchange_n(&hdr->tail, &pos, pos + 1, \ |
| 832 |
|
| 1, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) { \ |
| 833 |
|
| slot->value = value; \ |
| 834 |
|
| __atomic_store_n(&slot->sequence, (STYPE)(pos + 1), \ |
| 835 |
|
| __ATOMIC_RELEASE); \ |
| 836 |
|
| /* Order publication before the waiter-count read. */ \ |
| 837 |
|
| __atomic_add_fetch(&hdr->stat_push_ok, 1, __ATOMIC_SEQ_CST); \ |
| 838 |
|
| queue_wake_consumers(hdr); \ |
| 839 |
|
| return 1; \ |
| 840 |
|
| } \ |
| 841 |
|
| spins = 0; \ |
| 842 |
|
| } else if (diff < 0) { \ |
| 843 |
|
| __atomic_add_fetch(&hdr->stat_push_full, 1, __ATOMIC_RELAXED); \ |
| 844 |
|
| return 0; \ |
| 845 |
|
| } else { \ |
| 846 |
|
| uint64_t next_pos = __atomic_load_n(&hdr->tail, __ATOMIC_RELAXED);\ |
| 847 |
|
| if (next_pos == pos) { \ |
| 848 |
|
| queue_spin_pause(); \ |
| 849 |
|
| if (++spins > QUEUE_SPIN_LIMIT) return 0; \ |
| 850 |
|
| continue; \ |
| 851 |
|
| } \ |
| 852 |
|
| pos = next_pos; \ |
| 853 |
|
| spins = 0; \ |
| 854 |
|
| } \ |
| 855 |
|
| } \ |
| 856 |
|
| } \ |
| 857 |
|
| \ |
| 858 |
|
| static inline int queue_##PFX##_try_pop(QueueHandle *h, VTYPE *value) { \ |
| 859 |
|
| QueueHeader *hdr = h->hdr; \ |
| 860 |
|
| SLOT *slots = (SLOT *)h->slots; \ |
| 861 |
|
| uint32_t mask = h->cap_mask; \ |
| 862 |
|
| uint64_t pos = __atomic_load_n(&hdr->head, __ATOMIC_RELAXED); \ |
| 863 |
|
| int spins = 0; \ |
| 864 |
|
| for (;;) { \ |
| 865 |
|
| SLOT *slot = &slots[pos & mask]; \ |
| 866 |
|
| STYPE seq = __atomic_load_n(&slot->sequence, __ATOMIC_ACQUIRE); \ |
| 867 |
|
| DTYPE diff = (DTYPE)(seq - (STYPE)(pos + 1)); \ |
| 868 |
|
| if (diff == 0) { \ |
| 869 |
|
| if (__atomic_compare_exchange_n(&hdr->head, &pos, pos + 1, \ |
| 870 |
|
| 1, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) { \ |
| 871 |
|
| *value = slot->value; \ |
| 872 |
|
| __atomic_store_n(&slot->sequence, \ |
| 873 |
|
| (STYPE)(pos + h->capacity), __ATOMIC_RELEASE); \ |
| 874 |
|
| __atomic_add_fetch(&hdr->stat_pop_ok, 1, __ATOMIC_SEQ_CST); \ |
| 875 |
|
| queue_wake_producers(hdr); \ |
| 876 |
|
| return 1; \ |
| 877 |
|
| } \ |
| 878 |
|
| spins = 0; \ |
| 879 |
|
| } else if (diff < 0) { \ |
| 880 |
|
| __atomic_add_fetch(&hdr->stat_pop_empty, 1, __ATOMIC_RELAXED); \ |
| 881 |
|
| return 0; \ |
| 882 |
|
| } else { \ |
| 883 |
|
| uint64_t next_pos = __atomic_load_n(&hdr->head, __ATOMIC_RELAXED);\ |
| 884 |
|
| if (next_pos == pos) { \ |
| 885 |
|
| queue_spin_pause(); \ |
| 886 |
|
| if (++spins > QUEUE_SPIN_LIMIT) return 0; \ |
| 887 |
|
| continue; \ |
| 888 |
|
| } \ |
| 889 |
|
| pos = next_pos; \ |
| 890 |
|
| spins = 0; \ |
| 891 |
|
| } \ |
| 892 |
|
| } \ |
| 893 |
|
| } \ |
| 894 |
|
| \ |
| 895 |
|
| /* Pass a wake onward when reservations completed out of order. */ \ |
| 896 |
|
| static inline void queue_##PFX##_handoff(QueueHandle *h, bool producer) { \ |
| 897 |
|
| QueueHeader *hdr = h->hdr; \ |
| 898 |
|
| uint32_t *waiters = producer ? &hdr->push_waiters : &hdr->pop_waiters; \ |
| 899 |
|
| if (!__atomic_load_n(waiters, __ATOMIC_RELAXED)) return; \ |
| 900 |
|
| uint64_t pos = __atomic_load_n(producer ? &hdr->tail : &hdr->head, \ |
| 901 |
|
| __ATOMIC_RELAXED); \ |
| 902 |
|
| SLOT *slot = &((SLOT *)h->slots)[pos & h->cap_mask]; \ |
| 903 |
|
| STYPE seq = __atomic_load_n(&slot->sequence, __ATOMIC_ACQUIRE); \ |
| 904 |
|
| if ((DTYPE)(seq - (STYPE)(pos + (producer ? 0 : 1))) < 0) return; \ |
| 905 |
|
| if (producer) queue_wake_producers(hdr); \ |
| 906 |
|
| else queue_wake_consumers(hdr); \ |
| 907 |
|
| } \ |
| 908 |
|
| \ |
| 909 |
|
| static int queue_##PFX##_push_wait(QueueHandle *h, VTYPE value, \ |
| 910 |
|
| double timeout) { \ |
| 911 |
|
| if (queue_##PFX##_try_push(h, value)) return 1; \ |
| 912 |
|
| if (timeout == 0) return 0; \ |
| 913 |
|
| QueueHeader *hdr = h->hdr; \ |
| 914 |
|
| struct timespec deadline, remaining; \ |
| 915 |
|
| int has_deadline = (timeout > 0); \ |
| 916 |
|
| if (has_deadline) queue_make_deadline(timeout, &deadline); \ |
| 917 |
|
| for (;;) { \ |
| 918 |
|
| /* Announce waiter BEFORE sampling fseq and re-checking the \ |
| 919 |
|
| * condition. The reverse order races with a waker that pops \ |
| 920 |
|
| * between our check and our announce: the waker reads \ |
| 921 |
|
| * push_waiters==0, skips the push_futex bump, and our subsequent \ |
| 922 |
|
| * FUTEX_WAIT then sleeps on an unchanged seq with no future \ |
| 923 |
|
| * waker to wake it. */ \ |
| 924 |
|
| __atomic_add_fetch(&hdr->push_waiters, 1, __ATOMIC_SEQ_CST); \ |
| 925 |
|
| uint32_t fseq = __atomic_load_n(&hdr->push_futex, __ATOMIC_ACQUIRE); \ |
| 926 |
|
| if (queue_##PFX##_try_push(h, value)) { \ |
| 927 |
|
| __atomic_sub_fetch(&hdr->push_waiters, 1, __ATOMIC_RELEASE); \ |
| 928 |
|
| queue_##PFX##_handoff(h, true); \ |
| 929 |
|
| return 1; \ |
| 930 |
|
| } \ |
| 931 |
|
| const struct timespec *pts = &queue_recheck; \ |
| 932 |
|
| if (has_deadline) { \ |
| 933 |
|
| if (!queue_remaining_time(&deadline, &remaining)) { \ |
| 934 |
|
| __atomic_sub_fetch(&hdr->push_waiters, 1, __ATOMIC_RELEASE); \ |
| 935 |
|
| return 0; \ |
| 936 |
|
| } \ |
| 937 |
|
| if (remaining.tv_sec < QUEUE_RECHECK_SEC) pts = &remaining; \ |
| 938 |
|
| } \ |
| 939 |
|
| long rc = syscall(SYS_futex, &hdr->push_futex, FUTEX_WAIT, \ |
| 940 |
|
| fseq, pts, NULL, 0); \ |
| 941 |
|
| __atomic_sub_fetch(&hdr->push_waiters, 1, __ATOMIC_RELEASE); \ |
| 942 |
|
| if (queue_##PFX##_try_push(h, value)) { \ |
| 943 |
|
| queue_##PFX##_handoff(h, true); \ |
| 944 |
|
| return 1; \ |
| 945 |
|
| } \ |
| 946 |
|
| if (has_deadline && rc == -1 && errno == ETIMEDOUT && !queue_remaining_time(&deadline, &remaining)) return 0; \ |
| 947 |
|
| } \ |
| 948 |
|
| } \ |
| 949 |
|
| \ |
| 950 |
|
| static int queue_##PFX##_pop_wait(QueueHandle *h, VTYPE *value, \ |
| 951 |
|
| double timeout) { \ |
| 952 |
|
| if (queue_##PFX##_try_pop(h, value)) return 1; \ |
| 953 |
|
| if (timeout == 0) return 0; \ |
| 954 |
|
| QueueHeader *hdr = h->hdr; \ |
| 955 |
|
| struct timespec deadline, remaining; \ |
| 956 |
|
| int has_deadline = (timeout > 0); \ |
| 957 |
|
| if (has_deadline) queue_make_deadline(timeout, &deadline); \ |
| 958 |
|
| for (;;) { \ |
| 959 |
|
| /* Announce BEFORE sampling fseq / checking condition; see \ |
| 960 |
|
| * queue_##PFX##_push_wait for the missed-wakeup race this \ |
| 961 |
|
| * ordering closes. */ \ |
| 962 |
|
| __atomic_add_fetch(&hdr->pop_waiters, 1, __ATOMIC_SEQ_CST); \ |
| 963 |
|
| uint32_t fseq = __atomic_load_n(&hdr->pop_futex, __ATOMIC_ACQUIRE); \ |
| 964 |
|
| if (queue_##PFX##_try_pop(h, value)) { \ |
| 965 |
|
| __atomic_sub_fetch(&hdr->pop_waiters, 1, __ATOMIC_RELEASE); \ |
| 966 |
|
| queue_##PFX##_handoff(h, false); \ |
| 967 |
|
| return 1; \ |
| 968 |
|
| } \ |
| 969 |
|
| const struct timespec *pts = &queue_recheck; \ |
| 970 |
|
| if (has_deadline) { \ |
| 971 |
|
| if (!queue_remaining_time(&deadline, &remaining)) { \ |
| 972 |
|
| __atomic_sub_fetch(&hdr->pop_waiters, 1, __ATOMIC_RELEASE); \ |
| 973 |
|
| return 0; \ |
| 974 |
|
| } \ |
| 975 |
|
| if (remaining.tv_sec < QUEUE_RECHECK_SEC) pts = &remaining; \ |
| 976 |
|
| } \ |
| 977 |
|
| long rc = syscall(SYS_futex, &hdr->pop_futex, FUTEX_WAIT, \ |
| 978 |
|
| fseq, pts, NULL, 0); \ |
| 979 |
|
| __atomic_sub_fetch(&hdr->pop_waiters, 1, __ATOMIC_RELEASE); \ |
| 980 |
|
| if (queue_##PFX##_try_pop(h, value)) { \ |
| 981 |
|
| queue_##PFX##_handoff(h, false); \ |
| 982 |
|
| return 1; \ |
| 983 |
|
| } \ |
| 984 |
|
| if (has_deadline && rc == -1 && errno == ETIMEDOUT && !queue_remaining_time(&deadline, &remaining)) return 0; \ |
| 985 |
|
| } \ |
| 986 |
|
| } \ |
| 987 |
|
| \ |
| 988 |
|
| static inline int queue_##PFX##_peek(QueueHandle *h, VTYPE *value) { \ |
| 989 |
|
| SLOT *slots = (SLOT *)h->slots; \ |
| 990 |
|
| uint64_t pos = __atomic_load_n(&h->hdr->head, __ATOMIC_ACQUIRE); \ |
| 991 |
|
| SLOT *slot = &slots[pos & h->cap_mask]; \ |
| 992 |
|
| STYPE seq = __atomic_load_n(&slot->sequence, __ATOMIC_ACQUIRE); \ |
| 993 |
|
| if ((DTYPE)(seq - (STYPE)(pos + 1)) == 0) { \ |
| 994 |
|
| *value = slot->value; \ |
| 995 |
|
| return 1; \ |
| 996 |
|
| } \ |
| 997 |
|
| return 0; \ |
| 998 |
|
| } \ |
| 999 |
|
| \ |
| 1000 |
|
| static inline uint64_t queue_##PFX##_size(QueueHandle *h) { \ |
| 1001 |
|
| uint64_t tail = __atomic_load_n(&h->hdr->tail, __ATOMIC_RELAXED); \ |
| 1002 |
|
| uint64_t head = __atomic_load_n(&h->hdr->head, __ATOMIC_RELAXED); \ |
| 1003 |
|
| uint64_t diff = tail - head; \ |
| 1004 |
|
| if ((int64_t)diff < 0) return 0; \ |
| 1005 |
|
| return diff > h->capacity ? h->capacity : diff; \ |
| 1006 |
|
| } \ |
| 1007 |
|
| \ |
| 1008 |
|
| static void queue_##PFX##_clear(QueueHandle *h) { \ |
| 1009 |
|
| VTYPE tmp; \ |
| 1010 |
|
| while (queue_##PFX##_try_pop(h, &tmp)) {} \ |
| 1011 |
|
| } |
| 1012 |
|
| |
| 1013 |
|
| /* Instantiate for Int (64-bit seq + 64-bit value = 16 bytes/slot) */ |
| 1014 |
2836 |
▶ | DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
Branch: 42 branches
| Branch | True | False | Total |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
44 |
5 |
49 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
3 |
34 |
37 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
7 |
3 |
10 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
100 |
7 |
107 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
0 |
7 |
7 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
7 |
0 |
7 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
0 |
7 |
7 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
7 |
0 |
7 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
0 |
7 |
7 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
2 |
5 |
7 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
5 |
2 |
7 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
2 |
0 |
2 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
2 |
0 |
2 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
2 |
0 |
2 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
2 |
0 |
2 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
1010 |
4 |
1014 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
1 |
3 |
4 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
3 |
0 |
3 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
0 |
3 |
3 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
3 |
0 |
3 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
0 |
3 |
3 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
2 |
1 |
3 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
1 |
2 |
3 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
2 |
0 |
2 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
2 |
0 |
2 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
2 |
0 |
2 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
2 |
0 |
2 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
1 |
5 |
6 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
6 |
0 |
6 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
323 |
39 |
362 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
323 |
0 |
323 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
39 |
0 |
39 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
1227 |
14 |
1241 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
1227 |
0 |
1227 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
14 |
0 |
14 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t) |
0 |
0 |
0 |
|
| 1015 |
|
| |
| 1016 |
|
| /* Instantiate for Int32 (32-bit seq + 32-bit value = 8 bytes/slot) */ |
| 1017 |
0 |
▶ | DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
Branch: 42 branches
| Branch | True | False | Total |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
16 |
1 |
17 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
7 |
7 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
2 |
0 |
2 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
99 |
2 |
101 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
2 |
2 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
2 |
0 |
2 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
2 |
2 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
2 |
0 |
2 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
2 |
2 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
2 |
2 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
2 |
0 |
2 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
2 |
2 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
2 |
0 |
2 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
144 |
7 |
151 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
144 |
0 |
144 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
7 |
0 |
7 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
45 |
1 |
46 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
45 |
0 |
45 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
1 |
0 |
1 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t) |
0 |
0 |
0 |
|
| 1018 |
|
| |
| 1019 |
|
| /* Instantiate for Int16 (32-bit seq + 16-bit value = 8 bytes/slot) */ |
| 1020 |
0 |
▶ | DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
Branch: 42 branches
| Branch | True | False | Total |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
1 |
1 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
18 |
1 |
19 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
18 |
0 |
18 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
1 |
0 |
1 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
20 |
0 |
20 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
20 |
0 |
20 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
| DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t) |
0 |
0 |
0 |
|
| 1021 |
|
| |
| 1022 |
|
| /* ================================================================ |
| 1023 |
|
| * Str queue: mutex-protected with circular arena |
| 1024 |
|
| * ================================================================ */ |
| 1025 |
|
| |
| 1026 |
|
| /* Under the mutex: a woken large value may not fit while a smaller one does. |
| 1027 |
|
| * Wake its peers once per removal, without changing the space futex sequence. */ |
| 1028 |
2297 |
| static inline void queue_str_arena_blocked(QueueHandle *h) { |
| 1029 |
2297 |
| QueueHeader *hdr = h->hdr; |
| 1030 |
2297 |
▶ | if (hdr->str_arena_wait) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->str_arena_wait) return; |
917 |
1380 |
2297 |
|
| 1031 |
1380 |
| hdr->str_arena_wait = 1; |
| 1032 |
1380 |
▶ | if (h->arena_cap - hdr->arena_used >= 8 |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->arena_cap - hdr->arena_used >= 8 |
1364 |
16 |
1380 |
|
| 1033 |
1364 |
▶ | && __atomic_load_n(&hdr->push_waiters, __ATOMIC_RELAXED)) |
Branch: 1 branch
| Branch | True | False | Total |
| && __atomic_load_n(&hdr->push_waiters, __ATOMIC_RELAXED)) |
0 |
1364 |
1364 |
|
| 1034 |
0 |
| syscall(SYS_futex, &hdr->push_futex, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); |
| 1035 |
|
| } |
| 1036 |
|
| |
| 1037 |
17792 |
| static inline uint32_t queue_str_producer_wake_count(QueueHeader *hdr, uint32_t n) { |
| 1038 |
17792 |
▶ | if (!hdr->str_arena_wait) return n; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hdr->str_arena_wait) return n; |
16411 |
1381 |
17792 |
|
| 1039 |
1381 |
| hdr->str_arena_wait = 0; |
| 1040 |
1381 |
| return INT_MAX; |
| 1041 |
|
| } |
| 1042 |
|
| |
| 1043 |
|
| #ifndef MADV_POPULATE_WRITE |
| 1044 |
|
| #define MADV_POPULATE_WRITE 23 |
| 1045 |
|
| #endif |
| 1046 |
|
| |
| 1047 |
|
| /* Allocates the pages under [p, p+len) as a write would, but fails instead of raising SIGBUS; |
| 1048 |
|
| * does nothing on kernels without MADV_POPULATE_WRITE (before 5.14). */ |
| 1049 |
49 |
| static int queue_populate(char *p, uint64_t len) { |
| 1050 |
|
| static uintptr_t page; |
| 1051 |
|
| static int unsupported; |
| 1052 |
49 |
▶ | if (unsupported) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (unsupported) return 0; |
0 |
49 |
49 |
|
| 1053 |
49 |
▶ | if (!page) page = (uintptr_t)sysconf(_SC_PAGESIZE); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!page) page = (uintptr_t)sysconf(_SC_PAGESIZE); |
16 |
33 |
49 |
|
| 1054 |
49 |
| uintptr_t a = (uintptr_t)p & ~(page - 1); |
| 1055 |
49 |
| uintptr_t e = ((uintptr_t)p + len + page - 1) & ~(page - 1); |
| 1056 |
49 |
| int r, tries = 0; |
| 1057 |
49 |
| do r = madvise((void *)a, e - a, MADV_POPULATE_WRITE); |
| 1058 |
49 |
▶ | while (r < 0 && (errno == EINTR || errno == EAGAIN) && ++tries < 8); |
Branch: 4 branches
| Branch | True | False | Total |
| while (r < 0 && (errno == EINTR || errno == EAGAIN) && ++tries < 8); |
0 |
49 |
49 |
| while (r < 0 && (errno == EINTR || errno == EAGAIN) && ++tries < 8); |
0 |
0 |
0 |
| while (r < 0 && (errno == EINTR || errno == EAGAIN) && ++tries < 8); |
0 |
0 |
0 |
| while (r < 0 && (errno == EINTR || errno == EAGAIN) && ++tries < 8); |
0 |
0 |
0 |
|
| 1059 |
49 |
▶ | if (r < 0 && errno == EINVAL) unsupported = 1, r = 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (r < 0 && errno == EINVAL) unsupported = 1, r = 0; |
0 |
49 |
49 |
| if (r < 0 && errno == EINVAL) unsupported = 1, r = 0; |
0 |
0 |
0 |
|
| 1060 |
49 |
| return r; |
| 1061 |
|
| } |
| 1062 |
|
| |
| 1063 |
|
| /* Under the mutex, before writing arena bytes [pos, end); only push_front's wrap to the top writes above the mark. */ |
| 1064 |
18876 |
| static int queue_arena_populate(QueueHandle *h, uint32_t pos, uint32_t end) { |
| 1065 |
18876 |
| QueueHeader *hdr = h->hdr; |
| 1066 |
18876 |
| uint32_t mark = hdr->arena_populated; |
| 1067 |
18876 |
▶ | if (end <= mark) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (end <= mark) return 1; |
18827 |
49 |
18876 |
|
| 1068 |
49 |
| uint64_t from = pos > mark ? pos : mark, to = end; |
| 1069 |
49 |
▶ | if (from == mark) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (from == mark) { |
49 |
0 |
49 |
|
| 1070 |
49 |
| uint64_t step = (uint64_t)mark + QUEUE_POPULATE_CHUNK; |
| 1071 |
49 |
▶ | if (step > h->arena_cap) step = h->arena_cap; |
Branch: 1 branch
| Branch | True | False | Total |
| if (step > h->arena_cap) step = h->arena_cap; |
43 |
6 |
49 |
|
| 1072 |
49 |
▶ | if (step > to) to = step; |
Branch: 1 branch
| Branch | True | False | Total |
| if (step > to) to = step; |
49 |
0 |
49 |
|
| 1073 |
|
| } |
| 1074 |
49 |
▶ | if (queue_populate(h->arena + from, to - from) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (queue_populate(h->arena + from, to - from) < 0) { |
0 |
49 |
49 |
|
| 1075 |
0 |
▶ | h->fail_errno = errno == EFAULT ? ENOSPC : errno; |
Branch: 1 branch
| Branch | True | False | Total |
| h->fail_errno = errno == EFAULT ? ENOSPC : errno; |
0 |
0 |
0 |
|
| 1076 |
0 |
| return 0; |
| 1077 |
|
| } |
| 1078 |
49 |
▶ | if (from == mark) __atomic_store_n(&hdr->arena_populated, (uint32_t)to, __ATOMIC_RELAXED); |
Branch: 1 branch
| Branch | True | False | Total |
| if (from == mark) __atomic_store_n(&hdr->arena_populated, (uint32_t)to, __ATOMIC_RELAXED); |
49 |
0 |
49 |
|
| 1079 |
49 |
| return 1; |
| 1080 |
|
| } |
| 1081 |
|
| |
| 1082 |
|
| /* Push one item while mutex is already held. Returns 1=ok, 0=full, -2=too long, QUEUE_FAILED. */ |
| 1083 |
11154 |
| static inline int queue_str_push_locked(QueueHandle *h, const char *str, |
| 1084 |
|
| uint32_t len, bool utf8) { |
| 1085 |
11154 |
| QueueHeader *hdr = h->hdr; |
| 1086 |
|
| |
| 1087 |
11154 |
▶ | if (len > QUEUE_STR_LEN_MASK) return -2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > QUEUE_STR_LEN_MASK) return -2; |
0 |
11154 |
11154 |
|
| 1088 |
|
| |
| 1089 |
11154 |
▶ | if (hdr->tail - hdr->head >= h->capacity) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->tail - hdr->head >= h->capacity) { |
12 |
11142 |
11154 |
|
| 1090 |
12 |
| __atomic_add_fetch(&hdr->stat_push_full, 1, __ATOMIC_RELAXED); |
| 1091 |
12 |
| return 0; |
| 1092 |
|
| } |
| 1093 |
|
| |
| 1094 |
11142 |
| uint32_t alloc = (len + 7) & ~7u; |
| 1095 |
11142 |
▶ | if (alloc == 0) alloc = 8; |
Branch: 1 branch
| Branch | True | False | Total |
| if (alloc == 0) alloc = 8; |
2 |
11140 |
11142 |
|
| 1096 |
11142 |
▶ | if (alloc > h->arena_cap) return -2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (alloc > h->arena_cap) return -2; |
1 |
11141 |
11142 |
|
| 1097 |
11141 |
| uint32_t saved_wpos = hdr->arena_wpos; |
| 1098 |
11141 |
| uint32_t pos = saved_wpos; |
| 1099 |
11141 |
| uint64_t skip = alloc; |
| 1100 |
|
| |
| 1101 |
11141 |
▶ | if (pos >= h->arena_cap || (uint64_t)pos + alloc > h->arena_cap) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pos >= h->arena_cap || (uint64_t)pos + alloc > h->arena_cap) { |
11138 |
3 |
11141 |
| if (pos >= h->arena_cap || (uint64_t)pos + alloc > h->arena_cap) { |
573 |
10565 |
11138 |
|
| 1102 |
576 |
▶ | skip += (pos < h->arena_cap) ? (h->arena_cap - pos) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| skip += (pos < h->arena_cap) ? (h->arena_cap - pos) : 0; |
573 |
3 |
576 |
|
| 1103 |
576 |
| pos = 0; |
| 1104 |
|
| } |
| 1105 |
|
| |
| 1106 |
11141 |
▶ | if ((uint64_t)hdr->arena_used + skip > h->arena_cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint64_t)hdr->arena_used + skip > h->arena_cap) { |
1155 |
9986 |
11141 |
|
| 1107 |
1155 |
▶ | if (hdr->tail == hdr->head) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->tail == hdr->head) { |
0 |
1155 |
1155 |
|
| 1108 |
0 |
| saved_wpos = 0; |
| 1109 |
0 |
| pos = 0; |
| 1110 |
0 |
| skip = alloc; |
| 1111 |
|
| } else { |
| 1112 |
1155 |
| queue_str_arena_blocked(h); |
| 1113 |
1155 |
| __atomic_add_fetch(&hdr->stat_push_full, 1, __ATOMIC_RELAXED); |
| 1114 |
1155 |
| return 0; |
| 1115 |
|
| } |
| 1116 |
|
| } |
| 1117 |
|
| |
| 1118 |
9986 |
▶ | if (!queue_arena_populate(h, pos, pos + alloc)) return QUEUE_FAILED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_arena_populate(h, pos, pos + alloc)) return QUEUE_FAILED; |
0 |
9986 |
9986 |
|
| 1119 |
9986 |
| memcpy(h->arena + pos, str, len); |
| 1120 |
|
| |
| 1121 |
9986 |
| uint32_t idx = (uint32_t)(hdr->tail & h->cap_mask); |
| 1122 |
9986 |
| QueueStrSlot *slot = &((QueueStrSlot *)h->slots)[idx]; |
| 1123 |
9986 |
| slot->arena_off = pos; |
| 1124 |
9986 |
▶ | slot->packed_len = len | (utf8 ? QUEUE_STR_UTF8_FLAG : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| slot->packed_len = len | (utf8 ? QUEUE_STR_UTF8_FLAG : 0); |
6 |
9980 |
9986 |
|
| 1125 |
9986 |
| slot->arena_skip = (uint32_t)skip; |
| 1126 |
9986 |
| slot->prev_wpos = saved_wpos; |
| 1127 |
|
| |
| 1128 |
9986 |
| queue_set_arena(hdr, pos + alloc, |
| 1129 |
9986 |
▶ | hdr->tail == hdr->head ? (uint32_t)skip : hdr->arena_used + (uint32_t)skip); |
Branch: 1 branch
| Branch | True | False | Total |
| hdr->tail == hdr->head ? (uint32_t)skip : hdr->arena_used + (uint32_t)skip); |
1344 |
8642 |
9986 |
|
| 1130 |
|
| /* Publish after the payload and arena state, including on weakly ordered CPUs. */ |
| 1131 |
9986 |
| __atomic_store_n(&hdr->tail, hdr->tail + 1, __ATOMIC_RELEASE); |
| 1132 |
9986 |
| __atomic_add_fetch(&hdr->stat_push_ok, 1, __ATOMIC_RELAXED); |
| 1133 |
9986 |
| return 1; |
| 1134 |
|
| } |
| 1135 |
|
| |
| 1136 |
11146 |
| static inline int queue_str_try_push(QueueHandle *h, const char *str, |
| 1137 |
|
| uint32_t len, bool utf8) { |
| 1138 |
11146 |
▶ | if (!queue_str_lock(h)) return QUEUE_FAILED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_str_lock(h)) return QUEUE_FAILED; |
4 |
11142 |
11146 |
|
| 1139 |
11142 |
| int r = queue_str_push_locked(h, str, len, utf8); |
| 1140 |
11142 |
| queue_mutex_unlock(h->hdr); |
| 1141 |
11142 |
▶ | if (r == 1) queue_wake_consumers(h->hdr); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r == 1) queue_wake_consumers(h->hdr); |
9976 |
1166 |
11142 |
|
| 1142 |
11142 |
| return r; |
| 1143 |
|
| } |
| 1144 |
|
| |
| 1145 |
|
| /* Pop one item while mutex is held. Returns 1=ok, 0=empty, -1=OOM. */ |
| 1146 |
10260 |
| static inline int queue_str_pop_locked(QueueHandle *h, const char **out_str, |
| 1147 |
|
| uint32_t *out_len, bool *out_utf8) { |
| 1148 |
10260 |
| QueueHeader *hdr = h->hdr; |
| 1149 |
|
| |
| 1150 |
10260 |
▶ | if (hdr->tail == hdr->head) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->tail == hdr->head) { |
1313 |
8947 |
10260 |
|
| 1151 |
1313 |
| __atomic_add_fetch(&hdr->stat_pop_empty, 1, __ATOMIC_RELAXED); |
| 1152 |
1313 |
| return 0; |
| 1153 |
|
| } |
| 1154 |
|
| |
| 1155 |
8947 |
| uint32_t idx = (uint32_t)(hdr->head & h->cap_mask); |
| 1156 |
8947 |
| QueueStrSlot *slot = &((QueueStrSlot *)h->slots)[idx]; |
| 1157 |
|
| |
| 1158 |
8947 |
| uint32_t off = slot->arena_off; |
| 1159 |
8947 |
| uint32_t len = slot->packed_len & QUEUE_STR_LEN_MASK; |
| 1160 |
|
| /* arena_off/len come from the mmap'd slot; a local peer can corrupt the |
| 1161 |
|
| * backing file. Bound the record against the arena so a poisoned slot |
| 1162 |
|
| * yields an empty payload instead of an out-of-bounds read (CWE-125). */ |
| 1163 |
8947 |
▶ | if ((uint64_t)off + len > h->arena_cap) len = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint64_t)off + len > h->arena_cap) len = 0; |
0 |
8947 |
8947 |
|
| 1164 |
8947 |
| *out_utf8 = (slot->packed_len & QUEUE_STR_UTF8_FLAG) != 0; |
| 1165 |
|
| |
| 1166 |
8947 |
▶ | if (!queue_ensure_copy_buf(h, len + 1)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_ensure_copy_buf(h, len + 1)) |
0 |
8947 |
8947 |
|
| 1167 |
0 |
| return -1; |
| 1168 |
8947 |
▶ | if (len > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > 0) |
8945 |
2 |
8947 |
|
| 1169 |
8945 |
| memcpy(h->copy_buf, h->arena + off, len); |
| 1170 |
8947 |
| h->copy_buf[len] = '\0'; |
| 1171 |
8947 |
| *out_str = h->copy_buf; |
| 1172 |
8947 |
| *out_len = len; |
| 1173 |
|
| |
| 1174 |
8947 |
| int last = hdr->tail == hdr->head + 1; |
| 1175 |
8947 |
| uint32_t skip = slot->arena_skip; |
| 1176 |
|
| /* Consume before crediting the bytes back: a crash between the two then |
| 1177 |
|
| * leaves a self-healing over-count, not a message the next push overwrites. */ |
| 1178 |
8947 |
| __atomic_store_n(&hdr->head, hdr->head + 1, __ATOMIC_RELAXED); |
| 1179 |
|
| |
| 1180 |
8947 |
▶ | if (last) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (last) { |
1361 |
7586 |
8947 |
|
| 1181 |
1361 |
| queue_set_arena(hdr, 0, 0); |
| 1182 |
7586 |
▶ | } else if (hdr->arena_used <= skip) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (hdr->arena_used <= skip) { |
0 |
7586 |
7586 |
|
| 1183 |
0 |
| __atomic_store_n(&hdr->arena_used, 0, __ATOMIC_RELEASE); |
| 1184 |
|
| } else { |
| 1185 |
7586 |
| __atomic_store_n(&hdr->arena_used, hdr->arena_used - skip, __ATOMIC_RELEASE); |
| 1186 |
|
| } |
| 1187 |
|
| |
| 1188 |
8947 |
| __atomic_add_fetch(&hdr->stat_pop_ok, 1, __ATOMIC_RELAXED); |
| 1189 |
8947 |
| return 1; |
| 1190 |
|
| } |
| 1191 |
|
| |
| 1192 |
|
| /* Under the mutex; 0 when empty. */ |
| 1193 |
63 |
| static inline uint32_t queue_str_front_len(QueueHandle *h) { |
| 1194 |
63 |
| QueueHeader *hdr = h->hdr; |
| 1195 |
63 |
▶ | if (hdr->tail == hdr->head) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->tail == hdr->head) return 0; |
16 |
47 |
63 |
|
| 1196 |
47 |
| return ((QueueStrSlot *)h->slots)[hdr->head & h->cap_mask].packed_len & QUEUE_STR_LEN_MASK; |
| 1197 |
|
| } |
| 1198 |
|
| |
| 1199 |
10182 |
| static inline int queue_str_try_pop(QueueHandle *h, const char **out_str, |
| 1200 |
|
| uint32_t *out_len, bool *out_utf8) { |
| 1201 |
10182 |
▶ | if (!queue_str_lock(h)) return QUEUE_FAILED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_str_lock(h)) return QUEUE_FAILED; |
3 |
10179 |
10182 |
|
| 1202 |
10179 |
| int r = queue_str_pop_locked(h, out_str, out_len, out_utf8); |
| 1203 |
10179 |
▶ | uint32_t wake = r == 1 ? queue_str_producer_wake_count(h->hdr, 1) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| uint32_t wake = r == 1 ? queue_str_producer_wake_count(h->hdr, 1) : 0; |
8883 |
1296 |
10179 |
|
| 1204 |
10179 |
| queue_mutex_unlock(h->hdr); |
| 1205 |
10179 |
▶ | if (wake) queue_wake_producers_n(h->hdr, wake); |
Branch: 1 branch
| Branch | True | False | Total |
| if (wake) queue_wake_producers_n(h->hdr, wake); |
8883 |
1296 |
10179 |
|
| 1206 |
10179 |
| return r; |
| 1207 |
|
| } |
| 1208 |
|
| |
| 1209 |
40102 |
| static inline uint64_t queue_str_size(QueueHandle *h) { |
| 1210 |
40102 |
| QueueHeader *hdr = h->hdr; |
| 1211 |
40102 |
| uint64_t tail = __atomic_load_n(&hdr->tail, __ATOMIC_RELAXED); |
| 1212 |
40102 |
| uint64_t head = __atomic_load_n(&hdr->head, __ATOMIC_RELAXED); |
| 1213 |
40102 |
| uint64_t diff = tail - head; |
| 1214 |
40102 |
▶ | if ((int64_t)diff < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((int64_t)diff < 0) return 0; |
0 |
40102 |
40102 |
|
| 1215 |
40102 |
| return diff > h->capacity ? h->capacity : diff; |
| 1216 |
|
| } |
| 1217 |
|
| |
| 1218 |
22 |
| static int queue_str_clear(QueueHandle *h) { |
| 1219 |
22 |
| QueueHeader *hdr = h->hdr; |
| 1220 |
22 |
▶ | if (!queue_str_lock(h)) return QUEUE_FAILED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_str_lock(h)) return QUEUE_FAILED; |
1 |
21 |
22 |
|
| 1221 |
|
| /* One store empties it: zeroing head and tail apart would leave tail-head garbage. */ |
| 1222 |
21 |
| __atomic_store_n(&hdr->head, hdr->tail, __ATOMIC_RELAXED); |
| 1223 |
21 |
| queue_set_arena(hdr, 0, 0); |
| 1224 |
21 |
| queue_mutex_unlock(hdr); |
| 1225 |
|
| /* clear is a bulk transition -- wake every blocked producer and |
| 1226 |
|
| * consumer so they re-evaluate state, not just one of each. */ |
| 1227 |
21 |
▶ | if (__atomic_load_n(&hdr->push_waiters, __ATOMIC_RELAXED) > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (__atomic_load_n(&hdr->push_waiters, __ATOMIC_RELAXED) > 0) { |
12 |
9 |
21 |
|
| 1228 |
12 |
| __atomic_add_fetch(&hdr->push_futex, 1, __ATOMIC_RELEASE); |
| 1229 |
12 |
| syscall(SYS_futex, &hdr->push_futex, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); |
| 1230 |
|
| } |
| 1231 |
21 |
▶ | if (__atomic_load_n(&hdr->pop_waiters, __ATOMIC_RELAXED) > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (__atomic_load_n(&hdr->pop_waiters, __ATOMIC_RELAXED) > 0) { |
0 |
21 |
21 |
|
| 1232 |
0 |
| __atomic_add_fetch(&hdr->pop_futex, 1, __ATOMIC_RELEASE); |
| 1233 |
0 |
| syscall(SYS_futex, &hdr->pop_futex, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); |
| 1234 |
|
| } |
| 1235 |
21 |
| return 1; |
| 1236 |
|
| } |
| 1237 |
|
| |
| 1238 |
|
| /* Peek: read front element without consuming (exact, under mutex). */ |
| 1239 |
9 |
| static inline int queue_str_peek(QueueHandle *h, const char **out_str, |
| 1240 |
|
| uint32_t *out_len, bool *out_utf8) { |
| 1241 |
9 |
| QueueHeader *hdr = h->hdr; |
| 1242 |
9 |
▶ | if (!queue_str_lock(h)) return QUEUE_FAILED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_str_lock(h)) return QUEUE_FAILED; |
1 |
8 |
9 |
|
| 1243 |
8 |
▶ | if (hdr->tail == hdr->head) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->tail == hdr->head) { |
1 |
7 |
8 |
|
| 1244 |
1 |
| queue_mutex_unlock(hdr); |
| 1245 |
1 |
| return 0; |
| 1246 |
|
| } |
| 1247 |
7 |
| uint32_t idx = (uint32_t)(hdr->head & h->cap_mask); |
| 1248 |
7 |
| QueueStrSlot *slot = &((QueueStrSlot *)h->slots)[idx]; |
| 1249 |
7 |
| uint32_t off = slot->arena_off; |
| 1250 |
7 |
| uint32_t len = slot->packed_len & QUEUE_STR_LEN_MASK; |
| 1251 |
7 |
▶ | if ((uint64_t)off + len > h->arena_cap) len = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint64_t)off + len > h->arena_cap) len = 0; |
0 |
7 |
7 |
|
| 1252 |
7 |
| *out_utf8 = (slot->packed_len & QUEUE_STR_UTF8_FLAG) != 0; |
| 1253 |
7 |
▶ | if (!queue_ensure_copy_buf(h, len + 1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_ensure_copy_buf(h, len + 1)) { |
0 |
7 |
7 |
|
| 1254 |
0 |
| queue_mutex_unlock(hdr); |
| 1255 |
0 |
| return -1; |
| 1256 |
|
| } |
| 1257 |
7 |
▶ | if (len > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > 0) |
7 |
0 |
7 |
|
| 1258 |
7 |
| memcpy(h->copy_buf, h->arena + off, len); |
| 1259 |
7 |
| h->copy_buf[len] = '\0'; |
| 1260 |
7 |
| *out_str = h->copy_buf; |
| 1261 |
7 |
| *out_len = len; |
| 1262 |
7 |
| queue_mutex_unlock(hdr); |
| 1263 |
7 |
| return 1; |
| 1264 |
|
| } |
| 1265 |
|
| |
| 1266 |
|
| /* Push to front of queue (requeue). Str only -- Int is strictly FIFO. */ |
| 1267 |
10040 |
| static inline int queue_str_push_front(QueueHandle *h, const char *str, |
| 1268 |
|
| uint32_t len, bool utf8) { |
| 1269 |
10040 |
| QueueHeader *hdr = h->hdr; |
| 1270 |
10040 |
▶ | if (!queue_str_lock(h)) return QUEUE_FAILED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_str_lock(h)) return QUEUE_FAILED; |
2 |
10038 |
10040 |
|
| 1271 |
|
| |
| 1272 |
10038 |
▶ | if (len > QUEUE_STR_LEN_MASK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > QUEUE_STR_LEN_MASK) { |
0 |
10038 |
10038 |
|
| 1273 |
0 |
| queue_mutex_unlock(hdr); |
| 1274 |
0 |
| return -2; |
| 1275 |
|
| } |
| 1276 |
|
| |
| 1277 |
10038 |
| uint64_t size = hdr->tail - hdr->head; |
| 1278 |
10038 |
▶ | if (size >= h->capacity) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (size >= h->capacity) { |
6 |
10032 |
10038 |
|
| 1279 |
6 |
| __atomic_add_fetch(&hdr->stat_push_full, 1, __ATOMIC_RELAXED); |
| 1280 |
6 |
| queue_mutex_unlock(hdr); |
| 1281 |
6 |
| return 0; |
| 1282 |
|
| } |
| 1283 |
|
| |
| 1284 |
10032 |
| uint32_t alloc = (len + 7) & ~7u; |
| 1285 |
10032 |
▶ | if (alloc == 0) alloc = 8; |
Branch: 1 branch
| Branch | True | False | Total |
| if (alloc == 0) alloc = 8; |
0 |
10032 |
10032 |
|
| 1286 |
10032 |
▶ | if (alloc > h->arena_cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (alloc > h->arena_cap) { |
0 |
10032 |
10032 |
|
| 1287 |
0 |
| queue_mutex_unlock(hdr); |
| 1288 |
0 |
| return -2; |
| 1289 |
|
| } |
| 1290 |
|
| |
| 1291 |
|
| /* Allocate at the READ end of the circular arena, growing BACKWARD, so |
| 1292 |
|
| * the new element sits just before the current front in ring order. The |
| 1293 |
|
| * arena is thus always laid out in queue order (front..back is a single |
| 1294 |
|
| * contiguous ring region), which the free accounting relies on. The |
| 1295 |
|
| * invariant arena_wpos == (read_frontier + arena_used) mod arena_cap is |
| 1296 |
|
| * preserved by every op, so pop_back can just retreat arena_wpos by the |
| 1297 |
|
| * slot's skip. */ |
| 1298 |
10032 |
| uint32_t cap = (uint32_t)h->arena_cap; |
| 1299 |
10032 |
▶ | if (cap == 0) { queue_mutex_unlock(hdr); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (cap == 0) { queue_mutex_unlock(hdr); return 0; } |
0 |
10032 |
10032 |
|
| 1300 |
|
| uint32_t pos, skip; |
| 1301 |
10032 |
| int empty = hdr->tail == hdr->head; |
| 1302 |
|
| |
| 1303 |
10032 |
▶ | if (empty) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (empty) { |
1348 |
8684 |
10032 |
|
| 1304 |
|
| /* Empty: the element is both front and back. Place it at the arena |
| 1305 |
|
| * start, resetting the ring (also defragments a drained arena). */ |
| 1306 |
1348 |
| pos = 0; |
| 1307 |
1348 |
| skip = alloc; |
| 1308 |
|
| } else { |
| 1309 |
8684 |
| uint32_t rpos = (uint32_t)(((uint64_t)hdr->arena_wpos |
| 1310 |
8684 |
| + cap - (hdr->arena_used % cap)) % cap); |
| 1311 |
8684 |
▶ | if (alloc <= rpos) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (alloc <= rpos) { |
6560 |
2124 |
8684 |
|
| 1312 |
6560 |
| pos = rpos - alloc; /* fits contiguously below the front */ |
| 1313 |
6560 |
| skip = alloc; |
| 1314 |
|
| } else { |
| 1315 |
2124 |
| pos = cap - alloc; /* wrap: place at the top of the arena, */ |
| 1316 |
2124 |
| skip = alloc + rpos; /* wasting the [0, rpos) bytes below it */ |
| 1317 |
|
| } |
| 1318 |
8684 |
▶ | if ((uint64_t)hdr->arena_used + skip > cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint64_t)hdr->arena_used + skip > cap) { |
1142 |
7542 |
8684 |
|
| 1319 |
1142 |
| queue_str_arena_blocked(h); |
| 1320 |
1142 |
| __atomic_add_fetch(&hdr->stat_push_full, 1, __ATOMIC_RELAXED); |
| 1321 |
1142 |
| queue_mutex_unlock(hdr); |
| 1322 |
1142 |
| return 0; |
| 1323 |
|
| } |
| 1324 |
|
| } |
| 1325 |
8890 |
▶ | if (!queue_arena_populate(h, pos, pos + alloc)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_arena_populate(h, pos, pos + alloc)) { |
0 |
8890 |
8890 |
|
| 1326 |
0 |
| queue_mutex_unlock(hdr); |
| 1327 |
0 |
| return QUEUE_FAILED; |
| 1328 |
|
| } |
| 1329 |
8890 |
▶ | if (empty) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (empty) { |
1348 |
7542 |
8890 |
|
| 1330 |
1348 |
| queue_set_arena(hdr, alloc, alloc); |
| 1331 |
|
| } else { |
| 1332 |
7542 |
| hdr->arena_used += skip; /* arena_wpos unchanged: rpos moved down */ |
| 1333 |
|
| } |
| 1334 |
|
| |
| 1335 |
8890 |
| memcpy(h->arena + pos, str, len); |
| 1336 |
|
| |
| 1337 |
8890 |
| uint32_t idx = (uint32_t)((hdr->head - 1) & h->cap_mask); |
| 1338 |
8890 |
| QueueStrSlot *slot = &((QueueStrSlot *)h->slots)[idx]; |
| 1339 |
8890 |
| slot->arena_off = pos; |
| 1340 |
8890 |
▶ | slot->packed_len = len | (utf8 ? QUEUE_STR_UTF8_FLAG : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| slot->packed_len = len | (utf8 ? QUEUE_STR_UTF8_FLAG : 0); |
1 |
8889 |
8890 |
|
| 1341 |
8890 |
| slot->arena_skip = skip; |
| 1342 |
8890 |
| slot->prev_wpos = hdr->arena_wpos; /* retained for layout stability */ |
| 1343 |
8890 |
| __atomic_store_n(&hdr->head, hdr->head - 1, __ATOMIC_RELEASE); |
| 1344 |
|
| |
| 1345 |
8890 |
| __atomic_add_fetch(&hdr->stat_push_ok, 1, __ATOMIC_RELAXED); |
| 1346 |
8890 |
| queue_mutex_unlock(hdr); |
| 1347 |
8890 |
| queue_wake_consumers(hdr); |
| 1348 |
8890 |
| return 1; |
| 1349 |
|
| } |
| 1350 |
|
| |
| 1351 |
|
| /* Pop from back (tail). Str only -- undoes the most recent push. */ |
| 1352 |
10193 |
| static inline int queue_str_pop_back(QueueHandle *h, const char **out_str, |
| 1353 |
|
| uint32_t *out_len, bool *out_utf8) { |
| 1354 |
10193 |
| QueueHeader *hdr = h->hdr; |
| 1355 |
10193 |
▶ | if (!queue_str_lock(h)) return QUEUE_FAILED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_str_lock(h)) return QUEUE_FAILED; |
2 |
10191 |
10193 |
|
| 1356 |
|
| |
| 1357 |
10191 |
▶ | if (hdr->tail == hdr->head) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->tail == hdr->head) { |
1310 |
8881 |
10191 |
|
| 1358 |
1310 |
| __atomic_add_fetch(&hdr->stat_pop_empty, 1, __ATOMIC_RELAXED); |
| 1359 |
1310 |
| queue_mutex_unlock(hdr); |
| 1360 |
1310 |
| return 0; |
| 1361 |
|
| } |
| 1362 |
|
| |
| 1363 |
|
| /* Unpublish before the release store that credits the arena bytes back. */ |
| 1364 |
8881 |
| __atomic_store_n(&hdr->tail, hdr->tail - 1, __ATOMIC_RELAXED); |
| 1365 |
8881 |
| uint32_t idx = (uint32_t)(hdr->tail & h->cap_mask); |
| 1366 |
8881 |
| QueueStrSlot *slot = &((QueueStrSlot *)h->slots)[idx]; |
| 1367 |
|
| |
| 1368 |
8881 |
| uint32_t off = slot->arena_off; |
| 1369 |
8881 |
| uint32_t len = slot->packed_len & QUEUE_STR_LEN_MASK; |
| 1370 |
8881 |
▶ | if ((uint64_t)off + len > h->arena_cap) len = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint64_t)off + len > h->arena_cap) len = 0; |
0 |
8881 |
8881 |
|
| 1371 |
8881 |
| *out_utf8 = (slot->packed_len & QUEUE_STR_UTF8_FLAG) != 0; |
| 1372 |
|
| |
| 1373 |
8881 |
▶ | if (!queue_ensure_copy_buf(h, len + 1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_ensure_copy_buf(h, len + 1)) { |
0 |
8881 |
8881 |
|
| 1374 |
0 |
| __atomic_store_n(&hdr->tail, hdr->tail + 1, __ATOMIC_RELAXED); /* rollback */ |
| 1375 |
0 |
| queue_mutex_unlock(hdr); |
| 1376 |
0 |
| return -1; |
| 1377 |
|
| } |
| 1378 |
8881 |
▶ | if (len > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > 0) |
8881 |
0 |
8881 |
|
| 1379 |
8881 |
| memcpy(h->copy_buf, h->arena + off, len); |
| 1380 |
8881 |
| h->copy_buf[len] = '\0'; |
| 1381 |
8881 |
| *out_str = h->copy_buf; |
| 1382 |
8881 |
| *out_len = len; |
| 1383 |
|
| |
| 1384 |
|
| /* Retreat the write frontier past this (back) element. The arena is kept |
| 1385 |
|
| * in queue order with arena_wpos == (read_frontier + arena_used) mod cap, |
| 1386 |
|
| * and pop_back does not move the front, so the new write frontier is just |
| 1387 |
|
| * the old one minus this slot's skip (its span plus any wrap waste). This |
| 1388 |
|
| * holds whether the element was appended (push) or prepended |
| 1389 |
|
| * (push_front). */ |
| 1390 |
8881 |
▶ | if (hdr->tail == hdr->head) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hdr->tail == hdr->head) { |
1310 |
7571 |
8881 |
|
| 1391 |
1310 |
| queue_set_arena(hdr, 0, 0); |
| 1392 |
7571 |
▶ | } else if (hdr->arena_used <= slot->arena_skip || h->arena_cap == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (hdr->arena_used <= slot->arena_skip || h->arena_cap == 0) { |
7571 |
0 |
7571 |
| } else if (hdr->arena_used <= slot->arena_skip || h->arena_cap == 0) { |
0 |
7571 |
7571 |
|
| 1393 |
0 |
| __atomic_store_n(&hdr->arena_used, 0, __ATOMIC_RELEASE); |
| 1394 |
|
| } else { |
| 1395 |
7571 |
| queue_set_arena(hdr, (uint32_t)(((uint64_t)hdr->arena_wpos |
| 1396 |
7571 |
| + h->arena_cap - (slot->arena_skip % h->arena_cap)) % h->arena_cap), |
| 1397 |
7571 |
| hdr->arena_used - slot->arena_skip); |
| 1398 |
|
| } |
| 1399 |
|
| |
| 1400 |
8881 |
| __atomic_add_fetch(&hdr->stat_pop_ok, 1, __ATOMIC_RELAXED); |
| 1401 |
8881 |
| uint32_t wake = queue_str_producer_wake_count(hdr, 1); |
| 1402 |
8881 |
| queue_mutex_unlock(hdr); |
| 1403 |
8881 |
▶ | if (wake) queue_wake_producers_n(hdr, wake); |
Branch: 1 branch
| Branch | True | False | Total |
| if (wake) queue_wake_producers_n(hdr, wake); |
8881 |
0 |
8881 |
|
| 1404 |
8881 |
| return 1; |
| 1405 |
|
| } |
| 1406 |
|
| |
| 1407 |
|
| /* In for a push, out for a pop. */ |
| 1408 |
|
| typedef struct { const char *str; uint32_t len; bool utf8; } QueueStrVal; |
| 1409 |
|
| typedef int (*queue_str_op)(QueueHandle *, QueueStrVal *); |
| 1410 |
|
| |
| 1411 |
1016 |
| static int queue_str_op_push(QueueHandle *h, QueueStrVal *v) { return queue_str_try_push(h, v->str, v->len, v->utf8); } |
| 1412 |
8 |
| static int queue_str_op_push_front(QueueHandle *h, QueueStrVal *v) { return queue_str_push_front(h, v->str, v->len, v->utf8); } |
| 1413 |
84 |
| static int queue_str_op_pop(QueueHandle *h, QueueStrVal *v) { return queue_str_try_pop(h, &v->str, &v->len, &v->utf8); } |
| 1414 |
10 |
| static int queue_str_op_pop_back(QueueHandle *h, QueueStrVal *v) { return queue_str_pop_back(h, &v->str, &v->len, &v->utf8); } |
| 1415 |
|
| |
| 1416 |
|
| /* Retries op until it is done or fails, or timeout (seconds, < 0 for none) runs out. */ |
| 1417 |
1088 |
| static int queue_str_wait(QueueHandle *h, queue_str_op op, QueueStrVal *v, int producer, double timeout) { |
| 1418 |
1088 |
| int r = op(h, v); |
| 1419 |
1088 |
▶ | if (r != 0 || timeout == 0) return r; |
Branch: 2 branches
| Branch | True | False | Total |
| if (r != 0 || timeout == 0) return r; |
14 |
1074 |
1088 |
| if (r != 0 || timeout == 0) return r; |
0 |
14 |
14 |
|
| 1420 |
14 |
| QueueHeader *hdr = h->hdr; |
| 1421 |
14 |
▶ | uint32_t *waiters = producer ? &hdr->push_waiters : &hdr->pop_waiters; |
Branch: 1 branch
| Branch | True | False | Total |
| uint32_t *waiters = producer ? &hdr->push_waiters : &hdr->pop_waiters; |
5 |
9 |
14 |
|
| 1422 |
14 |
▶ | uint32_t *futex = producer ? &hdr->push_futex : &hdr->pop_futex; |
Branch: 1 branch
| Branch | True | False | Total |
| uint32_t *futex = producer ? &hdr->push_futex : &hdr->pop_futex; |
5 |
9 |
14 |
|
| 1423 |
|
| struct timespec deadline, remaining; |
| 1424 |
14 |
| int has_deadline = timeout > 0; |
| 1425 |
14 |
▶ | if (has_deadline) queue_make_deadline(timeout, &deadline); |
Branch: 1 branch
| Branch | True | False | Total |
| if (has_deadline) queue_make_deadline(timeout, &deadline); |
14 |
0 |
14 |
|
| 1426 |
1 |
| for (;;) { |
| 1427 |
|
| /* Announce before sampling seq, so a peer that changes the queue in between wakes us. */ |
| 1428 |
15 |
| __atomic_add_fetch(waiters, 1, __ATOMIC_RELEASE); |
| 1429 |
15 |
| uint32_t seq = __atomic_load_n(futex, __ATOMIC_ACQUIRE); |
| 1430 |
15 |
| r = op(h, v); |
| 1431 |
15 |
▶ | if (r != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r != 0) { |
0 |
15 |
15 |
|
| 1432 |
0 |
| __atomic_sub_fetch(waiters, 1, __ATOMIC_RELEASE); |
| 1433 |
0 |
| break; |
| 1434 |
|
| } |
| 1435 |
|
| /* Periodic retries also recover a peer killed before its wakeup. */ |
| 1436 |
15 |
| const struct timespec *pts = &queue_recheck; |
| 1437 |
15 |
▶ | if (has_deadline) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (has_deadline) { |
15 |
0 |
15 |
|
| 1438 |
15 |
▶ | if (!queue_remaining_time(&deadline, &remaining)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!queue_remaining_time(&deadline, &remaining)) { |
0 |
15 |
15 |
|
| 1439 |
0 |
| __atomic_sub_fetch(waiters, 1, __ATOMIC_RELEASE); |
| 1440 |
0 |
| return 0; |
| 1441 |
|
| } |
| 1442 |
15 |
▶ | if (remaining.tv_sec < QUEUE_RECHECK_SEC) pts = &remaining; |
Branch: 1 branch
| Branch | True | False | Total |
| if (remaining.tv_sec < QUEUE_RECHECK_SEC) pts = &remaining; |
6 |
9 |
15 |
|
| 1443 |
|
| } |
| 1444 |
15 |
| long rc = syscall(SYS_futex, futex, FUTEX_WAIT, seq, pts, NULL, 0); |
| 1445 |
15 |
| __atomic_sub_fetch(waiters, 1, __ATOMIC_RELEASE); |
| 1446 |
15 |
| r = op(h, v); |
| 1447 |
15 |
▶ | if (r != 0) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r != 0) break; |
8 |
7 |
15 |
|
| 1448 |
7 |
▶ | if (has_deadline && rc == -1 && errno == ETIMEDOUT |
Branch: 3 branches
| Branch | True | False | Total |
| if (has_deadline && rc == -1 && errno == ETIMEDOUT |
7 |
0 |
7 |
| if (has_deadline && rc == -1 && errno == ETIMEDOUT |
6 |
1 |
7 |
| if (has_deadline && rc == -1 && errno == ETIMEDOUT |
6 |
0 |
6 |
|
| 1449 |
6 |
▶ | && !queue_remaining_time(&deadline, &remaining)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| && !queue_remaining_time(&deadline, &remaining)) return 0; |
6 |
0 |
6 |
|
| 1450 |
|
| } |
| 1451 |
8 |
▶ | if (r == QUEUE_FAILED) { /* pass on a wake we may have taken */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (r == QUEUE_FAILED) { /* pass on a wake we may have taken */ |
0 |
8 |
8 |
|
| 1452 |
0 |
▶ | if (producer) queue_wake_producers(hdr); |
Branch: 1 branch
| Branch | True | False | Total |
| if (producer) queue_wake_producers(hdr); |
0 |
0 |
0 |
|
| 1453 |
0 |
| else queue_wake_consumers(hdr); |
| 1454 |
|
| } |
| 1455 |
8 |
| return r; |
| 1456 |
|
| } |
| 1457 |
|
| |
| 1458 |
1010 |
| static int queue_str_push_wait(QueueHandle *h, const char *str, uint32_t len, bool utf8, double timeout) { |
| 1459 |
1010 |
| QueueStrVal v = { str, len, utf8 }; |
| 1460 |
1010 |
| return queue_str_wait(h, queue_str_op_push, &v, 1, timeout); |
| 1461 |
|
| } |
| 1462 |
|
| |
| 1463 |
4 |
| static int queue_str_push_front_wait(QueueHandle *h, const char *str, uint32_t len, bool utf8, double timeout) { |
| 1464 |
4 |
| QueueStrVal v = { str, len, utf8 }; |
| 1465 |
4 |
| return queue_str_wait(h, queue_str_op_push_front, &v, 1, timeout); |
| 1466 |
|
| } |
| 1467 |
|
| |
| 1468 |
74 |
| static int queue_str_pop_wait_with(QueueHandle *h, queue_str_op op, const char **out_str, |
| 1469 |
|
| uint32_t *out_len, bool *out_utf8, double timeout) { |
| 1470 |
|
| QueueStrVal v; |
| 1471 |
74 |
| int r = queue_str_wait(h, op, &v, 0, timeout); |
| 1472 |
74 |
▶ | if (r == 1) { *out_str = v.str; *out_len = v.len; *out_utf8 = v.utf8; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (r == 1) { *out_str = v.str; *out_len = v.len; *out_utf8 = v.utf8; } |
68 |
6 |
74 |
|
| 1473 |
74 |
| return r; |
| 1474 |
|
| } |
| 1475 |
|
| |
| 1476 |
68 |
| static int queue_str_pop_wait(QueueHandle *h, const char **out_str, uint32_t *out_len, bool *out_utf8, double timeout) { |
| 1477 |
68 |
| return queue_str_pop_wait_with(h, queue_str_op_pop, out_str, out_len, out_utf8, timeout); |
| 1478 |
|
| } |
| 1479 |
|
| |
| 1480 |
6 |
| static int queue_str_pop_back_wait(QueueHandle *h, const char **out_str, uint32_t *out_len, bool *out_utf8, double timeout) { |
| 1481 |
6 |
| return queue_str_pop_wait_with(h, queue_str_op_pop_back, out_str, out_len, out_utf8, timeout); |
| 1482 |
|
| } |
| 1483 |
|
| |
| 1484 |
|
| /* msync -- flush mapped pages to disk */ |
| 1485 |
5 |
| static inline int queue_sync(QueueHandle *h) { |
| 1486 |
5 |
| return msync(h->hdr, h->mmap_size, MS_SYNC); |
| 1487 |
|
| } |
| 1488 |
|
| |
| 1489 |
|
| /* ================================================================ |
| 1490 |
|
| * eventfd -- event-loop integration |
| 1491 |
|
| * ================================================================ */ |
| 1492 |
|
| |
| 1493 |
19 |
| static inline int queue_eventfd_create(QueueHandle *h) { |
| 1494 |
19 |
▶ | if (h->notify_fd >= 0) return h->notify_fd; |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->notify_fd >= 0) return h->notify_fd; |
1 |
18 |
19 |
|
| 1495 |
18 |
| h->notify_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
| 1496 |
18 |
▶ | if (h->notify_fd >= 0) h->notify_fd_owned = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->notify_fd >= 0) h->notify_fd_owned = 1; |
18 |
0 |
18 |
|
| 1497 |
18 |
| return h->notify_fd; |
| 1498 |
|
| } |
| 1499 |
|
| |
| 1500 |
|
| /* Replace the current notify fd. Closes the old fd ONLY if we created |
| 1501 |
|
| * it ourselves via queue_eventfd_create -- caller-supplied fds remain |
| 1502 |
|
| * the caller's property. */ |
| 1503 |
5 |
| static inline void queue_eventfd_set(QueueHandle *h, int fd) { |
| 1504 |
5 |
▶ | if (h->notify_fd == fd) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->notify_fd == fd) return; |
4 |
1 |
5 |
|
| 1505 |
1 |
▶ | if (h->notify_fd >= 0 && h->notify_fd != fd && h->notify_fd_owned) |
Branch: 3 branches
| Branch | True | False | Total |
| if (h->notify_fd >= 0 && h->notify_fd != fd && h->notify_fd_owned) |
1 |
0 |
1 |
| if (h->notify_fd >= 0 && h->notify_fd != fd && h->notify_fd_owned) |
1 |
0 |
1 |
| if (h->notify_fd >= 0 && h->notify_fd != fd && h->notify_fd_owned) |
1 |
0 |
1 |
|
| 1506 |
1 |
| close(h->notify_fd); |
| 1507 |
1 |
| h->notify_fd = fd; |
| 1508 |
1 |
| h->notify_fd_owned = 0; |
| 1509 |
|
| } |
| 1510 |
|
| |
| 1511 |
|
| /* Signal that data is available. Called after successful push. */ |
| 1512 |
12 |
| static inline void queue_notify(QueueHandle *h) { |
| 1513 |
12 |
▶ | if (h->notify_fd >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->notify_fd >= 0) { |
11 |
1 |
12 |
|
| 1514 |
11 |
| uint64_t one = 1; |
| 1515 |
11 |
| ssize_t __attribute__((unused)) rc = write(h->notify_fd, &one, sizeof(one)); |
| 1516 |
|
| } |
| 1517 |
12 |
| } |
| 1518 |
|
| |
| 1519 |
|
| /* Consume notification counter. Call from event-loop callback before pop. |
| 1520 |
|
| * Returns the accumulated counter, or -1 on error / no eventfd. */ |
| 1521 |
30 |
| static inline int64_t queue_eventfd_consume(QueueHandle *h) { |
| 1522 |
30 |
▶ | if (h->notify_fd < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (h->notify_fd < 0) return -1; |
0 |
30 |
30 |
|
| 1523 |
30 |
| uint64_t val = 0; |
| 1524 |
30 |
▶ | if (read(h->notify_fd, &val, sizeof(val)) != sizeof(val)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (read(h->notify_fd, &val, sizeof(val)) != sizeof(val)) return -1; |
4 |
26 |
30 |
|
| 1525 |
26 |
| return (int64_t)val; |
| 1526 |
|
| } |
| 1527 |
|
| |
| 1528 |
|
| #endif /* QUEUE_H */ |