Replace Events polling with notifications - #392
Open
realFlowControl wants to merge 10 commits into
Open
Conversation
Events polling with notifications
realFlowControl
force-pushed
the
florian/notification-driven-events-v2
branch
from
August 4, 2026 17:03
2dac736 to
eca6fca
Compare
realFlowControl
marked this pull request as ready for review
August 5, 2026 05:08
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Events::poll()currently waits by repeatedly checking targets and callingusleep(1)every tenth unsuccessful attempt. A user waiting two seconds for a Channel or Future still spends significant CPU time polling. This change implements the pipe-backed wakeup approach suggested in this discussion, allowing the kernel to suspend the polling thread until a target changes state.Summary
select()on POSIXHow it works
Each Channel link owns separate readable and writable notification objects, while each Future monitor owns one notification for its
READYstate. A notification lazily creates a non-blocking, close-on-exec pipe whenEvents::poll()first needs to wait for that target. Creating pipes lazily avoids consuming file descriptors for targets that are already ready, are removed before polling, are only used by non-blocking Events, or never require a wait.Readiness changes are synchronized while holding the target's existing mutex. When a target changes from not ready to ready, one dummy byte is written to its notification pipe. When a Channel becomes unready again, that byte is drained. Repeated updates while the readiness level is unchanged do not add more bytes.
Events::poll()first scans the actual Channel and Future states. If nothing is ready, it collects their notification descriptors and blocks inselect(). Afterselect()wakes, it scans the actual states again under their locks and returns one event. The descriptors are only wakeup hints.Future readiness is terminal, so its notification remains raised after completion. This allows multiple independent Events instances to observe the same completed Future. Channel readability is lowered when its value is consumed, ensuring one channel value is delivered to one consumer. Closed Channels remain raised so all observers can see the close state.
Performance check
I ran a small demonstration with eight Channels. A Runtime sleeps for two seconds and then sends a value to one Channel while the main thread measures wall and process CPU time around
Events::poll(). Both versions were built locally against the same PHP 8.4 ZTS build on Darwin arm64 and run once.develop(6447fd2)This is intentionally a small, single-run demonstration, but it exercises the exact idle-wait behavior being changed.
developdoes not consume a full two CPU-seconds because its polling loop periodically callsusleep(1), but it still uses approximately half of one core while waiting. The notification path spends the same two seconds blocked inselect()and uses effectively no CPU.Limitations
Native waiting currently uses POSIX pipes and
select(). The existing polling behavior remains as a fallback on Windows, when pipe creation fails, when a descriptor is outsideFD_SETSIZE, or whenselect()fails.When
Events::setBlocker()is configured,poll()continues invoking that callback while no target is ready instead of enteringselect(), preserving the callback’s existing waiting and interruption semantics.