From d961b39860e0d58b8a47303e026e615e1ec28893 Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Fri, 13 Nov 2026 00:12:30 -0500 Subject: [PATCH] Add timedWait to ConditionVariable for Haxe 5's Condition The Haxe nightly's sys.thread.Condition has timedWait, which calls hx::thread::ConditionVariable's, and libhxcpp had none, so every program that used Condition failed to compile and most of the nightly workflow's jobs failed. timedWait returns true once a signal or broadcast came during the wait and false when the time ran out. NaN, zero and a negative time do not wait, and the wait is capped at 1e9 seconds, which keeps the deadline inside the clock's range, as the Haxe side expects. MSVC's timed waits return no_timeout about half a millisecond before the deadline with nothing signalled, about half the time for a 50 ms wait, so a wakeup counts only when the count of signals moved, and one without waits again. ConditionTimedWait checks a wait that runs out, waits that do not wait, a signal from another thread, and a wait past the cap, with native calls that build with every Haxe. --- Changes.md | 1 + include/hx/thread/ConditionVariable.hpp | 2 + src/hx/thread/ConditionVariable.cpp | 35 +++++++++++++ test/regression/ConditionTimedWait/Main.hx | 52 +++++++++++++++++++ test/regression/ConditionTimedWait/build.hxml | 2 + test/regression/ConditionTimedWait/stdout.txt | 4 ++ 6 files changed, 96 insertions(+) create mode 100644 test/regression/ConditionTimedWait/Main.hx create mode 100644 test/regression/ConditionTimedWait/build.hxml create mode 100644 test/regression/ConditionTimedWait/stdout.txt diff --git a/Changes.md b/Changes.md index d76013cc0..d3372b98b 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ libhxcpp 6.0.0 (unreleased) ------------------------------------------------------------ +* Added timedWait to hx::thread::ConditionVariable, which Haxe 5's sys.thread.Condition.timedWait calls: with the Haxe nightly, every program that used Condition failed to compile. It returns true once a signal or broadcast came and false when the time ran out, and it waits out the wakeups MSVC's timed waits give just before the deadline with nothing signalled * Fixed an exception from blit, setSize or resize under the cppia JIT ending the process instead of reaching the script's catch, on typed arrays and on Array: their native calls had no exception bridge. The script catches it, as it does interpreted and in compiled code (libhxcpp #404) * Fixed resize() and cpp.NativeArray.setSize() on an Array under the cppia JIT evaluating the array expression twice, so a call in it ran twice and the array it returned the second time was the one resized. The JIT evaluates it once (libhxcpp #411) * Fixed indexOf and lastIndexOf on an Array under the cppia JIT ignoring fromIndex: the JIT never generated it, so a call or exception in it was lost too. The search starts from it, as it does interpreted and in compiled code (libhxcpp #410) diff --git a/include/hx/thread/ConditionVariable.hpp b/include/hx/thread/ConditionVariable.hpp index aef1f83e1..825a7e4e4 100644 --- a/include/hx/thread/ConditionVariable.hpp +++ b/include/hx/thread/ConditionVariable.hpp @@ -18,6 +18,8 @@ namespace hx bool tryAcquire(); void release(); void wait(); + // True when woken, false when the time ran out + bool timedWait(double seconds); void signal(); void broadcast(); diff --git a/src/hx/thread/ConditionVariable.cpp b/src/hx/thread/ConditionVariable.cpp index 1c86cc020..cb3cf5577 100644 --- a/src/hx/thread/ConditionVariable.cpp +++ b/src/hx/thread/ConditionVariable.cpp @@ -3,11 +3,14 @@ #include #include #include +#include struct hx::thread::ConditionVariable_obj::Impl { std::mutex mutex; std::condition_variable_any condition; + // Signals and broadcasts so far, which tell a timed wait that one came + std::atomic signals{ 0 }; }; hx::thread::ConditionVariable_obj::ConditionVariable_obj() : impl(new Impl()) @@ -56,12 +59,44 @@ void hx::thread::ConditionVariable_obj::wait() impl->condition.wait(impl->mutex); } +// Haxe's Condition.timedWait: true once a signal or broadcast came, false when +// the time ran out. NaN, zero and a negative time do not wait, and the wait is +// capped at 1e9 seconds, which keeps the deadline inside the clock's range; +// Haxe counts a capped wait that ran out as a spurious wakeup. MSVC's +// wait_for and wait_until can return no_timeout just before the deadline with +// nothing signalled, so a wakeup counts only when the count of signals moved, +// and one without waits again until the deadline +bool hx::thread::ConditionVariable_obj::timedWait(double seconds) +{ + if (!(seconds > 0)) + seconds = 0; + else if (seconds > 1e9) + seconds = 1e9; + + auto deadline = std::chrono::steady_clock::now() + + std::chrono::duration_cast(std::chrono::duration(seconds)); + unsigned int before = impl->signals.load(); + + hx::AutoGCFreeZone zone; + + while (true) + { + bool ranOut = impl->condition.wait_until(impl->mutex, deadline) == std::cv_status::timeout; + if (impl->signals.load() != before) + return true; + if (ranOut || std::chrono::steady_clock::now() >= deadline) + return false; + } +} + void hx::thread::ConditionVariable_obj::signal() { + impl->signals++; impl->condition.notify_one(); } void hx::thread::ConditionVariable_obj::broadcast() { + impl->signals++; impl->condition.notify_all(); } diff --git a/test/regression/ConditionTimedWait/Main.hx b/test/regression/ConditionTimedWait/Main.hx new file mode 100644 index 000000000..09cf0f4ba --- /dev/null +++ b/test/regression/ConditionTimedWait/Main.hx @@ -0,0 +1,52 @@ +// hx::thread::ConditionVariable had no timedWait, which Haxe 5's +// sys.thread.Condition calls, so every program using Condition failed to +// compile with the Haxe nightly. The checks call it from native code, which +// builds with every Haxe. +@:cppFileCode(' +#include +#include +static Dynamic makeCondition() { return hx::thread::ConditionVariable(new hx::thread::ConditionVariable_obj()); } +static hx::thread::ConditionVariable_obj *cv(Dynamic c) { return (hx::thread::ConditionVariable_obj *)c.mPtr; } +static void acquireCondition(Dynamic c) { cv(c)->acquire(); } +static void releaseCondition(Dynamic c) { cv(c)->release(); } +static void signalCondition(Dynamic c) { cv(c)->signal(); } +static bool timedWaitCondition(Dynamic c, double s) { return cv(c)->timedWait(s); } +') +class Main { + static var condition:Dynamic = untyped __cpp__("makeCondition()"); + + // Waits with the lock held, as Condition.timedWait is called + static function wait(seconds:Float):{woken:Bool, took:Float} { + var start = haxe.Timer.stamp(); + var woken:Bool = untyped __cpp__("timedWaitCondition({0}, {1})", condition, seconds); + return {woken: woken, took: haxe.Timer.stamp() - start}; + } + + // A thread that signals once it gets the lock, which the wait gives up + static function waitForSignal(seconds:Float):{woken:Bool, took:Float} { + untyped __cpp__("acquireCondition({0})", condition); + sys.thread.Thread.create(() -> { + untyped __cpp__("acquireCondition({0})", condition); + untyped __cpp__("signalCondition({0})", condition); + untyped __cpp__("releaseCondition({0})", condition); + }); + var result = wait(seconds); + untyped __cpp__("releaseCondition({0})", condition); + return result; + } + + static function main() { + untyped __cpp__("acquireCondition({0})", condition); + var timeout = wait(0.05); + var none = [wait(0), wait(-1), wait(Math.NaN)]; + untyped __cpp__("releaseCondition({0})", condition); + Sys.println("a wait no one signals runs out: " + (!timeout.woken && timeout.took >= 0.04)); + Sys.println("zero, negative and NaN waits return at once: " + Lambda.foreach(none, r -> !r.woken && r.took < 1)); + + var signalled = waitForSignal(30); + Sys.println("a signal ends the wait: " + (signalled.woken && signalled.took < 10)); + // Past the cap, the deadline would overflow the clock and the wait end at once + var long = waitForSignal(1e300); + Sys.println("a wait past the cap still waits for the signal: " + long.woken); + } +} diff --git a/test/regression/ConditionTimedWait/build.hxml b/test/regression/ConditionTimedWait/build.hxml new file mode 100644 index 000000000..b484e67e7 --- /dev/null +++ b/test/regression/ConditionTimedWait/build.hxml @@ -0,0 +1,2 @@ +-m Main +--cpp bin diff --git a/test/regression/ConditionTimedWait/stdout.txt b/test/regression/ConditionTimedWait/stdout.txt new file mode 100644 index 000000000..d98600a6d --- /dev/null +++ b/test/regression/ConditionTimedWait/stdout.txt @@ -0,0 +1,4 @@ +a wait no one signals runs out: true +zero, negative and NaN waits return at once: true +a signal ends the wait: true +a wait past the cap still waits for the signal: true