diff --git a/src/jvm.ts b/src/jvm.ts index 37e2bcc..d322d30 100644 --- a/src/jvm.ts +++ b/src/jvm.ts @@ -136,8 +136,11 @@ const WARM_MIN_CLASSES = 200; const WOKEN_RUN = 16; -// instructions a thread runs per turn +// instructions a thread runs per turn, at most and at least const SLICE = 20000; +const MIN_SLICE = 200; +// a turn this long (ms) is a spin or a long computation +const LONG_TURN = 0.05; export class JVM { readonly opts: JvmOptions; @@ -164,6 +167,7 @@ export class JVM { // threads another thread woke in its turn: they run before the rotation, so a hand-off does not wait behind spinners private woken: JThread[] = []; private wokenStreak = 0; + private shortTurns = 0; private queued = new Set(); private scheduled = false; private hashSeed = 0x2545f491; @@ -1109,12 +1113,32 @@ export class JVM { this.queued.delete(t); if (t.state !== RUNNABLE) continue; sched.thread = t; - spent += SLICE - runThread(t, SLICE); + const q = t.quantum || SLICE; + // only a thread that stayed runnable is timed, so hand-offs read no clock + const t0 = t.timed ? performance.now() : 0; + spent += q - runThread(t, q); if (t.unmounting !== null) unmountBlocked(t); - if (t.state === RUNNABLE && !this.queued.has(t)) { - this.queued.add(t); - this.runQueue.push(t); + if (t.state !== RUNNABLE) { + this.shortTurns++; + t.quantum = SLICE; + t.timed = false; + } else { + // a thread that runs long turns while others run short ones gets shorter turns + const now = performance.now(); + const dt = now - t0; + const others = this.shortTurns !== t.shortSeen && (this.runQueue.length > 0 || this.woken.length > 0); + const long = t.timed && dt > LONG_TURN; + if (t.timed && q === SLICE && !long) this.shortTurns++; + t.quantum = !others ? SLICE : long ? Math.max(MIN_SLICE, q >> 1) : q; + t.timed = true; + if (!this.queued.has(t)) { + this.queued.add(t); + this.runQueue.push(t); + } + if (now > end) break; + spent = 0; } + t.shortSeen = this.shortTurns; if (sched.pause) break; if (spent >= SLICE) { if (performance.now() > end) break; diff --git a/src/thread.ts b/src/thread.ts index d723d0d..e227d29 100644 --- a/src/thread.ts +++ b/src/thread.ts @@ -172,6 +172,11 @@ export class JThread { // what is left of the run loop's budget while compiled code runs: its // loops and the calls it interprets in place spend it slice = 0; + // instructions of its next turn, and the VM's count of short turns when it last ran + quantum = 0; + shortSeen = 0; + // its last turn ended with it still runnable + timed = false; // wakes the thread early from sleep, park or wait, for interrupt/unpark wake: (() => void) | null = null; // set when the bottom frame finishes diff --git a/test/java/SpinningNotifier.java b/test/java/SpinningNotifier.java new file mode 100644 index 0000000..1d4d44f --- /dev/null +++ b/test/java/SpinningNotifier.java @@ -0,0 +1,35 @@ +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; + +// The main thread spins on notify() while virtual threads wait on the same +// locks; the spinning must not hold up the threads it wakes. +public class SpinningNotifier { + public static void main(String[] args) { + int threads = 3000; + Object[] locks = {new Object(), new Object(), new Object(), new Object()}; + var finished = new AtomicInteger(); + try (var executor = Executors.newVirtualThreadPerTaskExecutor()) { + for (int i = 0; i < threads; i++) { + Object lock = locks[i % locks.length]; + executor.submit(() -> { + synchronized (lock) { + lock.wait(Long.MAX_VALUE); + } + finished.incrementAndGet(); + return null; + }); + synchronized (lock) { + lock.notify(); + } + } + while (finished.get() < threads) { + for (Object lock : locks) { + synchronized (lock) { + lock.notify(); + } + } + } + } + System.out.println("finished " + finished.get()); + } +}