Skip to content

[std] Add cond_timed_wait primitive - #336

Merged
Simn merged 4 commits into
masterfrom
claude/jolly-wozniak-93trgh
Nov 6, 2026
Merged

Simn merged 4 commits into
masterfrom
claude/jolly-wozniak-93trgh

Conversation

@Simn

@Simn Simn commented Nov 1, 2026

Copy link
Copy Markdown
Member

I've seen the deprecation notice. This is a small, self-contained addition, so I'm opening it in case it can still go in.

Adds std@cond_timed_wait : 'condition -> timeout:number -> bool. It backs the new sys.thread.Condition.wait(?timeout:Float):Bool in HaxeFoundation/haxe#13109.

  • It waits for at most timeout seconds and returns false if the timeout expired, true otherwise (signalled or a spurious wakeup). The mutex is held again on return in both cases.
  • POSIX uses pthread_cond_timedwait, with a 64-bit deadline computed from gettimeofday, like lock_wait. Windows uses SleepConditionVariableSRW and returns false on ERROR_TIMEOUT.
  • Zero, negative and NaN timeouts don't block. Timeouts are capped at 1e9 seconds (about 31 years) so the deadline can't overflow. On Windows the cap also stops the wait reaching INFINITE.

Haxe can't depend on this being present, since Neko 2.4.1 and older don't have it. The Haxe side will fall back to a polling loop when the primitive can't be loaded.

Testing

  • Added tests/cond_timed_wait.neko and a ctest entry for it. It checks that a 0.1 s wait times out with the mutex still held, that 0, -1, NaN and integer 0 timeouts return false, and that a 1e12 s wait returns true when another thread signals it.
  • On Linux the build (with optional libraries off) has no warnings in thread.c, and ctest -R cond_timed_wait passes.
  • I couldn't run the Windows branch here. This PR's CI compiles it.

Generated by Claude Code

Wait on a condition variable 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
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
SleepConditionVariableSRW takes a DWORD of milliseconds, so waits of more
than about 49.7 days are capped. A capped wait that runs out had not
reached the requested timeout, so return true like a spurious wakeup
instead of false, which means that the timeout expired.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
The Windows timed wait returned true for every error other than
ERROR_TIMEOUT, while the POSIX path returns false for any error. Such
errors are misuse that should not occur, and returning true would make a
predicate loop spin on a persistent error, so return false like on POSIX.
Only a capped wait that runs out still counts as a spurious wakeup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
Comment thread libs/std/thread.c Resolved
Comment thread tests/CMakeLists.txt Resolved
- gettimeofday follows changes to the system time, which could shorten
  or lengthen a timed wait. Create condition variables with
  CLOCK_MONOTONIC and compute the deadline with clock_gettime. macOS has
  no pthread_condattr_setclock, so use the relative
  pthread_cond_timedwait_relative_np there.
- Give the cond_timed_wait ctest a timeout, so that a hanging wait fails
  the test instead of the whole run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
@Simn
Simn requested a review from tobil4sk November 6, 2026 08:53
@tobil4sk tobil4sk changed the title Add cond_timed_wait primitive [std] Add cond_timed_wait primitive Nov 6, 2026

@tobil4sk tobil4sk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me now. 60 seconds seems a bit excessive for the timeout (running locally I would probably Ctrl-C after 5-10s), but hopefully this is just a precaution anyway.

@Simn
Simn merged commit 4969ac6 into master Nov 6, 2026
47 of 48 checks passed
@Simn
Simn deleted the claude/jolly-wozniak-93trgh branch November 6, 2026 13:00
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