File tree 1 file changed +15
-6
lines changed
1 file changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -87,13 +87,22 @@ func (m *Mutex) Lock() {
87
87
// and use of TryLock is often a sign of a deeper problem
88
88
// in a particular use of mutexes.
89
89
func (m * Mutex ) TryLock () bool {
90
- if atomic .CompareAndSwapInt32 (& m .state , 0 , mutexLocked ) {
91
- if race .Enabled {
92
- race .Acquire (unsafe .Pointer (m ))
93
- }
94
- return true
90
+ old := m .state
91
+ if old & (mutexLocked | mutexStarving ) != 0 {
92
+ return false
93
+ }
94
+
95
+ // There may be a goroutine waiting for the mutex, but we are
96
+ // running now and can try to grab the mutex before that
97
+ // goroutine wakes up.
98
+ if ! atomic .CompareAndSwapInt32 (& m .state , old , old | mutexLocked ) {
99
+ return false
100
+ }
101
+
102
+ if race .Enabled {
103
+ race .Acquire (unsafe .Pointer (m ))
95
104
}
96
- return false
105
+ return true
97
106
}
98
107
99
108
func (m * Mutex ) lockSlow () {
You can’t perform that action at this time.
0 commit comments