Skip to content

Share the audio poll cursor across threads - #411

Open
patchzyy wants to merge 1 commit into
mainfrom
fix-audio-poll-double-count
Open

patchzyy wants to merge 1 commit into
mainfrom
fix-audio-poll-double-count

Conversation

@patchzyy

@patchzyy patchzyy commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

What

ConsumeAudioPollDeltaMicros kept its last-poll time in a thread_local, assuming every audio pump runs on the guest thread. That isn't true: when the guest blocks in a FIFO drain, the "Aurora FIFO processor" thread runs ServiceGuestTimingDuringAuroraFrameWait → Audio_HLE_PollDeferred through aurora's producer wait callback. Each thread then credited the same stall (up to the 100 ms cap), the extra audio was pushed in a burst, and QueueHasCapacityLocked dropped the blocks that didn't fit in the 120 ms queue. Every dropped 3 ms block is a hard splice in the output.

The cursor is now a single process-wide atomic, advanced with a CAS. The 100 ms cap and the sub-microsecond remainder behave as before, and a caller whose timestamp is already behind the cursor gets 0.

Testing

PAL, --race-cpu, cold shader cache, recording what SDL actually sends to the device:

  • Before: 0.09–0.55 s of time credited twice per run, 27–37 dropped blocks right after the track load, and the output queue full warning in every run. Some runs had an audible splice during the race intro.
  • After: no double counting in any run, no drops at race start, and no warning (including a clean build with the instrumentation removed).

This removes one source of race-start crackle. It doesn't cover dropouts from host stalls longer than the buffer, and I couldn't reproduce the online countdown reports locally, so this may not be the whole story for those.

Summary by CodeRabbit

  • Bug Fixes
    • Audio timing is now accounted for consistently when multiple audio processing tasks run concurrently, preventing the same elapsed interval from being counted more than once.
    • Large timing gaps are capped, and smaller intervals retain fractional precision. Backwards time intervals are ignored without advancing the timing reference, helping keep subsequent audio timing accurate.

Audio_HLE_PollDeferred also runs on Aurora's FIFO processor thread (via the
producer wait callback) while the guest blocks in a FIFO drain. The
thread_local cursor let each thread credit the same stall, and the extra
blocks overflowed the 120 ms output queue as dropped blocks.
@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The audio poll timer now uses a process-wide atomic cursor. Concurrent pumps claim elapsed time with compare-and-exchange. The calculation handles negative intervals, caps intervals above 100,000 microseconds, and retains sub-microsecond remainders.

Changes: Audio poll timing

Layer / File(s) Summary
Shared poll cursor
runtime/src/hle/audio/audio.cpp
ConsumeAudioPollDeltaMicros now uses a process-wide atomic cursor and compare-and-exchange to claim elapsed time. Negative intervals return zero without changing the cursor. Intervals above 100,000 microseconds return the cap and advance the cursor to the current time. Uncapped intervals retain the sub-microsecond remainder.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to 74a8b

This change makes the audio poll timer share one cursor across threads, which stops the same stall from being counted twice during a FIFO drain. It should reduce audio crackle at race start. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the audio poll cursor moves from thread-local storage to a process-wide cursor shared across threads. The raw summary confirms this change in runtime/src/hle/audio/aud…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the audio poll cursor moves from thread-local storage to a process-wide cursor shared across threads. The raw summary confirms this change in runtime/src/hle/audio/audio.cpp, and the objectives describe the atomic compare-and-swap cursor that fixes double-counting between the guest thread and the FIFO processor thread. The title is concise, specific, and has no noise.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b4627e2a-7172-4b7d-be4a-423e22815931
Commits

Reviewing files that changed from the base of the PR and between ffbde83 and 74a8b10.

Files selected for processing: 1

  • runtime/src/hle/audio/audio.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant