-
Notifications
You must be signed in to change notification settings - Fork 1
/
nb-chan-send.go
297 lines (253 loc) · 7.87 KB
/
nb-chan-send.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
© 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"sync/atomic"
)
// Send sends a single value on the channel
// - non-blocking, thread-safe, panic-free and error-free
func (n *NBChan[T]) Send(value T) {
if n.isCloseInvoked.Load() {
return // no send after Close(), atomic performance: noop
}
n.preSend()
n.inputLock.Lock()
defer n.postSend()
if n.isCloseInvoked.Load() {
return // no send after Close() return: noop
}
// try to provide value to thread
if !n.noThread.Load() && n.alertOrLaunchThreadWithValue(value) {
return // value was handed to thread return: done
}
// save value in [NBChan.inputQueue]
if n.inputQueue == nil {
n.inputQueue = n.newQueue(1) // enforce allocation size
}
n.inputQueue = append(n.inputQueue, value)
atomic.StoreUint64(&n.inputCapacity, uint64(cap(n.inputQueue)))
atomic.AddUint64(&n.unsentCount, 1)
}
// Send sends many values non-blocking, thread-safe, panic-free and error-free on the channel
// - if values is length 0 or nil, SendMany only returns count and capacity
func (n *NBChan[T]) SendMany(values []T) {
var valueCount = len(values)
if n.isCloseInvoked.Load() || valueCount == 0 {
return // no send after Close(), atomic performance: noop
}
n.preSend()
n.inputLock.Lock()
defer n.postSend()
if n.isCloseInvoked.Load() {
return // no send after Close() return: noop
}
if !n.noThread.Load() && n.alertOrLaunchThreadWithValue(values[0]) {
values = values[1:]
valueCount--
if valueCount == 0 {
return // one value handed to thread return: complete
}
}
// save values in [NBChan.inputQueue]
if n.inputQueue == nil {
n.inputQueue = n.newQueue(valueCount)
}
n.inputQueue = append(n.inputQueue, values...)
atomic.StoreUint64(&n.inputCapacity, uint64(cap(n.inputQueue)))
atomic.AddUint64(&n.unsentCount, uint64(valueCount))
}
// preSend registers a Send/SendMany invocation pre-inputLock
func (n *NBChan[T]) preSend() {
if atomic.AddUint64(&n.sends, 1) == 1 {
n.sendsWait.HoldWaiters()
}
}
// post send is deferred for [NBChan.Send] and [NBChan.SendMany]
// - release inputLock
// - alert thread if no pending Get and values ar present
func (n *NBChan[T]) postSend() {
n.inputLock.Unlock()
// decrement sends
var sends = int(atomic.AddUint64(&n.sends, ^uint64(0)))
// update data available
var unsentCount = atomic.LoadUint64(&n.unsentCount)
n.setDataAvailable(unsentCount > 0)
if n.noThread.Load() {
return // no thread progress to manage
}
// ensure thread progress
// if no data only release any sends waiters
if unsentCount == 0 {
// release thread if it is waiting for pending sends
if sends == 0 {
n.sendsWait.ReleaseWaiters()
}
return
}
// a send ended and data is available
// release any sends waiters to get the new data
n.sendsWait.ReleaseWaiters()
// alert any waiting always-threads to get the new data
n.alertThread(nil)
}
func (n *NBChan[T]) ensureInput(size int) (queue []T) {
n.inputLock.Lock()
defer n.inputLock.Unlock()
if n.inputQueue != nil {
return
}
n.inputQueue = n.newQueue(size)
return
}
func (n *NBChan[T]) ensureOutput(size int) (queue []T) {
n.outputLock.Lock()
defer n.outputLock.Unlock()
if n.outputQueue != nil {
return
}
n.outputQueue = n.newQueue(size)
return
}
// newQueue allocates a new queue slice
// - capacity is at least count elements
// - the slice is empty
func (n *NBChan[T]) newQueue(count int) (queue []T) {
// determine size
var size = int(atomic.LoadUint64(&n.allocationSize))
if size > 0 {
if count > size {
size = count
}
} else {
size = defaultNBChanSize
if count > size {
size = count * 2
}
}
// return allocated zero-length queue
return make([]T, size)[:0]
}
// alertOrLaunchThreadWithValue attempts to launch thread using value
// - invoked while holding [NBChan.inputLock]
// - Invoked by Send/SendMany
// - value has nit been added to unsentCount yet
func (n *NBChan[T]) alertOrLaunchThreadWithValue(value T) (didProvideValue bool) {
// if Get in progress or NBChan is not empty, don’t launch thread
if atomic.LoadUint64(&n.gets) > 0 || // [NBChan.Get] in progress
atomic.LoadUint64(&n.unsentCount) > 0 { // channel is not empty
return // gets are in progress or not empty return: no thread interaction allowed
}
if n.alertThread(&value) {
didProvideValue = true
return
} else if didProvideValue = n.tryStartThread(); !didProvideValue {
return // this invocation did not win thread-launch return : didProvideValue: false
}
atomic.AddUint64(&n.unsentCount, 1)
n.startThread(value)
return // launched new thread return: didProvideValue: true
}
// tryStartThread attempts to be the thread that gets to launch sendThread
// - inside threadLock
// - on isRunningThread true, closesOnThreadSend must be valid
func (n *NBChan[T]) tryStartThread() (isWinner bool) {
var ch chan struct{}
var didWin bool
if n.closesOnThreadSend.Load() == nil {
ch = make(chan struct{})
didWin = n.closesOnThreadSend.CompareAndSwap(nil, &ch)
}
n.threadLock.Lock()
defer n.threadLock.Unlock()
// check for thread already running or this invocation should not launch it
if isWinner = n.isRunningThread.CompareAndSwap(false, true); !isWinner {
return // thread was already running return
}
if !didWin {
if ch == nil {
ch = make(chan struct{})
}
n.closesOnThreadSend.Store(&ch) // fresh channel
}
return
}
// startThread launches the send thread
// - on Send/SendMany when unsentCount was 0 and no gets in progress
// - on postGet if unsentCount > 0 and gets in progress went to 0
// - isRunningThread.CompareAndSwap winner invokes startThread
// - thread runs until unsentCount is 0 inside threadLock
func (n *NBChan[T]) startThread(value T) {
var endCh = make(chan struct{})
n.threadWait.Store(&endCh)
go n.sendThread(value) // send err in new thread
}
func (n *NBChan[T]) waitForSendThread() {
var ch chan struct{}
if chp := n.threadWait.Load(); chp == nil {
return
} else {
ch = *chp
}
<-ch
}
// alertThread alerts any waiting always-threads
// - invoked from Send/SendMany
// - value has not been added to unsentCount yet
func (n *NBChan[T]) alertThread(valuep *T) (didAlert bool) {
if didAlert = n.threadAlertPending.CompareAndSwap(true, false); !didAlert {
return // sendThread not waiting for alert or this invocation not winner return
}
// alert sendThread
if valuep != nil {
atomic.AddUint64(&n.unsentCount, 1)
n.threadAlertValue.Store(valuep)
}
n.threadAlertWait.ReleaseWaiters()
return
}
func (n *NBChan[T]) updateDataAvailable() (dataCh chan struct{}) {
if n.closableChan.IsClosed() {
return n.setDataAvailableAfterClose()
}
return n.setDataAvailable(atomic.LoadUint64(&n.unsentCount) > 0)
}
func (n *NBChan[T]) setDataAvailableAfterClose() (dataCh chan struct{}) {
return n.setDataAvailable(true)
}
func (n *NBChan[T]) setDataAvailable(isAvailable bool) (dataCh chan struct{}) {
if chp := n.dataWaitCh.Load(); chp != nil && n.isDataAvailable.Load() == isAvailable {
dataCh = *chp
return // initialized and in correct state return: noop
}
n.availableLock.Lock()
defer n.availableLock.Unlock()
// not yet initialized case
var chp = n.dataWaitCh.Load()
if chp == nil {
dataCh = make(chan struct{})
if isAvailable {
close(dataCh)
}
n.dataWaitCh.Store(&dataCh)
n.isDataAvailable.Store(isAvailable)
return // channel initialized and state set return
}
// is state correct?
dataCh = *chp
if n.isDataAvailable.Load() == isAvailable {
return // channel was initialized and state was correct return
}
// should channel be closed: if data is available
if isAvailable {
close(*chp)
n.isDataAvailable.Store(true)
return // channel closed andn state updated return
}
// replace with open channel: data is not available
dataCh = make(chan struct{})
n.dataWaitCh.Store(&dataCh)
n.isDataAvailable.Store(false)
return // new open channel stored and state updated return
}