Add computeIntensive for running CPU-intensive operations off virtual threads - #483
Merged
Merged
Conversation
…rtual threads) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…ry interrupt check, precise abandonOnInterrupt semantics, timeout caveat Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…ad pool Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…nto the caller-interrupt path Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…upt interplay, nesting detection limits
- fail the nested-deadlock test fast via a time limit, instead of hanging - simplify onCallerInterrupted: capture the task's exception in the catch arm, dropping the redundant post-loop re-fetch and the unreachable self-suppression guard; comment the structurally-unreachable match arm - explain the lazy-val-forcing idiom in setOxComputeExecutor, and give it a proper scaladoc summary; document the lock's serialization role in completing - fix a garden-path sentence in the computeIntensive scaladoc - rename ComputeTaskState to ComputeIntensiveTaskState, completing's parameter c to op - docs: compile-check the examples via mdoc, clarify the custom-executor lifecycle sentence, add a missing period Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…d of a nullable field Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Virtual threads are not preempted by the JVM scheduler: a long-running, CPU-bound computation monopolizes a carrier thread, and a handful of such computations can starve all other virtual threads in the process (JEP 444 explicitly excludes time-sharing).
cede()(#481) covers short, instrumentable bursts; this PR adds the complement for long-running or non-instrumentable compute, following the pattern the JDK team recommends (run such work on platform threads, letting the OS preempt) and the shape other runtimes converged on (Kotlin'sDispatchers.Default, Tokio's dedicated compute pool, Reactor'sSchedulers.parallel()).Changes
computeIntensive(f)/computeIntensive(executor)(f): runsfon a pool of platform threads, blocking the calling (virtual) thread until completion — structured by construction (the computation never outlives the enclosing scope), composes withfork/par/race/timeout.ox-compute-N); replaceable viasetOxComputeExecutor(mirroringsetOxThreadFactory) orOxApp.Settings.computeExecutor(with binary-compat overloads).InterruptedExceptionis correctly distinguished from caller interruption; the interrupt never leaks to a subsequent task on the reused pool thread.ForkLocals read defaults,forkinside the computation fails fast.doc/utils/compute-intensive.mdpage + cross-links from thecede()docs.eitherinterop, and a time-limited starvation smoke test.Notes
setOxComputeExecutor/OxApp.Settings.computeExecutorwiring path is deliberately untested: it mutates JVM-global state and would poison other suites (same situation as the existingthreadFactorysetting, which is also untested). Only the "throws after first use" path is covered.🤖 Generated with Claude Code