Skip to content

Commit

Permalink
Provide OwnedMutexGuard::mutex()
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 c647ff6 commit 02b1274
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tokio/src/sync/mutex.rs
Expand Up @@ -621,6 +621,34 @@ impl<T: ?Sized + fmt::Display> fmt::Display for MutexGuard<'_, T> {

// === impl OwnedMutexGuard ===

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

impl<T: ?Sized> Drop for OwnedMutexGuard<T> {
fn drop(&mut self) {
self.lock.s.release(1)
Expand Down

0 comments on commit 02b1274

Please sign in to comment.