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.
50 lines
830 B
50 lines
830 B
7 years ago
|
package done
|
||
7 years ago
|
|
||
|
import (
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
7 years ago
|
// Instance is a utility for notifications of something being done.
|
||
|
type Instance struct {
|
||
7 years ago
|
access sync.Mutex
|
||
|
c chan struct{}
|
||
|
closed bool
|
||
|
}
|
||
|
|
||
7 years ago
|
// New returns a new Done.
|
||
|
func New() *Instance {
|
||
|
return &Instance{
|
||
7 years ago
|
c: make(chan struct{}),
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
// Done returns true if Close() is called.
|
||
7 years ago
|
func (d *Instance) Done() bool {
|
||
7 years ago
|
select {
|
||
7 years ago
|
case <-d.Wait():
|
||
7 years ago
|
return true
|
||
|
default:
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
// Wait returns a channel for waiting for done.
|
||
7 years ago
|
func (d *Instance) Wait() <-chan struct{} {
|
||
7 years ago
|
return d.c
|
||
|
}
|
||
|
|
||
7 years ago
|
// Close marks this Done 'done'. This method may be called multiple times. All calls after first call will have no effect on its status.
|
||
7 years ago
|
func (d *Instance) Close() error {
|
||
7 years ago
|
d.access.Lock()
|
||
|
defer d.access.Unlock()
|
||
|
|
||
|
if d.closed {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
d.closed = true
|
||
|
close(d.c)
|
||
|
|
||
|
return nil
|
||
|
}
|