talc/
locking.rs

1//! Note this only contains [`AssumeUnlockable`] which is not generally recommended.
2//! Use of the `spin` crate's mutex with [`Talck`](crate::Talc) is a good default.
3
4/// #### WARNING: [`AssumeUnlockable`] may cause undefined behaviour without `unsafe` code!
5///
6/// A dummy [`RawMutex`](lock_api::RawMutex) implementation to skip synchronization on single threaded systems.
7///
8/// # Safety
9/// [`AssumeUnlockable`] is highly unsafe and may cause undefined behaviour if multiple
10/// threads enter a critical section it guards, even without explicit unsafe code.
11///
12/// Note that uncontended spin locks are cheap. Usage is only recommended on
13/// platforms that don't have atomics or are exclusively single threaded.
14///
15/// Through no fault of its own, `lock_api`'s API does not allow for safe
16/// encapsulation of this functionality. This is a hack for backwards compatibility.
17pub struct AssumeUnlockable;
18
19// SAFETY: nope
20unsafe impl lock_api::RawMutex for AssumeUnlockable {
21    const INIT: AssumeUnlockable = AssumeUnlockable;
22
23    // A spinlock guard can be sent to another thread and unlocked there
24    type GuardMarker = lock_api::GuardSend;
25
26    fn lock(&self) {}
27
28    fn try_lock(&self) -> bool {
29        true
30    }
31
32    unsafe fn unlock(&self) {}
33}