8309408: Thread.sleep cleanup

Reviewed-by: dholmes, stefank
This commit is contained in:
Alan Bateman 2023-06-16 07:26:27 +00:00
parent 32243ef47d
commit 227656f3a9
10 changed files with 45 additions and 48 deletions

View file

@ -200,7 +200,7 @@ JVM_SetNativeThreadName
JVM_SetPrimitiveArrayElement
JVM_SetStackWalkContinuation
JVM_SetThreadPriority
JVM_Sleep
JVM_SleepNanos
JVM_StartThread
JVM_SupportsCX8
JVM_TotalMemory

View file

@ -276,7 +276,7 @@ JNIEXPORT void JNICALL
JVM_Yield(JNIEnv *env, jclass threadClass);
JNIEXPORT void JNICALL
JVM_Sleep(JNIEnv *env, jclass threadClass, jlong nanos);
JVM_SleepNanos(JNIEnv *env, jclass threadClass, jlong nanos);
JNIEXPORT jobject JNICALL
JVM_CurrentCarrierThread(JNIEnv *env, jclass threadClass);

View file

@ -3044,7 +3044,7 @@ JVM_LEAF(void, JVM_Yield(JNIEnv *env, jclass threadClass))
os::naked_yield();
JVM_END
JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong nanos))
JVM_ENTRY(void, JVM_SleepNanos(JNIEnv* env, jclass threadClass, jlong nanos))
if (nanos < 0) {
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nanosecond timeout value out of range");
}

View file

@ -478,6 +478,25 @@ public class Thread implements Runnable {
}
}
/**
* Sleep for the specified number of nanoseconds, subject to the precision
* and accuracy of system timers and schedulers.
*/
private static void sleepNanos(long nanos) throws InterruptedException {
ThreadSleepEvent event = beforeSleep(nanos);
try {
if (currentThread() instanceof VirtualThread vthread) {
vthread.sleepNanos(nanos);
} else {
sleepNanos0(nanos);
}
} finally {
afterSleep(event);
}
}
private static native void sleepNanos0(long nanos) throws InterruptedException;
/**
* Causes the currently executing thread to sleep (temporarily cease
* execution) for the specified number of milliseconds, subject to
@ -499,22 +518,10 @@ public class Thread implements Runnable {
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
long nanos = MILLISECONDS.toNanos(millis);
ThreadSleepEvent event = beforeSleep(nanos);
try {
if (currentThread() instanceof VirtualThread vthread) {
vthread.sleepNanos(nanos);
} else {
sleep0(nanos);
}
} finally {
afterSleep(event);
}
sleepNanos(nanos);
}
private static native void sleep0(long nanos) throws InterruptedException;
/**
* Causes the currently executing thread to sleep (temporarily cease
* execution) for the specified number of milliseconds plus the specified
@ -549,17 +556,7 @@ public class Thread implements Runnable {
// total sleep time, in nanoseconds
long totalNanos = MILLISECONDS.toNanos(millis);
totalNanos += Math.min(Long.MAX_VALUE - totalNanos, nanos);
ThreadSleepEvent event = beforeSleep(totalNanos);
try {
if (currentThread() instanceof VirtualThread vthread) {
vthread.sleepNanos(totalNanos);
} else {
sleep0(totalNanos);
}
} finally {
afterSleep(event);
}
sleepNanos(totalNanos);
}
/**
@ -583,17 +580,7 @@ public class Thread implements Runnable {
if (nanos < 0) {
return;
}
ThreadSleepEvent event = beforeSleep(nanos);
try {
if (currentThread() instanceof VirtualThread vthread) {
vthread.sleepNanos(nanos);
} else {
sleep0(nanos);
}
} finally {
afterSleep(event);
}
sleepNanos(nanos);
}
/**

View file

@ -39,7 +39,7 @@ static JNINativeMethod methods[] = {
{"start0", "()V", (void *)&JVM_StartThread},
{"setPriority0", "(I)V", (void *)&JVM_SetThreadPriority},
{"yield0", "()V", (void *)&JVM_Yield},
{"sleep0", "(J)V", (void *)&JVM_Sleep},
{"sleepNanos0", "(J)V", (void *)&JVM_SleepNanos},
{"currentCarrierThread", "()" THD, (void *)&JVM_CurrentCarrierThread},
{"currentThread", "()" THD, (void *)&JVM_CurrentThread},
{"setCurrentThread", "(" THD ")V", (void *)&JVM_SetCurrentThread},

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -36,6 +36,7 @@
* -XX:+WhiteBoxAPI
* -XX:CompileCommand=dontinline,compiler.codecache.stress.Helper$TestCase::method
* -XX:CompileCommand=exclude,java.lang.Thread::sleep
* -XX:CompileCommand=exclude,java.lang.Thread::sleepNanos
* -XX:CompileCommand=exclude,java.lang.Thread::beforeSleep
* -XX:CompileCommand=exclude,java.lang.Thread::afterSleep
* -XX:CompileCommand=exclude,java.lang.Math::min
@ -48,6 +49,7 @@
* -XX:+WhiteBoxAPI
* -XX:CompileCommand=dontinline,compiler.codecache.stress.Helper$TestCase::method
* -XX:CompileCommand=exclude,java.lang.Thread::sleep
* -XX:CompileCommand=exclude,java.lang.Thread::sleepNanos
* -XX:CompileCommand=exclude,java.lang.Thread::beforeSleep
* -XX:CompileCommand=exclude,java.lang.Thread::afterSleep
* -XX:CompileCommand=exclude,java.lang.Math::min

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -104,13 +104,13 @@ public class ownedMonitorsStackDepthInfo001a extends AbstractJDWPDebuggee {
}
static int adjustStackDepth(int depth) {
// The stack depth does not take into account the extra frame below Thread.sleep() (sleep0),
// The stack depth does not take into account the extra frames below Thread.sleep()
// so we need to add it to the stack depth. See LockingThread.expectedDepth(), which is where the
// depth is calculated.
if (depth == -1) {
return -1;
} else {
return depth + 1;
return depth + 2;
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -656,7 +656,10 @@ class SleepingThread extends BaseThread {
expectedLength += 4;
expectedMethods.add(Thread.class.getName() + ".sleep");
expectedMethods.add(Thread.class.getName() + ".sleep0");
expectedMethods.add(Thread.class.getName() + ".sleepNanos");
expectedMethods.add(Thread.class.getName() + ".sleepNanos0");
expectedMethods.add(Thread.class.getName() + ".beforeSleep");
expectedMethods.add(Thread.class.getName() + ".afterSleep");
expectedMethods.add(Thread.class.getName() + ".currentCarrierThread");
expectedMethods.add(Thread.class.getName() + ".currentThread");
// jdk.internal.event.ThreadSleepEvent not accessible

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -36,7 +36,11 @@ public class SleepingThread extends RecursiveMonitoringThread {
private Object readyLock = new Object();
private static final String[] expectedMethods = {
"java.lang.Thread.sleep",
"java.lang.Thread.sleep0",
"java.lang.Thread.sleepNanos",
"java.lang.Thread.sleepNanos0",
"java.lang.Thread.beforeSleep",
"java.lang.Thread.afterSleep",
"java.util.concurrent.TimeUnit.toNanos",
"jdk.internal.event.ThreadSleepEvent.<clinit>",
"jdk.internal.event.ThreadSleepEvent.isTurnedOn",
"jdk.internal.event.ThreadSleepEvent.isEnabled",

View file

@ -140,7 +140,8 @@ public class strace001 {
private static boolean fillTrace() {
expectedSystemTrace = new String[]{
"java.lang.Thread.sleep",
"java.lang.Thread.sleep0",
"java.lang.Thread.sleepNanos",
"java.lang.Thread.sleepNanos0",
"java.lang.Thread.beforeSleep",
"java.lang.Thread.afterSleep",
"java.lang.Thread.yield",