mirror of https://github.com/v2ray/v2ray-core
Darien Raymond
8 years ago
6 changed files with 120 additions and 30 deletions
@ -1,35 +1,51 @@
|
||||
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 chan struct{} |
||||
done sync.WaitGroup |
||||
} |
||||
|
||||
// NewCloseSignal creates a new CancelSignal.
|
||||
func NewCloseSignal() *CancelSignal { |
||||
return &CancelSignal{ |
||||
cancel: make(chan struct{}), |
||||
done: 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 |
||||
} |
||||
|
||||
// Done signals the caller that the goroutine has completely finished.
|
||||
func (this *CancelSignal) Done() { |
||||
close(this.done) |
||||
// 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() <-chan struct{} { |
||||
return this.done |
||||
func (this *CancelSignal) WaitForDone() { |
||||
this.done.Wait() |
||||
} |
Loading…
Reference in new issue