-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex.go
97 lines (77 loc) · 1.28 KB
/
mutex.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package g
import (
"sync"
)
type Mutex struct {
mutex *sync.Mutex
}
func NewMutex() *Mutex {
return &Mutex{
mutex: &sync.Mutex{},
}
}
func (s *Mutex) Lock() {
s.mutex.Lock()
}
func (s *Mutex) TryLock() bool {
return s.mutex.TryLock()
}
func (s *Mutex) Unlock() {
s.mutex.Unlock()
}
func (s *Mutex) Locker() sync.Locker {
return s.mutex
}
func (s *Mutex) WithLock(fc func()) {
if fc != nil {
s.Lock()
defer s.Unlock() // in case `call` panics
fc()
}
}
type RWMutex struct {
mutex *sync.RWMutex
}
func NewRWMutex() *RWMutex {
return &RWMutex{
mutex: &sync.RWMutex{},
}
}
func (s *RWMutex) Lock() {
s.mutex.Lock()
}
func (s *RWMutex) RLock() {
s.mutex.RLock()
}
func (s *RWMutex) RLocker() sync.Locker {
return s.mutex.RLocker()
}
func (s *RWMutex) RUnlock() {
s.mutex.RUnlock()
}
func (s *RWMutex) TryLock() bool {
return s.mutex.TryLock()
}
func (s *RWMutex) TryRLock() bool {
return s.mutex.TryRLock()
}
func (s *RWMutex) Unlock() {
s.mutex.Unlock()
}
func (s *RWMutex) Locker() sync.Locker {
return s.mutex
}
func (s *RWMutex) WithLock(fc func()) {
if fc != nil {
s.Lock()
defer s.Unlock() // in case `call` panics
fc()
}
}
func (s *RWMutex) WithRLock(fc func()) {
if fc != nil {
s.RLock()
defer s.RUnlock() // in case `call` panics
fc()
}
}