You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Follow-ups to Condition.timedWait: timeouts and mutex semantics across targets #13155
Leftovers from #13109. Some of the fixes belong in HashLink, hxcpp or Neko, but they're collected here so they don't get lost.
Timeout API shapes.Lock.wait(?timeout):Bool, Semaphore.tryAcquire(?timeout):Bool and Condition.timedWait(timeout):Bool all take timeouts in different shapes. They could be made consistent.
HashLink timeout overflow.hl_semaphore_try_acquire and hl_lock_wait overflow on negative or huge timeouts, the same bug Fix timeout handling in hl_condition_timed_wait hashlink#1008 fixed in hl_condition_timed_wait. That PR can serve as the reference fix.
Wall-clock deadlines. HashLink's timed waits and Neko's lock_wait compute deadlines from gettimeofday, so changing the system time affects them. [std] Add cond_timed_wait primitive neko#336 shows the monotonic replacement: CLOCK_MONOTONIC via pthread_condattr_setclock, and pthread_cond_timedwait_relative_np on macOS.
Mutex recursion. The mutex behind Condition (and Mutex) is recursive on hl, jvm and python, but not on cpp, neko and eval. We should specify one behaviour; TestCondition.testTryAcquire has a commented-out assertion for non-recursive.
hxcpp, Apple.CountingSemaphore.apple.cpp truncates the timeout to whole seconds before converting it.
jvm Semaphore.tryAcquire. It uses the same Int64.fromFloat conversion that Condition.timedWait had to cap, so it throws for timeouts above about 104 days (2^53 ns).
Neko fallback test. CI only exercises one path of Condition.timedWait on Neko: the native cond_timed_wait or the polling fallback for older runtimes, depending on the prebuilt Neko. A test that forces the fallback would cover both.
I think Lock vs Semaphore should be decided for haxe 5, they are basically the same primitive except Semaphore can have an initial count and has a different api. The tryAcquire vs acquire split is probably reasonable to have. We can either deprecate Lock, or keep Lock (which more code is already using than Semaphore) but deprecate wait in favour of split methods.
Semaphore and Condition have different shapes because they are different primitives:
Semaphore.acquire() - block until we can acquire
Semaphore.tryAcquire() - attempt to acquire but return false immediately if it is locked
Semaphore.tryAcquire(f) - attempt to acquire but return false after timeout
vs Condition:
Condition.wait() - block until the condition is triggered
(missing) - there is no equivalent, because condition variables have no state to query (contrary to what the name "variable" suggests)
Condition.timedWait(f) - block until the condition is triggered but return false after timeout
For reference, here is Lock as well:
Lock.wait() - block until we can acquire
Lock.wait(0) - attempt to acquire but return false immediately if it is locked (maybe still calls native wait?)
Lock.wait(f) - attempt to acquire but return false after timeout
I'm in favor of deprecating Lock. The initial count is a big API gap in my opinion.
It could be added as an optional argument to new Lock(), having a default count of 0 is sensible and convenient for most use cases.
I'm not sure about your second point on Condition though because there's a mutex involved there as well, at least conceptually.
I guess the condition variable equivalent would be to acquire the mutex and check the variable without calling wait. But unlike Semaphore where the count is part of the object, Condition does not store the variable value in the object and you have to check it externally. So that's still something that Condition can't really have an api for.
Activity
tobil4sk commentedon Nov 6, 2026
I think Lock vs Semaphore should be decided for haxe 5, they are basically the same primitive except Semaphore can have an initial count and has a different api. The
tryAcquirevsacquiresplit is probably reasonable to have. We can either deprecate Lock, or keep Lock (which more code is already using than Semaphore) but deprecate wait in favour of split methods.Semaphore and Condition have different shapes because they are different primitives:
Semaphore.acquire()- block until we can acquireSemaphore.tryAcquire()- attempt to acquire but return false immediately if it is lockedSemaphore.tryAcquire(f)- attempt to acquire but return false after timeoutvs Condition:
Condition.wait()- block until the condition is triggeredCondition.timedWait(f)- block until the condition is triggered but return false after timeoutFor reference, here is Lock as well:
Lock.wait()- block until we can acquireLock.wait(0)- attempt to acquire but return false immediately if it is locked (maybe still calls native wait?)Lock.wait(f)- attempt to acquire but return false after timeoutSimn commentedon Nov 6, 2026
I'm in favor of deprecating Lock. The initial count is a big API gap in my opinion.
I'm not sure about your second point on Condition though because there's a mutex involved there as well, at least conceptually.
tobil4sk commentedon Nov 6, 2026
It could be added as an optional argument to
new Lock(), having a default count of0is sensible and convenient for most use cases.I guess the condition variable equivalent would be to acquire the mutex and check the variable without calling wait. But unlike
Semaphorewhere the count is part of the object,Conditiondoes not store the variable value in the object and you have to check it externally. So that's still something thatConditioncan't really have an api for.