-
Notifications
You must be signed in to change notification settings - Fork 0
/
work_chain.go
84 lines (73 loc) · 1.42 KB
/
work_chain.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
package utils
import (
"runtime"
)
type WorkResult struct {
e error
w Runner
}
type WorkChain struct {
abort chan struct{}
results chan *WorkResult
}
func (self *WorkChain) Release() {
close(self.abort)
}
type Runner interface {
Run() error
}
func NewWorkChain(runners []Runner) (ret *WorkChain) {
ret = &WorkChain{}
ret.abort = make(chan struct{})
if len(runners) == 0 {
return
}
// Spawn as many workers as allowed threads
workers := runtime.GOMAXPROCS(0)
if len(runners) < workers {
workers = len(runners)
}
// Create a task channel and spawn the verifiers
var (
inputs = make(chan int)
done = make(chan int, workers)
errors = make([]*WorkResult, len(runners))
)
for i := 0; i < workers; i++ {
go func() {
for index := range inputs {
runner := runners[index]
errors[index] = &WorkResult{runner.Run(), runner}
done <- index
}
}()
}
ret.results = make(chan *WorkResult, len(runners))
go func() {
defer close(inputs)
var (
in, out = 0, 0
checked = make([]bool, len(runners))
inputs = inputs
)
for {
select {
case inputs <- in:
if in++; in == len(runners) {
// Reached end of headers. Stop sending to workers.
inputs = nil
}
case index := <-done:
for checked[index] = true; checked[out]; out++ {
ret.results <- errors[out]
if out == len(runners)-1 {
return
}
}
case <-ret.abort:
return
}
}
}()
return
}