You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/common/signal/cancel.go

52 lines
1.0 KiB

package signal
8 years ago
import (
"sync"
)
9 years ago
// CancelSignal is a signal passed to goroutine, in order to cancel the goroutine on demand.
type CancelSignal struct {
cancel chan struct{}
8 years ago
done sync.WaitGroup
}
9 years ago
// NewCloseSignal creates a new CancelSignal.
func NewCloseSignal() *CancelSignal {
return &CancelSignal{
cancel: make(chan struct{}),
}
}
8 years ago
func (this *CancelSignal) WaitThread() {
this.done.Add(1)
}
9 years ago
// Cancel signals the goroutine to stop.
func (this *CancelSignal) Cancel() {
close(this.cancel)
}
8 years ago
func (this *CancelSignal) Cancelled() bool {
select {
case <-this.cancel:
return true
default:
return false
}
}
9 years ago
// WaitForCancel should be monitored by the goroutine for when to stop.
func (this *CancelSignal) WaitForCancel() <-chan struct{} {
return this.cancel
}
8 years ago
// FinishThread signals that current goroutine has finished.
func (this *CancelSignal) FinishThread() {
this.done.Done()
}
9 years ago
// WaitForDone is used by caller to wait for the goroutine finishes.
8 years ago
func (this *CancelSignal) WaitForDone() {
this.done.Wait()
}