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 21
21
package grpcsync
22
22
23
23
import (
24
- "sync"
25
24
"sync/atomic"
26
25
)
27
26
28
27
// Event represents a one-time event that may occur in the future.
29
28
type Event struct {
30
- fired int32
29
+ fired atomic. Bool
31
30
c chan struct {}
32
- o sync.Once
33
31
}
34
32
35
33
// Fire causes e to complete. It is safe to call multiple times, and
36
34
// 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.
38
37
func (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 ) {
42
39
close (e .c )
43
- ret = true
44
- })
45
- return ret
40
+ return true
41
+ }
42
+ return false
46
43
}
47
44
48
45
// Done returns a channel that will be closed when Fire is called.
@@ -52,7 +49,7 @@ func (e *Event) Done() <-chan struct{} {
52
49
53
50
// HasFired returns true if Fire has been called.
54
51
func (e * Event ) HasFired () bool {
55
- return atomic . LoadInt32 ( & e .fired ) == 1
52
+ return e .fired . Load ()
56
53
}
57
54
58
55
// NewEvent returns a new, ready-to-use Event.
You can’t perform that action at this time.
0 commit comments