Skip to content

Add cede() for yielding the current virtual thread - #481

Merged
adamw merged 1 commit into
masterfrom
cede
Jul 21, 2026
Merged

Add cede() for yielding the current virtual thread#481
adamw merged 1 commit into
masterfrom
cede

Conversation

@adamw

@adamw adamw commented Jul 21, 2026

Copy link
Copy Markdown
Member

Adds cede() to ox.control, next to checkInterrupt():

inline def cede(): Unit =
  checkInterrupt()
  Thread.`yield`()

Virtual threads are not preempted, so a CPU-bound loop can starve other virtual threads (in an experiment with 2x-cores busy-looping VTs, half of them received zero CPU time). cede() gives compute-intensive code a way to cooperate with the scheduler — and, since it also checks the interrupt flag, with the cancellation protocol: a busy loop calling cede() becomes both fair and cancellable.

Why checkInterrupt + Thread.yield

Based on experiments comparing yielding techniques on virtual threads (JDK 21 & 26, Linux, 8 cores), following up on this gist:

  • Thread.yield genuinely unmounts and resubmits the continuation to the back of the scheduler's queue (external submit when the carrier's local queue is empty) — it reliably prevents starvation (89–93% liveness-probe capture vs 2% baseline), at ~1µs per call.
  • LockSupport.parkNanos(1) gives near-perfect round-robin fairness, but costs ~40–46µs per call (timer round-trip), stalls the caller for that time, and silently degrades to a no-op when the interrupt flag is set — i.e. exactly when the enclosing scope is being cancelled. Since Thread.yield is ~50x cheaper, calling it more frequently (every ~1ms of computation) matches parkNanos(1)'s liveness at a quarter of the overhead. The trade-off is mentioned in the scaladoc.
  • LockSupport.parkNanos(0) is a true no-op.
  • Thread.sleep(0) behaves identically to checkInterrupt + Thread.yield (that's literally its JDK implementation for virtual threads), but only as an @implNote — the explicit version doesn't rely on unspecified behaviour.

The interrupt check makes cede() a cancellation boundary, consistent with e.g. cats-effect's IO.cede.

Includes tests and a docs entry in utils/control-flow.md.

🤖 Generated with Claude Code

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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