1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/common/signal/done.go
wuxiangzhou2010 5a08411ee2 fix typo
2018-04-02 15:52:16 +08:00

55 lines
873 B
Go
Executable File

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