Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: throttle goroutines on async chans #8900

dvyukov opened this issue Oct 7, 2014 · 1 comment

runtime: throttle goroutines on async chans #8900

dvyukov opened this issue Oct 7, 2014 · 1 comment


Copy link

dvyukov commented Oct 7, 2014

There is a common trick for reduction of contention on sync primitives. Unfortunately it
does not have a common name, but see mutexWaiterShift handling in sync/mutex.go for an
example. We can use it for async channels to reduce contention as well. Namely:

- add a waiterAwake flag to Hchan
- when a goroutine sends to async chan and there is a parked receiver, if
waiterAwake=false unpark it and set waiterAwake, otherwise do nothing
- if the woken receiver discovers empty queue, it resets waiterAwake before parking again
- if the woken receiver successfully dequeues from the queue, it does:
    if queue empty || no parked receivers {
      waiterAwake = false
    } else {
      unpark one parked receiver
      // responsibility to manage waiterAwake and subsequent parked receivers
      // transfers to that next recevier

This algorithm ensures that there is at most 1 excessive receiver loafs about producing
unnecessary contention. When this receivers goes away, it wakes up another receiver. And
so on.
Copy link

i3d commented Jul 6, 2015

Under the 11506's scenario, the sender goroutine will also participate in receiving the value (or an-already-running goroutine), so who gets to run once the parked receiver gets unparked?

Let's say in your proposed workflow,

goroutine 1 sent.
goroutine 2 unparked.

Now I think 1 and 2 are both possible to get dequeue the value. At this moment, how schedule picks which to run?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

3 participants