Skip to content

Commit

Permalink
Add Thread.onSpinWait + ensureParallelism
Browse files Browse the repository at this point in the history
  • Loading branch information
pchilano committed Dec 18, 2024
1 parent a7f68de commit 00c8aeb
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions test/jdk/java/lang/Thread/virtual/MonitorEnterWaitOOME.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import jdk.test.lib.thread.VThreadRunner;

public class MonitorEnterWaitOOME {
static volatile Object data;
static Thread.State dummyState = Thread.State.RUNNABLE; // load java.lang.Thread$State
Expand All @@ -57,6 +59,8 @@ public static void main(String[] args) throws Throwable {
final boolean testWait = args.length >= 1 ? Boolean.parseBoolean(args[0]) : false;
final long timeout = testWait && args.length == 2 ? Long.parseLong(args[1]) : 0L;

VThreadRunner.ensureParallelism(2);

Thread vthread;
var lock = new Object();
var canFillHeap = new AtomicBoolean();
Expand Down Expand Up @@ -118,12 +122,16 @@ private static Object[] fillHeap() {

private static void awaitTrue(AtomicBoolean ready) {
// Don't call anything that might allocate from the Java heap.
while (!ready.get()) {}
while (!ready.get()) {
Thread.onSpinWait();
}
}

private static void awaitState(Thread thread, Thread.State expectedState) {
// Don't call anything that might allocate from the Java heap.
while (thread.getState() != expectedState) {}
while (thread.getState() != expectedState) {
Thread.onSpinWait();
}
}

private static void joinVThread(Thread vthread, AtomicBoolean ready, AtomicReference<Throwable> exRef) throws Throwable {
Expand All @@ -133,6 +141,7 @@ private static void joinVThread(Thread vthread, AtomicBoolean ready, AtomicRefer
if (ex != null) {
throw ex;
}
Thread.onSpinWait();
}
vthread.join();
}
Expand Down

0 comments on commit 00c8aeb

Please sign in to comment.