noxu-latch 3.0.2

Latching primitives for Noxu DB (shared/exclusive latches)
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
//! Exclusive (mutex-like) latch.
//!
//! Provides exclusive latching implemented with `noxu_sync::Mutex`.
//! Reentrancy is prevented: attempting to acquire a latch already held by
//! the current thread will panic, detecting accidental reentrant calls.

use crate::{LatchContext, LatchError};
use noxu_sync::Mutex;
use std::fmt;
use std::sync::atomic::{AtomicU64, Ordering};
use std::thread;

/// An exclusive (mutex-like) latch.
///
/// This latch provides exclusive/write access only. It prevents reentrant
/// acquisition by the same thread, which increases reliability by detecting
/// accidental reentrant calls.
pub struct ExclusiveLatch {
    context: LatchContext,
    inner: Mutex<()>,
    /// Thread ID of the current owner (0 if not held).
    owner: AtomicU64,
}

impl ExclusiveLatch {
    /// Creates a new exclusive latch.
    pub fn new(context: LatchContext) -> Self {
        ExclusiveLatch {
            context,
            inner: Mutex::new(()),
            owner: AtomicU64::new(0),
        }
    }

    /// Creates a new exclusive latch with the given name.
    pub fn named(name: impl Into<String>) -> Self {
        Self::new(LatchContext::new(name))
    }

    /// Acquires the latch for exclusive access.
    ///
    /// Returns `Ok(guard)` on success, or `Err(LatchError::Timeout)` if the
    /// acquisition times out.
    ///
    /// # Panics
    ///
    /// Panics if the latch is already held by the calling thread (reentrancy
    /// detected). Reentrancy is a programming error and must not be silenced.
    pub fn acquire(&self) -> Result<ExclusiveLatchGuard<'_>, LatchError> {
        let current = thread_id();
        if self.owner.load(Ordering::Relaxed) == current {
            panic!(
                "Latch already held: {} (thread {:?})",
                self.context.name,
                thread::current().name()
            );
        }

        let timeout = self.context.timeout;
        let guard = self.inner.try_lock_for(timeout).ok_or_else(|| {
            LatchError::Timeout(format!(
                "Latch acquisition timed out after {}ms: {}",
                timeout.as_millis(),
                self.context.name
            ))
        })?;
        self.owner.store(current, Ordering::Relaxed);
        Ok(ExclusiveLatchGuard { latch: self, _guard: guard })
    }

    /// Attempts to acquire the latch without blocking.
    ///
    /// Returns `Some(guard)` if the latch was acquired, `None` if it is
    /// currently held by another thread.
    ///
    /// # Panics
    ///
    /// Panics if the latch is already held by the calling thread.
    pub fn try_acquire(&self) -> Option<ExclusiveLatchGuard<'_>> {
        let current = thread_id();
        if self.owner.load(Ordering::Relaxed) == current {
            panic!(
                "Latch already held: {} (thread {:?})",
                self.context.name,
                thread::current().name()
            );
        }

        self.inner.try_lock().map(|guard| {
            self.owner.store(current, Ordering::Relaxed);
            ExclusiveLatchGuard { latch: self, _guard: guard }
        })
    }

    /// Returns true if the latch is currently held by any thread.
    pub fn is_locked(&self) -> bool {
        self.inner.is_locked()
    }

    /// Returns true if the latch is held by the current thread.
    pub fn is_owner(&self) -> bool {
        self.owner.load(Ordering::Relaxed) == thread_id()
    }

    /// Returns the context for this latch.
    pub fn context(&self) -> &LatchContext {
        &self.context
    }

    /// Releases the latch if held by the current thread.
    /// Does nothing if not held by the current thread.
    ///
    /// Equivalent of `release_if_owner()` — releases only if held by this thread.
    pub fn release_if_owner(&self) {
        if self.is_owner() {
            self.owner.store(0, Ordering::Relaxed);
            // SAFETY: We verified ownership above
            unsafe { self.inner.force_unlock() };
        }
    }
}

impl fmt::Debug for ExclusiveLatch {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "ExclusiveLatch({}, locked={})",
            self.context.name,
            self.is_locked()
        )
    }
}

/// RAII guard for an exclusive latch. Releases the latch when dropped.
pub struct ExclusiveLatchGuard<'a> {
    latch: &'a ExclusiveLatch,
    _guard: noxu_sync::MutexGuard<'a, ()>,
}

impl Drop for ExclusiveLatchGuard<'_> {
    fn drop(&mut self) {
        self.latch.owner.store(0, Ordering::Relaxed);
    }
}

/// Returns a unique identifier for the current thread.
fn thread_id() -> u64 {
    // Use the raw ThreadId hash as a stable identifier since
    // ThreadId::as_u64() is unstable.
    use std::hash::{Hash, Hasher};
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    thread::current().id().hash(&mut hasher);
    hasher.finish()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;

    #[test]
    fn test_acquire_release() {
        let latch = ExclusiveLatch::named("test");
        assert!(!latch.is_locked());
        {
            let _guard = latch.acquire().expect("acquire");
            assert!(latch.is_locked());
            assert!(latch.is_owner());
        }
        assert!(!latch.is_locked());
    }

    #[test]
    fn test_try_acquire() {
        let latch = Arc::new(ExclusiveLatch::named("test"));
        let guard = latch.try_acquire();
        assert!(guard.is_some());

        // Another thread should fail to acquire
        let latch2 = latch.clone();
        let handle = std::thread::spawn(move || latch2.try_acquire().is_none());
        assert!(handle.join().unwrap());
    }

    #[test]
    #[should_panic(expected = "Latch already held")]
    fn test_reentrant_panics() {
        let latch = ExclusiveLatch::named("test");
        let _guard = latch.acquire().expect("first acquire");
        let _ = latch.acquire(); // Should panic before returning
    }

    #[test]
    fn test_release_if_owner() {
        let latch = ExclusiveLatch::named("test");
        {
            let _guard = latch.acquire().expect("acquire");
            assert!(latch.is_owner());
        }
        // After guard dropped, release_if_owner should be a no-op
        latch.release_if_owner();
        assert!(!latch.is_locked());
    }

    #[test]
    fn test_acquire_timeout() {
        use std::time::Duration;
        let ctx = crate::LatchContext::with_timeout(
            "test-timeout",
            Duration::from_millis(50),
        );
        let latch = Arc::new(ExclusiveLatch::new(ctx));

        let latch2 = latch.clone();
        let barrier = Arc::new(std::sync::Barrier::new(2));
        let barrier2 = barrier.clone();
        let handle = std::thread::spawn(move || {
            let _g = latch2.acquire().expect("acquire in spawned thread");
            barrier2.wait(); // signal: lock is held
            std::thread::sleep(Duration::from_millis(200));
        });

        barrier.wait(); // wait until other thread holds the lock
        // acquire() should return Err(LatchError::Timeout) instead of panicking.
        let result = latch.acquire();
        assert!(result.is_err(), "expected latch timeout error, got Ok");
        let _ = handle.join();
    }

    #[test]
    fn test_context_name_and_timeout() {
        use std::time::Duration;
        let ctx = crate::LatchContext::with_timeout(
            "my-latch",
            Duration::from_secs(1),
        );
        let latch = ExclusiveLatch::new(ctx);
        assert_eq!(latch.context().name, "my-latch");
        assert_eq!(latch.context().timeout, Duration::from_secs(1));
    }

    #[test]
    fn test_is_not_owner_when_not_held() {
        let latch = ExclusiveLatch::named("test-owner");
        assert!(!latch.is_owner());
        assert!(!latch.is_locked());
    }

    #[test]
    fn test_is_owner_only_in_owning_thread() {
        let latch = Arc::new(ExclusiveLatch::named("test-owner-thread"));
        let _guard = latch.acquire().expect("acquire");
        assert!(latch.is_owner());

        // Another thread should see is_owner() == false even while we hold it
        let latch2 = latch.clone();
        let handle = std::thread::spawn(move || {
            assert!(!latch2.is_owner(), "non-owner thread should not be owner");
            assert!(latch2.is_locked(), "latch should be locked");
        });
        handle.join().unwrap();
    }

    #[test]
    fn test_concurrent_acquire_serializes() {
        use std::sync::atomic::{AtomicUsize, Ordering};
        let latch = Arc::new(ExclusiveLatch::named("serial-test"));
        let counter = Arc::new(AtomicUsize::new(0));
        let concurrent = Arc::new(AtomicUsize::new(0));
        let violations = Arc::new(AtomicUsize::new(0));

        let threads: Vec<_> = (0..4)
            .map(|_| {
                let latch = latch.clone();
                let counter = counter.clone();
                let concurrent = concurrent.clone();
                let violations = violations.clone();
                std::thread::spawn(move || {
                    for _ in 0..25 {
                        let _guard = latch.acquire().expect("acquire");
                        // We should be the only thread in this section
                        let prev = concurrent.fetch_add(1, Ordering::SeqCst);
                        if prev != 0 {
                            violations.fetch_add(1, Ordering::SeqCst);
                        }
                        counter.fetch_add(1, Ordering::SeqCst);
                        concurrent.fetch_sub(1, Ordering::SeqCst);
                    }
                })
            })
            .collect();

        for t in threads {
            t.join().unwrap();
        }
        assert_eq!(counter.load(Ordering::SeqCst), 100);
        assert_eq!(
            violations.load(Ordering::SeqCst),
            0,
            "mutual exclusion violated"
        );
    }

    #[test]
    fn test_try_acquire_reentrant_panics() {
        // try_acquire also panics on reentrant attempt
        let result = std::panic::catch_unwind(|| {
            let latch = ExclusiveLatch::named("try-reentrant");
            let _guard = latch.acquire();
            // try_acquire should panic since we already own it
            let _guard2 = latch.try_acquire();
        });
        assert!(result.is_err(), "expected panic on reentrant try_acquire");
    }

    #[test]
    fn test_debug_format() {
        let latch = ExclusiveLatch::named("debug-test");
        let s = format!("{:?}", latch);
        assert!(s.contains("debug-test"));
        assert!(s.contains("locked=false"));
    }

    // -----------------------------------------------------------------------
    // Ported from LatchTest.java — exclusive latch invariants
    // -----------------------------------------------------------------------

    /// Acquiring an already-held exclusive latch must panic (reentrancy detection).
    #[test]
    fn test_acquire_reacquire_panics() {
        let result = std::panic::catch_unwind(|| {
            let latch = ExclusiveLatch::named("noxu-reacquire");
            let _g1 = latch.acquire().expect("first acquire");
            // Second acquire on same thread must panic.
            let _ = latch.acquire();
        });
        assert!(result.is_err(), "reentrant acquire should panic");
    }

    /// Releasing a latch that is not held must panic.
    #[test]
    fn test_release_not_held_panics() {
        // release_if_owner on a latch not held should be a no-op (not panic).
        // But acquiring twice panics, so we verify the second-acquire path
        // by catching the panic (tested above).  Here verify the "not owner"
        // path via release_if_owner which is safe when not held.
        let latch = ExclusiveLatch::named("noxu-not-held");
        assert!(!latch.is_locked());
        // release_if_owner on a not-held latch should be a no-op.
        latch.release_if_owner();
        assert!(!latch.is_locked());
    }

    /// `try_acquire` returns None when held by another thread, and Some when available.
    #[test]
    fn test_try_acquire_no_wait() {
        let latch = Arc::new(ExclusiveLatch::named("noxu-no-wait"));
        let barrier = Arc::new(std::sync::Barrier::new(2));

        // Thread 1 holds the latch.
        let latch2 = latch.clone();
        let barrier2 = barrier.clone();
        let held = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let held2 = held;
        let released = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let released2 = released.clone();

        let h = std::thread::spawn(move || {
            let _g = latch2.acquire();
            held2.store(true, std::sync::atomic::Ordering::SeqCst);
            barrier2.wait(); // signal acquired
            // Wait until the test thread says we can release.
            while !released2.load(std::sync::atomic::Ordering::SeqCst) {
                std::thread::yield_now();
            }
        });

        barrier.wait(); // wait until thread holds the latch

        // try_acquire should fail while thread 1 holds it.
        assert!(!latch.is_owner(), "main thread should not be owner");
        let r = latch.try_acquire();
        assert!(
            r.is_none(),
            "try_acquire should fail while other thread holds it"
        );
        assert!(latch.is_locked());

        // Signal thread to release.
        released.store(true, std::sync::atomic::Ordering::SeqCst);
        h.join().unwrap();

        // Now try_acquire should succeed.
        let g = latch.try_acquire();
        assert!(g.is_some(), "try_acquire should succeed after release");
        assert!(latch.is_locked());
        drop(g);
        assert!(!latch.is_locked());
    }

    /// A second thread blocks on `acquire` while the first holds it;
    /// after the first releases, the second is granted.
    #[test]
    fn test_wait_blocks_until_released() {
        let latch = Arc::new(ExclusiveLatch::named("noxu-wait"));
        let acquired = Arc::new(std::sync::atomic::AtomicBool::new(false));

        // Thread 1 acquires the latch.
        let g = latch.acquire().expect("acquire");
        assert!(latch.is_locked());

        let latch2 = latch.clone();
        let acquired2 = acquired.clone();
        let h = std::thread::spawn(move || {
            // This will block until thread 1 releases.
            let _g2 = latch2.acquire().expect("acquire in spawned thread");
            acquired2.store(true, std::sync::atomic::Ordering::SeqCst);
        });

        // Give thread 2 time to block.
        std::thread::sleep(std::time::Duration::from_millis(30));
        assert!(!acquired.load(std::sync::atomic::Ordering::SeqCst));

        // Release; thread 2 must wake and acquire.
        drop(g);
        h.join().unwrap();
        assert!(acquired.load(std::sync::atomic::Ordering::SeqCst));
    }

    /// N threads wait sequentially; each is granted after the previous releases.
    #[test]
    fn test_multiple_waiters_sequential_grant() {
        use std::sync::atomic::{AtomicUsize, Ordering};
        const N: usize = 5;
        let latch = Arc::new(ExclusiveLatch::named("noxu-multi-wait"));
        let order = Arc::new(AtomicUsize::new(0));

        // Main thread acquires.
        let g = latch.acquire().expect("acquire");

        let mut handles = Vec::new();
        for i in 0..N {
            let latch2 = latch.clone();
            let order2 = order.clone();
            let h = std::thread::spawn(move || {
                // Stagger entry slightly so they queue up in order.
                std::thread::sleep(std::time::Duration::from_millis(
                    5 * (i as u64 + 1),
                ));
                let _g = latch2.acquire().expect("acquire in spawned thread");
                order2.fetch_add(1, Ordering::SeqCst);
            });
            handles.push(h);
        }

        // Wait until all threads are likely blocked.
        std::thread::sleep(std::time::Duration::from_millis(80));

        // Release; threads unblock one by one.
        drop(g);
        for h in handles {
            h.join().unwrap();
        }
        assert_eq!(
            order.load(Ordering::SeqCst),
            N,
            "all waiters should have been granted"
        );
    }
}