8305425: Thread.isAlive0 doesn't need to call into the VM
Co-authored-by: Aleksey Shipilev <shade@openjdk.org> Reviewed-by: shade, coleenp, alanb
This commit is contained in:
parent
b5d204c3a4
commit
35cb303a2c
@ -162,7 +162,6 @@ JVM_IsRecord
|
||||
JVM_IsSameClassPackage
|
||||
JVM_IsSharingEnabled
|
||||
JVM_IsSupportedJNIVersion
|
||||
JVM_IsThreadAlive
|
||||
JVM_IsVMGeneratedMethodIx
|
||||
JVM_LatestUserDefinedLoader
|
||||
JVM_LoadZipLibrary
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1997, 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
|
||||
@ -266,9 +266,6 @@ JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, job
|
||||
JNIEXPORT void JNICALL
|
||||
JVM_StartThread(JNIEnv *env, jobject thread);
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
JVM_IsThreadAlive(JNIEnv *env, jobject thread);
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
JVM_SetThreadPriority(JNIEnv *env, jobject thread, jint prio);
|
||||
|
||||
|
@ -3020,12 +3020,6 @@ JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
|
||||
JVM_END
|
||||
|
||||
|
||||
JVM_ENTRY(jboolean, JVM_IsThreadAlive(JNIEnv* env, jobject jthread))
|
||||
oop thread_oop = JNIHandles::resolve_non_null(jthread);
|
||||
return java_lang_Thread::is_alive(thread_oop);
|
||||
JVM_END
|
||||
|
||||
|
||||
JVM_ENTRY(void, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio))
|
||||
ThreadsListHandle tlh(thread);
|
||||
oop java_thread = nullptr;
|
||||
|
@ -739,7 +739,9 @@ static void ensure_join(JavaThread* thread) {
|
||||
// Thread is exiting. So set thread_status field in java.lang.Thread class to TERMINATED.
|
||||
java_lang_Thread::set_thread_status(threadObj(), JavaThreadStatus::TERMINATED);
|
||||
// Clear the native thread instance - this makes isAlive return false and allows the join()
|
||||
// to complete once we've done the notify_all below
|
||||
// to complete once we've done the notify_all below. Needs a release() to obey Java Memory Model
|
||||
// requirements.
|
||||
OrderAccess::release();
|
||||
java_lang_Thread::set_thread(threadObj(), nullptr);
|
||||
lock.notify_all(thread);
|
||||
// Ignore pending exception, since we are exiting anyway
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1994, 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
|
||||
@ -226,8 +226,15 @@ public class Thread implements Runnable {
|
||||
registerNatives();
|
||||
}
|
||||
|
||||
/* Reserved for exclusive use by the JVM, maybe move to FieldHolder */
|
||||
private long eetop;
|
||||
/*
|
||||
* Reserved for exclusive use by the JVM. Cannot be moved to the FieldHolder
|
||||
* as it needs to be set by the VM for JNI attaching threads, before executing
|
||||
* the constructor that will create the FieldHolder. The historically named
|
||||
* `eetop` holds the address of the underlying VM JavaThread, and is set to
|
||||
* non-zero when the thread is started, and reset to zero when the thread terminates.
|
||||
* A non-zero value indicates this thread isAlive().
|
||||
*/
|
||||
private volatile long eetop;
|
||||
|
||||
// thread id
|
||||
private final long tid;
|
||||
@ -1840,9 +1847,8 @@ public class Thread implements Runnable {
|
||||
* This method is non-final so it can be overridden.
|
||||
*/
|
||||
boolean alive() {
|
||||
return isAlive0();
|
||||
return eetop != 0;
|
||||
}
|
||||
private native boolean isAlive0();
|
||||
|
||||
/**
|
||||
* Throws {@code UnsupportedOperationException}.
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1994, 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
|
||||
@ -37,7 +37,6 @@
|
||||
|
||||
static JNINativeMethod methods[] = {
|
||||
{"start0", "()V", (void *)&JVM_StartThread},
|
||||
{"isAlive0", "()Z", (void *)&JVM_IsThreadAlive},
|
||||
{"setPriority0", "(I)V", (void *)&JVM_SetThreadPriority},
|
||||
{"yield0", "()V", (void *)&JVM_Yield},
|
||||
{"sleep0", "(J)V", (void *)&JVM_Sleep},
|
||||
|
114
test/jdk/java/lang/Thread/IsAlive.java
Normal file
114
test/jdk/java/lang/Thread/IsAlive.java
Normal file
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright 2023, Amazon.com Inc. 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8305425
|
||||
* @summary Check Thread.isAlive
|
||||
* @run main/othervm/timeout=10 IsAlive
|
||||
*/
|
||||
|
||||
public class IsAlive {
|
||||
|
||||
static boolean spinnerDone;
|
||||
|
||||
private static void spin() {
|
||||
try {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
Thread.sleep(100);
|
||||
}
|
||||
} catch (InterruptedException ie) {
|
||||
// Do nothing, just exit
|
||||
}
|
||||
spinnerDone = true;
|
||||
}
|
||||
|
||||
static volatile boolean checkerReady;
|
||||
|
||||
private static void check(Thread t) {
|
||||
while (!t.isAlive()) {
|
||||
// Burn hard, without any sleeps.
|
||||
// Check that we discover the thread is alive eventually.
|
||||
}
|
||||
|
||||
checkerReady = true;
|
||||
|
||||
while (t.isAlive()) {
|
||||
// Burn hard, without any sleeps.
|
||||
// Check that we discover the thread is not alive eventually.
|
||||
}
|
||||
|
||||
if (!spinnerDone) {
|
||||
throw new RuntimeException("Last write of terminated thread was not seen!");
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertAlive(Thread t) {
|
||||
if (!t.isAlive()) {
|
||||
throw new IllegalStateException("Thread " + t + " is not alive, but it should be");
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertNotAlive(Thread t) {
|
||||
if (t.isAlive()) {
|
||||
throw new IllegalStateException("Thread " + t + " is alive, but it should not be");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
Thread spinner = new Thread(IsAlive::spin);
|
||||
spinner.setName("Spinner");
|
||||
spinner.setDaemon(true);
|
||||
|
||||
Thread checker = new Thread(() -> check(spinner));
|
||||
checker.setName("Checker");
|
||||
checker.setDaemon(true);
|
||||
|
||||
assertNotAlive(spinner);
|
||||
assertNotAlive(checker);
|
||||
|
||||
System.out.println("Starting spinner");
|
||||
spinner.start();
|
||||
assertAlive(spinner);
|
||||
|
||||
System.out.println("Starting checker");
|
||||
checker.start();
|
||||
assertAlive(checker);
|
||||
|
||||
System.out.println("Waiting for checker to catch up");
|
||||
while (!checkerReady) {
|
||||
Thread.sleep(100);
|
||||
}
|
||||
|
||||
System.out.println("Interrupting and joining spinner");
|
||||
spinner.interrupt();
|
||||
spinner.join();
|
||||
assertNotAlive(spinner);
|
||||
|
||||
System.out.println("Joining checker");
|
||||
checker.join();
|
||||
assertNotAlive(checker);
|
||||
|
||||
System.out.println("Complete");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user