sharpemu/tests/SharpEmu.Libs.Tests/Pthread
kuba 6db42fe981 [Cpu] Answer pthread_self and pthread_getspecific in native stubs
The managed import gateway costs a guest-to-host stack switch, a full guest
register pack, a GCHandle dereference and the whole dispatch bookkeeping
before the HLE export even runs. For the two hottest trivial imports that is
three orders of magnitude more work than the answer itself: one UE5 title's
load measured 12.9M scePthreadGetspecific calls (38 s of wall time) and 4.3M
scePthreadSelf calls, at roughly 2.9 us each.

The few values those two imports need are now published into a per-host-thread
native block whose address lives in a native TLS slot, rebound whenever a
thread enters or is restored as a guest thread, and the two imports are
patched to emitted x86-64 stubs that read the block directly. pthread-specific
values live in native per-thread tables so a stub and the managed export read
exactly the same storage and cannot drift apart. The managed path stays the
source of truth for every case a stub declines -- no block published, no guest
thread bound, no value table, a key outside the table -- which it reaches by
jumping to the import's normal trampoline with every register intact,
including the incoming RAX variadic vector count.

The value-table slot follows whichever handle the current thread reports, the
bound guest thread or the host thread's own synthetic handle, so
pthread_getspecific is answered on unbound host threads as well; the primary
execution thread never enters through the guest-thread path and is a major
caller in UE5 titles. The self-handle slot stays guest-only, because
pthread_self on an unbound thread registers that thread's CPU context with the
guest scheduler and must keep reaching the managed export. Each stub also
bumps a per-thread hit counter, reported next to the existing HLE cost
histogram, so a call that misses the stub is visible rather than invisible.

The stubs are gated to macOS x64, where the emitted TLS read matches the
platform's pthread key array, and switched off whenever import or pthread
tracing needs every call to pass through the gateway.
2026-09-29 23:15:00 +02:00
..
PthreadCondAttrSemanticsTests.cs Alpha Rebuild Progress (#887) 2026-09-20 04:09:27 +03:00
PthreadCondSemanticsTests.cs [HLE] Fix POSIX condition variable semantics (#113) (#223) 2026-07-16 00:24:05 +03:00
PthreadMutexSemanticsTests.cs Alpha Rebuild Progress (#887) 2026-09-20 04:09:27 +03:00
PthreadRwlockSemanticsTests.cs Alpha Rebuild Progress (#887) 2026-09-20 04:09:27 +03:00
PthreadSemaphoreSemanticsTests.cs [Kernel] Implement pthread semaphore exports (#424) 2026-07-19 04:18:16 +03:00
PthreadSpecificSemanticsTests.cs [Cpu] Answer pthread_self and pthread_getspecific in native stubs 2026-09-29 23:15:00 +02:00