Skip to content

Commit

Permalink
runtime: state more explicitly the behavior for buffered channels in …
Browse files Browse the repository at this point in the history
…the fast path under extreme conditions.

This information can be useful when implement something like this:

write side:

eventCh := make(chan struct{}, 1) // buffer size of exactly 1
// something happened before (X)
// notify via chan
select {
case eventCh <- struct{}{}:
default:
}

read side:

for {
     select {
         case <-eventCh:
         // new event comes, handle it
    }
}

This doc makes it explicit that right after `<-eventCh`, the `selectnbsend` in the write side is guaranteed to succeed.
  • Loading branch information
blockchaindevsh committed Dec 19, 2019
1 parent a1a67e6 commit 757cf4a
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/runtime/chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ func chansend(c *hchan, ep unsafe.Pointer, block bool, callerpc uintptr) bool {
// channel wasn't closed during the first observation. However, nothing here
// guarantees forward progress. We rely on the side effects of lock release in
// chanrecv() and closechan() to update this thread's view of c.closed and full().
//
// After lock release in chanrecv() and closechan(), c.closed and full() is guaranteed to be observed here.
// For buffered channels, this fast path will always be false right after a successful chanrecv().
if !block && c.closed == 0 && full(c) {
return false
}
Expand Down

0 comments on commit 757cf4a

Please sign in to comment.