Skip to content

Commit

Permalink
sync: add blocking_lock to Mutex (#4130)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-rustin committed Sep 23, 2021
1 parent ea19606 commit 7ce8f05
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tokio/src/sync/mutex.rs
Expand Up @@ -301,6 +301,40 @@ impl<T: ?Sized> Mutex<T> {
MutexGuard { lock: self }
}

/// Blocking lock this mutex. When the lock has been acquired, function returns a
/// [`MutexGuard`].
///
/// This method is intended for use cases where you
/// need to use this mutex in asynchronous code as well as in synchronous code.
///
/// # Examples
///
/// ```
/// use std::sync::Arc;
/// use tokio::sync::Mutex;
///
/// #[tokio::main]
/// async fn main() {
/// let mutex = Arc::new(Mutex::new(1));
///
/// let mutex1 = Arc::clone(&mutex);
/// let sync_code = tokio::task::spawn_blocking(move || {
/// let mut n = mutex1.blocking_lock();
/// *n = 2;
/// });
///
/// sync_code.await.unwrap();
///
/// let n = mutex.lock().await;
/// assert_eq!(*n, 2);
/// }
///
/// ```
#[cfg(feature = "sync")]
pub fn blocking_lock(&self) -> MutexGuard<'_, T> {
crate::future::block_on(self.lock())
}

/// Locks this mutex, causing the current task to yield until the lock has
/// been acquired. When the lock has been acquired, this returns an
/// [`OwnedMutexGuard`].
Expand Down

0 comments on commit 7ce8f05

Please sign in to comment.