-
Notifications
You must be signed in to change notification settings - Fork 0
/
done.go
executable file
·54 lines (44 loc) · 873 Bytes
/
done.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
package signal
import (
"sync"
)
// Done is a utility for notifications of something being done.
type Done struct {
access sync.Mutex
c chan struct{}
closed bool
}
// NewDone returns a new Done.
func NewDone() *Done {
return &Done{
c: make(chan struct{}),
}
}
// Done returns true if Close() is called.
func (d *Done) Done() bool {
select {
case <-d.c:
return true
default:
return false
}
}
// C returns a channel for waiting for done.
func (d *Done) C() chan struct{} {
return d.c
}
// Wait blocks until Close() is called.
func (d *Done) Wait() {
<-d.c
}
// Close marks this Done 'done'. This method may be called multiple times. All calls after first call will have no effect on its status.
func (d *Done) Close() error {
d.access.Lock()
defer d.access.Unlock()
if d.closed {
return nil
}
d.closed = true
close(d.c)
return nil
}