forked from cschleiden/go-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
232 lines (179 loc) · 4.24 KB
/
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
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
224
225
226
227
228
229
230
231
232
package sync
type Channel[T any] interface {
Send(ctx Context, v T)
SendNonblocking(v T) (ok bool)
Receive(ctx Context) (v T, ok bool)
ReceiveNonBlocking() (v T, ok bool)
Close()
}
type ChannelInternal[T any] interface {
Closed() bool
ReceiveNonBlocking() (v T, ok bool)
// AddReceiveCallback adds a callback that is called once when a value is sent to the channel. This is similar
// to the blocking `Receive` method, but is not blocking a coroutine.
AddReceiveCallback(cb func(v T, ok bool))
}
// Ensure channel implementation support internal interface
var _ ChannelInternal[struct{}] = (*channel[struct{}])(nil)
func NewChannel[T any]() Channel[T] {
return &channel[T]{
c: make([]T, 0),
}
}
func NewBufferedChannel[T any](size int) Channel[T] {
return &channel[T]{
c: make([]T, 0, size),
size: size,
}
}
type channel[T any] struct {
c []T
receivers []func(value T, ok bool)
senders []func() T
closed bool
size int
}
func (c *channel[T]) Close() {
c.closed = true
// If there are still blocked senders, error
if len(c.senders) > 0 {
panic("send on closed channel")
}
// Drain buffered values
for len(c.receivers) > 0 {
r := c.receivers[0]
c.receivers[0] = nil
c.receivers = c.receivers[1:]
// Send zero value to pending receiver
var v T
r(v, false)
}
}
func (c *channel[T]) Send(ctx Context, v T) {
cr := getCoState(ctx)
addedSender := false
sentValue := false
for {
if c.trySend(v) {
cr.MadeProgress()
return
}
if !addedSender {
addedSender = true
cb := func() T {
sentValue = true
return v
}
c.senders = append(c.senders, cb)
}
// No waiting receiver, yield
cr.Yield()
// Was our sender called while we yielded? If so, we can return
if sentValue {
cr.MadeProgress()
return
}
}
}
func (c *channel[T]) SendNonblocking(v T) bool {
return c.trySend(v)
}
func (c *channel[T]) Receive(ctx Context) (v T, ok bool) {
cr := getCoState(ctx)
addedListener := false
receivedValue := false
for {
// Try to receive from buffered channel or blocked sender
if v, ok, rok := c.tryReceive(); rok {
cr.MadeProgress()
return v, ok
}
// Register handler to receive value once
if !addedListener {
cb := func(rv T, rok bool) {
receivedValue = true
v = rv
ok = rok
}
c.receivers = append(c.receivers, cb)
addedListener = true
}
cr.Yield()
// If we received a value via the callback, return
if receivedValue {
cr.MadeProgress()
return v, ok
}
}
}
func (c *channel[T]) ReceiveNonBlocking() (T, bool) {
if v, ok, rok := c.tryReceive(); rok {
return v, ok
}
return *new(T), false
}
func (c *channel[T]) hasValue() bool {
return len(c.c) > 0
}
func (c *channel[T]) canReceive() bool {
return c.hasValue() || len(c.senders) > 0 || c.closed
}
func (c *channel[T]) canSend() bool {
if c.closed {
return false
}
return len(c.receivers) > 0 || c.hasCapacity()
}
func (c *channel[T]) trySend(v T) bool {
// If closed, we can't send, panic.
if c.closed {
panic("channel closed")
}
// Are there any existing blocked receivers? If so, unblock the first one with
// the value.
if len(c.receivers) > 0 {
r := c.receivers[0]
c.receivers[0] = nil
c.receivers = c.receivers[1:]
r(v, true)
return true
}
// No waiting receiver, if we have capacity try to add the value to the buffer
if c.hasCapacity() {
c.c = append(c.c, v)
return true
}
// No receiver waiting and no capacity, we can't send.
return false
}
func (c *channel[T]) tryReceive() (v T, ok bool, rok bool) {
// If channel is buffered, return value if available
if c.hasValue() {
v = c.c[0]
c.c = c.c[1:]
return v, true, true
}
// If channel has been closed and no values in buffer (if buffered) return zero
// element
if c.closed {
return *new(T), false, true
}
// Any blocked senders? If so, receive from the first one
if len(c.senders) > 0 {
s := c.senders[0]
c.senders[0] = nil
c.senders = c.senders[1:]
return s(), true, true
}
// Could not receive value
return v, ok, false
}
func (c *channel[T]) hasCapacity() bool {
return len(c.c) < c.size
}
func (c *channel[T]) AddReceiveCallback(cb func(v T, ok bool)) {
c.receivers = append(c.receivers, cb)
}
func (c *channel[T]) Closed() bool {
return c.closed
}