diff --git a/make/data/hotspot-symbols/symbols-unix b/make/data/hotspot-symbols/symbols-unix index 0870a8c2187..c08ce4590fc 100644 --- a/make/data/hotspot-symbols/symbols-unix +++ b/make/data/hotspot-symbols/symbols-unix @@ -162,7 +162,6 @@ JVM_IsRecord JVM_IsSameClassPackage JVM_IsSharingEnabled JVM_IsSupportedJNIVersion -JVM_IsThreadAlive JVM_IsVMGeneratedMethodIx JVM_LatestUserDefinedLoader JVM_LoadZipLibrary diff --git a/src/hotspot/share/include/jvm.h b/src/hotspot/share/include/jvm.h index defb99f0406..55e640cd8b0 100644 --- a/src/hotspot/share/include/jvm.h +++ b/src/hotspot/share/include/jvm.h @@ -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); diff --git a/src/hotspot/share/prims/jvm.cpp b/src/hotspot/share/prims/jvm.cpp index 432ac6692bd..221c5b95d70 100644 --- a/src/hotspot/share/prims/jvm.cpp +++ b/src/hotspot/share/prims/jvm.cpp @@ -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; diff --git a/src/hotspot/share/runtime/javaThread.cpp b/src/hotspot/share/runtime/javaThread.cpp index 124e6c3223b..9eee1028c86 100644 --- a/src/hotspot/share/runtime/javaThread.cpp +++ b/src/hotspot/share/runtime/javaThread.cpp @@ -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 diff --git a/src/java.base/share/classes/java/lang/Thread.java b/src/java.base/share/classes/java/lang/Thread.java index 9f0c22ec9de..0c1380d553f 100644 --- a/src/java.base/share/classes/java/lang/Thread.java +++ b/src/java.base/share/classes/java/lang/Thread.java @@ -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}. diff --git a/src/java.base/share/native/libjava/Thread.c b/src/java.base/share/native/libjava/Thread.c index 4230d167cdc..fea2995bb05 100644 --- a/src/java.base/share/native/libjava/Thread.c +++ b/src/java.base/share/native/libjava/Thread.c @@ -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}, diff --git a/test/jdk/java/lang/Thread/IsAlive.java b/test/jdk/java/lang/Thread/IsAlive.java new file mode 100644 index 00000000000..ffb7ca6fea5 --- /dev/null +++ b/test/jdk/java/lang/Thread/IsAlive.java @@ -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"); + } +}