forked from go-hep/hep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel.go
91 lines (82 loc) · 2.19 KB
/
parallel.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
// Copyright 2017 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// the following has been extracted from Rog Peppe's parallel package
// published under the new BSD-3 clause license
// http://opensource.org/licenses/BSD-3-Clause
//
// original sources are available at:
// code.google.com/p/rog-go/parallel
// The parallel package provides a way of running functions
// concurrently while limiting the maximum number
// running at once.
package parallel // import "go-hep.org/x/hep/fwk/utils/parallel"
import (
"fmt"
"sync"
)
// Run represents a number of functions running concurrently.
type Run struct {
limiter chan struct{}
done chan error
err chan error
wg sync.WaitGroup
}
// Errors holds any errors encountered during
// the parallel run.
type Errors []error
func (errs Errors) Error() string {
switch len(errs) {
case 0:
return "no error"
case 1:
return errs[0].Error()
}
return fmt.Sprintf("%s (and %d more)", errs[0].Error(), len(errs)-1)
}
// NewRun returns a new parallel instance. It will run up to maxPar
// functions concurrently.
func NewRun(maxPar int) *Run {
r := &Run{
limiter: make(chan struct{}, maxPar),
done: make(chan error),
err: make(chan error),
}
go func() {
var errs Errors
for e := range r.done {
errs = append(errs, e)
}
// TODO sort errors by original order of Do request?
if len(errs) > 0 {
r.err <- errs
} else {
r.err <- nil
}
}()
return r
}
// Do requests that r run f concurrently. If there are already the maximum
// number of functions running concurrently, it will block until one of
// them has completed. Do may itself be called concurrently.
func (r *Run) Do(f func() error) {
r.limiter <- struct{}{}
r.wg.Add(1)
go func() {
defer func() {
r.wg.Done()
<-r.limiter
}()
if err := f(); err != nil {
r.done <- err
}
}()
}
// Wait marks the parallel instance as complete and waits for all the
// functions to complete. If any errors were encountered, it returns an
// Errors value describing all the errors in arbitrary order.
func (r *Run) Wait() error {
r.wg.Wait()
close(r.done)
return <-r.err
}