-
Notifications
You must be signed in to change notification settings - Fork 9
/
mutex.go
171 lines (150 loc) · 3.05 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package sync
import (
"context"
"sync"
"time"
)
// Set the behavier on unlock unlocked mutex
var PanicOnBug = true
// another mutex implementation with TryLock method
type Mutex interface {
Lock()
UnLock()
// TryLock return true if it fetch mutex
TryLock() bool
// TryLockTimeout return true if it fetch mutex, return false if timeout
TryLockTimeout(timeout time.Duration) bool
// TryLockTimeout return true if it fetch mutex, return false if context done
TryLockContext(ctx context.Context) bool
}
func NewMutex() Mutex {
m := &mutex{ch: make(chan struct{}, 1)}
m.ch <- struct{}{}
return m
}
type mutex struct {
ch chan struct{}
}
func (m *mutex) Lock() {
<-m.ch
}
func (m *mutex) UnLock() {
select {
case m.ch <- struct{}{}:
default:
if PanicOnBug {
panic("unlock of unlocked mutex")
}
}
}
func (m *mutex) TryLock() bool {
select {
case <-m.ch:
return true
default:
}
return false
}
func (m *mutex) TryLockTimeout(timeout time.Duration) bool {
tm := time.NewTimer(timeout)
select {
case <-m.ch:
tm.Stop()
return true
case <-tm.C:
}
return false
}
func (m *mutex) TryLockContext(ctx context.Context) bool {
select {
case <-m.ch:
return true
case <-ctx.Done():
}
return false
}
type MutexGroup interface {
Lock(i interface{})
UnLock(i interface{})
UnLockAndFree(i interface{})
// TryLock return true if it fetch mutex
TryLock(i interface{}) bool
// TryLockTimeout return true if it fetch mutex, return false if timeout
TryLockTimeout(i interface{}, timeout time.Duration) bool
// TryLockTimeout return true if it fetch mutex, return false if context done
TryLockContext(i interface{}, ctx context.Context) bool
}
func NewMutexGroup() MutexGroup {
return &mutexGroup{group: make(map[interface{}]*entry)}
}
type mutexGroup struct {
mu sync.Mutex
group map[interface{}]*entry
}
type entry struct {
ref int
mu Mutex
}
func (m *mutexGroup) get(i interface{}, ref int) Mutex {
m.mu.Lock()
defer m.mu.Unlock()
en, ok := m.group[i]
if !ok {
if ref > 0 {
en = &entry{mu: NewMutex()}
m.group[i] = en
} else if PanicOnBug {
panic("unlock of unlocked mutex")
} else {
return nil
}
}
en.ref += ref
return en.mu
}
func (m *mutexGroup) Lock(i interface{}) {
m.get(i, 1).Lock()
}
func (m *mutexGroup) UnLock(i interface{}) {
mu := m.get(i, -1)
if mu != nil {
mu.UnLock()
}
}
func (m *mutexGroup) UnLockAndFree(i interface{}) {
m.mu.Lock()
defer m.mu.Unlock()
en, ok := m.group[i]
if !ok {
if PanicOnBug {
panic("unlock of unlocked mutex")
}
return
}
en.ref--
if en.ref == 0 {
delete(m.group, i)
}
en.mu.UnLock()
}
func (m *mutexGroup) TryLock(i interface{}) bool {
locked := m.get(i, 1).TryLock()
if !locked {
m.get(i, -1)
}
return locked
}
func (m *mutexGroup) TryLockTimeout(i interface{}, timeout time.Duration) bool {
locked := m.get(i, 1).TryLockTimeout(timeout)
if !locked {
m.get(i, -1)
}
return locked
}
func (m *mutexGroup) TryLockContext(i interface{}, ctx context.Context) bool {
locked := m.get(i, 1).TryLockContext(ctx)
if !locked {
m.get(i, -1)
}
return locked
}