-
Notifications
You must be signed in to change notification settings - Fork 1
/
waitgroup.go
49 lines (41 loc) · 950 Bytes
/
waitgroup.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
package synx
import (
"sync"
)
// WaitGroup is like sync.WaitGroup with a signal channel.
type WaitGroup struct {
wg sync.WaitGroup
doneCh chan struct{}
}
// NewWaitGroup returns a new WaitGroup.
func NewWaitGroup() *WaitGroup {
return &WaitGroup{
doneCh: make(chan struct{}),
}
}
// Go run the given fn guarded with a wait group.
func (wg *WaitGroup) Go(fn func()) {
wg.Add(1)
go func() {
defer wg.Done()
fn()
}()
}
// Add has same behaviour as sync.WaitGroup.
func (wg *WaitGroup) Add(delta int) {
wg.wg.Add(delta)
}
// Done has same behaviour as sync.WaitGroup.
func (wg *WaitGroup) Done() {
wg.wg.Done()
}
// Wait has same behaviour as sync.WaitGroup.
func (wg *WaitGroup) Wait() {
wg.wg.Wait()
close(wg.doneCh)
}
// DoneChan returns a channel that will be closed on completion.
// Note: Wait method must be executed by the user to make it work.
func (wg *WaitGroup) DoneChan() <-chan struct{} {
return wg.doneCh
}