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.
v2ray-core/common/signal/done.go

55 lines
874 B

package signal
import (
"sync"
)
7 years ago
// Done is an utility for notifications of something being done.
type Done struct {
access sync.Mutex
c chan struct{}
closed bool
}
7 years ago
// NewDone returns a new Done.
func NewDone() *Done {
return &Done{
c: make(chan struct{}),
}
}
7 years ago
// Done returns true if Close() is called.
func (d *Done) Done() bool {
select {
case <-d.c:
return true
default:
return false
}
}
7 years ago
// C returns a channel for waiting for done.
func (d *Done) C() chan struct{} {
return d.c
}
7 years ago
// Wait blocks until Close() is called.
func (d *Done) Wait() {
<-d.c
}
7 years ago
// Close marks this Done 'done'. This method may be called mutliple 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
}