summaryrefslogtreecommitdiff
path: root/internal/done.go
blob: 54e5e8e55ded968934b469514ee8fda56d4ff83f (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
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{}),
	}
}

// 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)
	}
}