-
Notifications
You must be signed in to change notification settings - Fork 1
/
conduit.go
141 lines (117 loc) · 2.57 KB
/
conduit.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
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"context"
"sync"
)
type ConduitDo[T any] struct {
cond *sync.Cond
values []T // behind cond lock
waitCount int // behind cond lock
isCancel AtomicBool
}
func NewConduit[T any](done Doneable, ctx context.Context) (conduit Conduit[T]) {
done = EnsureDoneable(done)
defer done.Done()
c := ConduitDo[T]{
cond: sync.NewCond(&sync.Mutex{}),
}
OnCancel(c.onCancel, ctx)
return &c
}
func (ct *ConduitDo[T]) Put(value T) (IsCanceled bool) {
ct.cond.L.Lock()
defer ct.cond.L.Unlock()
ct.values = append(ct.values, value)
ct.cond.Signal()
return ct.isCancel.IsTrue()
}
func (ct *ConduitDo[T]) PutSlice(values []T) (IsCanceled bool) {
ct.cond.L.Lock()
defer ct.cond.L.Unlock()
ct.values = append(ct.values, values...)
ct.cond.Signal()
return ct.isCancel.IsTrue()
}
func (ct *ConduitDo[T]) Get() (value T, ok bool) {
ct.cond.L.Lock()
defer ct.cond.L.Unlock()
isWaiting := false
defer func() {
if isWaiting {
ct.waitCount--
}
}()
for {
if len(ct.values) > 0 {
value = ct.values[0]
ct.values = ct.values[1:]
ok = true
return // got value return
}
if ct.isCancel.IsTrue() {
return // channel closed and empty return: zero-value, ok == false
}
if !isWaiting {
isWaiting = true
ct.waitCount++
}
ct.cond.Wait()
}
}
func (ct *ConduitDo[T]) GetSlice(max int) (values []T, ok bool) {
ct.cond.L.Lock()
defer ct.cond.L.Unlock()
isWaiting := false
defer func() {
if isWaiting {
ct.waitCount--
}
}()
for {
if len(ct.values) > 0 {
if max == 0 || max >= len(ct.values) { // entire ct.values
values = ct.values
ct.values = nil
} else { // part of ct.values
values = make([]T, max)
copy(values, ct.values)
ct.values = ct.values[max:]
}
ok = true
return // got data return, ok == true
}
if ct.isCancel.IsTrue() {
return // channel closed and empty return: zero-value, ok == false
}
if !isWaiting {
isWaiting = true
ct.waitCount++
}
ct.cond.Wait()
}
}
func (ct *ConduitDo[T]) IsCanceled() (IsCanceled bool) {
return ct.isCancel.IsTrue()
}
func (ct *ConduitDo[T]) IsEmpty() (isEmpty bool) {
return ct.Count() == 0
}
func (ct *ConduitDo[T]) Count() (count int) {
ct.cond.L.Lock()
defer ct.cond.L.Unlock()
return len(ct.values)
}
func (ct *ConduitDo[T]) WaitCount() (waitCount int) {
ct.cond.L.Lock()
defer ct.cond.L.Unlock()
return ct.waitCount
}
func (ct *ConduitDo[T]) onCancel() {
if ct.isCancel.Set() {
ct.cond.Broadcast()
}
}