File tree Expand file tree Collapse file tree 1 file changed +8
-11
lines changed Expand file tree Collapse file tree 1 file changed +8
-11
lines changed Original file line number Diff line number Diff line change 2121package grpcsync
2222
2323import (
24- "sync"
2524 "sync/atomic"
2625)
2726
2827// Event represents a one-time event that may occur in the future.
2928type Event struct {
30- fired int32
29+ fired atomic. Bool
3130 c chan struct {}
32- o sync.Once
3331}
3432
3533// Fire causes e to complete. It is safe to call multiple times, and
3634// concurrently. It returns true iff this call to Fire caused the signaling
37- // channel returned by Done to close.
35+ // channel returned by Done to close. If Fire returns false, it is possible
36+ // the Done channel has not been closed yet.
3837func (e * Event ) Fire () bool {
39- ret := false
40- e .o .Do (func () {
41- atomic .StoreInt32 (& e .fired , 1 )
38+ if e .fired .CompareAndSwap (false , true ) {
4239 close (e .c )
43- ret = true
44- })
45- return ret
40+ return true
41+ }
42+ return false
4643}
4744
4845// Done returns a channel that will be closed when Fire is called.
@@ -52,7 +49,7 @@ func (e *Event) Done() <-chan struct{} {
5249
5350// HasFired returns true if Fire has been called.
5451func (e * Event ) HasFired () bool {
55- return atomic . LoadInt32 ( & e .fired ) == 1
52+ return e .fired . Load ()
5653}
5754
5855// NewEvent returns a new, ready-to-use Event.
You can’t perform that action at this time.
0 commit comments