-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkers.go
96 lines (75 loc) · 1.83 KB
/
workers.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
package main
import (
"sync"
"time"
"gitgud.io/softashell/rpgmaker-patch-translator/block"
"github.com/vbauerster/mpb"
"github.com/vbauerster/mpb/decor"
)
type blockWork struct {
id int // Only needed to preserve order in patch file
block block.PatchBlock
}
func createFileWorkers(fileCount int) (chan string, chan error) {
workerCount := cFileThreads
if workerCount < 1 {
workerCount = 1
} else if workerCount > fileCount {
workerCount = fileCount
}
jobs := make(chan string, workerCount)
results := make(chan error, workerCount)
p := mpb.New(
mpb.WithRefreshRate(100 * time.Millisecond),
)
bar := p.AddBar(int64(fileCount),
mpb.PrependDecorators(
decor.Name("Overall progress", decor.WC{W: 25, C: decor.DSyncSpace}),
decor.CountersNoUnit("%d / %d", decor.WC{C: decor.DSyncSpace}),
),
)
lock := sync.Mutex{}
// Start workers
for w := 1; w <= workerCount; w++ {
go func(jobs <-chan string, results chan<- error) {
for j := range jobs {
results <- processFile(p, j)
bar.Increment()
}
lock.Lock()
defer lock.Unlock()
workerCount--
if workerCount < 1 {
close(results)
p.Wait()
}
}(jobs, results)
}
return jobs, results
}
func createBlockWorkers(blockCount int) (chan blockWork, chan blockWork) {
workerCount := cBlockThreads
if workerCount < 1 {
workerCount = 1
} else if workerCount > blockCount {
workerCount = blockCount
}
jobs := make(chan blockWork, workerCount)
results := make(chan blockWork, workerCount)
lock := sync.Mutex{}
for w := 1; w <= workerCount; w++ {
go func(jobs <-chan blockWork, results chan<- blockWork) {
for j := range jobs {
j.block = block.ParseBlock(j.block)
results <- j
}
lock.Lock()
defer lock.Unlock()
workerCount--
if workerCount < 1 {
close(results)
}
}(jobs, results)
}
return jobs, results
}