kernel: skip the locked read-modify-write when no signal is pending #1168

Closed
github-bot wants to merge 1 commit from gh-pr/1168/head into gh-pr/1168/base
Owner

@⁠romainhedouin on GitHub · 2026-10-07 22:37 UTC · original
romainhedouin:perf/pending-signal-fast-path → main · +3 −0 · 1 files · 1 commits · closed without merging

Whenever a thread wakes up from a sleep, a semaphore or a wait, it checks all 64 signal numbers for a pending signal. Each check was an atomic fetch_and, even though nothing is pending almost every time.

Now it reads the mask first and only does the fetch_and when the bit is actually set. A signal that arrives right after the read is simply picked up on the next poll.

On an M series Mac, checking all 64 signals on an empty mask takes 0.130 us instead of 0.208 us. With 4 threads checking the same thread object (synthetic) it is 0.13 us instead of 3.74 us.

What I tested, with a standalone test program built before and after the change: the bit semantics, out of range numbers, and 4 threads queuing signals while 3 others take them, checking each signal is delivered exactly once. It passes on main and on this branch, and it fails if I break the change on purpose (inverted check, or a load plus store instead of the fetch_and). The emulator does not build on arm64 macOS, so I linked the existing objects by hand and stubbed the 9 symbols from the x86 only files. Not run on x86_64.

I wrote this as a unit test but it seemed superfluous, let me know if I should add it to the PR

AI tools were used for code review and for drafting the checks I ran. I reviewed the diff and ran them

**@⁠romainhedouin** on GitHub · 2026-10-07 22:37 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1168) `romainhedouin:perf/pending-signal-fast-path` → `main` · +3 −0 · 1 files · 1 commits · closed without merging Whenever a thread wakes up from a sleep, a semaphore or a wait, it checks all 64 signal numbers for a pending signal. Each check was an atomic `fetch_and`, even though nothing is pending almost every time. Now it reads the mask first and only does the `fetch_and` when the bit is actually set. A signal that arrives right after the read is simply picked up on the next poll. On an M series Mac, checking all 64 signals on an empty mask takes 0.130 us instead of 0.208 us. With 4 threads checking the same thread object (synthetic) it is 0.13 us instead of 3.74 us. What I tested, with a standalone test program built before and after the change: the bit semantics, out of range numbers, and 4 threads queuing signals while 3 others take them, checking each signal is delivered exactly once. It passes on main and on this branch, and it fails if I break the change on purpose (inverted check, or a load plus store instead of the `fetch_and`). The emulator does not build on arm64 macOS, so I linked the existing objects by hand and stubbed the 9 symbols from the x86 only files. Not run on x86_64. I wrote this as a unit test but it seemed superfluous, let me know if I should add it to the PR AI tools were used for code review and for drafting the checks I ran. I reviewed the diff and ran them
PthreadTakePendingSignal did a fetch_and per signal number on every sleep or wait wake. Load the mask first and only clear the bit when it is set.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:46:08 +00:00
github-bot closed this pull request 2026-11-26 09:46:08 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.