Skip to content

Commit

Permalink
socket: use atomic.Bool for needDisarm
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Layher <[email protected]>
  • Loading branch information
mdlayher committed Aug 23, 2023
1 parent 1c44ffe commit f8fb5bb
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,10 +739,7 @@ func rwT[T any](c *Conn, rw rwContext[T]) (T, error) {
doneC = make(chan struct{})

// Atomic: reports whether we have to disarm the deadline.
//
// TODO(mdlayher): switch back to atomic.Bool when we drop support for
// Go 1.18.
needDisarm int64
needDisarm atomic.Bool
)

// On cancel, clean up the watcher.
Expand All @@ -758,7 +755,7 @@ func rwT[T any](c *Conn, rw rwContext[T]) (T, error) {
return *new(T), err
}
setDeadline = true
atomic.AddInt64(&needDisarm, 1)
needDisarm.Store(true)
} else {
// The context does not have an explicit deadline. We have to watch for
// cancelation so we can propagate that signal to immediately unblock
Expand All @@ -774,7 +771,7 @@ func rwT[T any](c *Conn, rw rwContext[T]) (T, error) {
case <-rw.Context.Done():
// Cancel the operation. Make the caller disarm after poll
// returns.
atomic.AddInt64(&needDisarm, 1)
needDisarm.Store(true)
_ = deadline(time.Unix(0, 1))
case <-doneC:
// Nothing to do.
Expand All @@ -792,7 +789,7 @@ func rwT[T any](c *Conn, rw rwContext[T]) (T, error) {
return ready(err)
})

if atomic.LoadInt64(&needDisarm) > 0 {
if needDisarm.Load() {
_ = deadline(time.Time{})
}

Expand Down

0 comments on commit f8fb5bb

Please sign in to comment.