File tree Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,17 @@ pub struct RawSpinlock {
26
26
locked : AtomicBool ,
27
27
}
28
28
29
+ impl RawSpinlock {
30
+ // Can fail to lock even if the spinlock is not locked. May be more efficient than `try_lock`
31
+ // when called in a loop.
32
+ fn try_lock_weak ( & self ) -> bool {
33
+ // The Orderings are the same as try_lock, and are still correct here.
34
+ self . locked
35
+ . compare_exchange_weak ( false , true , Ordering :: Acquire , Ordering :: Relaxed )
36
+ . is_ok ( )
37
+ }
38
+ }
39
+
29
40
unsafe impl RawMutex for RawSpinlock {
30
41
const INIT : RawSpinlock = RawSpinlock {
31
42
locked : AtomicBool :: new ( false ) ,
@@ -35,7 +46,7 @@ unsafe impl RawMutex for RawSpinlock {
35
46
type GuardMarker = GuardSend ;
36
47
37
48
fn lock ( & self ) {
38
- while !self . try_lock ( ) {
49
+ while !self . try_lock_weak ( ) {
39
50
// Wait until the lock looks unlocked before retrying
40
51
// Code from https://github.com/mvdnes/spin-rs/commit/d3e60d19adbde8c8e9d3199c7c51e51ee5a20bf6
41
52
//
You can’t perform that action at this time.
0 commit comments