blob: 94f928958af0462693f8568ff753f1f2e76e2b7c (
plain)
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
|
package internal
import (
"sync"
)
// Done is a cleanup/shutdown helper.
type Done struct {
ch chan struct{}
mutex sync.Mutex
}
// NewDone returns a new cleanup/shutdown helper.
func NewDone() *Done {
return &Done{
ch: make(chan struct{}),
}
}
func (d *Done) String() string {
select {
case <-d.Done():
return "Done(yes)"
default:
return "Done(no)"
}
}
// Done returns the done channel (closed when done)
func (d *Done) Done() <-chan struct{} {
return d.ch
}
// Shutdown closes the done channel. It can be called multiple times.
func (d *Done) Shutdown() {
d.mutex.Lock()
defer d.mutex.Unlock()
select {
case <-d.ch:
return
default:
close(d.ch)
}
}
|