-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add OwnedRwLockReadGuard and OwnedRwLockWriteGuard #3340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
67244da
sync: add OwnedRwLockReadGuard and OwnedRwLockWriteGuard
a28a420
sync: fix typo in RwLock loom test
44b5f30
sync: add mapped variants to owned RwLock guards
b1ab3af
sync: add RwLock::try_{read, write}_owned
927c840
sync: fix RwLock doc tests
0b07251
sync: add PhantomData to owned RwLock guards
57625db
sync: fix compilation of RwLock guards
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| use crate::sync::rwlock::RwLock; | ||
| use std::fmt; | ||
| use std::ops; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Owned RAII structure used to release the shared read access of a lock when | ||
| /// dropped. | ||
| /// | ||
| /// This structure is created by the [`read_owned`] method on | ||
| /// [`RwLock`]. | ||
| /// | ||
| /// [`read_owned`]: method@crate::sync::RwLock::read_owned | ||
| /// [`RwLock`]: struct@crate::sync::RwLock | ||
| pub struct OwnedRwLockReadGuard<T: ?Sized> { | ||
| pub(super) lock: Arc<RwLock<T>>, | ||
| pub(super) data: *const T, | ||
| } | ||
|
|
||
| impl<T: ?Sized> ops::Deref for OwnedRwLockReadGuard<T> { | ||
| type Target = T; | ||
|
|
||
| fn deref(&self) -> &T { | ||
| unsafe { &*self.data } | ||
| } | ||
| } | ||
|
|
||
| impl<T: ?Sized> fmt::Debug for OwnedRwLockReadGuard<T> | ||
| where | ||
| T: fmt::Debug, | ||
| { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt::Debug::fmt(&**self, f) | ||
| } | ||
| } | ||
|
|
||
| impl<T: ?Sized> fmt::Display for OwnedRwLockReadGuard<T> | ||
| where | ||
| T: fmt::Display, | ||
| { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt::Display::fmt(&**self, f) | ||
| } | ||
| } | ||
|
|
||
| impl<T: ?Sized> Drop for OwnedRwLockReadGuard<T> { | ||
| fn drop(&mut self) { | ||
| self.lock.s.release(1); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| use crate::sync::rwlock::owned_read_guard::OwnedRwLockReadGuard; | ||
| use crate::sync::rwlock::RwLock; | ||
| use std::fmt; | ||
| use std::mem::{self, ManuallyDrop}; | ||
| use std::ops; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Owned RAII structure used to release the exclusive write access of a lock when | ||
| /// dropped. | ||
| /// | ||
| /// This structure is created by the [`write_owned`] method | ||
| /// on [`RwLock`]. | ||
| /// | ||
| /// [`write_owned`]: method@crate::sync::RwLock::write_owned | ||
| /// [`RwLock`]: struct@crate::sync::RwLock | ||
| pub struct OwnedRwLockWriteGuard<T: ?Sized> { | ||
| // ManuallyDrop allows us to destructure into this field without running the destructor. | ||
| pub(super) lock: ManuallyDrop<Arc<RwLock<T>>>, | ||
| pub(super) data: *mut T, | ||
| } | ||
|
|
||
| impl<T: ?Sized> OwnedRwLockWriteGuard<T> { | ||
| /// Atomically downgrades a write lock into a read lock without allowing | ||
| /// any writers to take exclusive access of the lock in the meantime. | ||
| /// | ||
| /// **Note:** This won't *necessarily* allow any additional readers to acquire | ||
| /// locks, since [`RwLock`] is fair and it is possible that a writer is next | ||
| /// in line. | ||
| /// | ||
| /// Returns an RAII guard which will drop this read access of the `RwLock` | ||
| /// when dropped. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// # use tokio::sync::RwLock; | ||
| /// # use std::sync::Arc; | ||
| /// # | ||
| /// # #[tokio::main] | ||
| /// # async fn main() { | ||
| /// let lock = Arc::new(RwLock::new(1)); | ||
| /// | ||
| /// let n = lock.clone().write_owned().await; | ||
| /// | ||
| /// let cloned_lock = lock.clone(); | ||
| /// let handle = tokio::spawn(async move { | ||
| /// *cloned_lock.write_owned().await = 2; | ||
| /// }); | ||
| /// | ||
| /// let n = n.downgrade(); | ||
| /// assert_eq!(*n, 1, "downgrade is atomic"); | ||
| /// | ||
| /// drop(n); | ||
| /// handle.await.unwrap(); | ||
| /// assert_eq!(*lock.read().await, 2, "second writer obtained write lock"); | ||
| /// # } | ||
| /// ``` | ||
| pub fn downgrade(mut self) -> OwnedRwLockReadGuard<T> { | ||
| let lock = unsafe { ManuallyDrop::take(&mut self.lock) }; | ||
| let data = self.data; | ||
|
|
||
| // Release all but one of the permits held by the write guard | ||
| lock.s.release(super::MAX_READS - 1); | ||
| // NB: Forget to avoid drop impl from being called. | ||
| mem::forget(self); | ||
| OwnedRwLockReadGuard { lock, data } | ||
| } | ||
| } | ||
|
|
||
| impl<T: ?Sized> ops::Deref for OwnedRwLockWriteGuard<T> { | ||
| type Target = T; | ||
|
|
||
| fn deref(&self) -> &T { | ||
| unsafe { &*self.data } | ||
| } | ||
| } | ||
|
|
||
| impl<T: ?Sized> ops::DerefMut for OwnedRwLockWriteGuard<T> { | ||
| fn deref_mut(&mut self) -> &mut T { | ||
| unsafe { &mut *self.data } | ||
| } | ||
| } | ||
|
|
||
| impl<T: ?Sized> fmt::Debug for OwnedRwLockWriteGuard<T> | ||
| where | ||
| T: fmt::Debug, | ||
| { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt::Debug::fmt(&**self, f) | ||
| } | ||
| } | ||
|
|
||
| impl<T: ?Sized> fmt::Display for OwnedRwLockWriteGuard<T> | ||
| where | ||
| T: fmt::Display, | ||
| { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt::Display::fmt(&**self, f) | ||
| } | ||
| } | ||
|
|
||
| impl<T: ?Sized> Drop for OwnedRwLockWriteGuard<T> { | ||
| fn drop(&mut self) { | ||
| self.lock.s.release(super::MAX_READS); | ||
| unsafe { ManuallyDrop::drop(&mut self.lock) }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.