kividb 1.2.1

A fast Redis®-compatible in-memory store written in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
// src/command_gate.rs
//
// A transaction, or a script, is one step as far as every other client is
// concerned.
//
// Redis gets that for free: it runs one command at a time, so nothing can
// happen between the commands of an `EXEC` or the `redis.call`s of a script.
// KiviDB runs commands on many threads. Without something to stop them, a
// client's `MGET a b` could land between the `DECRBY a` and the `INCRBY b` of
// somebody else's transfer and read a sum that never existed; a plain write
// could land between an `EXEC`'s check of its watched keys and its first
// command.
//
// This is that something. Every command passes through the gate, shared, for
// as long as it runs. A transaction or a script takes it alone: it waits for
// the commands in flight to finish, and holds new ones back, until it is
// done.
//
// The shared side is on the path of every command, so it has to cost next to
// nothing. As in `write_gate`, there is one lock per slot and each thread is
// pinned to a slot: entering is an uncontended operation on a cache line only
// that thread touches. Taking the gate alone pays for it by taking every
// slot a thread has — not every slot there is: a server with four threads
// has a handful, and a script that took all 128 spent more on the gate than
// a short script spends on its work.
//
// A thread that gets its first slot while the gate is being taken alone may
// have a slot the taker did not count. Such a thread also goes through
// `LATE`, which every taker takes after its slots, until no taker that
// could have missed it is left. Which of the two happened is settled by the
// order of two steps on each side: the taker says it is taking the gate and
// then counts the slots; the thread takes its slot and then looks for
// takers. One of them sees the other.
//
// A late thread goes through `LATE` without queueing behind a taker that is
// waiting for it, so a few of them running command after command, their
// turns overlapping, could keep it from a taker for ever — and every taker
// after that one queues behind it. So a taker that cannot have `LATE` within
// `RECOUNT` counts the slots again and takes the ones it missed: the late
// threads then wait at their own slots, `LATE` empties, and the taker gets
// it. Slots only ever get added, at most `SLOTS` of them, so the counting
// ends.
//
// It is a different gate from `write_gate`, which exists so that a snapshot
// can be cut between two whole writes. A cut can last as long as the dataset
// takes to write out, and must not stop reads; this gate is held alone only
// for as long as a transaction or a script runs, and has to.

use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::cell::Cell;
use std::marker::PhantomData;
use std::sync::atomic::{AtomicUsize, Ordering};

/// More slots than worker threads on any machine this runs on; threads beyond
/// that share slots, which is correct, just slightly less cache-friendly.
const SLOTS: usize = 128;

/// How long a taker waits for `LATE` before it counts the slots again. See
/// the top.
const RECOUNT: std::time::Duration = std::time::Duration::from_millis(2);

/// Padded so neighbouring slots never share a cache line.
#[repr(align(128))]
struct Slot(RwLock<()>);

static GATE: [Slot; SLOTS] = [const { Slot(parking_lot::const_rwlock(())) }; SLOTS];

/// Slots handed out so far, counting each once: `0..IN_USE` are the ones to
/// take to hold the gate alone. Only goes up.
static IN_USE: AtomicUsize = AtomicUsize::new(0);

/// How many are taking the gate alone, or hold it.
static TAKERS: AtomicUsize = AtomicUsize::new(0);

/// The slot of the threads a taker may not have counted. See the top.
static LATE: Slot = Slot(parking_lot::const_rwlock(()));

static OVERFLOW: AtomicUsize = AtomicUsize::new(0);

const UNASSIGNED: usize = usize::MAX;

/// A thread's slot.
///
/// A slot is not given to another thread when its thread ends. A new thread
/// always gets the next one, higher than any a taker that is waiting has
/// already taken — so a thread's first command is never held up behind a
/// taker that is itself waiting for somebody else. Threads that come and go
/// (the blocking pool's) use the slots up in time, and the gate is then what
/// it was before slots were counted: all of them, shared.
struct MySlot {
    slot: Cell<usize>,
    /// A taker may not have counted this thread's slot: it goes through
    /// `LATE` as well.
    late: Cell<bool>,
}

/// Give the calling thread a slot.
#[cold]
fn assign() -> usize {
    let next = IN_USE.fetch_add(1, Ordering::SeqCst);
    let slot = if next < SLOTS {
        next
    } else {
        // More threads than slots: they share, which is correct, just
        // slightly less cache-friendly. Every slot there is is taken by a
        // taker from here on.
        IN_USE.store(SLOTS, Ordering::SeqCst);
        OVERFLOW.fetch_add(1, Ordering::Relaxed) % SLOTS
    };
    MY_SLOT.with(|mine| {
        mine.slot.set(slot);
        // After the slot is taken, not before: see the top.
        mine.late.set(TAKERS.load(Ordering::SeqCst) > 0);
    });
    slot
}

/// The calling thread's slot, and whether it must go through `LATE` too.
#[inline]
fn my_slot() -> (usize, bool) {
    let (slot, late) = MY_SLOT.with(|mine| (mine.slot.get(), mine.late.get()));
    if slot == UNASSIGNED {
        let slot = assign();
        return (slot, MY_SLOT.with(|mine| mine.late.get()));
    }
    if late {
        return (slot, still_late());
    }
    (slot, false)
}

/// Whether a thread that was late still is: not once no taker is left, for
/// every taker from then on counts its slot.
#[cold]
fn still_late() -> bool {
    if TAKERS.load(Ordering::SeqCst) == 0 {
        MY_SLOT.with(|mine| mine.late.set(false));
        false
    } else {
        true
    }
}

/// Counts a taker in for as long as it is taking the gate or holds it.
struct Taking;

impl Taking {
    fn begin() -> Taking {
        TAKERS.fetch_add(1, Ordering::SeqCst);
        Taking
    }
}

impl Drop for Taking {
    fn drop(&mut self) {
        TAKERS.fetch_sub(1, Ordering::SeqCst);
    }
}

thread_local! {
    static MY_SLOT: MySlot = const {
        MySlot {
            slot: Cell::new(UNASSIGNED),
            late: Cell::new(false),
        }
    };
    /// How deep this thread is in commands: a script's `redis.call`, or a
    /// command run by `EXEC`, is a command inside a command.
    static DEPTH: Cell<u32> = const { Cell::new(0) };
    /// This thread holds the gate alone.
    static ALONE: Cell<bool> = const { Cell::new(false) };
}

/// Held by a command for as long as it runs.
pub struct Guard {
    /// `None` for a command inside a command: the outermost one on this
    /// thread already holds the slot.
    _slot: Option<RwLockReadGuard<'static, ()>>,
    /// Held as well by a thread a taker may not have counted.
    _late: Option<RwLockReadGuard<'static, ()>>,
    /// Tied to the thread that took it (the depth counter is thread-local).
    _not_send: PhantomData<*const ()>,
}

impl Drop for Guard {
    fn drop(&mut self) {
        DEPTH.with(|d| d.set(d.get().saturating_sub(1)));
    }
}

/// Enter the gate as an ordinary command.
///
/// Safe to nest on one thread: an inner entry takes nothing, because taking
/// the slot a second time would queue behind anything waiting to take the
/// gate alone — which is itself waiting for the outer guard.
#[inline]
pub fn enter() -> Guard {
    let nested = DEPTH.with(|d| {
        let depth = d.get();
        d.set(depth + 1);
        depth > 0
    });
    let (slot, late) = if nested {
        (None, None)
    } else {
        let (slot, late) = my_slot();
        let slot = GATE[slot].0.read();
        // Not behind a taker that is waiting for `LATE`: that taker may be
        // waiting for a command that is waiting for this one.
        (Some(slot), late.then(|| LATE.0.read_recursive()))
    };
    Guard {
        _slot: slot,
        _late: late,
        _not_send: PhantomData,
    }
}

/// [`enter`], for a caller that will not wait for ever: each time `slice`
/// has passed and the gate is still held alone, `give_up` is asked whether to
/// stop waiting. `None` if it said yes.
///
/// A script holds the gate alone for as long as it runs, and a script may
/// never end. The commands that arrive once it is known to be running are
/// turned away before they get here (`BUSY`); one that arrives as it
/// starts is not, and used to wait here until the script ended — and if it
/// came from the client that was going to kill the script, that was never.
#[inline]
pub fn enter_unless(
    slice: std::time::Duration,
    mut give_up: impl FnMut() -> bool,
) -> Option<Guard> {
    let nested = DEPTH.with(|d| d.get() > 0);
    let (slot, late) = if nested {
        (None, None)
    } else {
        let (slot, late) = my_slot();
        let mut wait_for = |gate: &'static RwLock<()>, late: bool| {
            // Open, nearly always: taken at once, as `enter` takes it.
            let at_once = if late {
                gate.try_read_recursive()
            } else {
                gate.try_read()
            };
            if let Some(held) = at_once {
                return Some(held);
            }
            loop {
                let in_time = if late {
                    gate.try_read_recursive_for(slice)
                } else {
                    gate.try_read_for(slice)
                };
                if let Some(held) = in_time {
                    return Some(held);
                }
                if give_up() {
                    return None;
                }
            }
        };
        let slot = wait_for(&GATE[slot].0, false)?;
        let late = if late {
            Some(wait_for(&LATE.0, true)?)
        } else {
            None
        };
        (Some(slot), late)
    };
    DEPTH.with(|d| d.set(d.get() + 1));
    Some(Guard {
        _slot: slot,
        _late: late,
        _not_send: PhantomData,
    })
}

/// Held by a transaction or a script: no other command is running, and none
/// starts, until this is dropped.
pub struct AloneGuard {
    // Let go of in this order: the slots, and then being counted a taker.
    _slots: Vec<RwLockWriteGuard<'static, ()>>,
    _taking: Option<Taking>,
    /// Whether this guard is the one that closed the gate.
    outermost: bool,
    _not_send: PhantomData<*const ()>,
}

impl Drop for AloneGuard {
    fn drop(&mut self) {
        if self.outermost {
            ALONE.with(|a| a.set(false));
        }
        DEPTH.with(|d| d.set(d.get().saturating_sub(1)));
    }
}

/// Enter the gate alone: wait for every command in flight to finish, and hold
/// back new ones, until the guard is dropped.
///
/// Slots are taken in index order, so two transactions queue behind one
/// another instead of deadlocking.
///
/// From inside a command on this thread — a script queued in a transaction,
/// a script calling a script — it takes nothing: the thread cannot wait for
/// itself, and whatever the outer command holds is what orders it.
pub fn enter_alone() -> AloneGuard {
    let nested = DEPTH.with(|d| {
        let depth = d.get();
        d.set(depth + 1);
        depth > 0
    });
    if nested {
        return AloneGuard {
            _slots: Vec::new(),
            _taking: None,
            outermost: false,
            _not_send: PhantomData,
        };
    }
    // Said first, and the slots counted after: see the top.
    let taking = Taking::begin();
    let slots = close(None).expect("a taker without a way to give up gets the gate");
    ALONE.with(|a| a.set(true));
    AloneGuard {
        _slots: slots,
        _taking: Some(taking),
        outermost: true,
        _not_send: PhantomData,
    }
}

/// [`enter_alone`], for a caller that will not wait for ever — a second
/// script, arriving as the first starts. See [`enter_unless`].
pub fn enter_alone_unless(
    slice: std::time::Duration,
    mut give_up: impl FnMut() -> bool,
) -> Option<AloneGuard> {
    if DEPTH.with(|d| d.get() > 0) {
        return Some(enter_alone());
    }
    let taking = Taking::begin();
    // On giving up, the slots taken so far are let go of inside.
    let slots = close(Some((slice, &mut give_up)))?;
    DEPTH.with(|d| d.set(d.get() + 1));
    ALONE.with(|a| a.set(true));
    Some(AloneGuard {
        _slots: slots,
        _taking: Some(taking),
        outermost: true,
        _not_send: PhantomData,
    })
}

/// Take every slot in use, in index order, and then `LATE`; count again and
/// take the slots added meanwhile whenever `LATE` cannot be had within
/// `RECOUNT` (see the top). With `patience`, a slot that is not free is
/// waited for a `slice` at a time, and `give_up` is asked after each slice,
/// and after each wait for `LATE`: `None` if it said yes, with everything
/// taken let go of again.
fn close(
    mut patience: Option<(std::time::Duration, &mut dyn FnMut() -> bool)>,
) -> Option<Vec<RwLockWriteGuard<'static, ()>>> {
    let mut slots = Vec::with_capacity(IN_USE.load(Ordering::SeqCst).min(SLOTS) + 1);
    loop {
        // Only goes up, so never below what is already held.
        let in_use = IN_USE.load(Ordering::SeqCst).min(SLOTS);
        for slot in &GATE[slots.len()..in_use] {
            let held = match &mut patience {
                None => slot.0.write(),
                Some((slice, give_up)) => match slot.0.try_write() {
                    Some(held) => held,
                    None => loop {
                        if let Some(held) = slot.0.try_write_for(*slice) {
                            break held;
                        }
                        if give_up() {
                            return None;
                        }
                    },
                },
            };
            slots.push(held);
        }
        if let Some(late) = LATE.0.try_write_for(RECOUNT) {
            slots.push(late);
            return Some(slots);
        }
        if let Some((_, give_up)) = &mut patience
            && give_up()
        {
            return None;
        }
    }
}

/// Whether anything is taking the gate alone, or holds it alone: for a
/// holder of the gate that can let go of it between steps of its work.
#[inline]
pub fn taker_waiting() -> bool {
    TAKERS.load(Ordering::Relaxed) > 0
}

/// Whether the calling thread holds the gate alone — whether what it is
/// running is part of a transaction or a script.
#[inline]
pub fn alone_on_this_thread() -> bool {
    ALONE.with(|a| a.get())
}

/// Commands that do not go through the gate.
///
/// * The ones a client must be able to send *while a script is running*:
///   that is what they are for.
/// * The ones that wait — for replicas, for the network — for as long as
///   their timeout. Holding the gate while they do would hold up every
///   transaction on the server for that long, and they touch no data while
///   they wait.
/// * The ones that give this node slots, which first delete the keys it
///   still had of them if a deletion of those is under way: a pass over the
///   keyspace, in batches of their own (`migration::delete_before_coming`).
///   A transaction or script queued behind them would hold every other
///   command back for that long. `SETSLOT ... NODE` to another node takes
///   the gate in its handler, which knows which node it names.
#[inline(always)]
pub fn skips(name: &str, cmd: &[&[u8]]) -> bool {
    let sub = |word: &[u8]| cmd.get(1).is_some_and(|s| s.eq_ignore_ascii_case(word));
    let arg = |at: usize, word: &[u8]| cmd.get(at).is_some_and(|s| s.eq_ignore_ascii_case(word));
    match name {
        "WAIT" | "WAITAOF" | "MIGRATE" | "SHUTDOWN" | "DEBUG" => true,
        "CLUSTER" => {
            sub(b"ADDSLOTS")
                || sub(b"ADDSLOTSRANGE")
                || (sub(b"SETSLOT") && (arg(3, b"IMPORTING") || arg(3, b"NODE")))
        }
        "SCRIPT" => sub(b"KILL"),
        "FUNCTION" => sub(b"KILL") || sub(b"STATS"),
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, AtomicU64};
    use std::time::Duration;

    /// While the gate is held alone, no other command is between its start
    /// and its end.
    #[test]
    fn nothing_else_runs_while_the_gate_is_held_alone() {
        let in_flight = Arc::new(AtomicU64::new(0));
        let stop = Arc::new(AtomicBool::new(false));
        let workers: Vec<_> = (0..6)
            .map(|_| {
                let in_flight = Arc::clone(&in_flight);
                let stop = Arc::clone(&stop);
                std::thread::spawn(move || {
                    while !stop.load(Ordering::Relaxed) {
                        let _guard = enter();
                        in_flight.fetch_add(1, Ordering::SeqCst);
                        std::thread::yield_now();
                        in_flight.fetch_sub(1, Ordering::SeqCst);
                    }
                })
            })
            .collect();
        for _ in 0..200 {
            let _alone = enter_alone();
            assert!(alone_on_this_thread());
            for _ in 0..20 {
                assert_eq!(in_flight.load(Ordering::SeqCst), 0);
            }
        }
        assert!(!alone_on_this_thread());
        stop.store(true, Ordering::Relaxed);
        for w in workers {
            w.join().unwrap();
        }
    }

    /// A command inside a command takes nothing, and a transaction started
    /// from inside one does not wait for itself.
    #[test]
    fn nesting_does_not_deadlock() {
        let outer = enter_alone();
        let inner = enter();
        let script_in_transaction = enter_alone();
        assert!(alone_on_this_thread());
        drop(script_in_transaction);
        // Still the outer transaction's.
        assert!(alone_on_this_thread());
        drop(inner);
        drop(outer);
        assert!(!alone_on_this_thread());

        let command = enter();
        let nested = enter_alone();
        assert!(!alone_on_this_thread(), "it could not close the gate");
        drop(nested);
        drop(command);
        // And the gate is free again.
        let done = Arc::new(AtomicBool::new(false));
        let flag = Arc::clone(&done);
        let t = std::thread::spawn(move || {
            let _alone = enter_alone();
            flag.store(true, Ordering::SeqCst);
        });
        t.join().unwrap();
        assert!(done.load(Ordering::SeqCst));
    }

    /// The gate is the process's: a test beside this one may be taking it
    /// alone. Its taker then waits for `mine`, and the other thread, queued
    /// behind the taker, would wait for ever while this one waits for it. So
    /// the other thread gives up after a while, `mine` is let go of, and the
    /// check is made again.
    #[test]
    fn commands_do_not_wait_for_each_other() {
        for _ in 0..50 {
            let mine = enter();
            let entered = std::thread::spawn(|| {
                let deadline = std::time::Instant::now() + Duration::from_millis(200);
                enter_unless(Duration::from_millis(5), || {
                    std::time::Instant::now() > deadline
                })
                .is_some()
            })
            .join()
            .unwrap();
            drop(mine);
            if entered {
                return;
            }
        }
        panic!("a command never got in beside another");
    }

    #[test]
    fn the_commands_that_must_get_through() {
        assert!(skips("SCRIPT", &[b"SCRIPT", b"kill"]));
        assert!(!skips("SCRIPT", &[b"SCRIPT", b"LOAD", b"return 1"]));
        assert!(skips("FUNCTION", &[b"FUNCTION", b"STATS"]));
        assert!(skips("WAIT", &[b"WAIT", b"1", b"0"]));
        assert!(skips("MIGRATE", &[b"MIGRATE"]));
        assert!(!skips("GET", &[b"GET", b"k"]));
        assert!(!skips("EXEC", &[b"EXEC"]));
    }

    /// A command that will not wait for ever is let through when the gate
    /// opens, and told so when it is asked to give up first.
    #[test]
    fn a_wait_for_the_gate_can_be_given_up() {
        let (held_tx, held_rx) = std::sync::mpsc::channel();
        let (release_tx, release_rx) = std::sync::mpsc::channel::<()>();
        let holder = std::thread::spawn(move || {
            let _alone = enter_alone();
            held_tx.send(()).unwrap();
            release_rx.recv().unwrap();
        });
        held_rx.recv().unwrap();

        let slice = Duration::from_millis(5);
        let mut asked = 0;
        assert!(
            enter_unless(slice, || {
                asked += 1;
                asked == 3
            })
            .is_none()
        );
        assert_eq!(asked, 3);
        assert!(enter_alone_unless(slice, || true).is_none());
        // Having given up, it holds nothing: the next one is not nested.
        assert!(!alone_on_this_thread());

        release_tx.send(()).unwrap();
        holder.join().unwrap();
        // The gate is the process's: another test running beside this one
        // may hold it for a moment, so these wait rather than give up.
        assert!(enter_unless(slice, || false).is_some());
        let alone = enter_alone_unless(slice, || false).expect("the gate is open");
        assert!(alone_on_this_thread());
        // Inside it, a command of the same thread is let in at once.
        assert!(enter_unless(slice, || panic!("it does not wait")).is_some());
        drop(alone);
        assert!(!alone_on_this_thread());
    }
}

#[cfg(test)]
mod late_threads {
    use super::*;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, AtomicU64};
    use std::time::Duration;

    /// Threads that arrive while a taker is on its way, and then run command
    /// after command, do not keep `LATE` from it for ever. They used to: the
    /// taker had counted the slots before they had any, their turns through
    /// `LATE` overlapped, and every taker after it queued behind it.
    #[test]
    fn late_threads_running_commands_back_to_back_do_not_starve_a_taker() {
        // A command under way: the taker below counts its slot, and waits for
        // it. While the taker waits, threads that arrive are late.
        let (held_tx, held_rx) = std::sync::mpsc::channel();
        let (release_tx, release_rx) = std::sync::mpsc::channel::<()>();
        let holder = std::thread::spawn(move || {
            let _command = enter();
            held_tx.send(()).unwrap();
            release_rx.recv().unwrap();
        });
        held_rx.recv().unwrap();

        let (got_tx, got_rx) = std::sync::mpsc::channel();
        let taker = std::thread::spawn(move || {
            let _alone = enter_alone();
            got_tx.send(()).unwrap();
        });
        // Let it count the slots and queue behind the holder.
        std::thread::sleep(Duration::from_millis(50));

        let stop = Arc::new(AtomicBool::new(false));
        let crowd: Vec<_> = (0..6)
            .map(|_| {
                let stop = Arc::clone(&stop);
                std::thread::spawn(move || {
                    while !stop.load(Ordering::Relaxed) {
                        let _command = enter();
                        std::thread::yield_now();
                    }
                })
            })
            .collect();
        // Running command after command through `LATE` by now.
        std::thread::sleep(Duration::from_millis(50));
        release_tx.send(()).unwrap();
        holder.join().unwrap();

        let got_in = got_rx.recv_timeout(Duration::from_secs(20)).is_ok();
        stop.store(true, Ordering::Relaxed);
        for thread in crowd {
            thread.join().unwrap();
        }
        assert!(got_in, "the taker never got the gate");
        taker.join().unwrap();
    }

    /// Threads that run their first command while the gate is being taken
    /// alone are kept out as well as the ones that were there before: the
    /// taker did not count their slots, and they go through `LATE`.
    #[test]
    fn a_thread_that_starts_while_the_gate_is_taken_is_kept_out() {
        let in_flight = Arc::new(AtomicU64::new(0));
        let stop = Arc::new(AtomicBool::new(false));
        // Short-lived threads, one after another, each new to the gate.
        let spawners: Vec<_> = (0..4)
            .map(|_| {
                let in_flight = Arc::clone(&in_flight);
                let stop = Arc::clone(&stop);
                std::thread::spawn(move || {
                    while !stop.load(Ordering::Relaxed) {
                        let in_flight = Arc::clone(&in_flight);
                        std::thread::spawn(move || {
                            for _ in 0..3 {
                                let _command = enter();
                                in_flight.fetch_add(1, Ordering::SeqCst);
                                std::hint::spin_loop();
                                in_flight.fetch_sub(1, Ordering::SeqCst);
                            }
                        })
                        .join()
                        .unwrap();
                    }
                })
            })
            .collect();
        for _ in 0..2000 {
            let _alone = enter_alone();
            assert_eq!(
                in_flight.load(Ordering::SeqCst),
                0,
                "a command ran beside it"
            );
            std::hint::spin_loop();
            assert_eq!(
                in_flight.load(Ordering::SeqCst),
                0,
                "a command started beside it"
            );
        }
        stop.store(true, Ordering::Relaxed);
        for spawner in spawners {
            spawner.join().unwrap();
        }
    }
}