-
Notifications
You must be signed in to change notification settings - Fork 2
/
group.go
71 lines (62 loc) · 1.14 KB
/
group.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
package execgroup
import "sync"
// Group synchronizes dependent concurrent execution.
type Group struct {
cond *sync.Cond
deps []*Group
err error
nexec int64
}
// NewGroup initializes and returns a new Group.
func NewGroup(deps []*Group) *Group {
cond := &sync.Cond{L: &sync.Mutex{}}
g := &Group{
cond: cond,
deps: deps,
}
return g
}
// Exec begins executing fn and prevents any waiting goroutines from resuming
// until fn returns.
func (g *Group) Exec(fn func() error) error {
g.cond.L.Lock()
defer g.cond.L.Unlock()
if g.err != nil {
err := g.err
g.err = nil
return err
}
g.nexec++
go g.exec(fn)
return nil
}
func (g *Group) exec(fn func() error) {
var err error
defer func() {
g.cond.L.Lock()
if err != nil && g.err == nil {
g.err = err
}
g.nexec--
g.cond.Broadcast()
g.cond.L.Unlock()
}()
for _, dep := range g.deps {
err = dep.Wait()
if err != nil {
return
}
}
err = fn()
}
// Wait blocks until the group has no running functions.
func (g *Group) Wait() error {
g.cond.L.Lock()
for g.nexec > 0 && g.err == nil {
g.cond.Wait()
}
err := g.err
g.err = nil
g.cond.L.Unlock()
return err
}