Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/jvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<JThread>();
private scheduled = false;
private hashSeed = 0x2545f491;
Expand Down Expand Up @@ -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++;
Comment thread
project516-review-bot[bot] marked this conversation as resolved.
Resolved
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;
Expand Down
5 changes: 5 additions & 0 deletions src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions test/java/SpinningNotifier.java
Original file line number Diff line number Diff line change
@@ -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());
}
}