Honor the condvar clock in pthread_cond_timedwait on Darwin - #58
Open
HashimTheArab wants to merge 1 commit into
Open
Honor the condvar clock in pthread_cond_timedwait on Darwin#58HashimTheArab wants to merge 1 commit into
HashimTheArab wants to merge 1 commit into
Conversation
Darwin has no pthread_condattr_setclock, so every game condvar was a CLOCK_REALTIME wait while the game arms deadlines on CLOCK_MONOTONIC (WebRTC/NetherNet does). A monotonic deadline read as realtime is in 1970, the wait times out instantly and the thread spins: one NetherNet Signal thread alone burned a full core at the main menu on macOS. Keep the clock beside each host condvar and turn monotonic deadlines into pthread_cond_timedwait_relative_np; pthread_cond_timedwait_monotonic_np is now its own entry point instead of aliasing the realtime one. Measured: main-menu CPU 100% -> 3-4%. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012bijEWrxdPwBcdeTP6iyXj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
host_condattrskipspthread_condattr_setclockunder__APPLE__(Darwin has no such call), so every game condvar is a CLOCK_REALTIME wait on the host, while the game arms deadlines on CLOCK_MONOTONIC (WebRTC / NetherNet does, viapthread_condattr_setclock— andpthread_cond_timedwait_monotonic_npwas aliased straight to the realtimepthread_cond_timedwait).A monotonic deadline interpreted as realtime is a timestamp in 1970, so the wait returns
ETIMEDOUTimmediately and the caller loops. Sampled on macOS 26 / Apple Silicon at the main menu of 1.26.45: the NetherNet Signal thread had 77% of its samples insideshim::pthread_cond_timedwait → _pthread_cond_wait → __gettimeofday, pinning one full core forever. This is the same class of symptom as minecraft-linux/mcpelauncher-manifest#1009.Change
Keep the clock beside each host condvar (
host_cond { pthread_cond_t cond; clockid_t clock; }— the cond stays the first member, soto_hostis unchanged) and, on Apple, turn monotonic deadlines into relative waits withpthread_cond_timedwait_relative_np. Static-initialised condvars default to CLOCK_REALTIME as bionic does.pthread_cond_timedwait_monotonic_npgets its own entry point that always treats the deadline as monotonic. Non-Apple behaviour is unchanged.Result
Main-menu CPU of
mcpelauncher-client-arm64-v8a(hidden window, 10 fps cap so rendering is out of the picture): 100% → 3–4%, andgettimeofdaydisappears from the profile. Game plays normally.🤖 Generated with Claude Code
https://claude.ai/code/session_012bijEWrxdPwBcdeTP6iyXj