TrackingSpinLock asks the OS for the thread id on every lock and unlock on macOS #1171

Closed
opened 2026-11-26 09:46:18 +00:00 by github-bot · 0 comments
Owner

@⁠romainhedouin on GitHub · 2026-10-07 22:56 UTC · original

TrackingSpinLock remembers which thread holds it, to catch a recursive lock and an unlock from the wrong thread. For that, lock() and unlock() both call CurrentThread():

github.com/KytyPS5/KytyPS5@420bb51d6c/src/graphics/host_gpu/regionManager.h (L37-L38)

github.com/KytyPS5/KytyPS5@420bb51d6c/src/graphics/host_gpu/regionManager.h (L51-L52)

On Linux the id is cached in a thread_local, so it is looked up once per thread:

github.com/KytyPS5/KytyPS5@420bb51d6c/src/graphics/host_gpu/regionManager.h (L64-L66)

On macOS it is looked up again every time, so each lock and unlock pair calls pthread_mach_thread_np(pthread_self()) twice:

github.com/KytyPS5/KytyPS5@420bb51d6c/src/graphics/host_gpu/regionManager.h (L62-L63)

Windows calls GetCurrentThreadId, which is a cheap read, so it does not have this problem.

The lock is not rarely used. It is taken on every memory tracker query, once per region touched:

github.com/KytyPS5/KytyPS5@420bb51d6c/src/graphics/host_gpu/memoryTracker.cpp (L72)

and it is the texture cache lock:

github.com/KytyPS5/KytyPS5@420bb51d6c/src/graphics/host_gpu/renderer/cache/textureCache.h (L168)

Both are hit while preparing every draw.

I measured an uncontended lock and unlock with a small standalone program against the real header: 8.64 ns today, 2.88 ns with the id cached (Apple Silicon). My rough estimate is about 25 lock pairs per draw, so around 0.15 ms per frame at 1000 draws. Small, but the fix is three lines and only makes macOS behave like Linux.

Fix: #1169

**@⁠romainhedouin** on GitHub · 2026-10-07 22:56 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1171) `TrackingSpinLock` remembers which thread holds it, to catch a recursive lock and an unlock from the wrong thread. For that, `lock()` and `unlock()` both call `CurrentThread()`: https://github.com/KytyPS5/KytyPS5/blob/420bb51d6c0de9ea1cc4a1b065b6dc605f4f030e/src/graphics/host_gpu/regionManager.h#L37-L38 https://github.com/KytyPS5/KytyPS5/blob/420bb51d6c0de9ea1cc4a1b065b6dc605f4f030e/src/graphics/host_gpu/regionManager.h#L51-L52 On Linux the id is cached in a `thread_local`, so it is looked up once per thread: https://github.com/KytyPS5/KytyPS5/blob/420bb51d6c0de9ea1cc4a1b065b6dc605f4f030e/src/graphics/host_gpu/regionManager.h#L64-L66 On macOS it is looked up again every time, so each lock and unlock pair calls `pthread_mach_thread_np(pthread_self())` twice: https://github.com/KytyPS5/KytyPS5/blob/420bb51d6c0de9ea1cc4a1b065b6dc605f4f030e/src/graphics/host_gpu/regionManager.h#L62-L63 Windows calls `GetCurrentThreadId`, which is a cheap read, so it does not have this problem. The lock is not rarely used. It is taken on every memory tracker query, once per region touched: https://github.com/KytyPS5/KytyPS5/blob/420bb51d6c0de9ea1cc4a1b065b6dc605f4f030e/src/graphics/host_gpu/memoryTracker.cpp#L72 and it is the texture cache lock: https://github.com/KytyPS5/KytyPS5/blob/420bb51d6c0de9ea1cc4a1b065b6dc605f4f030e/src/graphics/host_gpu/renderer/cache/textureCache.h#L168 Both are hit while preparing every draw. I measured an uncontended lock and unlock with a small standalone program against the real header: 8.64 ns today, 2.88 ns with the id cached (Apple Silicon). My rough estimate is about 25 lock pairs per draw, so around 0.15 ms per frame at 1000 draws. Small, but the fix is three lines and only makes macOS behave like Linux. Fix: #1169
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:46:18 +00:00
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/KytyPS5--KytyPS5#1171
No description provided.