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
9 years ago
|
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.
|
||
9 years ago
|
type CancelSignal struct {
|
||
|
cancel chan struct{}
|
||
8 years ago
|
done sync.WaitGroup
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// NewCloseSignal creates a new CancelSignal.
|
||
9 years ago
|
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.
|
||
9 years ago
|
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.
|
||
9 years ago
|
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
|
}
|
||
|
|
||
9 years ago
|
// WaitForDone is used by caller to wait for the goroutine finishes.
|
||
8 years ago
|
func (this *CancelSignal) WaitForDone() {
|
||
|
this.done.Wait()
|
||
9 years ago
|
}
|