-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwait.go
More file actions
48 lines (39 loc) · 1.08 KB
/
wait.go
File metadata and controls
48 lines (39 loc) · 1.08 KB
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
package background
import (
"sync"
)
type waitBackground struct {
*group
sync.WaitGroup
}
// WaitTail detaches after waitable background initialization.
// The tail is supposed to stay in a background job associated with
// created Background.
//
// WaitTail uses sync.WaitGroup and shares all its mechanics.
type WaitTail interface {
// Done calls sync.WaitGroup's Done method
Done()
// Add calls sync.WaitGroup's Add method
Add(i int)
}
// WithWait returns new waitable Background with merged children.
//
// The returned WaitTail is used to increment and decrement Background's WaitGroup counter.
func WithWait(children ...Background) (Background, WaitTail) {
s := withWait(children...)
return s, s
}
func withWait(children ...Background) *waitBackground {
return &waitBackground{
group: merge(children...),
}
}
// Wait blocks until Backgrounds's and Backgrounds's children counters are zero.
func (w *waitBackground) Wait() {
w.WaitGroup.Wait()
w.group.Wait()
}
func (w *waitBackground) DependsOn(children ...Background) Background {
return withDependency(w, children...)
}