Skip to content

Commit 435b42e

Browse files
committed
add TryLockResult and WouldBlock
1 parent c4f7fdf commit 435b42e

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

library/std/src/sync/nonpoison/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
//! The difference from the locks in the [`poison`] module is that the locks in this module will not
44
//! become poisoned when a thread panics while holding a guard.
55
6+
/// A type alias for the result of a nonblocking locking method.
7+
pub type TryLockResult<Guard> = Result<Guard, WouldBlock>;
8+
9+
/// A lock could not be acquired at this time because the operation would otherwise block.
10+
pub struct WouldBlock;
11+
612
#[unstable(feature = "sync_nonpoison", issue = "134645")]
713
pub use self::mutex::MappedMutexGuard;
814
#[unstable(feature = "sync_nonpoison", issue = "134645")]

library/std/src/sync/nonpoison/mutex.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::marker::PhantomData;
44
use crate::mem::ManuallyDrop;
55
use crate::ops::{Deref, DerefMut};
66
use crate::ptr::NonNull;
7+
use crate::sync::nonpoison::{TryLockResult, WouldBlock};
78
use crate::sys::sync as sys;
89

910
/// A mutual exclusion primitive useful for protecting shared data.
@@ -256,8 +257,8 @@ impl<T: ?Sized> Mutex<T> {
256257
/// assert_eq!(*mutex.lock(), 10);
257258
/// ```
258259
#[unstable(feature = "nonpoison_mutex", issue = "134645")]
259-
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
260-
unsafe { if self.inner.try_lock() { Some(MutexGuard::new(self)) } else { None } }
260+
pub fn try_lock(&self) -> TryLockResult<MutexGuard<'_, T>> {
261+
unsafe { if self.inner.try_lock() { Ok(MutexGuard::new(self)) } else { Err(WouldBlock) } }
261262
}
262263

263264
/// Consumes this mutex, returning the underlying data.

0 commit comments

Comments
 (0)