-
Notifications
You must be signed in to change notification settings - Fork 47
/
latch.go
186 lines (157 loc) · 4.46 KB
/
latch.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
/*
Copyright (c) 2021 - Present. Blend Labs, Inc. All rights reserved
Use of this source code is governed by a MIT license that can be found in the LICENSE file.
*/
package async
import (
"sync"
"sync/atomic"
)
// NewLatch creates a new latch.
func NewLatch() *Latch {
l := new(Latch)
l.Reset()
return l
}
/*
Latch is a helper to coordinate goroutine lifecycles, specifically waiting for goroutines to start and end.
The lifecycle is generally as follows:
0 - stopped - goto 1
1 - starting - goto 2
2 - started - goto 3
3 - stopping - goto 0
Control flow is coordinated with chan struct{}, which acts as a semaphore but can only
alert (1) listener as it is buffered.
In order to start a `stopped` latch, you must call `.Reset()` first to initialize channels.
*/
type Latch struct {
sync.Mutex
state int32
starting chan struct{}
started chan struct{}
stopping chan struct{}
stopped chan struct{}
}
// Reset resets the latch.
func (l *Latch) Reset() {
l.Lock()
atomic.StoreInt32(&l.state, LatchStopped)
l.starting = make(chan struct{}, 1)
l.started = make(chan struct{}, 1)
l.stopping = make(chan struct{}, 1)
l.stopped = make(chan struct{}, 1)
l.Unlock()
}
// CanStart returns if the latch can start.
func (l *Latch) CanStart() bool {
return atomic.LoadInt32(&l.state) == LatchStopped
}
// CanStop returns if the latch can stop.
func (l *Latch) CanStop() bool {
return atomic.LoadInt32(&l.state) == LatchStarted
}
// IsStarting returns if the latch state is LatchStarting
func (l *Latch) IsStarting() bool {
return atomic.LoadInt32(&l.state) == LatchStarting
}
// IsStarted returns if the latch state is LatchStarted.
func (l *Latch) IsStarted() bool {
return atomic.LoadInt32(&l.state) == LatchStarted
}
// IsStopping returns if the latch state is LatchStopping.
func (l *Latch) IsStopping() bool {
return atomic.LoadInt32(&l.state) == LatchStopping
}
// IsStopped returns if the latch state is LatchStopped.
func (l *Latch) IsStopped() (isStopped bool) {
return atomic.LoadInt32(&l.state) == LatchStopped
}
// NotifyStarting returns the starting signal.
// It is used to coordinate the transition from stopped -> starting.
// There can only be (1) effective listener at a time for these events.
func (l *Latch) NotifyStarting() (notifyStarting <-chan struct{}) {
l.Lock()
notifyStarting = l.starting
l.Unlock()
return
}
// NotifyStarted returns the started signal.
// It is used to coordinate the transition from starting -> started.
// There can only be (1) effective listener at a time for these events.
func (l *Latch) NotifyStarted() (notifyStarted <-chan struct{}) {
l.Lock()
notifyStarted = l.started
l.Unlock()
return
}
// NotifyStopping returns the should stop signal.
// It is used to trigger the transition from running -> stopping -> stopped.
// There can only be (1) effective listener at a time for these events.
func (l *Latch) NotifyStopping() (notifyStopping <-chan struct{}) {
l.Lock()
notifyStopping = l.stopping
l.Unlock()
return
}
// NotifyStopped returns the stopped signal.
// It is used to coordinate the transition from stopping -> stopped.
// There can only be (1) effective listener at a time for these events.
func (l *Latch) NotifyStopped() (notifyStopped <-chan struct{}) {
l.Lock()
notifyStopped = l.stopped
l.Unlock()
return
}
// Starting signals the latch is starting.
// This is typically done before you kick off a goroutine.
func (l *Latch) Starting() {
if l.IsStarting() {
return
}
atomic.StoreInt32(&l.state, LatchStarting)
l.starting <- struct{}{}
}
// Started signals that the latch is started and has entered
// the `IsStarted` state.
func (l *Latch) Started() {
if l.IsStarted() {
return
}
atomic.StoreInt32(&l.state, LatchStarted)
l.started <- struct{}{}
}
// Stopping signals the latch to stop.
// It could also be thought of as `SignalStopping`.
func (l *Latch) Stopping() {
if l.IsStopping() {
return
}
atomic.StoreInt32(&l.state, LatchStopping)
l.stopping <- struct{}{}
}
// Stopped signals the latch has stopped.
func (l *Latch) Stopped() {
if l.IsStopped() {
return
}
atomic.StoreInt32(&l.state, LatchStopped)
l.stopped <- struct{}{}
}
// WaitStarted triggers `Starting` and waits for the `Started` signal.
func (l *Latch) WaitStarted() {
if !l.CanStart() {
return
}
started := l.NotifyStarted()
l.Starting()
<-started
}
// WaitStopped triggers `Stopping` and waits for the `Stopped` signal.
func (l *Latch) WaitStopped() {
if !l.CanStop() {
return
}
stopped := l.NotifyStopped()
l.Stopping()
<-stopped
}