mirror of https://github.com/v2ray/v2ray-core
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.
52 lines
1.0 KiB
52 lines
1.0 KiB
package signal
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
// CancelSignal is a signal passed to goroutine, in order to cancel the goroutine on demand.
|
|
type CancelSignal struct {
|
|
cancel chan struct{}
|
|
done sync.WaitGroup
|
|
}
|
|
|
|
// NewCloseSignal creates a new CancelSignal.
|
|
func NewCloseSignal() *CancelSignal {
|
|
return &CancelSignal{
|
|
cancel: make(chan struct{}),
|
|
}
|
|
}
|
|
|
|
func (this *CancelSignal) WaitThread() {
|
|
this.done.Add(1)
|
|
}
|
|
|
|
// Cancel signals the goroutine to stop.
|
|
func (this *CancelSignal) Cancel() {
|
|
close(this.cancel)
|
|
}
|
|
|
|
func (this *CancelSignal) Cancelled() bool {
|
|
select {
|
|
case <-this.cancel:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// WaitForCancel should be monitored by the goroutine for when to stop.
|
|
func (this *CancelSignal) WaitForCancel() <-chan struct{} {
|
|
return this.cancel
|
|
}
|
|
|
|
// FinishThread signals that current goroutine has finished.
|
|
func (this *CancelSignal) FinishThread() {
|
|
this.done.Done()
|
|
}
|
|
|
|
// WaitForDone is used by caller to wait for the goroutine finishes.
|
|
func (this *CancelSignal) WaitForDone() {
|
|
this.done.Wait()
|
|
}
|