-
Notifications
You must be signed in to change notification settings - Fork 0
/
close.go
35 lines (29 loc) · 888 Bytes
/
close.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
package signal
// CancelSignal is a signal passed to goroutine, in order to cancel the goroutine on demand.
type CancelSignal struct {
cancel chan struct{}
done chan struct{}
}
// NewCloseSignal creates a new CancelSignal.
func NewCloseSignal() *CancelSignal {
return &CancelSignal{
cancel: make(chan struct{}),
done: make(chan struct{}),
}
}
// Cancel signals the goroutine to stop.
func (this *CancelSignal) Cancel() {
close(this.cancel)
}
// WaitForCancel should be monitored by the goroutine for when to stop.
func (this *CancelSignal) WaitForCancel() <-chan struct{} {
return this.cancel
}
// Done signals the caller that the goroutine has completely finished.
func (this *CancelSignal) Done() {
close(this.done)
}
// WaitForDone is used by caller to wait for the goroutine finishes.
func (this *CancelSignal) WaitForDone() <-chan struct{} {
return this.done
}