summaryrefslogtreecommitdiff
path: root/internal/done.go
blob: 2326eee601352487f378cdee56778578adad3292 (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
package internal

import (
	"sync"
)

type Done struct {
	ch    chan struct{}
	mutex sync.Mutex
}

func NewDone() *Done {
	return &Done{
		ch: make(chan struct{}),
	}
}

func (d *Done) Done() <-chan struct{} {
	return d.ch
}

func (d *Done) Shutdown() {
	d.mutex.Lock()
	defer d.mutex.Unlock()

	select {
	case <-d.ch:
		return
	default:
		close(d.ch)
	}
}