|
| 1 | +//! This crate contains Shuttle's internal implementations of the `parking_lot` crate. |
| 2 | +//! Do not depend on this crate directly. Use the `shuttle-parking_lot` crate, which conditionally |
| 3 | +//! exposes these implementations with the `shuttle` feature or the original crate without it. |
| 4 | +//! |
| 5 | +//! [`Shuttle`]: <https://crates.io/crates/shuttle> |
| 6 | +//! |
| 7 | +//! [`parking_lot`]: <https://crates.io/crates/parking_lot> |
| 8 | +
|
| 9 | +use shuttle::sync; |
| 10 | + |
| 11 | +// TODO: All the `expect`s below are wrong — it is possible to poison a Shuttle mutex. |
| 12 | +// `clear_poison` should be added and used to avoid the `expect`. |
| 13 | +// TODO: Shuttle should be split up into a "core" crate which offers a more versatile |
| 14 | +// Mutex which is parametric on poisoning (and this crate would initialize it as non-poisonable) |
| 15 | + |
| 16 | +#[derive(Default, Debug)] |
| 17 | +pub struct Mutex<T: ?Sized> { |
| 18 | + inner: sync::Mutex<T>, |
| 19 | +} |
| 20 | + |
| 21 | +pub use sync::{MutexGuard, RwLockReadGuard, RwLockWriteGuard}; |
| 22 | + |
| 23 | +impl<T> Mutex<T> { |
| 24 | + /// Creates a new mutex in an unlocked state ready for use. |
| 25 | + pub fn new(value: T) -> Self { |
| 26 | + Self { |
| 27 | + inner: sync::Mutex::new(value), |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /// Consumes this mutex, returning the underlying data. |
| 32 | + #[inline] |
| 33 | + pub fn into_inner(self) -> T { |
| 34 | + self.inner |
| 35 | + .into_inner() |
| 36 | + .expect("This shouldn't happen as there is no way to poision a Shuttle Mutex.") |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl<T: ?Sized> Mutex<T> { |
| 41 | + /// Acquires a mutex, blocking the current thread until it is able to do so. |
| 42 | + /// |
| 43 | + /// This function will block the local thread until it is available to acquire |
| 44 | + /// the mutex. Upon returning, the thread is the only thread with the mutex |
| 45 | + /// held. An RAII guard is returned to allow scoped unlock of the lock. When |
| 46 | + /// the guard goes out of scope, the mutex will be unlocked. |
| 47 | + /// |
| 48 | + /// Attempts to lock a mutex in the thread which already holds the lock will |
| 49 | + /// result in a deadlock. |
| 50 | + #[inline] |
| 51 | + pub fn lock(&self) -> sync::MutexGuard<'_, T> { |
| 52 | + self.inner |
| 53 | + .lock() |
| 54 | + .expect("This shouldn't happen as there is no way to poision a Shuttle Mutex.") |
| 55 | + } |
| 56 | + |
| 57 | + /// Attempts to acquire this lock. |
| 58 | + /// |
| 59 | + /// If the lock could not be acquired at this time, then `None` is returned. |
| 60 | + /// Otherwise, an RAII guard is returned. The lock will be unlocked when the |
| 61 | + /// guard is dropped. |
| 62 | + #[inline] |
| 63 | + pub fn try_lock(&self) -> Option<sync::MutexGuard<'_, T>> { |
| 64 | + self.inner.try_lock().ok() |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// A reader-writer lock |
| 69 | +#[derive(Debug, Default)] |
| 70 | +pub struct RwLock<T: ?Sized> { |
| 71 | + inner: sync::RwLock<T>, |
| 72 | +} |
| 73 | + |
| 74 | +impl<T> RwLock<T> { |
| 75 | + /// Creates a new instance of an `RwLock<T>` which is unlocked. |
| 76 | + pub fn new(value: T) -> Self { |
| 77 | + Self { |
| 78 | + inner: sync::RwLock::new(value), |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /// Consumes this `RwLock`, returning the underlying data. |
| 83 | + #[inline] |
| 84 | + pub fn into_inner(self) -> T { |
| 85 | + self.inner |
| 86 | + .into_inner() |
| 87 | + .expect("This shouldn't happen as there is no way to poision a Shuttle RwLock.") |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl<T: ?Sized> RwLock<T> { |
| 92 | + /// Locks this `RwLock` with shared read access, blocking the current thread |
| 93 | + /// until it can be acquired. |
| 94 | + /// |
| 95 | + /// The calling thread will be blocked until there are no more writers which |
| 96 | + /// hold the lock. There may be other readers currently inside the lock when |
| 97 | + /// this method returns. |
| 98 | + /// |
| 99 | + /// Note that attempts to recursively acquire a read lock on a `RwLock` when |
| 100 | + /// the current thread already holds one may result in a deadlock. |
| 101 | + /// |
| 102 | + /// Returns an RAII guard which will release this thread's shared access |
| 103 | + /// once it is dropped. |
| 104 | + #[inline] |
| 105 | + pub fn read(&self) -> RwLockReadGuard<'_, T> { |
| 106 | + self.inner |
| 107 | + .read() |
| 108 | + .expect("This shouldn't happen as there is no way to poision a Shuttle RwLock.") |
| 109 | + } |
| 110 | + |
| 111 | + /// Attempts to acquire this `RwLock` with shared read access. |
| 112 | + /// |
| 113 | + /// If the access could not be granted at this time, then `None` is returned. |
| 114 | + /// Otherwise, an RAII guard is returned which will release the shared access |
| 115 | + /// when it is dropped. |
| 116 | + /// |
| 117 | + /// This function does not block. |
| 118 | + #[inline] |
| 119 | + pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>> { |
| 120 | + self.inner.try_read().ok() |
| 121 | + } |
| 122 | + |
| 123 | + /// Locks this `RwLock` with exclusive write access, blocking the current |
| 124 | + /// thread until it can be acquired. |
| 125 | + /// |
| 126 | + /// This function will not return while other writers or other readers |
| 127 | + /// currently have access to the lock. |
| 128 | + /// |
| 129 | + /// Returns an RAII guard which will drop the write access of this `RwLock` |
| 130 | + /// when dropped. |
| 131 | + #[inline] |
| 132 | + pub fn write(&self) -> RwLockWriteGuard<'_, T> { |
| 133 | + self.inner |
| 134 | + .write() |
| 135 | + .expect("This shouldn't happen as there is no way to poision a Shuttle RwLock.") |
| 136 | + } |
| 137 | + |
| 138 | + /// Attempts to lock this `RwLock` with exclusive write access. |
| 139 | + /// |
| 140 | + /// If the lock could not be acquired at this time, then `None` is returned. |
| 141 | + /// Otherwise, an RAII guard is returned which will release the lock when |
| 142 | + /// it is dropped. |
| 143 | + /// |
| 144 | + /// This function does not block. |
| 145 | + #[inline] |
| 146 | + pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>> { |
| 147 | + self.inner.try_write().ok() |
| 148 | + } |
| 149 | +} |
0 commit comments