-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
50 lines (46 loc) · 1020 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package advsync
import (
"reflect"
"sync"
)
func unlockSafe(mutex *sync.Mutex) bool {
reflectValue := reflect.ValueOf(mutex).Elem()
state := reflectValue.FieldByName("state")
if !state.IsValid() {
state = reflectValue.FieldByName("mu").FieldByName("state")
if !state.IsValid() {
return false
}
}
vb := state.Int()&mutexLocked == mutexLocked
if !vb {
return false
}
mutex.Unlock()
return true
}
func unlockSafeRW(mutexRW *sync.RWMutex) bool {
reflectValue := reflect.ValueOf(mutexRW).Elem().FieldByName("w")
state := reflectValue.FieldByName("state")
if !state.IsValid() {
state = reflectValue.FieldByName("mu").FieldByName("state")
if !state.IsValid() {
return false
}
}
vb := state.Int()&mutexLocked == mutexLocked
if !vb {
return false
}
mutexRW.Unlock()
return true
}
func rUnlockSafeRW(mutexRW *sync.RWMutex) bool {
state := reflect.ValueOf(mutexRW).Elem().FieldByName("readerCount")
vb := state.Int() > 0
if !vb {
return false
}
mutexRW.RUnlock()
return true
}