From 561ecd1c70cee6f4f8506fd668ea7a28843dfd56 Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Fri, 10 Jul 2020 18:36:18 +0200 Subject: [PATCH] Only use one guard type --- tokio/src/sync/mod.rs | 2 +- tokio/src/sync/rwlock.rs | 408 ++++----------------------------------- 2 files changed, 42 insertions(+), 368 deletions(-) diff --git a/tokio/src/sync/mod.rs b/tokio/src/sync/mod.rs index c5568f9ccdc..3d96106d2df 100644 --- a/tokio/src/sync/mod.rs +++ b/tokio/src/sync/mod.rs @@ -455,7 +455,7 @@ cfg_sync! { pub use semaphore::{Semaphore, SemaphorePermit, OwnedSemaphorePermit}; mod rwlock; - pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard, MappedRwLockReadGuard, MappedRwLockWriteGuard}; + pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}; mod task; pub(crate) use task::AtomicWaker; diff --git a/tokio/src/sync/rwlock.rs b/tokio/src/sync/rwlock.rs index 349b75c727d..c4b044b9325 100644 --- a/tokio/src/sync/rwlock.rs +++ b/tokio/src/sync/rwlock.rs @@ -79,335 +79,6 @@ pub struct RwLock { c: UnsafeCell, } -/// An RAII read lock guard returned by [`RwLockReadGuard::map`] or -/// [`MappedRwLockReadGuard::map`], which can point to a subfield of the -/// protected data. -/// -/// [`RwLockReadGuard::map`]: method@RwLockReadGuard::map -/// [`MappedRwLockReadGuard::map`]: method@MappedRwLockReadGuard::map -pub struct MappedRwLockReadGuard<'a, T: ?Sized> { - s: &'a Semaphore, - data: *const T, - marker: marker::PhantomData<&'a T>, -} - -impl<'a, T: ?Sized> MappedRwLockReadGuard<'a, T> { - /// Make a new `MappedRwLockReadGuard` for a component of the locked data. - /// - /// This operation cannot fail as the `MappedRwLockReadGuard` passed in - /// already locked the data. - /// - /// This is an associated function that needs to be - /// used as `MappedRwLockReadGuard::map(...)`. A method would interfere with - /// methods of the same name on the contents of the locked data. - /// - /// This is an asynchronous version of [`MappedRwLockReadGuard::map`] from the - /// [parking_lot crate]. - /// - /// [`MappedRwLockReadGuard::map`]: https://docs.rs/lock_api/latest/lock_api/struct.MappedRwLockReadGuard.html#method.map - /// [parking_lot crate]: https://crates.io/crates/parking_lot - /// - /// # Examples - /// - /// ``` - /// use tokio::sync::{RwLock, RwLockReadGuard, MappedRwLockReadGuard}; - /// - /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] - /// struct Bar(u32); - /// - /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] - /// struct Foo(Bar); - /// - /// # #[tokio::main] - /// # async fn main() { - /// let lock = RwLock::new(Foo(Bar(1))); - /// - /// let guard = lock.read().await; - /// let guard = RwLockReadGuard::map(guard, |f| &f.0); - /// let guard = MappedRwLockReadGuard::map(guard, |f| &f.0); - /// - /// assert_eq!(1, *guard); - /// # } - /// ``` - #[inline] - pub fn map(this: Self, f: F) -> MappedRwLockReadGuard<'a, U> - where - F: FnOnce(&T) -> &U, - { - let data = f(unsafe { &*this.data }); - let s = this.s; - // NB: Forget to avoid drop impl from being called. - mem::forget(this); - MappedRwLockReadGuard { - s, - data, - marker: marker::PhantomData, - } - } - - /// Attempts to make a new [`MappedRwLockReadGuard`] for a component of the - /// locked data. The original guard is returned if the closure returns - /// `None`. - /// - /// This operation cannot fail as the `MappedRwLockReadGuard` passed in - /// already locked the data. - /// - /// This is an associated function that needs to be used as - /// `MappedRwLockReadGuard::map(..)`. A method would interfere with methods - /// of the same name on the contents of the locked data. - /// - /// This is an asynchronous version of [`MappedRwLockReadGuard::try_map`] - /// from the [parking_lot crate]. - /// - /// [`MappedRwLockReadGuard::try_map`]: https://docs.rs/lock_api/latest/lock_api/struct.MappedRwLockReadGuard.html#method.try_map - /// [parking_lot crate]: https://crates.io/crates/parking_lot - /// - /// # Examples - /// - /// ``` - /// use tokio::sync::{RwLock, RwLockReadGuard, MappedRwLockReadGuard}; - /// - /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] - /// struct Bar(u32); - /// - /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] - /// struct Foo(Bar); - /// - /// # #[tokio::main] - /// # async fn main() { - /// let lock = RwLock::new(Foo(Bar(1))); - /// - /// let guard = lock.read().await; - /// let guard = RwLockReadGuard::try_map(guard, |f| Some(&f.0)).expect("should not fail"); - /// let guard = MappedRwLockReadGuard::try_map(guard, |f| Some(&f.0)).expect("should not fail"); - /// - /// assert_eq!(1, *guard); - /// # } - /// ``` - #[inline] - pub fn try_map(this: Self, f: F) -> Result, Self> - where - F: FnOnce(&T) -> Option<&U>, - { - let data = match f(unsafe { &*this.data }) { - Some(data) => data, - None => return Err(this), - }; - let s = this.s; - // NB: Forget to avoid drop impl from being called. - mem::forget(this); - Ok(MappedRwLockReadGuard { - s, - data, - marker: marker::PhantomData, - }) - } -} - -impl<'a, T: ?Sized> ops::Deref for MappedRwLockReadGuard<'a, T> { - type Target = T; - - #[inline] - fn deref(&self) -> &T { - unsafe { &*self.data } - } -} - -impl<'a, T: ?Sized> fmt::Debug for MappedRwLockReadGuard<'a, T> -where - T: fmt::Debug, -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&**self, f) - } -} - -impl<'a, T: ?Sized> fmt::Display for MappedRwLockReadGuard<'a, T> -where - T: fmt::Display, -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(&**self, f) - } -} - -impl<'a, T: ?Sized> Drop for MappedRwLockReadGuard<'a, T> { - fn drop(&mut self) { - self.s.release(1); - } -} - -/// An RAII write lock guard returned by [`RwLockWriteGuard::map`] or -/// [`MappedRwLockWriteGuard::map`], which can point to a subfield of the -/// protected data. -/// -/// [`RwLockWriteGuard::map`]: method@RwLockWriteGuard::map -/// [`MappedRwLockWriteGuard::map`]: method@MappedRwLockWriteGuard::map -pub struct MappedRwLockWriteGuard<'a, T: ?Sized> { - s: &'a Semaphore, - data: *mut T, - marker: marker::PhantomData<&'a mut T>, -} - -impl<'a, T> MappedRwLockWriteGuard<'a, T> { - /// Make a new `MappedRwLockWriteGuard` for a component of the locked data. - /// - /// This operation cannot fail as the `MappedRwLockWriteGuard` passed in - /// already locked the data. - /// - /// This is an associated function that needs to be - /// used as `MappedRwLockWriteGuard::map(...)`. A method would interfere with - /// methods of the same name on the contents of the locked data. - /// - /// This is an asynchronous version of [`MappedRwLockWriteGuard::map`] from the - /// [parking_lot crate]. - /// - /// [`MappedRwLockWriteGuard::map`]: https://docs.rs/lock_api/latest/lock_api/struct.MappedRwLockWriteGuard.html#method.map - /// [parking_lot crate]: https://crates.io/crates/parking_lot - /// - /// # Examples - /// - /// ``` - /// use tokio::sync::{RwLock, RwLockWriteGuard, MappedRwLockWriteGuard}; - /// - /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] - /// struct Bar(u32); - /// - /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] - /// struct Foo(Bar); - /// - /// # #[tokio::main] - /// # async fn main() { - /// let lock = RwLock::new(Foo(Bar(1))); - /// - /// { - /// let guard = lock.write().await; - /// let guard = RwLockWriteGuard::map(guard, |f| &mut f.0); - /// let mut guard = MappedRwLockWriteGuard::map(guard, |f| &mut f.0); - /// *guard = 2; - /// } - /// - /// assert_eq!(Foo(Bar(2)), *lock.read().await); - /// # } - /// ``` - #[inline] - pub fn map(this: Self, f: F) -> MappedRwLockWriteGuard<'a, U> - where - F: FnOnce(&mut T) -> &mut U, - { - let data = f(unsafe { &mut *this.data }); - let s = this.s; - // NB: Forget to avoid drop impl from being called. - mem::forget(this); - MappedRwLockWriteGuard { - s, - data, - marker: marker::PhantomData, - } - } - - /// Attempts to make a new [`MappedRwLockWriteGuard`] for a component of the - /// locked data. The original guard is returned if the closure returns - /// `None`. - /// - /// This operation cannot fail as the `MappedRwLockWriteGuard` passed in - /// already locked the data. - /// - /// This is an associated function that needs to be used as - /// `MappedRwLockWriteGuard::map(..)`. A method would interfere with methods - /// of the same name on the contents of the locked data. - /// - /// This is an asynchronous version of [`MappedRwLockWriteGuard::try_map`] - /// from the [parking_lot crate]. - /// - /// [`MappedRwLockWriteGuard::try_map`]: https://docs.rs/lock_api/latest/lock_api/struct.MappedRwLockWriteGuard.html#method.try_map - /// [parking_lot crate]: https://crates.io/crates/parking_lot - /// - /// # Examples - /// - /// ``` - /// use tokio::sync::{RwLock, RwLockWriteGuard, MappedRwLockWriteGuard}; - /// - /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] - /// struct Bar(u32); - /// - /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] - /// struct Foo(Bar); - /// - /// # #[tokio::main] - /// # async fn main() { - /// let lock = RwLock::new(Foo(Bar(1))); - /// - /// { - /// let guard = lock.write().await; - /// let guard = RwLockWriteGuard::try_map(guard, |f| Some(&mut f.0)).expect("should not fail"); - /// let mut guard = MappedRwLockWriteGuard::try_map(guard, |f| Some(&mut f.0)).expect("should not fail"); - /// *guard = 2; - /// } - /// - /// assert_eq!(Foo(Bar(2)), *lock.read().await); - /// # } - /// ``` - #[inline] - pub fn try_map(this: Self, f: F) -> Result, Self> - where - F: FnOnce(&mut T) -> Option<&mut U>, - { - let data = match f(unsafe { &mut *this.data }) { - Some(data) => data, - None => return Err(this), - }; - let s = this.s; - // NB: Forget to avoid drop impl from being called. - mem::forget(this); - Ok(MappedRwLockWriteGuard { - s, - data, - marker: marker::PhantomData, - }) - } -} - -impl<'a, T> ops::Deref for MappedRwLockWriteGuard<'a, T> { - type Target = T; - - #[inline] - fn deref(&self) -> &T { - unsafe { &*self.data } - } -} - -impl<'a, T> ops::DerefMut for MappedRwLockWriteGuard<'a, T> { - #[inline] - fn deref_mut(&mut self) -> &mut T { - unsafe { &mut *self.data } - } -} - -impl<'a, T> fmt::Debug for MappedRwLockWriteGuard<'a, T> -where - T: fmt::Debug, -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&**self, f) - } -} - -impl<'a, T> fmt::Display for MappedRwLockWriteGuard<'a, T> -where - T: fmt::Display, -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(&**self, f) - } -} - -impl<'a, T: ?Sized> Drop for MappedRwLockWriteGuard<'a, T> { - fn drop(&mut self) { - self.s.release(MAX_READS); - } -} - /// RAII structure used to release the shared read access of a lock when /// dropped. /// @@ -417,11 +88,13 @@ impl<'a, T: ?Sized> Drop for MappedRwLockWriteGuard<'a, T> { /// [`read`]: method@RwLock::read /// [`RwLock`]: struct@RwLock pub struct RwLockReadGuard<'a, T: ?Sized> { - lock: &'a RwLock, + s: &'a Semaphore, + data: *const T, + marker: marker::PhantomData<&'a T>, } impl<'a, T> RwLockReadGuard<'a, T> { - /// Make a new `MappedRwLockReadGuard` for a component of the locked data. + /// Make a new `RwLockReadGuard` for a component of the locked data. /// /// This operation cannot fail as the `RwLockReadGuard` passed in already /// locked the data. @@ -455,22 +128,22 @@ impl<'a, T> RwLockReadGuard<'a, T> { /// # } /// ``` #[inline] - pub fn map(this: Self, f: F) -> MappedRwLockReadGuard<'a, U> + pub fn map(this: Self, f: F) -> RwLockReadGuard<'a, U> where F: FnOnce(&T) -> &U, { let data = f(&*this) as *const U; - let s = &this.lock.s; + let s = this.s; // NB: Forget to avoid drop impl from being called. mem::forget(this); - MappedRwLockReadGuard { + RwLockReadGuard { s, data, marker: marker::PhantomData, } } - /// Attempts to make a new [`MappedRwLockReadGuard`] for a component of the + /// Attempts to make a new [`RwLockReadGuard`] for a component of the /// locked data. The original guard is returned if the closure returns /// `None`. /// @@ -506,7 +179,7 @@ impl<'a, T> RwLockReadGuard<'a, T> { /// # } /// ``` #[inline] - pub fn try_map(this: Self, f: F) -> Result, Self> + pub fn try_map(this: Self, f: F) -> Result, Self> where F: FnOnce(&T) -> Option<&U>, { @@ -514,10 +187,10 @@ impl<'a, T> RwLockReadGuard<'a, T> { Some(data) => data as *const U, None => return Err(this), }; - let s = &this.lock.s; + let s = this.s; // NB: Forget to avoid drop impl from being called. mem::forget(this); - Ok(MappedRwLockReadGuard { + Ok(RwLockReadGuard { s, data, marker: marker::PhantomData, @@ -545,7 +218,7 @@ where impl<'a, T: ?Sized> Drop for RwLockReadGuard<'a, T> { fn drop(&mut self) { - self.lock.s.release(1); + self.s.release(1); } } @@ -558,11 +231,13 @@ impl<'a, T: ?Sized> Drop for RwLockReadGuard<'a, T> { /// [`write`]: method@RwLock::write /// [`RwLock`]: struct@RwLock pub struct RwLockWriteGuard<'a, T: ?Sized> { - lock: &'a RwLock, + s: &'a Semaphore, + data: *mut T, + marker: marker::PhantomData<&'a mut T>, } impl<'a, T: ?Sized> RwLockWriteGuard<'a, T> { - /// Make a new `MappedRwLockWriteGuard` for a component of the locked data. + /// Make a new `RwLockWriteGuard` for a component of the locked data. /// /// This operation cannot fail as the `RwLockWriteGuard` passed in already /// locked the data. @@ -598,22 +273,22 @@ impl<'a, T: ?Sized> RwLockWriteGuard<'a, T> { /// # } /// ``` #[inline] - pub fn map(mut this: Self, f: F) -> MappedRwLockWriteGuard<'a, U> + pub fn map(mut this: Self, f: F) -> RwLockWriteGuard<'a, U> where F: FnOnce(&mut T) -> &mut U, { let data = f(&mut *this) as *mut U; - let s = &this.lock.s; + let s = this.s; // NB: Forget to avoid drop impl from being called. mem::forget(this); - MappedRwLockWriteGuard { + RwLockWriteGuard { s, data, marker: marker::PhantomData, } } - /// Attempts to make a new [`MappedRwLockWriteGuard`] for a component of + /// Attempts to make a new [`RwLockWriteGuard`] for a component of /// the locked data. The original guard is returned if the closure returns /// `None`. /// @@ -652,10 +327,7 @@ impl<'a, T: ?Sized> RwLockWriteGuard<'a, T> { /// # } /// ``` #[inline] - pub fn try_map( - mut this: Self, - f: F, - ) -> Result, Self> + pub fn try_map(mut this: Self, f: F) -> Result, Self> where F: FnOnce(&mut T) -> Option<&mut U>, { @@ -663,10 +335,10 @@ impl<'a, T: ?Sized> RwLockWriteGuard<'a, T> { Some(data) => data as *mut U, None => return Err(this), }; - let s = &this.lock.s; + let s = this.s; // NB: Forget to avoid drop impl from being called. mem::forget(this); - Ok(MappedRwLockWriteGuard { + Ok(RwLockWriteGuard { s, data, marker: marker::PhantomData, @@ -694,7 +366,7 @@ where impl<'a, T: ?Sized> Drop for RwLockWriteGuard<'a, T> { fn drop(&mut self) { - self.lock.s.release(MAX_READS); + self.s.release(MAX_READS); } } @@ -719,12 +391,6 @@ fn bounds() { check_sync::>(); check_unpin::>(); - check_send::>(); - check_unpin::>(); - - check_send::>(); - check_unpin::>(); - let rwlock = RwLock::new(0); check_send_sync_val(rwlock.read()); check_send_sync_val(rwlock.write()); @@ -735,17 +401,17 @@ fn bounds() { // RwLock. unsafe impl Send for RwLock where T: ?Sized + Send {} unsafe impl Sync for RwLock where T: ?Sized + Send + Sync {} -unsafe impl Sync for RwLockReadGuard<'_, T> where T: ?Sized + Send + Sync {} -unsafe impl Sync for RwLockWriteGuard<'_, T> where T: ?Sized + Send + Sync {} // NB: These impls need to be explicit since we're storing a raw pointer. // Safety: Stores a raw pointer to `T`, so if `T` is `Sync`, the lock guard over // `T` is `Send`. -unsafe impl<'a, T> Send for MappedRwLockReadGuard<'a, T> where T: ?Sized + Sync {} +unsafe impl Send for RwLockReadGuard<'_, T> where T: ?Sized + Sync {} +unsafe impl Sync for RwLockReadGuard<'_, T> where T: ?Sized + Send + Sync {} +unsafe impl Sync for RwLockWriteGuard<'_, T> where T: ?Sized + Send + Sync {} // Safety: Stores a raw pointer to `T`, so if `T` is `Sync`, the lock guard over // `T` is `Send` - but since this is also provides mutable access, we need to // make sure that `T` is `Send` since its value can be sent across thread // boundaries. -unsafe impl<'a, T> Send for MappedRwLockWriteGuard<'a, T> where T: ?Sized + Send + Sync {} +unsafe impl Send for RwLockWriteGuard<'_, T> where T: ?Sized + Send + Sync {} impl RwLock { /// Creates a new instance of an `RwLock` which is unlocked. @@ -804,7 +470,11 @@ impl RwLock { // handle to it through the Arc, which means that this can never happen. unreachable!() }); - RwLockReadGuard { lock: self } + RwLockReadGuard { + s: &self.s, + data: self.c.get(), + marker: marker::PhantomData, + } } /// Locks this rwlock with exclusive write access, causing the current task @@ -835,7 +505,11 @@ impl RwLock { // handle to it through the Arc, which means that this can never happen. unreachable!() }); - RwLockWriteGuard { lock: self } + RwLockWriteGuard { + s: &self.s, + data: self.c.get(), + marker: marker::PhantomData, + } } /// Consumes the lock, returning the underlying data. @@ -851,7 +525,7 @@ impl ops::Deref for RwLockReadGuard<'_, T> { type Target = T; fn deref(&self) -> &T { - unsafe { &*self.lock.c.get() } + unsafe { &*self.data } } } @@ -859,13 +533,13 @@ impl ops::Deref for RwLockWriteGuard<'_, T> { type Target = T; fn deref(&self) -> &T { - unsafe { &*self.lock.c.get() } + unsafe { &*self.data } } } impl ops::DerefMut for RwLockWriteGuard<'_, T> { fn deref_mut(&mut self) -> &mut T { - unsafe { &mut *self.lock.c.get() } + unsafe { &mut *self.data } } }