Skip to content

Commit 0494706

Browse files
Merge main into fix/cppia-static-call-no-host-glue
# Conflicts: # Changes.md
2 parents 9c3fda3 + d8f9651 commit 0494706

7 files changed

Lines changed: 106 additions & 1 deletion

File tree

‎Changes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ libhxcpp 6.0.0 (unreleased)
33
------------------------------------------------------------
44

55
* Fixed a malformed cppia file that calls a static function on a host interface crashing the loader: it looked the function up in the interface's script glue, which an interface does not have. The fuzz job found it
6+
* 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
67
* 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)
78
* 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)
89
* 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)

‎include/hx/thread/ConditionVariable.hpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace hx
1818
bool tryAcquire();
1919
void release();
2020
void wait();
21+
// True when woken, false when the time ran out
22+
bool timedWait(double seconds);
2123
void signal();
2224
void broadcast();
2325

‎src/hx/gc/Immix.cpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7263,7 +7263,16 @@ class GlobalAllocator
72637263
{
72647264
int oldest = 0;
72657265
for(int i=0;i<mAllBlocks.size();i++)
7266-
oldest = std::max(oldest, mAllBlocks[i]->maxStartAge());
7266+
{
7267+
BlockDataInfo *block = mAllBlocks[i];
7268+
// Runs between collections, while the zeroing thread may be
7269+
// reclaiming or zeroing the block under this lock
7270+
int expected = 0;
7271+
while (!block->mZeroLock.compare_exchange_weak(expected, 1))
7272+
expected = 0;
7273+
oldest = std::max(oldest, block->maxStartAge());
7274+
block->mZeroLock = 0;
7275+
}
72677276
return oldest;
72687277
}
72697278
#endif

‎src/hx/thread/ConditionVariable.cpp‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
#include <mutex>
44
#include <condition_variable>
55
#include <chrono>
6+
#include <atomic>
67

78
struct hx::thread::ConditionVariable_obj::Impl
89
{
910
std::mutex mutex;
1011
std::condition_variable_any condition;
12+
// Signals and broadcasts so far, which tell a timed wait that one came
13+
std::atomic<unsigned int> signals{ 0 };
1114
};
1215

1316
hx::thread::ConditionVariable_obj::ConditionVariable_obj() : impl(new Impl())
@@ -56,12 +59,44 @@ void hx::thread::ConditionVariable_obj::wait()
5659
impl->condition.wait(impl->mutex);
5760
}
5861

62+
// Haxe's Condition.timedWait: true once a signal or broadcast came, false when
63+
// the time ran out. NaN, zero and a negative time do not wait, and the wait is
64+
// capped at 1e9 seconds, which keeps the deadline inside the clock's range;
65+
// Haxe counts a capped wait that ran out as a spurious wakeup. MSVC's
66+
// wait_for and wait_until can return no_timeout just before the deadline with
67+
// nothing signalled, so a wakeup counts only when the count of signals moved,
68+
// and one without waits again until the deadline
69+
bool hx::thread::ConditionVariable_obj::timedWait(double seconds)
70+
{
71+
if (!(seconds > 0))
72+
seconds = 0;
73+
else if (seconds > 1e9)
74+
seconds = 1e9;
75+
76+
auto deadline = std::chrono::steady_clock::now() +
77+
std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<double>(seconds));
78+
unsigned int before = impl->signals.load();
79+
80+
hx::AutoGCFreeZone zone;
81+
82+
while (true)
83+
{
84+
bool ranOut = impl->condition.wait_until(impl->mutex, deadline) == std::cv_status::timeout;
85+
if (impl->signals.load() != before)
86+
return true;
87+
if (ranOut || std::chrono::steady_clock::now() >= deadline)
88+
return false;
89+
}
90+
}
91+
5992
void hx::thread::ConditionVariable_obj::signal()
6093
{
94+
impl->signals++;
6195
impl->condition.notify_one();
6296
}
6397

6498
void hx::thread::ConditionVariable_obj::broadcast()
6599
{
100+
impl->signals++;
66101
impl->condition.notify_all();
67102
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// hx::thread::ConditionVariable had no timedWait, which Haxe 5's
2+
// sys.thread.Condition calls, so every program using Condition failed to
3+
// compile with the Haxe nightly. The checks call it from native code, which
4+
// builds with every Haxe.
5+
@:cppFileCode('
6+
#include <hx/thread/ConditionVariable.hpp>
7+
#include <cmath>
8+
static Dynamic makeCondition() { return hx::thread::ConditionVariable(new hx::thread::ConditionVariable_obj()); }
9+
static hx::thread::ConditionVariable_obj *cv(Dynamic c) { return (hx::thread::ConditionVariable_obj *)c.mPtr; }
10+
static void acquireCondition(Dynamic c) { cv(c)->acquire(); }
11+
static void releaseCondition(Dynamic c) { cv(c)->release(); }
12+
static void signalCondition(Dynamic c) { cv(c)->signal(); }
13+
static bool timedWaitCondition(Dynamic c, double s) { return cv(c)->timedWait(s); }
14+
')
15+
class Main {
16+
static var condition:Dynamic = untyped __cpp__("makeCondition()");
17+
18+
// Waits with the lock held, as Condition.timedWait is called
19+
static function wait(seconds:Float):{woken:Bool, took:Float} {
20+
var start = haxe.Timer.stamp();
21+
var woken:Bool = untyped __cpp__("timedWaitCondition({0}, {1})", condition, seconds);
22+
return {woken: woken, took: haxe.Timer.stamp() - start};
23+
}
24+
25+
// A thread that signals once it gets the lock, which the wait gives up
26+
static function waitForSignal(seconds:Float):{woken:Bool, took:Float} {
27+
untyped __cpp__("acquireCondition({0})", condition);
28+
sys.thread.Thread.create(() -> {
29+
untyped __cpp__("acquireCondition({0})", condition);
30+
untyped __cpp__("signalCondition({0})", condition);
31+
untyped __cpp__("releaseCondition({0})", condition);
32+
});
33+
var result = wait(seconds);
34+
untyped __cpp__("releaseCondition({0})", condition);
35+
return result;
36+
}
37+
38+
static function main() {
39+
untyped __cpp__("acquireCondition({0})", condition);
40+
var timeout = wait(0.05);
41+
var none = [wait(0), wait(-1), wait(Math.NaN)];
42+
untyped __cpp__("releaseCondition({0})", condition);
43+
Sys.println("a wait no one signals runs out: " + (!timeout.woken && timeout.took >= 0.04));
44+
Sys.println("zero, negative and NaN waits return at once: " + Lambda.foreach(none, r -> !r.woken && r.took < 1));
45+
46+
var signalled = waitForSignal(30);
47+
Sys.println("a signal ends the wait: " + (signalled.woken && signalled.took < 10));
48+
// Past the cap, the deadline would overflow the clock and the wait end at once
49+
var long = waitForSignal(1e300);
50+
Sys.println("a wait past the cap still waits for the signal: " + long.woken);
51+
}
52+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-m Main
2+
--cpp bin
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a wait no one signals runs out: true
2+
zero, negative and NaN waits return at once: true
3+
a signal ends the wait: true
4+
a wait past the cap still waits for the signal: true

0 commit comments

Comments
 (0)