include/sa/sa_lease.h

filter: Statementstmt 93.1% 136 / 146 Branchbran 54.4% 61 / 112 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.3% 197 / 258 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/sa/sa_arena.h ↑ Summary xs/cms.xs »
1 #ifndef SA_LEASE_H
2 #define SA_LEASE_H
3  
4 /* sa_lease.h - one holder at a time, and a successor when it dies. Perl-free.
5 *
6 * ---- the problem ------------------------------------------------------------
7 *
8 * A pre-forked pool eventually needs exactly ONE worker to do something: run
9 * the cron, warm a cache, apply migrations, own a scheduler. "The first one"
10 * is not an answer, because the first one restarts. A lease is: whoever holds
11 * it is the one, and when the holder stops renewing it - because it exited,
12 * crashed, or wedged - a successor takes it over.
13 *
14 * This is the peer table's death-proof idea (sa_peer.h) turned into a feature.
15 * A holder is named by its pid; a successor may take the lease when it has
16 * LAPSED - the holder failed to renew before a deadline - or when the holder is
17 * PROVABLY DEAD, which lets failover happen faster than the deadline when the
18 * process is simply gone.
19 *
20 * ---- the deadline is primary, the pid check only makes it faster -----------
21 *
22 * The deadline is what carries correctness: a holder must renew before it
23 * lapses, and one that cannot - wedged, stopped, off-CPU too long - loses the
24 * lease when it lapses whether or not it is technically alive. That is the
25 * right answer: a leader that cannot renew is not leading.
26 *
27 * The pid check only ever makes a steal happen EARLIER, never against a live
28 * renewing holder: a holder that is gone is stolen from the moment a successor
29 * notices, rather than after the full deadline. A recycled pid that happens to
30 * be alive again just means the successor waits out the deadline instead, which
31 * is the slow path, not a wrong one.
32 *
33 * ---- the fencing token ------------------------------------------------------
34 *
35 * Every acquire bumps a generation, and acquire hands it back. A holder that
36 * was preempted, lost the lease to a successor, and then woke up and tried to
37 * act is the classic distributed-lock hazard. So renew and release check the
38 * generation: a holder carrying a stale one is told it has lost the lease, and
39 * a caller that stamps its outbound writes with the fence lets the resource
40 * itself reject a zombie leader's late write. See `fence`.
41 *
42 * ---- under the stripe lock, on purpose --------------------------------------
43 *
44 * Acquire, renew and release each take the slot's stripe lock. A lease changes
45 * hands every few seconds at most - it is leader election, not a per-request
46 * counter - so the lock costs nothing that matters and removes every
47 * compare-and-swap ordering hazard the hot-path tenants have to reason about.
48 *
49 * A lock that cannot be taken (a process died holding the stripe, which is the
50 * only way sa_at_lock fails) biases SAFE: acquire returns "not acquired" and
51 * renew returns "lost", so the failure can make leadership briefly unavailable
52 * but never doubly held. The deadline is the backstop that recovers it.
53 *
54 * Needs sa_arena.h and sa_peer.h (for sa_pid_alive / sa_getpid).
55 */
56  
57 #include "sa/sa_arena.h"
58 #include "sa/sa_peer.h"
59  
60 #define SA_LEASE_MAGIC 0x5341454Cu /* 'L','E','A','S' little-endian */
61  
62 #define SA_LEASES_MAX 64
63 #define SA_LEASE_NAME 32
64  
65 /* The lease table lives in a carved sub-region under a reserved name, so every
66 * process finds it the same way and one registry entry holds all leases. */
67 #define SA_LEASE_TABLE_NAME "\0leases"
68 #define SA_LEASE_TABLE_NLEN 7
69  
70 #define SA_L_EMPTY 0u
71 #define SA_L_CLAIMING 1u
72 #define SA_L_LIVE 2u
73  
74 typedef struct {
75 volatile uint32_t state; /* SA_L_* - published LAST at create */
76 uint32_t pad;
77 volatile uint64_t owner; /* holder pid; 0 = free */
78 volatile uint64_t gen; /* fencing token, bumped on every acquire */
79 volatile uint64_t expires; /* ms wall clock; the renew deadline */
80 volatile uint64_t acquires; /* successful acquires, including steals */
81 volatile uint64_t steals; /* acquires that took over another holder */
82 char name[SA_LEASE_NAME];
83 } sa_lease_slot;
84  
85 typedef struct {
86 volatile uint32_t magic; /* published LAST at init */
87 uint32_t nleases;
88 uint64_t slots_off; /* from the TABLE's base */
89 volatile unsigned char locks[SA_LOCK_STRIPES];
90 } sa_lease_hdr;
91  
92 /* The process-local handle. Nothing here is in the mapping: `slot` is this
93 * process's address for the shared lease, and `gen` is the fence this process
94 * last acquired at. */
95 #ifndef SA_LEASE_FWD
96 #define SA_LEASE_FWD
97 typedef struct sa_lease sa_lease;
98 #endif
99  
100 struct sa_lease {
101 sa_region *arena;
102 sa_lease_hdr *hdr;
103 sa_lease_slot *slot;
104 uint64_t gen; /* the generation this handle holds */
105 uint64_t hash; /* the stripe this slot locks on */
106 uint64_t ttl_ms; /* this handle's default lease length */
107 };
108  
109 /* acquire / renew results */
110 #define SA_LEASE_LOST 0 /* not the holder */
111 #define SA_LEASE_HELD 1 /* the holder, lease extended */
112  
113 128 static uint64_t sa_lease_table_bytes(void) {
114 128 return sa_align_up((uint64_t)sizeof(sa_lease_hdr))
115 128 + sa_align_up((uint64_t)SA_LEASES_MAX * (uint64_t)sizeof(sa_lease_slot));
116 }
117  
118 #define SA_LEASE_SLOTS(h) \
119 ((sa_lease_slot *)((char *)(h) + (size_t)(h)->slots_off))
120  
121 #if SA_HAVE_ATOMICS
122  
123 /* Find the lease table region, initialising it if nobody has yet - the same
124 * commit discipline as everywhere: write the header, publish magic last, and
125 * everybody else waits bounded. */
126 98 static sa_lease_hdr *sa_lease_table(sa_region *arena, int *err) {
127 sa_reg *e;
128 sa_lease_hdr *h;
129 uint64_t hash;
130  
131 98 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 98 0 98
132 98 ▶ if (!arena) { if (err) *err = SA_E_NOENT; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!arena) { if (err) *err = SA_E_NOENT; return NULL; } 0 98 98
if (!arena) { if (err) *err = SA_E_NOENT; return NULL; } 0 0 0
133  
134 98 e = sa_carve(arena, SA_LEASE_TABLE_NAME, SA_LEASE_TABLE_NLEN,
135 sa_lease_table_bytes(), SA_T_LEASE, err);
136 98 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 98 98
137  
138 98 h = (sa_lease_hdr *)sa_ptr_len(arena, e->off, sizeof(sa_lease_hdr));
139 98 ▶ if (!h) { if (err) *err = SA_E_NOENT; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!h) { if (err) *err = SA_E_NOENT; return NULL; } 0 98 98
if (!h) { if (err) *err = SA_E_NOENT; return NULL; } 0 0 0
140  
141 98 ▶ if (sa_at_load32_acq(&h->magic) != SA_LEASE_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load32_acq(&h->magic) != SA_LEASE_MAGIC) { 15 83 98
142 15 hash = sa_at_fnv(SA_LEASE_TABLE_NAME, SA_LEASE_TABLE_NLEN);
143 15 ▶ if (sa_at_lock(h->locks, hash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_lock(h->locks, hash)) { 15 0 15
144 15 ▶ if (sa_at_load32_acq(&h->magic) != SA_LEASE_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load32_acq(&h->magic) != SA_LEASE_MAGIC) { 15 0 15
145 15 ▶ if (sa_lease_table_bytes() > e->len) {
Branch: 1 branch
BranchTrueFalseTotal
if (sa_lease_table_bytes() > e->len) { 0 15 15
146 0 sa_at_unlock(h->locks, hash);
147 0 ▶ if (err) *err = SA_E_FULL;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_FULL; 0 0 0
148 0 return NULL;
149 }
150 15 memset((void *)h, 0, (size_t)sa_lease_table_bytes());
151 15 h->nleases = SA_LEASES_MAX;
152 15 h->slots_off = sa_align_up((uint64_t)sizeof(sa_lease_hdr));
153 15 sa_at_store32_rel(&h->magic, SA_LEASE_MAGIC);
154 }
155 15 sa_at_unlock(h->locks, hash);
156 }
157 15 ▶ if (!sa_wait_magic32(&h->magic, SA_LEASE_MAGIC)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_wait_magic32(&h->magic, SA_LEASE_MAGIC)) { 0 15 15
158 0 ▶ if (err) *err = SA_E_MAGIC;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_MAGIC; 0 0 0
159 0 return NULL;
160 }
161 }
162 98 return h;
163 }
164  
165 /* Find the named lease slot, or make it. The stripe lock keyed on the name
166 * stops two processes creating two slots for one name - which would be two
167 * leases nobody could tell apart, each with its own holder. */
168 98 static sa_lease_slot *sa_lease_slot_for(sa_lease_hdr *h, const char *name,
169 uint32_t nlen, int *err)
170 {
171 98 sa_lease_slot *slots = SA_LEASE_SLOTS(h);
172 uint64_t hash;
173 uint32_t i, n;
174  
175 98 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 98 0 98
176 98 ▶ if (!name || !nlen || nlen >= SA_LEASE_NAME) {
Branch: 3 branches
BranchTrueFalseTotal
if (!name || !nlen || nlen >= SA_LEASE_NAME) { 98 0 98
if (!name || !nlen || nlen >= SA_LEASE_NAME) { 98 0 98
if (!name || !nlen || nlen >= SA_LEASE_NAME) { 0 98 98
177 0 ▶ if (err) *err = SA_E_NAME;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_NAME; 0 0 0
178 0 return NULL;
179 }
180 98 n = h->nleases > SA_LEASES_MAX ? SA_LEASES_MAX : h->nleases;
181 98 hash = sa_at_fnv(name, nlen);
182  
183 /* A bind happens once per worker, at start, so it waits for the stripe as
184 * a carve does. A stripe still held after that is BUSY and not FULL: an
185 * OpenBSD smoker had six children bind one name at once, and one lost
186 * sa_at_lock's spin to the other five and was told the table had no room,
187 * with every slot but a handful free. */
188 98 ▶ if (!sa_lock_wait(h->locks, hash)) { if (err) *err = SA_E_BUSY; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!sa_lock_wait(h->locks, hash)) { if (err) *err = SA_E_BUSY; return NULL; } 0 98 98
if (!sa_lock_wait(h->locks, hash)) { if (err) *err = SA_E_BUSY; return NULL; } 0 0 0
189  
190 4576 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4508 68 4576
191 4508 sa_lease_slot *s = &slots[i];
192 4508 ▶ if (sa_at_load32_acq(&s->state) != SA_L_LIVE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load32_acq(&s->state) != SA_L_LIVE) continue; 4207 301 4508
193 301 ▶ if (strncmp(s->name, name, nlen) || s->name[nlen]) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (strncmp(s->name, name, nlen) || s->name[nlen]) continue; 30 271 301
if (strncmp(s->name, name, nlen) || s->name[nlen]) continue; 0 30 30
194 30 sa_at_unlock(h->locks, hash);
195 30 return s;
196 }
197 213 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 213 0 213
198 213 sa_lease_slot *s = &slots[i];
199 213 ▶ if (!sa_at_cas32(&s->state, SA_L_EMPTY, SA_L_CLAIMING)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_at_cas32(&s->state, SA_L_EMPTY, SA_L_CLAIMING)) continue; 145 68 213
200 68 memset(s->name, 0, SA_LEASE_NAME);
201 68 memcpy(s->name, name, nlen);
202 68 sa_at_store64_rel(&s->owner, 0);
203 68 sa_at_store64_rel(&s->gen, 0);
204 68 sa_at_store64_rel(&s->expires, 0);
205 68 sa_at_store64_rel(&s->acquires, 0);
206 68 sa_at_store64_rel(&s->steals, 0);
207 68 sa_at_store32_rel(&s->state, SA_L_LIVE);
208 68 sa_at_unlock(h->locks, hash);
209 68 return s;
210 }
211 0 sa_at_unlock(h->locks, hash);
212 0 ▶ if (err) *err = SA_E_FULL;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_FULL; 0 0 0
213 0 return NULL;
214 }
215  
216 98 static sa_lease *sa_lease_bind(sa_region *arena, const char *name,
217 uint32_t nlen, int *err)
218 {
219 sa_lease_hdr *h;
220 sa_lease_slot *s;
221 sa_lease *lh;
222  
223 98 h = sa_lease_table(arena, err);
224 98 ▶ if (!h) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return NULL; 0 98 98
225 98 s = sa_lease_slot_for(h, name, nlen, err);
226 98 ▶ if (!s) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return NULL; 0 98 98
227  
228 98 lh = (sa_lease *)calloc(1, sizeof(sa_lease));
229 98 ▶ if (!lh) { if (err) *err = SA_E_NOMEM; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!lh) { if (err) *err = SA_E_NOMEM; return NULL; } 0 98 98
if (!lh) { if (err) *err = SA_E_NOMEM; return NULL; } 0 0 0
230 98 lh->arena = arena;
231 98 lh->hdr = h;
232 98 lh->slot = s;
233 98 lh->gen = 0;
234 98 lh->hash = sa_at_fnv(name, nlen);
235 98 lh->ttl_ms = 30000; /* a sane default; the Perl surface overrides it */
236 98 return lh;
237 }
238  
239 98 static void sa_lease_free(sa_lease *lh) { free(lh); }
240  
241 /* Take the lease, or extend it if we already hold it. Returns SA_LEASE_HELD
242 * when this process now holds it, SA_LEASE_LOST when somebody else does and
243 * their lease is still current.
244 *
245 * `*stole` (optional) is set when the acquire took over a holder that had
246 * lapsed or died, rather than a free lease - which tells a caller it has just
247 * become leader after a predecessor failed, not started clean. */
248 209 static int sa_lease_acquire(sa_lease *lh, uint64_t ttl_ms, int *stole)
249 {
250 sa_lease_slot *s;
251 sa_lease_hdr *h;
252 uint64_t me, now, owner, exp;
253  
254 209 ▶ if (stole) *stole = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (stole) *stole = 0; 209 0 209
255 209 ▶ if (!lh) return SA_LEASE_LOST;
Branch: 1 branch
BranchTrueFalseTotal
if (!lh) return SA_LEASE_LOST; 0 209 209
256 209 s = lh->slot;
257 209 h = lh->hdr;
258 209 me = sa_getpid();
259 209 now = sa_now_ms();
260  
261 /* A held stripe is waited out on the per-request budget - the holder is
262 * doing three stores and was merely descheduled - and a stripe still held
263 * after that biases safe: not acquiring can never create a second holder,
264 * and the deadline recovers the lease later. */
265 209 ▶ if (!sa_lock_wait_brief(h->locks, lh->hash)) return SA_LEASE_LOST;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_lock_wait_brief(h->locks, lh->hash)) return SA_LEASE_LOST; 0 209 209
266  
267 209 owner = sa_at_load64_acq(&s->owner);
268 209 exp = sa_at_load64_acq(&s->expires);
269  
270 209 ▶ if (owner == me) {
Branch: 1 branch
BranchTrueFalseTotal
if (owner == me) { 19 190 209
271 /* Already ours: renew. gen is unchanged - it is still the same tenure. */
272 19 sa_at_store64_rel(&s->expires, now + ttl_ms);
273 19 lh->gen = sa_at_load64_acq(&s->gen);
274 19 sa_at_unlock(h->locks, lh->hash);
275 19 return SA_LEASE_HELD;
276 }
277  
278 190 ▶ if (owner == 0 || now >= exp || !sa_pid_alive(owner)) {
Branch: 3 branches
BranchTrueFalseTotal
if (owner == 0 || now >= exp || !sa_pid_alive(owner)) { 146 44 190
if (owner == 0 || now >= exp || !sa_pid_alive(owner)) { 111 35 146
if (owner == 0 || now >= exp || !sa_pid_alive(owner)) { 10 101 111
279 89 int taking_over = (owner != 0);
280 89 uint64_t g = sa_at_load64_acq(&s->gen) + 1;
281 89 sa_at_store64_rel(&s->gen, g);
282 89 sa_at_store64_rel(&s->owner, me);
283 89 sa_at_store64_rel(&s->expires, now + ttl_ms);
284 89 sa_at_fetch_add64(&s->acquires, 1);
285 89 ▶ if (taking_over) sa_at_fetch_add64(&s->steals, 1);
Branch: 1 branch
BranchTrueFalseTotal
if (taking_over) sa_at_fetch_add64(&s->steals, 1); 45 44 89
286 89 lh->gen = g;
287 89 ▶ if (stole) *stole = taking_over;
Branch: 1 branch
BranchTrueFalseTotal
if (stole) *stole = taking_over; 89 0 89
288 89 sa_at_unlock(h->locks, lh->hash);
289 89 return SA_LEASE_HELD;
290 }
291  
292 /* A live holder whose lease is current, and it is not us. */
293 101 sa_at_unlock(h->locks, lh->hash);
294 101 return SA_LEASE_LOST;
295 }
296  
297 /* Extend a lease we hold. Returns SA_LEASE_HELD when we still hold it at the
298 * generation we acquired, SA_LEASE_LOST when we have been superseded - a holder
299 * that lost the lease and comes back must find out, not silently keep going. */
300 34 static int sa_lease_renew(sa_lease *lh, uint64_t ttl_ms)
301 {
302 sa_lease_slot *s;
303 sa_lease_hdr *h;
304 uint64_t me, now;
305 34 int rc = SA_LEASE_LOST;
306  
307 34 ▶ if (!lh) return SA_LEASE_LOST;
Branch: 1 branch
BranchTrueFalseTotal
if (!lh) return SA_LEASE_LOST; 0 34 34
308 34 s = lh->slot;
309 34 h = lh->hdr;
310 34 me = sa_getpid();
311 34 now = sa_now_ms();
312  
313 /* The same wait as acquire. A stripe still held after it biases safe:
314 * renew returns "lost", so the caller steps down and the deadline hands
315 * the lease on. A caller that would rather not step down on a blip asks
316 * sa_lease_mine, which takes no lock. */
317 34 ▶ if (!sa_lock_wait_brief(h->locks, lh->hash)) return SA_LEASE_LOST;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_lock_wait_brief(h->locks, lh->hash)) return SA_LEASE_LOST; 0 34 34
318  
319 34 ▶ if (sa_at_load64_acq(&s->owner) == me
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load64_acq(&s->owner) == me 34 0 34
320 34 ▶ && sa_at_load64_acq(&s->gen) == lh->gen) {
Branch: 1 branch
BranchTrueFalseTotal
&& sa_at_load64_acq(&s->gen) == lh->gen) { 34 0 34
321 34 sa_at_store64_rel(&s->expires, now + ttl_ms);
322 34 rc = SA_LEASE_HELD;
323 }
324 34 sa_at_unlock(h->locks, lh->hash);
325 34 return rc;
326 }
327  
328 /* Give the lease up, so a successor takes it at once rather than after the
329 * deadline. Only the holder at the current generation can release it: a stale
330 * handle releasing would free a lease somebody else now holds. */
331 82 static int sa_lease_release(sa_lease *lh)
332 {
333 sa_lease_slot *s;
334 sa_lease_hdr *h;
335 uint64_t me;
336 82 int rc = 0;
337  
338 82 ▶ if (!lh) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!lh) return 0; 0 82 82
339 82 s = lh->slot;
340 82 h = lh->hdr;
341 82 me = sa_getpid();
342  
343 82 ▶ if (!sa_lock_wait_brief(h->locks, lh->hash)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_lock_wait_brief(h->locks, lh->hash)) return 0; 0 82 82
344 82 ▶ if (sa_at_load64_acq(&s->owner) == me
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load64_acq(&s->owner) == me 82 0 82
345 82 ▶ && sa_at_load64_acq(&s->gen) == lh->gen) {
Branch: 1 branch
BranchTrueFalseTotal
&& sa_at_load64_acq(&s->gen) == lh->gen) { 82 0 82
346 82 sa_at_store64_rel(&s->owner, 0);
347 82 sa_at_store64_rel(&s->expires, 0);
348 82 rc = 1;
349 }
350 82 sa_at_unlock(h->locks, lh->hash);
351 82 return rc;
352 }
353  
354 /* The pid that effectively holds the lease right now, or 0 when it is free or
355 * lapsed. Read-only and lock-free: a lapsed holder reports 0 because a
356 * successor could take the lease this instant, so reporting the stale pid would
357 * be a lie a caller might act on. */
358 39 static uint64_t sa_lease_holder(sa_lease *lh)
359 {
360 sa_lease_slot *s;
361 uint64_t owner, exp;
362 39 ▶ if (!lh) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!lh) return 0; 0 39 39
363 39 s = lh->slot;
364 39 owner = sa_at_load64_acq(&s->owner);
365 39 ▶ if (!owner) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!owner) return 0; 14 25 39
366 25 exp = sa_at_load64_acq(&s->expires);
367 25 ▶ if (sa_now_ms() >= exp) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sa_now_ms() >= exp) return 0; 11 14 25
368 14 ▶ if (!sa_pid_alive(owner)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_pid_alive(owner)) return 0; 0 14 14
369 14 return owner;
370 }
371  
372 /* Do we hold it, at the generation we last acquired, unlapsed? */
373 40 static int sa_lease_mine(sa_lease *lh)
374 {
375 sa_lease_slot *s;
376 40 ▶ if (!lh) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!lh) return 0; 0 40 40
377 40 s = lh->slot;
378 40 ▶ if (sa_at_load64_acq(&s->owner) != sa_getpid()) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load64_acq(&s->owner) != sa_getpid()) return 0; 14 26 40
379 26 ▶ if (sa_at_load64_acq(&s->gen) != lh->gen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load64_acq(&s->gen) != lh->gen) return 0; 0 26 26
380 26 return sa_now_ms() < sa_at_load64_acq(&s->expires);
381 }
382  
383 70 ▶static uint64_t sa_lease_fence(sa_lease *lh) { return lh ? lh->gen : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static uint64_t sa_lease_fence(sa_lease *lh) { return lh ? lh->gen : 0; } 70 0 70
384  
385 #else /* !SA_HAVE_ATOMICS */
386  
387 /* No atomics: no lease. Every function is still DEFINED, because the ABI table
388 * names them - a table entry cannot point at a function the build compiled out.
389 * The lease FAILS CLOSED: acquire never succeeds, so a build with no atomics
390 * simply has no leader election and no worker is ever told it is the one. That
391 * is the safe direction - "nobody is leader" cannot make two workers both act -
392 * and it matches how every other tenant degrades when it cannot use an atomic. */
393 static sa_lease *sa_lease_bind(sa_region *arena, const char *name,
394 uint32_t nlen, int *err) {
395 (void)arena; (void)name; (void)nlen;
396 if (err) *err = SA_E_NOATOMICS;
397 return NULL;
398 }
399 static void sa_lease_free(sa_lease *lh) { free(lh); }
400 static int sa_lease_acquire(sa_lease *lh, uint64_t ttl_ms, int *stole) {
401 (void)lh; (void)ttl_ms;
402 if (stole) *stole = 0;
403 return SA_LEASE_LOST;
404 }
405 static int sa_lease_renew(sa_lease *lh, uint64_t ttl_ms) {
406 (void)lh; (void)ttl_ms; return SA_LEASE_LOST;
407 }
408 static int sa_lease_release(sa_lease *lh) { (void)lh; return 0; }
409 static int sa_lease_mine(sa_lease *lh) { (void)lh; return 0; }
410 static uint64_t sa_lease_holder(sa_lease *lh) { (void)lh; return 0; }
411 static uint64_t sa_lease_fence(sa_lease *lh) { return lh ? lh->gen : 0; }
412  
413 #endif /* SA_HAVE_ATOMICS */
414  
415 #endif /* SA_LEASE_H */
« include/sa/sa_arena.h ↑ Summary xs/cms.xs »