File tree Expand file tree Collapse file tree 2 files changed +9
-2
lines changed
library/std/src/sync/nonpoison Expand file tree Collapse file tree 2 files changed +9
-2
lines changed Original file line number Diff line number Diff line change 3
3
//! The difference from the locks in the [`poison`] module is that the locks in this module will not
4
4
//! become poisoned when a thread panics while holding a guard.
5
5
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
+
6
12
#[ unstable( feature = "sync_nonpoison" , issue = "134645" ) ]
7
13
pub use self :: mutex:: MappedMutexGuard ;
8
14
#[ unstable( feature = "sync_nonpoison" , issue = "134645" ) ]
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ use crate::marker::PhantomData;
4
4
use crate :: mem:: ManuallyDrop ;
5
5
use crate :: ops:: { Deref , DerefMut } ;
6
6
use crate :: ptr:: NonNull ;
7
+ use crate :: sync:: nonpoison:: { TryLockResult , WouldBlock } ;
7
8
use crate :: sys:: sync as sys;
8
9
9
10
/// A mutual exclusion primitive useful for protecting shared data.
@@ -256,8 +257,8 @@ impl<T: ?Sized> Mutex<T> {
256
257
/// assert_eq!(*mutex.lock(), 10);
257
258
/// ```
258
259
#[ 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 ) } }
261
262
}
262
263
263
264
/// Consumes this mutex, returning the underlying data.
You can’t perform that action at this time.
0 commit comments