-
Notifications
You must be signed in to change notification settings - Fork 0
/
drumline.go
168 lines (158 loc) · 4.11 KB
/
drumline.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
// Package drumline provides a synchronization primitive for keeping multiple
// goroutines in lock step, within an established threshold.
package drumline
import (
"time"
"runtime"
"sync"
"sync/atomic"
"math"
)
// Drumline ensures that goroutines stay in step within an acceptable
// threshold.
type Drumline struct {
channels map[int]chan struct{}
resets map[int]chan struct{}
done map[int]struct{}
resetCh chan struct{}
scales map[int]int64
minscale int64
stepSize map[int]int64
steps map[int]*int64
doneCh chan int
buffer int
started bool
closed bool
quit chan struct{}
lock sync.RWMutex
}
// NewDrumline creates a drumline. `buffer` indicates the maximum number of
// steps ahead one goroutine can be relative to the least advanced goroutine.
func NewDrumline(buffer int) *Drumline {
return &Drumline{
channels: make(map[int]chan struct{}),
resets: make(map[int]chan struct{}),
scales: make(map[int]int64),
minscale: math.MaxInt64,
stepSize: make(map[int]int64),
steps: make(map[int]*int64),
resetCh: make(chan struct{}),
doneCh: make(chan int),
done: make(map[int]struct{}),
buffer: buffer,
quit: make(chan struct{}),
}
}
func (dl *Drumline) Add(i int) {
dl.AddScale(i, 1)
}
// Add starts tracking a new goroutine in the Drumline
func (dl *Drumline) AddScale(i int, scale int64) {
if scale < 1 {
scale = 1 // Prevent divide by zero errors later, and I don't know what negative scale would mean.
}
dl.lock.Lock()
dl.channels[i] = make(chan struct{}, dl.buffer)
dl.resets[i] = make(chan struct{})
dl.scales[i] = scale
dl.steps[i] = new(int64)
if scale < dl.minscale {
dl.minscale = scale
for j, s := range dl.scales {
dl.stepSize[j] = s / dl.minscale
}
}
dl.stepSize[i] = scale / dl.minscale
dl.lock.Unlock()
if !dl.started {
dl.started = true
go func() {
for {
for _, ch := range dl.channels {
select {
case <-ch:
case thread := <-dl.doneCh:
dl.lock.Lock()
delete(dl.channels, thread)
delete(dl.resets, thread)
dl.done[thread] = struct{}{}
dl.lock.Unlock()
if len(dl.channels) == 0 {
<-dl.quit
return
}
case <-dl.resetCh:
for _, v := range dl.resets {
select {
case v <- struct{}{}:
default:
}
runtime.Gosched()
}
case <-dl.quit:
return
}
}
}
}()
}
}
func (dl *Drumline) Done(i int) {
dl.doneCh <- i
}
// Step advances a specific goroutine. If this would put that goroutine too far
// ahead of the rest of the drumline, this will block until other goroutines
// start to catch up.
func (dl *Drumline) Step(i int) {
dl.lock.RLock()
if _, ok := dl.done[i]; ok {
dl.lock.RUnlock()
return
}
atomic.AddInt64(dl.steps[i], 1)
if *dl.steps[i] < dl.stepSize[i] {
dl.lock.RUnlock()
return
}
*dl.steps[i] = 0
ch := dl.channels[i]
rch := dl.resets[i]
dl.lock.RUnlock()
select {
case ch <- struct{}{}:
case <- rch:
}
}
// Reset returns a channel that will produce a message after the specified
// duration. If the message is consumed, the drumline will reset; if the
// message is not consumed the reset will expire. If the drumline is closed,
// the returned channel will be nil, and will never produce a message.
func (dl *Drumline) Reset(d time.Duration) chan time.Time {
if dl.closed { return nil }
ret := make(chan time.Time)
go func(ret chan time.Time) {
t := <- time.After(d)
select {
case ret <- t:
default:
return
}
for i := range dl.channels {
dl.lock.Lock()
dl.channels[i] = make(chan struct{}, dl.buffer)
dl.lock.Unlock()
}
select {
case dl.resetCh <- struct{}{}:
default:
}
}(ret)
return ret
}
// Close cleans up the drumline. Close() must be called when a Drumline is
// discarded, or it will not be garbage collected.
func (dl *Drumline) Close() {
dl.closed = true
dl.quit <- struct{}{}
close(dl.quit)
}