-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Use Pin to pin RWLock. #77865
Closed
Closed
Use Pin to pin RWLock. #77865
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 |
---|---|---|
@@ -1,88 +1,81 @@ | ||
use crate::marker::PhantomPinned; | ||
use crate::pin::Pin; | ||
use crate::sys::rwlock as imp; | ||
|
||
/// An OS-based reader-writer lock. | ||
/// | ||
/// This structure is entirely unsafe and serves as the lowest layer of a | ||
/// cross-platform binding of system rwlocks. It is recommended to use the | ||
/// safer types at the top level of this crate instead of this type. | ||
pub struct RWLock(imp::RWLock); | ||
pub struct RWLock { | ||
inner: imp::RWLock, | ||
_pinned: PhantomPinned, | ||
} | ||
|
||
impl RWLock { | ||
/// Creates a new reader-writer lock for use. | ||
/// | ||
/// Behavior is undefined if the reader-writer lock is moved after it is | ||
/// first used with any of the functions below. | ||
pub const fn new() -> RWLock { | ||
RWLock(imp::RWLock::new()) | ||
RWLock { inner: imp::RWLock::new(), _pinned: PhantomPinned } | ||
} | ||
|
||
/// Acquires shared access to the underlying lock, blocking the current | ||
/// thread to do so. | ||
/// | ||
/// Behavior is undefined if the rwlock has been moved between this and any | ||
/// previous method call. | ||
#[inline] | ||
pub unsafe fn read(&self) { | ||
self.0.read() | ||
pub fn read(self: Pin<&Self>) { | ||
unsafe { self.inner.read() } | ||
} | ||
|
||
/// Attempts to acquire shared access to this lock, returning whether it | ||
/// succeeded or not. | ||
/// | ||
/// This function does not block the current thread. | ||
/// | ||
/// Behavior is undefined if the rwlock has been moved between this and any | ||
/// previous method call. | ||
#[inline] | ||
pub unsafe fn try_read(&self) -> bool { | ||
self.0.try_read() | ||
pub fn try_read(self: Pin<&Self>) -> bool { | ||
unsafe { self.inner.try_read() } | ||
} | ||
|
||
/// Acquires write access to the underlying lock, blocking the current thread | ||
/// to do so. | ||
/// | ||
/// Behavior is undefined if the rwlock has been moved between this and any | ||
/// previous method call. | ||
#[inline] | ||
pub unsafe fn write(&self) { | ||
self.0.write() | ||
pub fn write(self: Pin<&Self>) { | ||
unsafe { self.inner.write() } | ||
} | ||
|
||
/// Attempts to acquire exclusive access to this lock, returning whether it | ||
/// succeeded or not. | ||
/// | ||
/// This function does not block the current thread. | ||
/// | ||
/// Behavior is undefined if the rwlock has been moved between this and any | ||
/// previous method call. | ||
#[inline] | ||
pub unsafe fn try_write(&self) -> bool { | ||
self.0.try_write() | ||
pub fn try_write(self: Pin<&Self>) -> bool { | ||
unsafe { self.inner.try_write() } | ||
} | ||
|
||
/// Unlocks previously acquired shared access to this lock. | ||
/// | ||
/// Behavior is undefined if the current thread does not have shared access. | ||
#[inline] | ||
pub unsafe fn read_unlock(&self) { | ||
self.0.read_unlock() | ||
pub unsafe fn read_unlock(self: Pin<&Self>) { | ||
self.inner.read_unlock() | ||
} | ||
|
||
/// Unlocks previously acquired exclusive access to this lock. | ||
/// | ||
/// Behavior is undefined if the current thread does not currently have | ||
/// exclusive access. | ||
#[inline] | ||
pub unsafe fn write_unlock(&self) { | ||
self.0.write_unlock() | ||
pub unsafe fn write_unlock(self: Pin<&Self>) { | ||
self.inner.write_unlock() | ||
} | ||
} | ||
|
||
/// Destroys OS-related resources with this RWLock. | ||
/// | ||
/// Behavior is undefined if there are any currently active users of this | ||
/// lock. | ||
impl Drop for RWLock { | ||
#[inline] | ||
pub unsafe fn destroy(&self) { | ||
self.0.destroy() | ||
fn drop(&mut self) { | ||
// SAFETY: The rwlock wasn't moved since using any of its | ||
// functions, because they all require a Pin. | ||
unsafe { self.inner.destroy() } | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this changing the safety preconditions? Previously there was something about "active users", which is no longer mentioned.
This seems related to #85434. The "no active users" condition is not actually upheld by the users of this code. So it makes sense to remove this comment here, but IMO then there should be a comment one layer down -- because there
pthread_mutex_destroy
is being called and the safety precondition does not ensure all that needs to be ensured. (Basically, this moves the "active users" comment instead of removing it entirely, and acknowledges that we have a gap in our safety reasoning here.)