Skip to content

[Agc] A marker from another thread skips a command buffer that native packet stubs are writing - #77

Open
xnetcat wants to merge 1 commit into
mainfrom
fix/agc-stub-append-ownership
Open

xnetcat wants to merge 1 commit into
mainfrom
fix/agc-stub-append-ownership

Conversation

@xnetcat

@xnetcat xnetcat commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Before submitting

Please read our contribution guidelines before opening a pull request:

➡️ CONTRIBUTING.md

By opening this pull request, you confirm that you have read and agree to follow the contribution guidelines.

Upstream 99f9fe9 (merged here in the 2026-11-21 sync) serializes HLE appends to one AGC command buffer under a striped lock and remembers each buffer's last ordinary writer, so a Push/PopMarker from another guest thread is skipped instead of reserving space inside the writer's packet. The native packet stubs from upstream #1196 (identical in this fork) append in guest context: the fixed-packet stubs (register direct and indirect setters, index setters, DrawIndexAuto, DrawIndexOffset, DrawIndex, DrawIndexIndirect, DrawIndirect, CbDispatch), sceAgcCbSetShRegisterRangeDirect and sceAgcCbSetShRegistersDirect advance the cursor themselves, take no lock and never reach the writer table. A buffer written only through them has no writer, or a stale one naming the marker's thread, so that marker was not skipped. The patcher, payload-address and CreatePrimState stubs do not move a cursor and are unchanged.

Each cursor-advancing stub now records (buffer, guest TLS base read from the host TLS slot the backend binds per guest thread) in a 256-slot host table, one cache line per slot, after its stores: 9 instructions, no branch. A marker refuses a buffer whose slot names another thread (logged for the first 4, counted); an ordinary export clears the slot when it takes the buffer over, as it already takes over from an export writer. Ordinary appends racing each other are not changed: the console's builders are lock-free guest code, so that would be a race in the game itself. SHARPEMU_AGC_STUB_WRITER=0 turns the record off (documented in docs/macos/configuration.md).

Testing

No game was run. The cost on the Demon's Souls hot path has not been measured.

  • AgcCommandBufferConcurrencyTests.PushMarker_FromAnotherThread_IsSkippedOnABufferANativeStubWroteLast covers a stub-only buffer, the writer's own marker, a handoff through an ordinary export, and a stale writer table. It fails without the AgcExports check (thread B's marker lands at the cursor A's stub just published) and passes with it.
  • AgcPacketStubTests.TheWriterRecordIsTheTableStoreOfTheCallingThreadsTlsBaseBeforeTheFastPathReturns decodes the emitted record and checks that it comes before every fast-path ret of the three stub kinds. It runs on any host.
  • AgcPacketStubTests.ACursorAdvancingStubRecordsItsBufferAndTheCallingThreadInTheWriterTable runs only on an x64 testhost. It executes the stubs and checks the table slot, and fails when the record is not emitted.
  • Release build: 0 errors. Full dotnet test SharpEmu.slnx -c Release on arm64: 0 failed. x64 (Rosetta) Libs subset Cpu|Kernel|Pthread|Loader|Agc: 3642 passed, 1 failed (GuestTsc_ReadAndFrequencyDescribeTheSameClock, a clock-ratio timing test that measured 1.18 under load). It passed 5 of 5 times run alone. uvx reuse lint passes.

Checklist

By submitting this pull request, you confirm that:

  • I have read and followed CONTRIBUTING.md.
  • I tested my changes or marked the testing section as N/A.
  • I wrote this pull request description myself and did not paste AI-generated explanations.
  • I listed the game(s) I tested (or marked the testing section as N/A).

… skip stub-written buffers

The cursor-advancing native stubs (fixed packets, sceAgcCbSetShRegisterRangeDirect,
sceAgcCbSetShRegistersDirect; upstream #1196) append in guest context without the
stripe lock or the writer table that upstream 99f9fe9 added. A buffer written
only through them has no writer, or a stale one, so a Push/PopMarker from another
guest thread was not skipped and reserved space at the cursor the stub was about
to store past.

Each such stub now stores (buffer, guest TLS base from the host TLS slot) in a
host table after its stores (one cache line per slot, 9 instructions, no branch).
A marker refuses a buffer whose slot names another thread (logged, counted); an
ordinary export clears the slot as it takes the buffer over. SHARPEMU_AGC_STUB_WRITER=0
turns it off.

Tests: the marker decision (red without the AgcExports check), the emitted record
decoded on any host, and on x64 the stubs leaving the record (red without it).
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

📦 Build artifacts — 546ffc1

Platform Download
macOS (osx-x64) sharpemu-osx-x64-e24f1a8
macOS app bundle (osx-x64) sharpemu-osx-x64-app-e24f1a8

From build run #573. Downloads require a GitHub login and expire after 90 days.

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