Skip to content

Commit 9e8459d

Browse files
Return at once from Semaphore.tryAcquire(-1) (#494)
Since #461, CountingSemaphore::tryAcquire returned at once for a negative timeout other than -1, which still waited with no limit for Haxe 5.0.0-preview.1's Lock.wait(). But preview.1's Lock is not built on Semaphore: it waits through __hxcpp_lock_wait, whose lock takes any negative timeout as no limit, as Haxe 4's does. Only the Haxe 5 nightlies from March to November 2026 passed -1 to the semaphore, and libhxcpp does not support nightlies; later ones call acquire() when Lock.wait() has no timeout. Keeping -1 meant a timeout worked out in whole seconds as deadline - now could still wait for ever once the deadline passed. Every negative timeout now tries once and returns, on every backend, as stock hxcpp does on Linux and macOS. Tests: ThreadWaitTimeouts expects tryAcquire(-1) on an empty semaphore to return false at once, and still checks that Lock.wait() with no timeout waits for a release. Against the old code it times out. HaxeFoundation/haxe#13114
1 parent 150bdef commit 9e8459d

6 files changed

Lines changed: 22 additions & 44 deletions

File tree

‎Changes.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
libhxcpp 6.0.3 (unreleased)
33
------------------------------------------------------------
44

5+
* Fixed Semaphore.tryAcquire(-1) waiting for ever where any other negative timeout returns at once, so a timeout worked out in whole seconds as deadline - now could wait with no limit once the deadline had passed. It was kept for Haxe 5.0.0-preview.1's Lock.wait() (libhxcpp #461), but preview.1's Lock waits through the runtime's own lock, which still takes a negative timeout as no limit, as Haxe 4's does. Only Haxe 5 nightlies from March to November 2026, which libhxcpp does not support, passed -1 to the semaphore; later ones call acquire() for no timeout. Every negative timeout returns at once now, as with stock hxcpp on Linux and macOS (HaxeFoundation/haxe#13114)
6+
57
libhxcpp 6.0.2
68
------------------------------------------------------------
79

‎src/hx/thread/CountingSemaphore.apple.cpp‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,10 @@ void hx::thread::CountingSemaphore_obj::release()
5454

5555
bool hx::thread::CountingSemaphore_obj::tryAcquire(Null<double> timeout)
5656
{
57-
// -1 waits with no limit: Haxe 5.0.0-preview.1's Lock.wait() passes it when
58-
// given no timeout. Any other negative timeout has run out already, as in
59-
// tryAcquire(deadline - now), and returns at once like 0, as does NaN; it
60-
// waited for ever before (libhxcpp #461)
61-
if (timeout.Default(0) == -1)
62-
{
63-
acquire();
64-
return true;
65-
}
66-
57+
// A timeout that is not positive, null and NaN among them, tries once and
58+
// returns. A negative one has run out already, as tryAcquire(deadline - now)
59+
// has once the deadline passes, and -1 is no exception: waiting with no
60+
// limit is acquire()'s job (libhxcpp #461)
6761
hx::AutoGCFreeZone zone;
6862

6963
double seconds = timeout.Default(0);

‎src/hx/thread/CountingSemaphore.posix.cpp‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,10 @@ void hx::thread::CountingSemaphore_obj::release()
7575

7676
bool hx::thread::CountingSemaphore_obj::tryAcquire(Null<double> timeout)
7777
{
78-
// -1 waits with no limit: Haxe 5.0.0-preview.1's Lock.wait() passes it when
79-
// given no timeout. Any other negative timeout has run out already, as in
80-
// tryAcquire(deadline - now), and returns at once like 0, as does NaN; it
81-
// waited for ever before (libhxcpp #461)
82-
if (timeout.Default(0) == -1)
83-
{
84-
acquire();
85-
return true;
86-
}
87-
78+
// A timeout that is not positive, null and NaN among them, tries once and
79+
// returns. A negative one has run out already, as tryAcquire(deadline - now)
80+
// has once the deadline passes, and -1 is no exception: waiting with no
81+
// limit is acquire()'s job (libhxcpp #461)
8882
if (hx::gPauseForCollect)
8983
__hxcpp_gc_safe_point();
9084
double seconds = timeout.Default(0);

‎src/hx/thread/CountingSemaphore.singlethreaded.cpp‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,10 @@ void hx::thread::CountingSemaphore_obj::release()
5454

5555
bool hx::thread::CountingSemaphore_obj::tryAcquire(Null<double> timeout)
5656
{
57-
// -1 waits with no limit: Haxe 5.0.0-preview.1's Lock.wait() passes it when
58-
// given no timeout. Any other negative timeout has run out already, as in
59-
// tryAcquire(deadline - now), and returns at once like 0, as does NaN; it
60-
// waited for ever before (libhxcpp #461)
61-
if (timeout.Default(0) == -1)
62-
{
63-
acquire();
64-
return true;
65-
}
66-
57+
// A timeout that is not positive, null and NaN among them, tries once and
58+
// returns. A negative one has run out already, as tryAcquire(deadline - now)
59+
// has once the deadline passes, and -1 is no exception: waiting with no
60+
// limit is acquire()'s job (libhxcpp #461)
6761
if (impl->permits > 0)
6862
{
6963
impl->permits--;

‎src/hx/thread/CountingSemaphore.win32.cpp‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,10 @@ void hx::thread::CountingSemaphore_obj::release()
5959

6060
bool hx::thread::CountingSemaphore_obj::tryAcquire(Null<double> timeout)
6161
{
62-
// -1 waits with no limit: Haxe 5.0.0-preview.1's Lock.wait() passes it when
63-
// given no timeout. Any other negative timeout has run out already, as in
64-
// tryAcquire(deadline - now), and returns at once like 0, as does NaN; it
65-
// waited for ever before (libhxcpp #461)
66-
if (timeout.Default(0) == -1)
67-
{
68-
acquire();
69-
return true;
70-
}
71-
62+
// A timeout that is not positive, null and NaN among them, tries once and
63+
// returns. A negative one has run out already, as tryAcquire(deadline - now)
64+
// has once the deadline passes, and -1 is no exception: waiting with no
65+
// limit is acquire()'s job (libhxcpp #461)
7266
if (hx::gPauseForCollect)
7367
__hxcpp_gc_safe_point();
7468
// Zero-timeout poll cannot block - no free zone needed. Also try a

‎test/regression/ThreadWaitTimeouts/Main.hx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ class Main {
4242
var empty = new Semaphore(0);
4343
var full = new Semaphore(3);
4444

45-
// -1 waits with no limit, which Haxe 5.0.0-preview.1's Lock.wait() relies on
46-
releaseLater(() -> empty.release());
47-
check("semaphore minus one", attempt(() -> empty.tryAcquire(-1) && full.tryAcquire(-1)));
48-
// Any other negative timeout has run out, and returns at once (libhxcpp #461)
45+
// A negative timeout has run out, and returns at once (libhxcpp #461). -1
46+
// waited with no limit before; on an empty semaphore that nobody releases,
47+
// the watchdog stops it
48+
check("semaphore minus one", attempt(() -> !empty.tryAcquire(-1) && full.tryAcquire(-1)));
4949
check("semaphore negative", attempt(() -> !empty.tryAcquire(-0.5) && full.tryAcquire(-0.5)));
5050
check("semaphore nan", attempt(() -> !empty.tryAcquire(Math.NaN) && full.tryAcquire(Math.NaN)));
5151

0 commit comments

Comments
 (0)