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.
49 lines
513 B
49 lines
513 B
7 years ago
|
package signal
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
type Done struct {
|
||
|
access sync.Mutex
|
||
|
c chan struct{}
|
||
|
closed bool
|
||
|
}
|
||
|
|
||
|
func NewDone() *Done {
|
||
|
return &Done{
|
||
|
c: make(chan struct{}),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (d *Done) Done() bool {
|
||
|
select {
|
||
|
case <-d.c:
|
||
|
return true
|
||
|
default:
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (d *Done) C() chan struct{} {
|
||
|
return d.c
|
||
|
}
|
||
|
|
||
|
func (d *Done) Wait() {
|
||
|
<-d.c
|
||
|
}
|
||
|
|
||
|
func (d *Done) Close() error {
|
||
|
d.access.Lock()
|
||
|
defer d.access.Unlock()
|
||
|
|
||
|
if d.closed {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
d.closed = true
|
||
|
close(d.c)
|
||
|
|
||
|
return nil
|
||
|
}
|