Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dynamic>: 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<Dynamic> 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<Dynamic> 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)
Expand Down
2 changes: 2 additions & 0 deletions include/hx/thread/ConditionVariable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
35 changes: 35 additions & 0 deletions src/hx/thread/ConditionVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <atomic>

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<unsigned int> signals{ 0 };
};

hx::thread::ConditionVariable_obj::ConditionVariable_obj() : impl(new Impl())
Expand Down Expand Up @@ -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::steady_clock::duration>(std::chrono::duration<double>(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();
}
52 changes: 52 additions & 0 deletions test/regression/ConditionTimedWait/Main.hx
Original file line number Diff line number Diff line change
@@ -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 <hx/thread/ConditionVariable.hpp>
#include <cmath>
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);
}
}
2 changes: 2 additions & 0 deletions test/regression/ConditionTimedWait/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-m Main
--cpp bin
4 changes: 4 additions & 0 deletions test/regression/ConditionTimedWait/stdout.txt
Original file line number Diff line number Diff line change
@@ -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
Loading