1 pub use ::alloc::sync::Arc;
2 use core::ops::{Deref, DerefMut};
3 use core::time::Duration;
4 use core::cell::{RefCell, Ref, RefMut};
6 pub type LockResult<Guard> = Result<Guard, ()>;
11 pub fn new() -> Condvar {
15 pub fn wait<'a, T>(&'a self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>> {
20 pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, _dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> {
24 pub fn notify_all(&self) {}
27 pub struct Mutex<T: ?Sized> {
31 #[must_use = "if unused the Mutex will immediately unlock"]
32 pub struct MutexGuard<'a, T: ?Sized + 'a> {
36 impl<T: ?Sized> Deref for MutexGuard<'_, T> {
39 fn deref(&self) -> &T {
44 impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
45 fn deref_mut(&mut self) -> &mut T {
51 pub fn new(inner: T) -> Mutex<T> {
52 Mutex { inner: RefCell::new(inner) }
55 pub fn lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
56 Ok(MutexGuard { lock: self.inner.borrow_mut() })
59 pub fn try_lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
60 Ok(MutexGuard { lock: self.inner.borrow_mut() })
64 pub struct RwLock<T: ?Sized> {
68 pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
72 pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
76 impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
79 fn deref(&self) -> &T {
84 impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
87 fn deref(&self) -> &T {
92 impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
93 fn deref_mut(&mut self) -> &mut T {
99 pub fn new(inner: T) -> RwLock<T> {
100 RwLock { inner: RefCell::new(inner) }
103 pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
104 Ok(RwLockReadGuard { lock: self.inner.borrow() })
107 pub fn write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
108 Ok(RwLockWriteGuard { lock: self.inner.borrow_mut() })
111 pub fn try_write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
112 // There is no try, grasshopper - only used for tests and expected to fail
117 pub type FairRwLock<T> = RwLock<T>;