include/sa/sa_wake.h

filter: Statementstmt 89.5% 111 / 124 Branchbran 56.4% 79 / 140 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 71.9% 190 / 264 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/sa/sa_map.h ↑ Summary include/sa/sa_cms.h »
1 #ifndef SA_WAKE_H
2 #define SA_WAKE_H
3  
4 /* sa_wake.h - telling a reader there is something to read. Perl-free.
5 *
6 * ---- a pipe, and why not something newer ------------------------------------
7 *
8 * Each process that wants to be woken owns a pipe. A publisher writes one byte
9 * into it; the reader has the read end in its event loop and drains it. eventfd
10 * is Linux-only and saves exactly one descriptor, which is not a trade worth
11 * making for a dist that has to work on the BSDs and macOS as well.
12 *
13 * The pipes are created BEFORE the fork, so the descriptor NUMBERS are the same
14 * in every process and can therefore live in shared memory as plain ints. That
15 * is also the limit of this mechanism: a process that attached to a named
16 * region inherited nothing, so it has no pipe and no way to be given one.
17 * `sa_wake_fd` answers -1 there and the caller polls instead. Saying so is
18 * better than shipping a named FIFO nobody asked for.
19 *
20 * ---- one poke, however many publishers ------------------------------------
21 *
22 * A busy ring would otherwise cost a write(2) per record. Each waker carries a
23 * `pending` flag, and ONLY the publisher that flips it from 0 to 1 writes a
24 * byte; everybody else has nothing to do, because a wakeup already in flight
25 * will find their record too. A reader clears the flag when it has drained.
26 *
27 * ---- the ordering that actually carries weight -----------------------------
28 *
29 * THE POKE FOLLOWS THE COMMIT. A publisher writes its record, commits it, and
30 * only then wakes anybody. Poke first and a reader wakes, finds a slot that is
31 * still being written, and goes back to sleep - and nothing will wake it again
32 * until the NEXT publish, which on a quiet ring may be a long time. So a wakeup
33 * from this ring always means there is something to read. t/12-wake.t holds a
34 * publisher inside its commit with the stall hook to prove it, because the
35 * window is nanoseconds wide otherwise and a test that races it proves nothing.
36 *
37 * A reader should still drain the pipe, then clear `pending`, then read the
38 * ring, and sa_wake_drained does the first two in that order. It is the
39 * conservative order rather than a load-bearing one HERE: because the reader
40 * reads the ring last, a byte swallowed early still belongs to a record it is
41 * about to find. The order matters in designs where the ring is read before the
42 * pipe is drained, and keeping it costs nothing - but the honest statement is
43 * that this dist's correctness rests on the poke following the commit, and a
44 * comment claiming otherwise would be describing somebody else's code.
45 *
46 * ---- a slot outlives its owner ---------------------------------------------
47 *
48 * Nothing releases a slot on the way out. `sa_wake_leave` is reached from
49 * `sa_abi_release`, and a worker that is killed by its supervisor, crashes, or
50 * simply `_exit`s runs no such thing - so `live` stays set for a process that
51 * no longer exists, and the next taker of that index is refused.
52 *
53 * That refusal is silent and permanent: the caller gets -1, has no descriptor
54 * to watch, and never reads the ring again. Under a prefork server it means
55 * the replacement for a crashed worker is deaf to every publish for the rest
56 * of its life, while every statistic still reports a healthy pool. So a take
57 * RECLAIMS a slot whose owner is gone - see sa_wake_reclaim.
58 *
59 * Needs sa_arena.h and sa_peer.h (for sa_pid_alive / sa_getpid).
60 */
61  
62 #include "sa/sa_arena.h"
63 #include "sa/sa_peer.h"
64  
65 #ifndef _WIN32
66 # include <unistd.h>
67 # include <fcntl.h>
68 # include <errno.h>
69 # include <sys/types.h>
70 # include <sys/stat.h>
71 #endif
72  
73 #define SA_WAKERS_MAX 64
74  
75 /* The waker table lives in a carved sub-region under a reserved name, so every
76 * process finds it the same way it finds anything else and nothing has to be
77 * passed out of band. */
78 #define SA_WAKERS_NAME "\0wakers"
79 #define SA_WAKERS_NLEN 7
80  
81 typedef struct {
82 volatile uint32_t pending; /* 1 = a wakeup is already in flight */
83 volatile uint32_t live; /* 0 = this slot belongs to nobody */
84 volatile uint64_t owner; /* the pid that took it */
85 int rfd;
86 int wfd;
87 } sa_waker;
88  
89 /* ---- A DESCRIPTOR NUMBER IS PROCESS-LOCAL STATE ----------------------------
90 *
91 * `rfd` and `wfd` above are ints in shared memory, and this dist's first rule
92 * says nothing in a region may be a pointer. A descriptor number is the same
93 * kind of thing as an address: it means something only inside the process that
94 * opened it. Fork children inherit the table AND the descriptors, so the
95 * numbers agree there - which is exactly what makes the bug invisible in every
96 * test that only forks.
97 *
98 * A process that ATTACHED BY NAME inherited nothing. Its fd 7 is its own fd 7:
99 * a log, a socket, the harness's TAP stream. Before this header carried an
100 * identity, an attached publisher's `sa_wake_poke` wrote a byte into whatever
101 * that was. Demonstrated: a \001 in an unrelated file, no error anywhere.
102 *
103 * So the table records what the pipes ARE - the device and inode of slot 0's
104 * read end, which are global identifiers rather than local ones - and every
105 * process checks ONCE, with one fstat, whether the descriptors in the table are
106 * the ones it actually holds. A process that fails the check does not poke and
107 * does not claim a slot; it polls, which is exactly what the POD already
108 * promises an attached process gets.
109 *
110 * The check is per process rather than per poke because it is a syscall, and
111 * the answer cannot change: a process either inherited these pipes or it did
112 * not. `sa_wake_leave` is the only thing that resets it.
113 */
114 typedef struct {
115 volatile uint32_t ready; /* published LAST: the identity is readable */
116 uint32_t n; /* how many slots were given pipes */
117 uint64_t dev; /* st_dev of slot 0's read end */
118 uint64_t ino; /* st_ino of it */
119 uint64_t maker; /* the pid that called pipe(), for diagnosis */
120 } sa_waker_hdr;
121  
122 /* The slots follow the header, aligned as everything else in a region is. */
123 #define SA_WAKERS_HDR_BYTES sa_align_up((uint64_t)sizeof(sa_waker_hdr))
124 #define SA_WAKERS_BYTES \
125 (SA_WAKERS_HDR_BYTES + (uint64_t)SA_WAKERS_MAX * sizeof(sa_waker))
126 #define SA_WAKER_SLOTS(hp) \
127 ((sa_waker *)((char *)(hp) + (size_t)SA_WAKERS_HDR_BYTES))
128  
129 /* Create `n` pipes. MUST run before the fork: the descriptors are inherited,
130 * which is what makes the numbers meaningful in every process.
131 *
132 * Idempotent, and a failure is silent - a region with no wakers still works,
133 * its readers simply have to poll. */
134 5 static int sa_wake_init(sa_region *r, uint32_t n) {
135 #if defined(_WIN32) || !SA_HAVE_ATOMICS
136 (void)r; (void)n;
137 return 0;
138 #else
139 sa_waker_hdr *hp;
140 sa_waker *w;
141 uint64_t off;
142 uint32_t i;
143 5 int err = SA_E_OK;
144  
145 5 ▶ if (!r || !r->map.base) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!r || !r->map.base) return 0; 5 0 5
if (!r || !r->map.base) return 0; 0 5 5
146 5 ▶ if (n < 1) n = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 1) n = 1; 0 5 5
147 5 ▶ if (n > SA_WAKERS_MAX) n = SA_WAKERS_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > SA_WAKERS_MAX) n = SA_WAKERS_MAX; 0 5 5
148  
149 {
150 5 sa_reg *e = sa_carve(r, SA_WAKERS_NAME, SA_WAKERS_NLEN,
151 5 SA_WAKERS_BYTES, SA_T_RAW, &err);
152 5 ▶ if (!e) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return 0; 0 5 5
153 5 off = e->off;
154 }
155 5 hp = (sa_waker_hdr *)sa_ptr(r, off);
156 5 ▶ if (!hp) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!hp) return 0; 0 5 5
157 5 w = SA_WAKER_SLOTS(hp);
158  
159 27 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 22 5 27
160 int fds[2];
161 22 ▶ if (sa_at_load32_acq(&w[i].live) || w[i].rfd) continue; /* already */
Branch: 2 branches
BranchTrueFalseTotal
if (sa_at_load32_acq(&w[i].live) || w[i].rfd) continue; /* already */ 22 0 22
if (sa_at_load32_acq(&w[i].live) || w[i].rfd) continue; /* already */ 0 22 22
162 22 ▶ if (pipe(fds) != 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (pipe(fds) != 0) return 0; 0 22 22
163 /* Both ends non-blocking, so a consumer that has stopped draining
164 * cannot block a publisher inside a write. */
165 22 fcntl(fds[0], F_SETFL, fcntl(fds[0], F_GETFL, 0) | O_NONBLOCK);
166 22 fcntl(fds[1], F_SETFL, fcntl(fds[1], F_GETFL, 0) | O_NONBLOCK);
167 /* CLOSE ON EXEC. These are inherited across a fork, which is the whole
168 * mechanism, and they have no business surviving an exec: a program
169 * that replaces its image is not a peer of this region and would hold
170 * both ends of every pipe open for as long as it ran, so a reader that
171 * exits never gives its writers EPIPE. */
172 22 fcntl(fds[0], F_SETFD, fcntl(fds[0], F_GETFD, 0) | FD_CLOEXEC);
173 22 fcntl(fds[1], F_SETFD, fcntl(fds[1], F_GETFD, 0) | FD_CLOEXEC);
174 22 w[i].rfd = fds[0];
175 22 w[i].wfd = fds[1];
176 22 sa_at_store32_rel(&w[i].pending, 0);
177 }
178  
179 /* The identity, published last: until `ready` lands nobody trusts the
180 * descriptors, which is the same commit discipline the region header and
181 * the ring header use one and two levels up. */
182 5 ▶ if (!sa_at_load32_acq(&hp->ready)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_at_load32_acq(&hp->ready)) { 5 0 5
183 struct stat st;
184 5 ▶ if (w[0].rfd && fstat(w[0].rfd, &st) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (w[0].rfd && fstat(w[0].rfd, &st) == 0) { 5 0 5
if (w[0].rfd && fstat(w[0].rfd, &st) == 0) { 5 0 5
185 5 hp->dev = (uint64_t)st.st_dev;
186 5 hp->ino = (uint64_t)st.st_ino;
187 5 hp->maker = sa_getpid();
188 5 hp->n = n;
189 5 sa_at_store32_rel(&hp->ready, 1);
190 }
191 }
192 0 ▶ else if (n > hp->n) {
Branch: 1 branch
BranchTrueFalseTotal
else if (n > hp->n) { 0 0 0
193 0 hp->n = n; /* a second init that made more pipes */
194 }
195  
196 5 r->wakers_off = off;
197 5 r->wakers_max = SA_WAKERS_MAX;
198 5 r->wakers_probed = 0;
199 5 r->wake_checked = 0; /* re-verify: we are the maker now */
200 5 return 1;
201 #endif
202 }
203  
204 /* How many slots were given pipes: the ones a poke walks and a take may claim.
205 * The table always has SA_WAKERS_MAX slots, but a pool of eight workers has
206 * nine pipes, and walking the other fifty-five on every publish cost more
207 * than the publish itself (phase 01 of the Hyperman port: 41.6 to 14.8ns). */
208 13 static uint32_t sa_wake_n(const sa_waker_hdr *hp) {
209 13 uint32_t n = hp->n;
210 13 return n > SA_WAKERS_MAX ? SA_WAKERS_MAX : n;
211 }
212  
213 /* Are the descriptors in this table the ones THIS process holds?
214 *
215 * One fstat, once per process. A fork child inherited the pipes and passes; a
216 * process that attached by name did not and fails, so it neither pokes nor
217 * claims a slot. Failing is not an error: it is the polling mode the POD
218 * already documents for an attached process. */
219 22 static int sa_wake_usable(sa_region *r) {
220 #if defined(_WIN32) || !SA_HAVE_ATOMICS
221 (void)r;
222 return 0;
223 #else
224 sa_waker_hdr *hp;
225 sa_waker *w;
226 struct stat st;
227  
228 22 ▶ if (!r || !r->wakers_off) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!r || !r->wakers_off) return 0; 22 0 22
if (!r || !r->wakers_off) return 0; 0 22 22
229 /* KEYED ON THE PID, because the handle survives a fork and the answer may
230 * not: a child that closed the inherited pipes and reopened those numbers
231 * as something else would otherwise keep the parent's verdict and write
232 * into whatever it opened. sa_getpid is a load once it is cached. */
233 22 ▶ if (r->wake_checked && r->wake_pid == sa_getpid()) return r->wake_ok;
Branch: 2 branches
BranchTrueFalseTotal
if (r->wake_checked && r->wake_pid == sa_getpid()) return r->wake_ok; 17 5 22
if (r->wake_checked && r->wake_pid == sa_getpid()) return r->wake_ok; 16 1 17
234  
235 6 r->wake_checked = 1;
236 6 r->wake_pid = sa_getpid();
237 6 r->wake_ok = 0;
238  
239 6 hp = (sa_waker_hdr *)sa_ptr(r, r->wakers_off);
240 6 ▶ if (!hp || !sa_at_load32_acq(&hp->ready)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!hp || !sa_at_load32_acq(&hp->ready)) return 0; 6 0 6
if (!hp || !sa_at_load32_acq(&hp->ready)) return 0; 0 6 6
241 6 w = SA_WAKER_SLOTS(hp);
242 6 ▶ if (!w[0].rfd) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!w[0].rfd) return 0; 0 6 6
243 6 ▶ if (fstat(w[0].rfd, &st) != 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (fstat(w[0].rfd, &st) != 0) return 0; 0 6 6
244 6 ▶ if ((uint64_t)st.st_dev != hp->dev || (uint64_t)st.st_ino != hp->ino)
Branch: 2 branches
BranchTrueFalseTotal
if ((uint64_t)st.st_dev != hp->dev || (uint64_t)st.st_ino != hp->ino) 5 1 6
if ((uint64_t)st.st_dev != hp->dev || (uint64_t)st.st_ino != hp->ino) 0 5 5
245 1 return 0;
246  
247 5 r->wake_ok = 1;
248 5 return 1;
249 #endif
250 }
251  
252 /* Claim a waker for this process, after the fork.
253 *
254 * The stale bytes are drained AS IT IS TAKEN, and that is not tidiness: a slot
255 * left with a byte in its pipe by a previous owner makes the new owner's first
256 * select return immediately, which looks exactly like a wakeup and hides the
257 * absence of a real one. A bug that hides itself by passing the test. */
258 /* Find the table a parent created, for a process that did not create it. */
259 2996 static int sa_wake_attach(sa_region *r) {
260 #if defined(_WIN32) || !SA_HAVE_ATOMICS
261 (void)r;
262 return 0;
263 #else
264 sa_reg *e;
265 2996 ▶ if (!r || !r->map.base) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!r || !r->map.base) return 0; 2996 0 2996
if (!r || !r->map.base) return 0; 0 2996 2996
266 2996 ▶ if (r->wakers_off) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (r->wakers_off) return 1; 8 2988 2996
267 /* A miss is cached: the table is made before the fork or not at all, so
268 * a process that once found nothing will not find it later. wake_init in
269 * THIS process clears the cache. */
270 2988 ▶ if (r->wakers_probed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->wakers_probed) return 0; 2968 20 2988
271 20 e = sa_find(r, SA_WAKERS_NAME, SA_WAKERS_NLEN);
272 20 ▶ if (!e) { r->wakers_probed = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!e) { r->wakers_probed = 1; return 0; } 20 0 20
273 0 r->wakers_off = e->off;
274 0 r->wakers_max = SA_WAKERS_MAX;
275 0 return 1;
276 #endif
277 }
278  
279 /* Take over a slot whose owner has died. 1 if this process now owns it.
280 *
281 * The handoff is a CAS on the OWNER rather than on `live`, for two reasons.
282 * Two processes reclaiming the same dead slot must not both win, and one of
283 * them has to lose on the same word it read. And clearing `live` first would
284 * publish the slot as free for as long as it took to take it again, which is
285 * the one state a third process could step into.
286 *
287 * A REUSED pid reads as alive and the slot is left alone. That costs a waker
288 * and leaves a reader polling, which is what an attached process already gets;
289 * the other direction would hand one process's pipe to another. The peer
290 * table makes the same trade in the other direction for the same reason. */
291 0 static int sa_wake_reclaim(sa_waker *w) {
292 #if defined(_WIN32) || !SA_HAVE_ATOMICS
293 (void)w;
294 return 0;
295 #else
296 uint64_t owner;
297 0 ▶ if (!sa_at_load32_acq(&w->live)) return 0; /* free: nothing to reclaim */
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_at_load32_acq(&w->live)) return 0; /* free: nothing to reclaim */ 0 0 0
298 0 owner = sa_at_load64_acq(&w->owner);
299 0 ▶ if (!owner || sa_pid_alive(owner)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!owner || sa_pid_alive(owner)) return 0; 0 0 0
if (!owner || sa_pid_alive(owner)) return 0; 0 0 0
300 0 return sa_at_cas64(&w->owner, owner, sa_getpid());
301 #endif
302 }
303  
304 5 static int sa_wake_take(sa_region *r, int idx) {
305 #if defined(_WIN32) || !SA_HAVE_ATOMICS
306 (void)r; (void)idx;
307 return -1;
308 #else
309 sa_waker_hdr *hp;
310 sa_waker *w;
311 uint32_t i;
312  
313 5 ▶ if (!sa_wake_attach(r)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_wake_attach(r)) return -1; 0 5 5
314 5 ▶ if (!r || !r->wakers_off) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!r || !r->wakers_off) return -1; 5 0 5
if (!r || !r->wakers_off) return -1; 0 5 5
315 /* Not ours to read from: see sa_wake_usable. A process that did not
316 * inherit these pipes would be reading whatever its own fd of that number
317 * happens to be. */
318 5 ▶ if (!sa_wake_usable(r)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_wake_usable(r)) return -1; 0 5 5
319 5 hp = (sa_waker_hdr *)sa_ptr(r, r->wakers_off);
320 5 ▶ if (!hp) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!hp) return -1; 0 5 5
321 5 w = SA_WAKER_SLOTS(hp);
322  
323 /* Only a slot that was given a pipe. A slot past `n` has descriptor 0 in
324 * both fields, and taking it would mean reading standard input. */
325 5 ▶ if (idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx >= 0) { 2 3 5
326 2 i = (uint32_t)idx;
327 2 ▶ if (i >= sa_wake_n(hp)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= sa_wake_n(hp)) return -1; 1 1 2
328 /* Free, or held by a process that is gone. A prefork supervisor hands
329 * a respawned worker the SAME index its predecessor died holding, so
330 * without the reclaim that worker never gets a descriptor. */
331 1 ▶ if (!sa_at_cas32(&w[i].live, 0, 1) && !sa_wake_reclaim(&w[i]))
Branch: 2 branches
BranchTrueFalseTotal
if (!sa_at_cas32(&w[i].live, 0, 1) && !sa_wake_reclaim(&w[i])) 0 1 1
if (!sa_at_cas32(&w[i].live, 0, 1) && !sa_wake_reclaim(&w[i])) 0 0 0
332 0 return -1;
333 }
334 else {
335 3 ▶ for (i = 0; i < sa_wake_n(hp); i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sa_wake_n(hp); i++) 3 0 3
336 3 ▶ if (sa_at_cas32(&w[i].live, 0, 1)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_cas32(&w[i].live, 0, 1)) break; 3 0 3
337 /* Only once nothing is actually free: a dead owner's slot is worth
338 * having, but not ahead of one nobody has ever taken. */
339 3 ▶ if (i >= sa_wake_n(hp))
Branch: 1 branch
BranchTrueFalseTotal
if (i >= sa_wake_n(hp)) 0 3 3
340 0 ▶ for (i = 0; i < sa_wake_n(hp); i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sa_wake_n(hp); i++) 0 0 0
341 0 ▶ if (sa_wake_reclaim(&w[i])) break;
Branch: 1 branch
BranchTrueFalseTotal
if (sa_wake_reclaim(&w[i])) break; 0 0 0
342 3 ▶ if (i >= sa_wake_n(hp)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= sa_wake_n(hp)) return -1; 0 3 3
343 }
344  
345 {
346 char junk[256];
347 4 ▶ while (read(w[i].rfd, junk, sizeof junk) > 0) { /* drain */ }
Branch: 1 branch
BranchTrueFalseTotal
while (read(w[i].rfd, junk, sizeof junk) > 0) { /* drain */ } 0 4 4
348 }
349 4 sa_at_store32_rel(&w[i].pending, 0);
350 4 sa_at_store64_rel(&w[i].owner, sa_getpid());
351 4 r->waker_idx = (int)i;
352 4 return (int)i;
353 #endif
354 }
355  
356 /* This process's read end, or -1 when it has none. */
357 7 static int sa_wake_fd(sa_region *r) {
358 #if defined(_WIN32) || !SA_HAVE_ATOMICS
359 (void)r;
360 return -1;
361 #else
362 sa_waker_hdr *hp;
363 7 ▶ if (!r || !r->wakers_off || r->waker_idx < 0) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!r || !r->wakers_off || r->waker_idx < 0) return -1; 7 0 7
if (!r || !r->wakers_off || r->waker_idx < 0) return -1; 6 1 7
if (!r || !r->wakers_off || r->waker_idx < 0) return -1; 0 6 6
364 6 ▶ if (!sa_wake_usable(r)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_wake_usable(r)) return -1; 1 5 6
365 5 hp = (sa_waker_hdr *)sa_ptr(r, r->wakers_off);
366 5 ▶ return hp ? SA_WAKER_SLOTS(hp)[r->waker_idx].rfd : -1;
Branch: 1 branch
BranchTrueFalseTotal
return hp ? SA_WAKER_SLOTS(hp)[r->waker_idx].rfd : -1; 5 0 5
367 #endif
368 }
369  
370 /* Wake everybody but this process. Called from publish. */
371 2991 static void sa_wake_poke(sa_region *r) {
372 #if defined(_WIN32) || !SA_HAVE_ATOMICS
373 (void)r;
374 #else
375 sa_waker_hdr *hp;
376 sa_waker *w;
377 uint32_t i, n;
378 2991 char b = 1;
379  
380 2992 ▶ if (!sa_wake_attach(r)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_wake_attach(r)) return; 2988 3 2991
381 /* THE CHECK THAT STOPS A STRAY WRITE. Without it, a publisher that
382 * attached by name writes this byte into whatever its own descriptor of
383 * that number happens to be. One fstat, once, per process. */
384 3 ▶ if (!sa_wake_usable(r)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_wake_usable(r)) return; 1 2 3
385 2 hp = (sa_waker_hdr *)sa_ptr(r, r->wakers_off);
386 2 ▶ if (!hp) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hp) return; 0 2 2
387 2 w = SA_WAKER_SLOTS(hp);
388 2 n = sa_wake_n(hp);
389  
390 8 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 6 2 8
391 6 ▶ if ((int)i == r->waker_idx) continue; /* not ourselves */
Branch: 1 branch
BranchTrueFalseTotal
if ((int)i == r->waker_idx) continue; /* not ourselves */ 1 5 6
392 5 ▶ if (!sa_at_load32_acq(&w[i].live)) continue; /* nobody there */
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_at_load32_acq(&w[i].live)) continue; /* nobody there */ 4 1 5
393 /* ONLY the caller that flips the flag writes. Everybody else has
394 * nothing to do: a wakeup already in flight will find their record
395 * too, and a second byte would buy nothing but a syscall. */
396 1 ▶ if (!sa_at_cas32(&w[i].pending, 0, 1)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_at_cas32(&w[i].pending, 0, 1)) continue; 0 1 1
397 {
398 1 ssize_t rc = write(w[i].wfd, &b, 1);
399 (void)rc; /* EAGAIN on a full pipe is fine: it is already awake */
400 }
401 }
402 #endif
403 }
404  
405 /* Drain the pipe and then clear the flag, in that order. See the header
406 * comment: the other order deafens a reader. */
407 8 static void sa_wake_drained(sa_region *r) {
408 #if defined(_WIN32) || !SA_HAVE_ATOMICS
409 (void)r;
410 #else
411 sa_waker_hdr *hp;
412 sa_waker *w;
413 char junk[256];
414  
415 8 ▶ if (!r || !r->wakers_off || r->waker_idx < 0) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!r || !r->wakers_off || r->waker_idx < 0) return; 8 0 8
if (!r || !r->wakers_off || r->waker_idx < 0) return; 8 0 8
if (!r || !r->wakers_off || r->waker_idx < 0) return; 0 8 8
416 8 ▶ if (!sa_wake_usable(r)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_wake_usable(r)) return; 0 8 8
417 8 hp = (sa_waker_hdr *)sa_ptr(r, r->wakers_off);
418 8 ▶ if (!hp) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hp) return; 0 8 8
419 8 w = SA_WAKER_SLOTS(hp);
420  
421 11 ▶ while (read(w[r->waker_idx].rfd, junk, sizeof junk) > 0) { /* drain */ }
Branch: 1 branch
BranchTrueFalseTotal
while (read(w[r->waker_idx].rfd, junk, sizeof junk) > 0) { /* drain */ } 3 8 11
422 8 sa_at_store32_rel(&w[r->waker_idx].pending, 0);
423 #endif
424 }
425  
426 1 static void sa_wake_leave(sa_region *r) {
427 #if !defined(_WIN32) && SA_HAVE_ATOMICS
428 1 ▶ if (r && r->wakers_off && r->waker_idx >= 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (r && r->wakers_off && r->waker_idx >= 0) { 1 0 1
if (r && r->wakers_off && r->waker_idx >= 0) { 1 0 1
if (r && r->wakers_off && r->waker_idx >= 0) { 1 0 1
429 1 sa_waker_hdr *hp = (sa_waker_hdr *)sa_ptr(r, r->wakers_off);
430 1 ▶ if (hp) sa_at_store32_rel(&SA_WAKER_SLOTS(hp)[r->waker_idx].live, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (hp) sa_at_store32_rel(&SA_WAKER_SLOTS(hp)[r->waker_idx].live, 0); 1 0 1
431 1 r->waker_idx = -1;
432 1 r->wake_checked = 0;
433 }
434 #else
435 (void)r;
436 #endif
437 1 }
438  
439 #endif /* SA_WAKE_H */
« include/sa/sa_map.h ↑ Summary include/sa/sa_cms.h »