Skip to content

Commit 47e4404

Browse files
ctillercopybara-github
authored andcommitted
[promises] MPSC Rewrite
Solve the thundering herd problem that was caused by the previous wakeup logic (now that that's becoming a problem) This change adapts the algorithm in mpscq (originally from dvyukov) for promises, and adds some useful functionality on top. The queue is closable: it's possible for the sender or receiver to unilaterally close the queue and force errors on send and receive (and all existing work thrown away). The queue is bounded: pushback is applied at the sender if the queue is too full. An API exists to allow readers of the queue to persist the tokens queued in the MPSC until the work item is complete sometime after - chaotic good will use this to allow messages to be kept outstanding until they are actually written to a socket. To arrange all this, the queue is split into three parts: 1. An unbounded MPSC of incoming data. If there is too much data queued we set a blocking bit, and the queuing promise will block until that bit is cleared. In the case of waiters, where the original mpscq would return nullptr, the new queue CAS's in a Waker object so we can get exact wakeup pingponging between reader and writer when that is appropriate. 2. A single threaded SPSC of "accepted" items. This (plus the items in #3) form the bound of the queue. When we are reading and under bound we pull in as many nodes as possible early (so as to kick start other systems to producing the next batch of work), and queue it in a separate SPSC. Next() can then pull from that directly for a small period. 3. A sea of accepted but not complete work items. The Queued<> type tracks these. When the objects are released, we finally release the tokens, and in doing so allow more work to be accepted. PiperOrigin-RevId: 766368817
1 parent c286931 commit 47e4404

File tree

23 files changed

+1394
-590
lines changed

23 files changed

+1394
-590
lines changed

CMakeLists.txt

Lines changed: 57 additions & 93 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)