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

RwLock guard lock ref recovery methods, owned guards #3930

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions tokio/src/sync/rwlock/owned_read_guard.rs
Expand Up @@ -113,6 +113,33 @@ impl<T: ?Sized, U: ?Sized> OwnedRwLockReadGuard<T, U> {
_p: PhantomData,
})
}

/// Returns a reference to the original `Arc<RwLock>`.
///
/// ```
/// use std::sync::Arc;
/// use tokio::sync::{OwnedRwLockReadGuard, RwLock};
///
/// async fn unlock_and_relock(guard: OwnedRwLockReadGuard<u32>) -> OwnedRwLockReadGuard<u32> {
/// println!("1. contains: {:?}", *guard);
/// let mutex: Arc<RwLock<u32>> = OwnedRwLockReadGuard::rwlock(&guard).clone();
/// drop(guard);
/// let guard = mutex.read_owned().await;
/// println!("2. contains: {:?}", *guard);
/// guard
/// }
/// #
/// # #[tokio::main]
/// # async fn main() {
/// # let rwlock = Arc::new(RwLock::new(0u32));
/// # let guard = rwlock.read_owned().await;
/// # unlock_and_relock(guard).await;
/// # }
/// ```
#[inline]
pub fn rwlock(this: &Self) -> &Arc<RwLock<T>> {
&this.lock
}
}

impl<T: ?Sized, U: ?Sized> ops::Deref for OwnedRwLockReadGuard<T, U> {
Expand Down
27 changes: 27 additions & 0 deletions tokio/src/sync/rwlock/owned_write_guard.rs
Expand Up @@ -192,6 +192,33 @@ impl<T: ?Sized> OwnedRwLockWriteGuard<T> {
_p: PhantomData,
}
}

/// Returns a reference to the original `Arc<RwLock>`.
///
/// ```
/// use std::sync::Arc;
/// use tokio::sync::{OwnedRwLockWriteGuard, RwLock};
///
/// async fn unlock_and_relock(guard: OwnedRwLockWriteGuard<u32>) -> OwnedRwLockWriteGuard<u32> {
/// println!("1. contains: {:?}", *guard);
/// let mutex: Arc<RwLock<u32>> = OwnedRwLockWriteGuard::rwlock(&guard).clone();
/// drop(guard);
/// let guard = mutex.write_owned().await;
/// println!("2. contains: {:?}", *guard);
/// guard
/// }
/// #
/// # #[tokio::main]
/// # async fn main() {
/// # let rwlock = Arc::new(RwLock::new(0u32));
/// # let guard = rwlock.write_owned().await;
/// # unlock_and_relock(guard).await;
/// # }
/// ```
#[inline]
pub fn rwlock(this: &Self) -> &Arc<RwLock<T>> {
&this.lock
}
}

impl<T: ?Sized> ops::Deref for OwnedRwLockWriteGuard<T> {
Expand Down