Repository navigation
Expand file tree
/
Copy pathheap_manager.go
More file actions
207 lines (187 loc) · 3.99 KB
/
Copy pathheap_manager.go
File metadata and controls
207 lines (187 loc) · 3.99 KB
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
package mpb
import (
"cmp"
"container/heap"
"errors"
"iter"
"sync"
"github.com/vbauerster/mpb/v8/decor"
)
type barHeapServer <-chan heapRequest
type barHeapClient chan<- heapRequest
type heapCmd int
const (
h_sync heapCmd = iota
h_push
h_render
h_iter
h_fix
)
type heapRequest struct {
cmd heapCmd
data any
}
type pushData struct {
bar *Bar
sync bool
}
type renderData struct {
width int
seqCh chan<- iter.Seq[*Bar]
offload <-chan heapRequest
}
type fixData struct {
bar *Bar
priority int
lazy bool
}
func newBarHeapManager(pwg *sync.WaitGroup, ps *pState) barHeapClient {
ch := make(chan heapRequest, cmp.Or(ps.hmQueueLen, defaultHmQueueLength))
pwg.Go(func() {
barHeapServer(ch).serve(ps.shutdownNotifier, ps.depleteHeap)
})
return barHeapClient(ch)
}
func (m barHeapServer) serve(shutdown <-chan any, depleteHeap chan<- *Bar) {
var bHeap barHeap
var sync bool
var prevLen int
var pMatrix map[int][]*decor.Sync
var aMatrix map[int][]*decor.Sync
if depleteHeap != nil {
defer func() {
for bHeap.Len() != 0 {
depleteHeap <- heap.Pop(&bHeap).(*Bar)
}
close(depleteHeap)
}()
}
for req := range m {
switch req.cmd {
case h_sync:
if sync || prevLen != bHeap.Len() {
pMatrix = make(map[int][]*decor.Sync)
aMatrix = make(map[int][]*decor.Sync)
for _, b := range bHeap {
table := b.wSyncTable()
for i, s := range table[0] {
pMatrix[i] = append(pMatrix[i], s)
}
for i, s := range table[1] {
aMatrix[i] = append(aMatrix[i], s)
}
}
sync, prevLen = false, bHeap.Len()
}
syncWidth(pMatrix, shutdown)
syncWidth(aMatrix, shutdown)
case h_push:
data := req.data.(pushData)
heap.Push(&bHeap, data.bar)
sync = sync || data.sync
case h_render:
var pushQ []heapRequest
data := req.data.(renderData)
for _, b := range bHeap {
go b.render(data.width)
}
data.seqCh <- func(yield func(*Bar) bool) {
for bHeap.Len() != 0 {
if !yield(heap.Pop(&bHeap).(*Bar)) {
return
}
}
}
for req := range data.offload {
pushQ = append(pushQ, req)
}
for _, req := range pushQ {
data := req.data.(pushData)
heap.Push(&bHeap, data.bar)
sync = sync || data.sync
}
case h_iter:
seqCh := req.data.(chan<- iter.Seq[*Bar])
done := make(chan struct{})
seqCh <- func(yield func(*Bar) bool) {
defer close(done)
for _, b := range bHeap {
if !yield(b) {
return
}
}
}
<-done
case h_fix:
data := req.data.(fixData)
if data.bar.index < 0 {
break
}
data.bar.priority = data.priority
if !data.lazy {
heap.Fix(&bHeap, data.bar.index)
}
}
}
}
func (m barHeapClient) sync() {
m <- heapRequest{cmd: h_sync}
}
func (m barHeapClient) push(bar *Bar, sync bool, offload chan<- heapRequest) {
req := heapRequest{cmd: h_push, data: pushData{
bar: bar,
sync: sync,
}}
select {
case m <- req:
default:
if offload != nil {
offload <- req
} else {
bar.container.bwg.Go(func() {
m <- req
})
}
}
}
func (m barHeapClient) render(width int, offload <-chan heapRequest) iter.Seq[*Bar] {
if offload == nil {
panic(errors.New("expected non nil offload chan heapRequest"))
}
seqCh := make(chan iter.Seq[*Bar], 1)
m <- heapRequest{cmd: h_render, data: renderData{
width: width,
seqCh: seqCh,
offload: offload,
}}
return <-seqCh
}
func (m barHeapClient) iter(seqCh chan<- iter.Seq[*Bar]) {
m <- heapRequest{cmd: h_iter, data: seqCh}
}
func (m barHeapClient) fix(b *Bar, priority int, lazy bool) {
data := fixData{b, priority, lazy}
m <- heapRequest{cmd: h_fix, data: data}
}
func syncWidth(matrix map[int][]*decor.Sync, done <-chan any) {
for _, column := range matrix {
go maxWidthDistributor(column, done)
}
}
func maxWidthDistributor(column []*decor.Sync, done <-chan any) {
var maxWidth int
loop:
for _, s := range column {
select {
case w := <-s.Tx:
if w > maxWidth {
maxWidth = w
}
case <-done:
break loop
}
}
for _, s := range column {
s.Rx <- maxWidth
}
}