Skip to content

Commit

Permalink
Provide RwLock{Read,Write}Guard::rwlock()
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
  • Loading branch information
ijackson committed Jul 5, 2021
1 parent 02b1274 commit c3af44d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tokio/src/sync/rwlock/owned_read_guard.rs
Expand Up @@ -113,6 +113,32 @@ 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
26 changes: 26 additions & 0 deletions tokio/src/sync/rwlock/owned_write_guard.rs
Expand Up @@ -192,6 +192,32 @@ 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

0 comments on commit c3af44d

Please sign in to comment.