Skip to content
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

Make RwLockReadGuard covariant #45

Merged
merged 4 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ impl<T: ?Sized> Mutex<T> {
pub fn get_mut(&mut self) -> &mut T {
unsafe { &mut *self.data.get() }
}

/// Unlocks the mutex directly.
///
/// # Safety
///
/// This function is intended to be used only in the case where the mutex is locked,
/// and the guard is subsequently forgotten. Calling this while you don't hold a lock
/// on the mutex will likely lead to UB.
pub(crate) unsafe fn unlock_unchecked(&self) {
// Remove the last bit and notify a waiting lock operation.
self.state.fetch_sub(1, Ordering::Release);
self.lock_ops.notify(1);
}
}
Jules-Bertholet marked this conversation as resolved.
Show resolved Hide resolved

impl<T: ?Sized> Mutex<T> {
Expand Down Expand Up @@ -562,10 +575,12 @@ impl<'a, T: ?Sized> MutexGuard<'a, T> {
}

impl<T: ?Sized> Drop for MutexGuard<'_, T> {
#[inline]
fn drop(&mut self) {
// Remove the last bit and notify a waiting lock operation.
self.0.state.fetch_sub(1, Ordering::Release);
self.0.lock_ops.notify(1);
// SAFETY: we are droppig the mutex guard, therefore unlocking the mutex.
Jules-Bertholet marked this conversation as resolved.
Show resolved Hide resolved
unsafe {
self.0.unlock_unchecked();
}
}
}

Expand Down Expand Up @@ -623,10 +638,12 @@ impl<T: ?Sized> MutexGuardArc<T> {
}

impl<T: ?Sized> Drop for MutexGuardArc<T> {
#[inline]
fn drop(&mut self) {
// Remove the last bit and notify a waiting lock operation.
self.0.state.fetch_sub(1, Ordering::Release);
self.0.lock_ops.notify(1);
// SAFETY: we are droppig the mutex guard, therefore unlocking the mutex.
unsafe {
self.0.unlock_unchecked();
}
}
}

Expand Down
Loading