mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-29 16:58:34 +00:00
v1.0.0
This commit is contained in:
parent
47d23e9972
commit
c7f7c08ead
711 changed files with 82154 additions and 2 deletions
49
common/signal/done/done.go
Normal file
49
common/signal/done/done.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package done
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Instance is a utility for notifications of something being done.
|
||||
type Instance struct {
|
||||
access sync.Mutex
|
||||
c chan struct{}
|
||||
closed bool
|
||||
}
|
||||
|
||||
// New returns a new Done.
|
||||
func New() *Instance {
|
||||
return &Instance{
|
||||
c: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
// Done returns true if Close() is called.
|
||||
func (d *Instance) Done() bool {
|
||||
select {
|
||||
case <-d.Wait():
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Wait returns a channel for waiting for done.
|
||||
func (d *Instance) Wait() <-chan struct{} {
|
||||
return 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 *Instance) Close() error {
|
||||
d.access.Lock()
|
||||
defer d.access.Unlock()
|
||||
|
||||
if d.closed {
|
||||
return nil
|
||||
}
|
||||
|
||||
d.closed = true
|
||||
close(d.c)
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue