Repository navigation
[eval] Use libuv mutex and condition for Lock and Semaphore - #13110
Conversation
Lock.wait and Semaphore.tryAcquire with a timeout used to spin on a non-blocking acquire with Unix.sleepf 0.0, because OCaml's Condition has no timed wait. Both now share a counting semaphore built on Luv.Mutex and Luv.Condition: uv_cond_timedwait blocks with a timeout on every platform, and luv's bindings for the blocking calls release the OCaml runtime lock while they block. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
|
I feel like I tried doing this before and there was some reason for not relying on the luv primitives, but I can't remember and I also don't find anything in the commit history. There's a good chance that this was before we moved from threads to domains, so whatever concerns I had would no longer be relevant. Still, could you just briefly pretend to be a grumpy maintainer who hates change, and try to come up with potential risks and arguments against this change? |
- Luv's binding for uv_mutex_lock releases and reacquires the OCaml runtime lock even when the mutex is free, which made uncontended Lock and Semaphore operations about 3 times slower than before. Try the non-blocking trylock first. - On Windows, uv_cond_timedwait converts its timeout to a DWORD of milliseconds, so cap each wait at about 24 days. The wait loop already re-checks the remaining time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXDYWEo4mt53psuM5hu
|
Putting on the grumpy-maintainer hat. I checked each point against the vendored libuv in luv 0.5.14 or with a measurement, and fixed the two that were real defects in this PR in ada7739. 1. "It's slower." Partly true, measured with 1M operations on eval:
The uncontended slowdown came from luv's 2. "Windows does something weird." It did. 3. "libuv calls 4. "Windows mutexes are recursive." 5. "Asynchronous exceptions leave the mutex locked." If a signal handler raises (e.g. 6. "Finalisers." Each object gets a 7. "More coupling to luv." 8. "Behaviour change." As for your earlier concern: I couldn't find a trace of it either. In luv 0.5.14 the blocking bindings explicitly release the runtime lock, so a Thread-era deadlock wouldn't happen today. I can't tell whether older luv versions behaved the same. After ada7739, I re-ran the eval thread tests (all OK) and hxcoro's eval suites: 369/369 with each dispatcher, Generated by Claude Code |
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
* 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>
In eval,
Lock.wait(timeout)andSemaphore.tryAcquire(timeout)spun on a non-blocking acquire withUnix.sleepf 0.0until the deadline, because OCaml'sConditionhas no timed wait. That kept a core busy for the whole timeout.This PR makes both use one counting semaphore built on
Luv.MutexandLuv.Condition:uv_cond_timedwaitblocks with a timeout on every platform:SleepConditionVariableCSon Windows,pthread_cond_timedwaitwithCLOCK_MONOTONICon Linux, andpthread_cond_timedwait_relative_npon macOS.uv_cond_wait,uv_cond_timedwaitanduv_mutex_lockare in theBlockinggroup ofluv_c_function_descriptions.ml, whichgenerate_c_functions.mlgenerates with~concurrency:Cstubs.unlocked. So they release the OCaml runtime lock while they block, which is what theUnix.sleepf 0.0in the old loops was there for.Gc.finalise.This is a first step toward a non-polling timed
Condition.waitin eval (discussed in #13109).Conditioncan then reuse the same structure. This PR doesn't depend on #13109.Testing
I built the compiler locally (OCaml 5.3.0, luv 0.5.14) and ran:
tests/threadswith--interp: 148 assertions, all OK. That coversTestLock,TestSemaphore,TestConditionandTestEval.Thread.createspawns domains, so this is the parallel case.LockandSemaphore:testswith the default,trampolineandthreadpooldispatchers: 369/369 each.testswith-debug -D coro.stack-traces: 369/369.thread-tests: 3/3.callstack-tests: 15/15.developmentLock.wait(1.0), no releaseSemaphore.tryAcquire(1.0), no releaseWith this PR,
tryAcquire(0),(-1)and(NaN)returnfalse.tryAcquire(1e12)andLock.wait(1e12)returntruewhen another thread releases after 200 ms, andnew Semaphore(2)hands out exactly two units.One behaviour change: a negative initial count for
new Semaphore(n)used to raiseInvalid_argumentfrom OCaml'sSemaphore.Counting.make. Now it's accepted, and acquiring waits until enough releases have happened.Generated by Claude Code