From 8311c58b99aa86f4a971ea208e1fb3a9a825d566 Mon Sep 17 00:00:00 2001 From: Botahamec Date: Wed, 6 Mar 2024 22:40:20 -0500 Subject: Added some examples --- src/mutex.rs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'src/mutex.rs') diff --git a/src/mutex.rs b/src/mutex.rs index aa7ec72..fa7bf36 100644 --- a/src/mutex.rs +++ b/src/mutex.rs @@ -84,15 +84,15 @@ unsafe impl RawMutex for RawSpin { /// /// [`lock`]: `Mutex::lock` /// [`try_lock`]: `Mutex::try_lock` -pub struct Mutex { +pub struct Mutex { raw: R, value: UnsafeCell, } /// A reference to a mutex that unlocks it when dropped -pub struct MutexRef<'a, R: RawMutex, T: ?Sized + 'a>(&'a Mutex); +pub struct MutexRef<'a, T: ?Sized + 'a, R: RawMutex = RawSpin>(&'a Mutex); -impl<'a, R: RawMutex, T: ?Sized + 'a> Drop for MutexRef<'a, R, T> { +impl<'a, T: ?Sized + 'a, R: RawMutex> Drop for MutexRef<'a, T, R> { fn drop(&mut self) { // safety: this guard is being destroyed, so the data cannot be // accessed without locking again @@ -100,7 +100,7 @@ impl<'a, R: RawMutex, T: ?Sized + 'a> Drop for MutexRef<'a, R, T> { } } -impl<'a, R: RawMutex, T: ?Sized + 'a> Deref for MutexRef<'a, R, T> { +impl<'a, T: ?Sized + 'a, R: RawMutex> Deref for MutexRef<'a, T, R> { type Target = T; fn deref(&self) -> &Self::Target { @@ -111,7 +111,7 @@ impl<'a, R: RawMutex, T: ?Sized + 'a> Deref for MutexRef<'a, R, T> { } } -impl<'a, R: RawMutex, T: ?Sized + 'a> DerefMut for MutexRef<'a, R, T> { +impl<'a, T: ?Sized + 'a, R: RawMutex> DerefMut for MutexRef<'a, T, R> { fn deref_mut(&mut self) -> &mut Self::Target { // safety: this is the only type that can use `value`, and we have a // mutable reference to this type, so there cannot be any other @@ -127,12 +127,12 @@ impl<'a, R: RawMutex, T: ?Sized + 'a> DerefMut for MutexRef<'a, R, T> { /// /// [`lock`]: `Mutex::lock` /// [`try_lock`]: `Mutex::try_lock` -pub struct MutexGuard<'a, R: RawMutex, T: ?Sized + 'a> { - mutex: MutexRef<'a, R, T>, +pub struct MutexGuard<'a, T: ?Sized + 'a, R: RawMutex = RawSpin> { + mutex: MutexRef<'a, T, R>, thread_key: ThreadKey, } -impl<'a, R: RawMutex, T: ?Sized + 'a> Deref for MutexGuard<'a, R, T> { +impl<'a, T: ?Sized + 'a, R: RawMutex> Deref for MutexGuard<'a, T, R> { type Target = T; fn deref(&self) -> &Self::Target { @@ -140,16 +140,16 @@ impl<'a, R: RawMutex, T: ?Sized + 'a> Deref for MutexGuard<'a, R, T> { } } -impl<'a, R: RawMutex, T: ?Sized + 'a> DerefMut for MutexGuard<'a, R, T> { +impl<'a, T: ?Sized + 'a, R: RawMutex> DerefMut for MutexGuard<'a, T, R> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.mutex } } -impl<'a, R: RawMutex, T: ?Sized + 'a> MutexGuard<'a, R, T> { +impl<'a, T: ?Sized + 'a, R: RawMutex> MutexGuard<'a, T, R> { /// Create a guard to the given mutex. Undefined if multiple guards to the /// same mutex exist at once. - const unsafe fn new(mutex: &'a Mutex, thread_key: ThreadKey) -> Self { + const unsafe fn new(mutex: &'a Mutex, thread_key: ThreadKey) -> Self { Self { mutex: MutexRef(mutex), thread_key, @@ -157,7 +157,7 @@ impl<'a, R: RawMutex, T: ?Sized + 'a> MutexGuard<'a, R, T> { } } -impl Mutex { +impl Mutex { /// Create a new unlocked `Mutex`. /// /// # Examples @@ -175,7 +175,7 @@ impl Mutex { } } -impl Mutex { +impl Mutex { /// Block the thread until this mutex can be locked, and lock it. /// /// Upon returning, the thread is the only thread with a lock on the @@ -199,7 +199,7 @@ impl Mutex { /// let key = ThreadKey::lock().unwrap(); /// assert_eq!(*mutex.lock(key), 10); /// ``` - pub fn lock(&self, key: ThreadKey) -> MutexGuard<'_, R, T> { + pub fn lock(&self, key: ThreadKey) -> MutexGuard<'_, T, R> { self.raw.lock(); // safety: we just locked the mutex @@ -209,7 +209,7 @@ impl Mutex { /// Lock without a [`ThreadKey`]. You must own the [`ThreadKey`] as long as /// the [`MutexRef`] is alive. This may cause deadlock if called multiple /// times without unlocking first. - pub(crate) unsafe fn lock_ref(&self) -> MutexRef<'_, R, T> { + pub(crate) unsafe fn lock_ref(&self) -> MutexRef<'_, T, R> { self.raw.lock(); MutexRef(self) @@ -243,7 +243,7 @@ impl Mutex { /// let key = ThreadKey::lock().unwrap(); /// assert_eq!(*mutex.lock(key), 10); /// ``` - pub fn try_lock(&self, key: ThreadKey) -> Result, ThreadKey> { + pub fn try_lock(&self, key: ThreadKey) -> Result, ThreadKey> { if self.raw.try_lock() { // safety: we just locked the mutex Ok(unsafe { MutexGuard::new(self, key) }) @@ -254,7 +254,7 @@ impl Mutex { /// Lock without a [`ThreadKey`]. It is undefined behavior to do this without /// owning the [`ThreadKey`]. - pub(crate) unsafe fn try_lock_ref(&self) -> Option> { + pub(crate) unsafe fn try_lock_ref(&self) -> Option> { self.raw.try_lock().then_some(MutexRef(self)) } @@ -290,10 +290,10 @@ impl Mutex { /// ``` #[allow(clippy::missing_const_for_fn)] #[must_use] - pub fn unlock(guard: MutexGuard<'_, R, T>) -> ThreadKey { + pub fn unlock(guard: MutexGuard<'_, T, R>) -> ThreadKey { guard.thread_key } } -unsafe impl Send for Mutex {} -unsafe impl Sync for Mutex {} +unsafe impl Send for Mutex {} +unsafe impl Sync for Mutex {} -- cgit v1.2.3