-
Notifications
You must be signed in to change notification settings - Fork 0
/
wake.go
223 lines (201 loc) · 5.13 KB
/
wake.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package wake
import (
"context"
"sync"
"sync/atomic"
)
type subsignal struct {
ch chan struct{}
}
func newSubsignal() *subsignal {
s := new(subsignal)
s.ch = make(chan struct{})
return s
}
type signal struct {
subsig atomic.Pointer[subsignal]
closed atomic.Bool
closeCh chan struct{}
direct chan struct{}
waits atomic.Int64
}
// Signaller struct contains methods to wake goroutines waiting on the corresponding Receiver (created by [New]). All methods are thread safe.
type Signaller struct {
sig *signal
}
// Signal wakes n goroutines (if there are any) and reports how many goroutines were awoken.
// If n <= 0 it wakes all goroutines and reported value is 0 (same as Broadcast()).
func (s *Signaller) Signal(n int) int {
if n <= 0 {
s.Broadcast()
return 0
}
if s.IsClosed() {
return 0
}
var count int
forloop:
for n > 0 {
// Note: It's possible for the signaller to be closed, potentially waking up goroutines.
// However, having two cases in the select statement might slow down execution.
select {
case s.sig.direct <- struct{}{}:
count++
n--
default:
break forloop
}
}
return count
}
// SignalWithContext wakes n goroutines and reports how many goroutines were awoken (by this call) and ctx.Err() if context was cancelled.
// It is a blocking operation and will be finished when all n goroutines are awoken, context is cancelled or signaller was closed.
// If n <= 0 it wakes all goroutines (same as Broadcast()) regardless of context cancellation.
// Error is always nil for closed signaller or if n <= 0.
func (s *Signaller) SignalWithContext(ctx context.Context, n int) (int, error) {
if n <= 0 {
s.Broadcast()
return 0, nil
}
var count int
for n > 0 {
select {
case s.sig.direct <- struct{}{}:
count++
n--
case <-ctx.Done():
return count, ctx.Err()
case <-s.sig.closeCh:
return count, nil
}
}
return count, nil
}
// Broadcast wakes up all goroutines.
func (s *Signaller) Broadcast() {
if s.IsClosed() {
return
}
subsig := s.sig.subsig.Swap(newSubsignal())
close(subsig.ch)
}
// WaitCount reports current number of goroutines waiting for signal.
func (s *Signaller) WaitCount() int {
return int(s.sig.waits.Load())
}
// Close closes signaller and wakes all waiting goroutines.
// The first Close() returns true and subsequent calls always return false.
func (s *Signaller) Close() bool {
first := !s.sig.closed.Swap(true)
if first {
close(s.sig.closeCh)
}
return first
}
// IsClosed reports if signaller is closed.
func (s *Signaller) IsClosed() bool {
return s.sig.closed.Load()
}
// Receiver struct contains methods to wait for signals sent by corresponding Signaller (created by [New]). All methods are thread safe.
type Receiver struct {
sig *signal
}
// Wait blocks until awaken by signaller (returns true) or signaller was closed (returns false).
func (r *Receiver) Wait() bool {
if r.IsClosed() {
return false
}
r.sig.waits.Add(1)
defer r.sig.waits.Add(-1)
subsig := r.sig.subsig.Load()
select {
case <-subsig.ch:
return true
case <-r.sig.closeCh:
return false
case <-r.sig.direct:
return true
}
}
// WaitWithContext blocks until awaken by signaller, context was cancelled or signaller was closed.
// Returns true and nil, if awaken by signaller.
// Returns false and nil, if signaller was closed.
// Returns false and ctx.Err(), if context was cancelled.
func (r *Receiver) WaitWithContext(ctx context.Context) (bool, error) {
if r.IsClosed() {
return false, nil
}
r.sig.waits.Add(1)
defer r.sig.waits.Add(-1)
subsig := r.sig.subsig.Load()
select {
case <-r.sig.closeCh:
return false, nil
case <-subsig.ch:
return true, nil
case <-ctx.Done():
return false, ctx.Err()
case <-r.sig.direct:
return true, nil
}
}
// IsClosed reports if signaller was closed.
func (r *Receiver) IsClosed() bool {
return r.sig.closed.Load()
}
// New creates Signaller and Receiver.
func New() (*Signaller, *Receiver) {
sig := new(signal)
sig.closeCh = make(chan struct{})
sig.direct = make(chan struct{})
sig.subsig.Store(newSubsignal())
s, r := new(Signaller), new(Receiver)
s.sig = sig
r.sig = sig
return s, r
}
// UnsafeWait is utilized by cond package. Direct usage is not recommended.
func UnsafeWait(r *Receiver, locker sync.Locker) bool {
if r.IsClosed() {
return false
}
subsig := r.sig.subsig.Load()
// We have to do it before Unlock, so Signal will behave as sync.Cond.Signal
r.sig.waits.Add(1)
locker.Unlock()
var ret bool
select {
case <-subsig.ch:
ret = true
case <-r.sig.direct:
ret = true
case <-r.sig.closeCh:
}
r.sig.waits.Add(-1)
locker.Lock()
return ret
}
// UnsafeWaitContext is utilized by cond package. Direct usage is not recommended.
func UnsafeWaitContext(r *Receiver, locker sync.Locker, ctx context.Context) (bool, error) {
if r.IsClosed() {
return false, nil
}
subsig := r.sig.subsig.Load()
// We have to do it before Unlock, so Signal will behave as sync.Cond.Signal
r.sig.waits.Add(1)
locker.Unlock()
var ret bool
var err error
select {
case <-subsig.ch:
ret = true
case <-ctx.Done():
err = ctx.Err()
case <-r.sig.direct:
ret = true
case <-r.sig.closeCh:
}
r.sig.waits.Add(-1)
locker.Lock()
return ret, err
}