kithara-platform 0.0.1-alpha5

Cross-platform primitives (sync, time, thread) for native and wasm32.
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
#![forbid(unsafe_code)]

use arc_swap::ArcSwapOption;

use crate::{
    sync::{
        Arc, Condvar, Mutex, MutexGuard,
        atomic::{AtomicU64, Ordering},
    },
    thread::{self, GateBackend, Thread},
    time::{Duration, Instant},
};

/// Shared edge API: a monotonic counter ("something happened") plus a wake
/// and a bounded wait. Snapshot [`current`](Self::current) BEFORE checking
/// your (possibly externally-locked) predicate, then pass that snapshot to a
/// wait — the wait returns immediately if the counter already moved, so a
/// `signal` racing the predicate check is never lost.
pub trait WaitGate {
    /// Snapshot the edge counter.
    fn current(&self) -> u64;

    /// Bump the counter and wake every waiter.
    fn signal(&self);

    /// Block until the counter differs from `since` or `timeout` elapses.
    /// Returns `true` if the counter moved (an event landed), `false` on a
    /// pure timeout.
    fn wait_timeout(&self, since: u64, timeout: Duration) -> bool;
}

/// Off-RT condvar gate over guarded state `S`. See the module docs for the
/// guarded-state vs edge usage.
#[derive(Default)]
pub struct CondvarGate<S> {
    cv: Condvar,
    state: Mutex<S>,
}

impl<S> CondvarGate<S> {
    /// Construct with the initial guarded state.
    pub fn new(state: S) -> Self {
        Self {
            state: Mutex::new(state),
            cv: Condvar::default(),
        }
    }

    /// Lock the guarded state.
    pub fn lock(&self) -> MutexGuard<'_, S> {
        self.state.lock()
    }

    delegate::delegate! {
        to self.cv {
            /// Wake every waiter. Call after mutating the guarded state (the wait
            /// re-checks its predicate on wake).
            pub fn notify_all(&self);
            /// Park until the next [`notify_all`](Self::notify_all). The caller holds
            /// `guard` (having re-checked its predicate under it); the lock is
            /// released for the park and re-acquired on wake.
            #[must_use]
            pub fn wait<'a>(&self, guard: MutexGuard<'a, S>) -> MutexGuard<'a, S>;
            /// Park until the next [`notify_all`](Self::notify_all) or `deadline`.
            #[must_use]
            #[call(wait_timeout)]
            pub fn wait_until<'a>(&self, guard: MutexGuard<'a, S>, deadline: Instant) -> MutexGuard<'a, S>;
        }
    }
}

impl WaitGate for CondvarGate<u64> {
    fn current(&self) -> u64 {
        *self.lock()
    }

    fn signal(&self) {
        {
            let mut guard = self.lock();
            *guard = guard.wrapping_add(1);
        }
        self.cv.notify_all();
    }

    fn wait_timeout(&self, since: u64, timeout: Duration) -> bool {
        let deadline = Instant::now() + timeout;
        let mut guard = self.lock();
        loop {
            if *guard != since {
                return true;
            }
            if Instant::now() >= deadline {
                return false;
            }
            guard = self.cv.wait_timeout(guard, deadline);
        }
    }
}

/// Single-waiter edge gate: signal advances atomically, loads the waiter
/// lock-free, and unparks. Its timed backstop preserves sequence progress.
pub struct ThreadGate {
    waiter: ArcSwapOption<Thread>,
    state: AtomicU64,
    waiter_id: AtomicU64,
    backend: GateBackend,
    retired_waiters: Mutex<Vec<Arc<Thread>>>,
}

impl Default for ThreadGate {
    fn default() -> Self {
        Self {
            backend: GateBackend::default(),
            state: AtomicU64::new(0),
            waiter: ArcSwapOption::empty(),
            retired_waiters: Mutex::new(Vec::new()),
            waiter_id: AtomicU64::new(0),
        }
    }
}

impl ThreadGate {
    const SEQUENCE_MASK: u64 = !Self::WAITING;
    const WAITING: u64 = 1 << 63;

    fn advance(&self) -> u64 {
        let mut current = self.state.load(Ordering::SeqCst);
        loop {
            let next = (current & Self::WAITING) | (current.wrapping_add(1) & Self::SEQUENCE_MASK);
            match self.state.compare_exchange_weak(
                current,
                next,
                Ordering::SeqCst,
                Ordering::SeqCst,
            ) {
                Ok(previous) => return previous,
                Err(observed) => current = observed,
            }
        }
    }

    /// Writer-held retirees make signal guard drops pure decrements; reclaim
    /// quiesced ones here, off the signal path.
    fn publish_waiter(&self) {
        let mut retired = self.retired_waiters.lock();
        retired.retain(|waiter| Arc::strong_count(waiter) > 1);
        if let Some(displaced) = self.waiter.swap(Some(Arc::new(thread::current()))) {
            retired.push(displaced);
        }
    }

    fn register(&self) {
        let waiter_id = thread::current_thread_id();
        if self.waiter_id.load(Ordering::Acquire) != waiter_id || self.waiter.load().is_none() {
            self.publish_waiter();
            self.waiter_id.store(waiter_id, Ordering::Release);
        }
        self.state.fetch_or(Self::WAITING, Ordering::SeqCst);
    }

    const fn sequence(state: u64) -> u64 {
        state & Self::SEQUENCE_MASK
    }
}

impl WaitGate for ThreadGate {
    fn current(&self) -> u64 {
        Self::sequence(self.state.load(Ordering::Acquire))
    }

    /// Advance the edge, snapshot the active waiter lock-free, and unpark it.
    fn signal(&self) {
        let previous = self.advance();
        if previous & Self::WAITING != 0 {
            let waiter = self.waiter.load();
            self.backend
                .unpark(self.waiter_id.load(Ordering::Relaxed), waiter.as_deref());
        }
    }

    fn wait_timeout(&self, since: u64, timeout: Duration) -> bool {
        self.register();
        let deadline = thread::gate_instant(&self.backend) + timeout;
        let result = loop {
            let state = self.state.load(Ordering::SeqCst);
            if Self::sequence(state) != since {
                break true;
            }
            let now = thread::gate_instant(&self.backend);
            if now >= deadline {
                break Self::sequence(self.state.load(Ordering::SeqCst)) != since;
            }
            self.backend.park_timeout(deadline - now);
        };
        self.state.fetch_and(Self::SEQUENCE_MASK, Ordering::SeqCst);
        result
    }
}

#[cfg(test)]
#[cfg(not(target_arch = "wasm32"))]
mod tests {
    use std::time::{Duration, Instant as StdInstant};

    use assert_no_alloc::assert_no_alloc;
    use kithara_test_utils::kithara;

    use super::*;
    use crate::{
        sync::{Arc, mpsc},
        thread,
    };

    #[kithara::test]
    fn edge_signal_advances_current() {
        let g = CondvarGate::<u64>::default();
        let s0 = g.current();
        g.signal();
        assert_ne!(g.current(), s0, "signal must advance the edge counter");
    }

    #[kithara::test]
    fn edge_wait_timeout_expires_without_signal() {
        let g = CondvarGate::<u64>::default();
        let s0 = g.current();
        assert!(
            !g.wait_timeout(s0, Duration::from_millis(10)),
            "no signal: wait_timeout returns false"
        );
    }

    #[kithara::test]
    fn guarded_state_wait_until_predicate() {
        let g = Arc::new(CondvarGate::new(false));
        let setter = Arc::clone(&g);
        let join = thread::spawn_named("gate-state-set", move || {
            thread::sleep(Duration::from_millis(5));
            *setter.lock() = true;
            setter.notify_all();
        });
        let mut guard = g.lock();
        while !*guard {
            guard = g.wait(guard);
        }
        assert!(*guard);
        drop(guard);
        join.join().expect("setter thread");
    }

    #[kithara::test]
    fn thread_gate_wait_timeout_expires_without_signal() {
        let g = ThreadGate::default();
        let s0 = g.current();
        assert!(!g.wait_timeout(s0, Duration::from_millis(10)));
    }

    #[kithara::test]
    fn thread_gate_wakes_on_cross_thread_signal() {
        let g = Arc::new(ThreadGate::default());
        let s0 = g.current();
        let signaller = Arc::clone(&g);
        let join = thread::spawn_named("threadgate-signal", move || {
            thread::sleep(Duration::from_millis(5));
            signaller.signal();
        });
        assert!(
            g.wait_timeout(s0, Duration::from_secs(5)),
            "cross-thread signal must wake before the backstop"
        );
        join.join().expect("signaller thread");
    }

    #[kithara::test]
    fn thread_gate_signal_before_wait_is_not_lost() {
        let g = ThreadGate::default();
        let s0 = g.current();
        // signal lands before any wait registers the waiter thread: the
        // counter bump alone must make the next wait return immediately.
        g.signal();
        assert!(g.wait_timeout(s0, Duration::from_millis(10)));
    }

    #[kithara::test(flash(false))]
    fn thread_gate_signal_during_waiter_publication_is_not_lost() {
        let gate = Arc::new(ThreadGate::default());
        let seed = gate.current();
        gate.signal();
        assert!(gate.wait_timeout(seed, Duration::ZERO));

        let publication = gate.retired_waiters.lock();
        let (started_tx, started_rx) = mpsc::channel();
        let waiter_gate = Arc::clone(&gate);
        let waiter = thread::spawn_named("threadgate-register-race", move || {
            let since = waiter_gate.current();
            started_tx.send(()).expect("start waiter registration");
            waiter_gate.wait_timeout(since, Duration::from_secs(5))
        });
        started_rx
            .recv_timeout(Instant::now() + Duration::from_secs(1))
            .expect("waiter registration started");

        gate.signal();
        drop(publication);

        assert!(waiter.join().expect("registration-race waiter"));
    }

    #[kithara::test(flash(false))]
    fn thread_gate_signal_progress_does_not_wait_for_waiter_publication() {
        let gate = Arc::new(ThreadGate::default());
        let seed = gate.current();
        gate.signal();
        assert!(gate.wait_timeout(seed, Duration::ZERO));

        let publication = gate.retired_waiters.lock();
        let (started_tx, started_rx) = mpsc::channel();
        let waiter_gate = Arc::clone(&gate);
        let waiter = thread::spawn_named("threadgate-publish-waiter", move || {
            let since = waiter_gate.current();
            started_tx.send(()).expect("start waiter publication");
            waiter_gate.wait_timeout(since, Duration::from_secs(5));
        });
        started_rx
            .recv_timeout(Instant::now() + Duration::from_secs(1))
            .expect("waiter publication started");

        let before = gate.current();
        let (progress_tx, progress_rx) = mpsc::channel();
        let signaller_gate = Arc::clone(&gate);
        let signaller = thread::spawn_named("threadgate-publish-signal", move || {
            signaller_gate.signal();
            progress_tx
                .send(signaller_gate.current())
                .expect("report signal progress");
        });
        let observed = progress_rx
            .recv_timeout(Instant::now() + Duration::from_secs(1))
            .expect("signal progresses while waiter publication is held");
        assert_ne!(observed, before, "signal must advance the edge");

        drop(publication);
        signaller.join().expect("publication-race signaller");
        waiter.join().expect("publication-race waiter");
    }

    #[kithara::test(flash(false))]
    fn thread_gate_refreshes_waiter_handle() {
        let g = Arc::new(ThreadGate::default());

        let first_gate = Arc::clone(&g);
        let first = thread::spawn_named("threadgate-stale-waiter", move || {
            let first_id = thread::current_thread_id();
            let since = first_gate.current();
            first_gate.signal();
            assert!(first_gate.wait_timeout(since, Duration::from_millis(10)));
            first_id
        });
        let first_id = first.join().expect("first waiter thread");

        let (registered_tx, registered_rx) = mpsc::channel();

        let second_gate = Arc::clone(&g);
        let second = thread::spawn_named("threadgate-current-waiter", move || {
            let since = second_gate.current();
            let second_id = thread::current_thread_id();
            registered_tx
                .send(second_id)
                .expect("report second waiter registration");

            let started = StdInstant::now();
            let moved = second_gate.wait_timeout(since, Duration::from_millis(250));
            let elapsed = started.elapsed();
            (second_id, elapsed, moved)
        });

        let second_id = registered_rx
            .recv_timeout(Instant::now() + Duration::from_secs(1))
            .expect("second waiter registered");
        assert!(
            first_id != second_id,
            "test requires two live waiter threads"
        );

        while g.state.load(Ordering::Acquire) & ThreadGate::WAITING == 0 {
            thread::yield_now();
        }
        g.signal();

        let (_second_id, elapsed, moved) = second.join().expect("second waiter thread");
        assert!(moved, "signal must advance the gate before waking");
        assert!(
            elapsed < Duration::from_millis(200),
            "signal woke stale waiter; current waiter only returned after {elapsed:?}"
        );
    }

    #[kithara::test(flash(false))]
    fn thread_gate_signal_does_not_allocate() {
        let gate = Arc::new(ThreadGate::default());
        let waiter_gate = Arc::clone(&gate);
        let waiter = thread::spawn_named("threadgate-allocation-waiter", move || {
            let since = waiter_gate.current();
            waiter_gate.wait_timeout(since, Duration::from_secs(1))
        });

        while gate.state.load(Ordering::Acquire) & ThreadGate::WAITING == 0 {
            thread::yield_now();
        }
        drop(gate.waiter.load());
        assert_no_alloc(|| gate.signal());

        assert!(waiter.join().expect("allocation waiter thread"));
    }

    /// The scheduler's shape: a `spawn_named` thread — counted `active` by the
    /// quiescence engine for its whole life — loops `wait_timeout` while
    /// something else keeps the edge moving. `wait_timeout` returns at once
    /// whenever the edge moved, so the loop never parks; a participant that
    /// never parks never lets the engine quiesce, and the virtual clock a
    /// sleeper waits on cannot advance. The sleep below is the discriminator:
    /// if a spinning participant can freeze the clock, it never returns.
    #[kithara::test]
    fn a_spinning_gate_waiter_must_not_freeze_the_virtual_clock() {
        let gate = Arc::new(ThreadGate::default());
        let stop = Arc::new(std::sync::atomic::AtomicBool::new(false));

        let waiter_gate = Arc::clone(&gate);
        let waiter_stop = Arc::clone(&stop);
        let waiter = thread::spawn_named("gate-spin-waiter", move || {
            while !waiter_stop.load(Ordering::Acquire) {
                let since = waiter_gate.current();
                waiter_gate.wait_timeout(since, Duration::from_millis(10));
            }
        });

        let signal_gate = Arc::clone(&gate);
        let signal_stop = Arc::clone(&stop);
        let signaller = thread::spawn_named("gate-spin-signaller", move || {
            while !signal_stop.load(Ordering::Acquire) {
                signal_gate.signal();
            }
        });

        thread::sleep(Duration::from_secs(1));

        stop.store(true, Ordering::Release);
        gate.signal();
        waiter.join().expect("spin waiter thread");
        signaller.join().expect("spin signaller thread");
    }
}