shutdown.go
676 Bytes
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
package util
import (
"sync"
)
// A small utility class for managing controlled shutdowns
type Shutdown struct {
sync.Mutex
inProgress bool
begin chan int // closed when the shutdown begins
complete chan int // closed when the shutdown completes
}
func NewShutdown() *Shutdown {
return &Shutdown{
begin: make(chan int),
complete: make(chan int),
}
}
func (s *Shutdown) Begin() {
s.Lock()
defer s.Unlock()
if s.inProgress == true {
return
} else {
s.inProgress = true
close(s.begin)
}
}
func (s *Shutdown) WaitBegin() {
<-s.begin
}
func (s *Shutdown) Complete() {
close(s.complete)
}
func (s *Shutdown) WaitComplete() {
<-s.complete
}