-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_by_channel.go
186 lines (149 loc) · 2.45 KB
/
sync_by_channel.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package channelsync
import (
"sync"
)
// sync.Locker
type Locker sync.Locker
// sync.Mutex
type Mutex struct {
c chan struct{}
}
func NewMutex() *Mutex {
return &Mutex{
c: make(chan struct{}, 1),
}
}
func (cm *Mutex) Lock() {
cm.c <- struct{}{}
}
func (cm *Mutex) Unlock() {
<-cm.c
}
// sync.RWMutex
type RWMutex struct {
c chan struct{} // main mutex
rn int // number of current readers
rnc chan struct{} // mutex for rn
}
func NewRWMutex() *RWMutex {
return &RWMutex{
c: make(chan struct{}, 1),
rnc: make(chan struct{}, 1),
}
}
func (cm *RWMutex) Lock() {
cm.c <- struct{}{}
}
func (cm *RWMutex) Unlock() {
<-cm.c
}
func (cm *RWMutex) RLock() {
cm.rnc <- struct{}{}
if cm.rn == 0 {
cm.c <- struct{}{}
}
cm.rn++
<-cm.rnc
}
func (cm *RWMutex) RUnlock() {
cm.rnc <- struct{}{}
cm.rn--
if cm.rn == 0 {
<-cm.c
}
<-cm.rnc
}
func (cm *RWMutex) RLocker() Locker {
return (*rlocker)(cm)
}
type rlocker RWMutex
func (r *rlocker) Lock() { (*RWMutex)(r).RLock() }
func (r *rlocker) Unlock() { (*RWMutex)(r).RUnlock() }
// sync.Once
type Once struct {
c chan struct{}
done chan struct{}
}
func NewOnce() *Once {
return &Once{
c: make(chan struct{}, 1),
done: make(chan struct{}),
}
}
func (co *Once) Do(f func()) {
select {
case co.c <- struct{}{}:
f()
close(co.done)
case <-co.done:
}
}
// sync.WaitGroup
type WaitGroup struct {
n int64
lock chan struct{}
wait chan struct{}
}
func NewWaitGroup() *WaitGroup {
cwg := &WaitGroup{
lock: make(chan struct{}, 1),
wait: make(chan struct{}),
}
close(cwg.wait)
return cwg
}
func (cwg *WaitGroup) Add(delta int) {
cwg.lock <- struct{}{}
cwg.n += int64(delta)
if delta > 0 {
if cwg.n == int64(delta) {
cwg.wait = make(chan struct{})
}
} else if cwg.n == 0 {
if delta < 0 {
close(cwg.wait)
}
} else if cwg.n < 0 {
panic("sync: negative WaitGroup counter")
}
<-cwg.lock
}
func (cwg *WaitGroup) Done() {
cwg.Add(-1)
}
func (cwg *WaitGroup) Wait() {
cwg.lock <- struct{}{}
wait := cwg.wait
<-cwg.lock
<-wait
}
// sync.Cond
type Cond struct {
L Locker
c chan struct{}
}
func NewCond(l Locker) *Cond {
return &Cond{
L: l,
c: make(chan struct{}),
}
}
func (cc *Cond) Wait() {
cc.L.Unlock()
<-cc.c
cc.L.Lock()
}
func (cc *Cond) Signal() {
cc.c <- struct{}{}
}
func (cc *Cond) Broadcast() {
for {
select {
case cc.c <- struct{}{}:
default:
return
}
}
}
// sync.Pool (hard to reimplement it)
type Pool sync.Pool