1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-05 13:35:23 +00:00
v2fly/common/signal/done.go

55 lines
874 B
Go
Raw Normal View History

2018-02-08 14:39:46 +00:00
package signal
import (
"sync"
)
2018-02-11 22:28:42 +00:00
// Done is an utility for notifications of something being done.
2018-02-08 14:39:46 +00:00
type Done struct {
access sync.Mutex
c chan struct{}
closed bool
}
2018-02-11 22:28:42 +00:00
// NewDone returns a new Done.
2018-02-08 14:39:46 +00:00
func NewDone() *Done {
return &Done{
c: make(chan struct{}),
}
}
2018-02-11 22:28:42 +00:00
// Done returns true if Close() is called.
2018-02-08 14:39:46 +00:00
func (d *Done) Done() bool {
select {
case <-d.c:
return true
default:
return false
}
}
2018-02-11 22:28:42 +00:00
// C returns a channel for waiting for done.
2018-02-08 14:39:46 +00:00
func (d *Done) C() chan struct{} {
return d.c
}
2018-02-11 22:28:42 +00:00
// Wait blocks until Close() is called.
2018-02-08 14:39:46 +00:00
func (d *Done) Wait() {
<-d.c
}
2018-03-15 02:32:10 +00:00
// Close marks this Done 'done'. This method may be called multiple times. All calls after first call will have no effect on its status.
2018-02-08 14:39:46 +00:00
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
}