1 pub use ::alloc::sync::Arc;
2 use core::ops::{Deref, DerefMut};
3 use core::time::Duration;
4 use core::cell::{RefCell, Ref, RefMut};
5 use super::{LockTestExt, LockHeldState};
7 pub type LockResult<Guard> = Result<Guard, ()>;
11 pub struct WaitTimeoutResult(bool);
12 impl WaitTimeoutResult {
13 pub fn timed_out(&self) -> bool { self.0 }
17 pub fn new() -> Condvar {
21 pub fn wait<'a, T>(&'a self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>> {
26 pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, _dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> {
30 pub fn wait_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, mut guard: MutexGuard<'a, T>, mut condition: F)
31 -> LockResult<MutexGuard<'a, T>> {
32 assert!(!condition(&mut *guard));
37 pub fn wait_timeout_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, mut guard: MutexGuard<'a, T>, dur: Duration, mut condition: F)
38 -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> {
39 if condition(&mut *guard) {
40 Ok((guard, WaitTimeoutResult(true)))
42 Ok((guard, WaitTimeoutResult(false)))
46 pub fn notify_all(&self) {}
49 pub struct Mutex<T: ?Sized> {
53 #[must_use = "if unused the Mutex will immediately unlock"]
54 pub struct MutexGuard<'a, T: ?Sized + 'a> {
58 impl<T: ?Sized> Deref for MutexGuard<'_, T> {
61 fn deref(&self) -> &T {
66 impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
67 fn deref_mut(&mut self) -> &mut T {
73 pub fn new(inner: T) -> Mutex<T> {
74 Mutex { inner: RefCell::new(inner) }
77 pub fn lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
78 Ok(MutexGuard { lock: self.inner.borrow_mut() })
81 pub fn try_lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
82 Ok(MutexGuard { lock: self.inner.borrow_mut() })
85 pub fn into_inner(self) -> LockResult<T> {
86 Ok(self.inner.into_inner())
90 impl<'a, T: 'a> LockTestExt<'a> for Mutex<T> {
92 fn held_by_thread(&self) -> LockHeldState {
93 if self.lock().is_err() { return LockHeldState::HeldByThread; }
94 else { return LockHeldState::NotHeldByThread; }
96 type ExclLock = MutexGuard<'a, T>;
98 fn unsafe_well_ordered_double_lock_self(&'a self) -> MutexGuard<T> { self.lock().unwrap() }
101 pub struct RwLock<T: ?Sized> {
105 pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
109 pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
113 impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
116 fn deref(&self) -> &T {
121 impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
124 fn deref(&self) -> &T {
129 impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
130 fn deref_mut(&mut self) -> &mut T {
131 self.lock.deref_mut()
136 pub fn new(inner: T) -> RwLock<T> {
137 RwLock { inner: RefCell::new(inner) }
140 pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
141 Ok(RwLockReadGuard { lock: self.inner.borrow() })
144 pub fn write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
145 Ok(RwLockWriteGuard { lock: self.inner.borrow_mut() })
148 pub fn try_write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
149 match self.inner.try_borrow_mut() {
150 Ok(lock) => Ok(RwLockWriteGuard { lock }),
156 impl<'a, T: 'a> LockTestExt<'a> for RwLock<T> {
158 fn held_by_thread(&self) -> LockHeldState {
159 if self.write().is_err() { return LockHeldState::HeldByThread; }
160 else { return LockHeldState::NotHeldByThread; }
162 type ExclLock = RwLockWriteGuard<'a, T>;
164 fn unsafe_well_ordered_double_lock_self(&'a self) -> RwLockWriteGuard<T> { self.write().unwrap() }
167 pub type FairRwLock<T> = RwLock<T>;