Skip to content

Commit 709023d

Browse files
authored
grpcsync/event: Simplify synchronization (#8308)
1 parent d36b02e commit 709023d

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

internal/grpcsync/event.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,25 @@
2121
package grpcsync
2222

2323
import (
24-
"sync"
2524
"sync/atomic"
2625
)
2726

2827
// Event represents a one-time event that may occur in the future.
2928
type 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.
3837
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) {
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.
5451
func (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.

0 commit comments

Comments
 (0)