Repository navigation
Add timed wait to sys.thread.Condition - #13109
Conversation
Declare `wait(?timeout:Float):Bool` and document its semantics. The target implementations follow in later commits. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
Use the new hxcpp `ConditionVariable::timedWait`, and install the hxcpp branch that adds it in CI until HaxeFoundation/hxcpp#1456 is merged. Add thread tests for timed waits: timing out with the mutex still held, being signaled, non-positive and NaN timeouts, and huge timeouts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
hl uses the existing `condition_timed_wait` native and clamps the timeout itself, because older runtimes don't. CI builds the HashLink branch with the native fixes until HaxeFoundation/hashlink#1008 is merged. neko uses the new `cond_timed_wait` primitive when it is available (HaxeFoundation/neko#336), and otherwise polls with growing sleeps, using a counter that `signal` and `broadcast` increment to detect notifications. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
The build job's xmldoc step checks every target's std against the core API, so jvm and python must implement the new signature before CI can run any tests. - jvm: Condition.await with nanoseconds, like Semaphore.tryAcquire. - python: threading.Condition.wait, clamped to threading.TIMEOUT_MAX, above which Python raises an OverflowError. - eval: OCaml's Condition has no timed wait, so poll with growing sleeps like StdLock.wait and detect notifications with a counter that signal and broadcast increment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
The docgen job builds dox for cpp, which includes sys.thread.Condition, and installs hxcpp separately from the cpp tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
|
I'm fine with the change in the context of this PR, but could you check again if there's really no way to have a cross-platform non-polling wait for Eval? I know that it's tricky because Windows is weird, but let's take another look at this. |
|
I looked again. There is a non-polling option that works on every platform: back eval's
The change would be confined to
One thing to note: I'm happy to switch to this in this PR. CI exercises eval on Linux, macOS and Windows, so we'd find out straight away. Or I can do it in a follow-up. Generated by Claude Code |
Int64.fromFloat throws for values above 2^53, so a timeout above about 104 days threw while the mutex was held, and testTimedWaitHuge hung. Cap the wait at 9e6 seconds and report a capped wait that runs out as a spurious wakeup instead of a timeout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
Replace the polling timed wait with Luv.Condition.timedwait, like Lock and Semaphore since #13110, which also removes the notification counter. libuv's mutex is recursive on Windows and aborts the process on misuse on POSIX, while OCaml's mutex raised Sys_error. Track the owning eval thread so that the mutex stays non-recursive on every platform and acquiring twice, or releasing or waiting without holding the mutex, throws a Haxe exception instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
|
eval's It addresses the two
Tested locally:
Generated by Claude Code |
|
In #13110 we had this:
Could you check if this also applies to any of the related PRs here? |
|
I checked every Windows path. None of them overflows, but two of them reported a timeout too early. Both are now fixed:
So now every target reports I compiled the new Windows branches of HashLink and Neko with Generated by Claude Code |
HaxeFoundation/hashlink#1008 has been merged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
|
Just confirming, was the change to the HL PR something to consider for the neko or hxcpp PRs too? |
|
Yes for Neko; it's already done. hxcpp doesn't need it.
Generated by Claude Code |
HaxeFoundation/hxcpp#1456 has been merged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
On Windows, MSVC's condition_variable_any::wait_for reports no_timeout when the OS wait returns slightly before the deadline, so a single wait(0.1) can return true. The API allows that as a spurious wakeup, so wait in a loop until a timeout is reported, like callers are expected to. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
|
I need a sanity check on the recursive mutex situation. Does this really only apply to Eval on Windows? |
|
No, it's broader than that. Eval on Windows was only where this PR would have introduced recursion (libuv's
This PR keeps eval non-recursive, as it was before, on every platform. It doesn't change any other target's recursion. The differences also affect If we want a defined behaviour, I'd specify
I'd keep that out of this PR, which doesn't change how locking behaves. Generated by Claude Code |
As discussed in review: wait() keeps its original signature and documentation, and the timed variant becomes timedWait(timeout):Bool, whose result always means something. This follows Semaphore, which has a blocking acquire():Void and a separate tryAcquire(?timeout):Bool. The target implementations are unchanged apart from the split. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
|
Fixed by merging Generated by Claude Code |
Relax the documentation: very large timeouts may be cut short at a target-specific limit, and such a wait returns true. Make python, cpp, hl and neko return true when the timeout exceeds their cap, as jvm and eval already do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
* Use Condition.timedWait in WakeUpFlag sys.thread.Condition got a timed wait in HaxeFoundation/haxe#13109, so WakeUpFlag can block on a condition variable instead of a semaphore. This removes the invariant that exactly one permit exists while the state isn't Unset: clear() no longer has to take a permit that a racing setter may not have released yet, and requestWakeUp() becomes a single exchange instead of a CAS loop. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXGMZFfSGDAqgHttxSU * Fold the timed wait into WakeUpFlag.waitUntil Both schedulers already loop around waitUntil, recomputing the due time after every return, so the wait itself doesn't need its own loop for spurious wake-ups and the remaining time. waitUntil now waits once and may return early, which replaces waitAndClear and tryWaitAndClear. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXGMZFfSGDAqgHttxSU --------- Co-authored-by: Claude <noreply@anthropic.com>
Adds a timed wait to
sys.thread.Conditionon every threaded target. The native changes it uses are all merged (HaxeFoundation/hxcpp#1456, HaxeFoundation/hashlink#1008, HaxeFoundation/neko#336), and CI uses the default branches of hxcpp and HashLink again.API
wait()keeps its signature and documentation. FollowingSemaphore(blockingacquire():Voidplus a separatetryAcquire(?timeout):Bool), the timed variant is a separate method whose result always means something.timeoutis relative and in seconds.falseonly if the timeout expired, andtruewhen signalled or on a spurious wakeup.Per-target work
std/sys/thread/Condition.hxthreading.Condition.wait(timeout), clamped tothreading.TIMEOUT_MAX, above which Python raisesOverflowError. That limit is about 49 days on Windows.Condition.await(nanos, TimeUnit.NANOSECONDS), likeSemaphore.tryAcquire. Each wait is capped at 9e6 s, becauseInt64.fromFloatthrows above 2^53 ns. A capped wait that runs out counts as a spurious wakeup.condition_timed_wait, which HashLink has had since at least 1.12. The Haxe side clamps the timeout itself, because older runtimes don't.true, and negative or huge timeouts broke on all platforms. On POSIX, a1e12s timeout overflowed and timed out immediately. Older Windows runtimes will still report every timed-out wait astrue.cond_timed_wait([std] Add cond_timed_wait primitive neko#336, merged) when the runtime has it. Otherwise, for example on Neko 2.4.1 and older, it polls with growing sleeps (at most 10 ms) and a counter thatsignal/broadcastincrement, so it can tell a real notification from a timeout.neko_latestbinaries, so it switches from the fallback to the native primitive once a build that includes [std] Add cond_timed_wait primitive neko#336 is published. That native wait uses a monotonic clock.ConditionVariable::timedWait(double)from Add timedWait to ConditionVariable hxcpp#1456 (merged)Luv.MutexandLuv.Condition, likeLockandSemaphoresince [eval] Use libuv mutex and condition for Lock and Semaphore #13110, sotimedWaitis a real timed wait and doesn't poll.abort()on misuse elsewhere, so the owning eval thread is tracked. The mutex stays non-recursive on every platform. Acquiring it twice, or releasing or waiting without holding it, throws a Haxe exception instead of aborting or undefined behaviour.Tests (
tests/threads/src/cases/TestCondition.hx)falseand the mutex is still held (it loops, since a single wait may wake spuriously)timedWait(0),timedWait(-1)andtimedWait(NaN)returnfalseConditionitself.Follow-ups (out of scope)
Lock.wait(?timeout):Bool,Semaphore.tryAcquire(?timeout):BoolandCondition.timedWait(timeout):Boolnow have three different shapes. Making them consistent could be a separate step.hl_semaphore_try_acquireandhl_lock_waithave the same timeout overflow that Fix timeout handling in hl_condition_timed_wait hashlink#1008 fixed inhl_condition_timed_wait, which can serve as their reference.lock_wait, compute deadlines fromgettimeofday, the wall clock. [std] Add cond_timed_wait primitive neko#336 shows the monotonic replacement:CLOCK_MONOTONICviapthread_condattr_setclock, andpthread_cond_timedwait_relative_npon macOS.Conditionis recursive on hl, jvm and python but not on cpp, neko and eval. Specifying it as non-recursive would need changes on those targets.CountingSemaphore.apple.cpptruncates the timeout to whole seconds before converting it.Semaphore.tryAcquireuses the sameInt64.fromFloatconversion, so it throws for timeouts above about 104 days.🤖 Generated with Claude Code
https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu