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 6, 2021
1 parent 41e44c8 commit 5aa6958
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tokio/src/sync/mutex.rs
Expand Up @@ -622,6 +622,35 @@ 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 5aa6958

Please sign in to comment.