| 1 |
package latch |
| 2 |
|
| 3 |
import "sync/atomic" |
| 4 |
|
| 5 |
|
| 6 |
type Interface interface { |
| 7 |
|
| 8 |
Done() <-chan struct{} |
| 9 |
|
| 10 |
Close() |
| 11 |
|
| 12 |
Closed() bool |
| 13 |
} |
| 14 |
|
| 15 |
type L struct { |
| 16 |
value int32 |
| 17 |
line chan struct{} |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
func New() Interface { |
| 22 |
return new(L).Reset() |
| 23 |
} |
| 24 |
|
| 25 |
func (l *L) Done() <-chan struct{} { return l.line } |
| 26 |
|
| 27 |
|
| 28 |
func (l *L) Close() { |
| 29 |
if atomic.AddInt32(&l.value, 1) == 1 { |
| 30 |
close(l.line) |
| 31 |
} |
| 32 |
<-l.line |
| 33 |
} |
| 34 |
|
| 35 |
func (l *L) Closed() (result bool) { |
| 36 |
select { |
| 37 |
case <-l.line: |
| 38 |
result = true |
| 39 |
default: |
| 40 |
} |
| 41 |
return |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
func (l *L) Reset() *L { |
| 46 |
l.line, l.value = make(chan struct{}), 0 |
| 47 |
return l |
| 48 |
} |