Skip to content

Add timedWait to ConditionVariable - #1456

Merged
Aidan63 merged 1 commit into
masterfrom
claude/jolly-wozniak-93trgh
Nov 5, 2026
Merged

Aidan63 merged 1 commit into
masterfrom
claude/jolly-wozniak-93trgh

Conversation

@Simn

@Simn Simn commented Nov 1, 2026

Copy link
Copy Markdown
Member

Adds bool ConditionVariable_obj::timedWait(double seconds), the native side of the timed sys.thread.Condition.wait(?timeout:Float):Bool in HaxeFoundation/haxe#13109.

  • Uses std::condition_variable_any::wait_for inside hx::AutoGCFreeZone, like wait(). It waits on the monotonic clock.
  • Returns false if the timeout expired and true otherwise (signalled or a spurious wakeup). The mutex is held again on return in both cases.
  • Zero, negative and NaN timeouts don't block. Timeouts are capped at 1e9 seconds (about 31 years), so the nanosecond deadline can't overflow.
  • wait() is unchanged, so existing Haxe code that uses Condition keeps working.

Testing

  • The changed file compiles against hxcpp's headers (g++ -std=c++17 -fsyntax-only).
  • A standalone copy of the wait logic checked the expected behaviour: a 0.1 s timeout returns false after 0.1 s with the mutex held; 0, -1 and NaN return false at once; a 1e12 timeout returns true when signalled after 0.1 s.
  • End-to-end tests run through Haxe's tests/threads (TestCondition) on Add timed wait to sys.thread.Condition haxe#13109. That branch's CI will install this hxcpp branch until this is merged.

Generated by Claude Code

Wait for at most the given number of seconds and return false if the
timeout expired. Non-positive and NaN timeouts don't block, and very
large ones are clamped so that the deadline does not overflow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
Simn pushed a commit to HaxeFoundation/haxe that referenced this pull request Nov 1, 2026
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
@Simn

Simn commented Nov 5, 2026

Copy link
Copy Markdown
Member Author

@Aidan63 Could you check this?

@Aidan63
Aidan63 merged commit 028bca6 into master Nov 5, 2026
63 checks passed
Simn pushed a commit to HaxeFoundation/haxe that referenced this pull request Nov 5, 2026
HaxeFoundation/hxcpp#1456 has been merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
@tobil4sk
tobil4sk deleted the claude/jolly-wozniak-93trgh branch November 5, 2026 10:51
Simn added a commit to HaxeFoundation/haxe that referenced this pull request Nov 6, 2026
* Add optional timeout to sys.thread.Condition.wait

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

* [cpp] Implement Condition.wait with timeout, add tests

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] [neko] Implement Condition.wait with timeout

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

* [jvm] [python] [eval] Implement Condition.wait with timeout

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

* [ci] Use the hxcpp branch with timedWait for docgen as well

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

* [jvm] Fix Condition.wait with huge timeouts

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

* [eval] Use libuv mutex and condition for Condition

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

* [ci] Build HashLink from its default branch again

HaxeFoundation/hashlink#1008 has been merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu

* [ci] Install hxcpp from its default branch again

HaxeFoundation/hxcpp#1456 has been merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu

* [tests] Allow spurious wakeups in testTimedWaitTimesOut

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

* Split Condition.wait(?timeout) into wait() and timedWait(timeout)

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

* Treat capped timedWait as spurious wakeup on all targets

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
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.

3 participants