Skip to content

Give every timed wait an exact deadline - #1357

Merged
yogthos merged 4 commits into
mainfrom
fix/timed-wait-exact-deadline
Oct 14, 2026
Merged

yogthos merged 4 commits into
mainfrom
fix/timed-wait-exact-deadline

Conversation

@yogthos

@yogthos yogthos commented Oct 14, 2026

Copy link
Copy Markdown
Member

Stacked on #1340 (base is fix/sleep-overshoot; retarget to main once that merges).

#1340 made Thread/sleep exact. Every other timed wait still built its deadline as a whole millisecond rounded up from now, so a 1ms wait took ~2ms where the JDK takes ~1.25ms. This makes mono-deadline exact for all of them and fixes the places that compared or subtracted deadlines against the truncated mono-millis clock.

Sites: timed deref of promise/future, BlockingQueue poll/offer, CountDownLatch.await, ReentrantLock.tryLock, awaitTermination, Future.get, CompletableFuture.get, FutureTask.get, Thread.join, Object.wait, Process.waitFor, await-for, core.async timeout, scheduled executor delays/getDelay/keep-alive, and the io poller (socket SO_TIMEOUT on read/accept). jolt has no Semaphore or Condition, so those aren't covered.

Median of 101 x 1ms waits, macOS, in us:

site                 JDK 21   before   after
deref promise        1267     1995     1273
BlockingQueue.poll   1342     1997     1270
latch await          1300     1988     1271
tryLock              1299     3525     1270
awaitTermination     1289     1996     1274
Process.waitFor      1296     2528     1274
Object.wait          1284     1995     1277
read SO_TIMEOUT 1    1169     2373     1178
poll 500us            680        0      637

Also fixed:

  • tu->ms truncated sub-millisecond units, so (.poll q 500 TimeUnit/MICROSECONDS) returned at once and a 1500us schedule delay was 1ms.
  • A timed tryLock ignored the interrupt flag. The JDK throws InterruptedException even when the lock is free.
  • tryLock kept polling for a whole millisecond past its deadline, with a 1ms nap on top.
  • ms->duration floored fractional ms, which turned a sub-ms pause into a busy loop.

One-shot waits now pass a timeout that starts at the first actual wait (jolt-timeout in locks.ss), so a wait that answers at once doesn't read the clock. Building an exact deadline costs a rational division, and skipping it makes the non-blocking paths faster. A/B/A, ns/op min (median):

settled deref 1      128 (129) / 69 (70) / 131 (136)
latch(0) await 1ms   145 (145) / 83 (85) / 149 (154)
free tryLock+unlock  116 (116) / 74 (74) / 120 (124)
offer+poll 1ms       107 (107) / 107 (107) / 110 (115)
Future.get done      148 (149) / 93 (94) / 154 (156)

Tests: async-timer-test section 11 times each site's 1ms wait interleaved with a sleep(1) from the same run, and retries a round that noise puts behind. All 15 rows fail on #1340's code. There's a socket-test row for SO_TIMEOUT, and corpus rows for interrupted tryLock, negative timeouts and sub-ms schedule delay, all checked against the clojure CLI.

Timed waits other than Thread/sleep built their deadline as a whole
millisecond rounded up from now, so a 1ms deref, poll, latch await, join,
waitFor, core.async timeout or socket SO_TIMEOUT took ~2ms (tryLock 3.5ms)
where the JDK takes ~1.25ms. mono-deadline is exact now, the poll loops
(tryLock, waitFor, the poller) size their last nap by the time actually
left, and tu->ms keeps sub-millisecond units instead of truncating them.

One-shot waits take a timeout that starts at the first wait (jolt-timeout),
so a wait that answers at once reads no clock; settled derefs and free
tryLocks get ~40% cheaper. A timed tryLock now throws on an interrupted
thread, as the JDK's does.
@yogthos
yogthos changed the base branch from fix/sleep-overshoot to main October 14, 2026 11:20
The old row needed under 500us between schedule and getDelay, which a cold
JVM on CI does not manage. Compare two schedules instead: b is made and read
after a, so on the JVM its delay is at least 999us more whatever the clock
does, while the truncating conversion made both 1ms.
@yogthos
yogthos merged commit ad86793 into main Oct 14, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant