Skip to content

Commit

Permalink
8305425: Thread.isAlive0 doesn't need to call into the VM
Browse files Browse the repository at this point in the history
Reviewed-by: phh, mdoerr
Backport-of: 35cb303a2c0c8b32de257c02e012a1928a6b4594
  • Loading branch information
shipilev committed Jun 28, 2023
1 parent 8204dfa commit 4fd0ee5
Showing 7 changed files with 131 additions and 17 deletions.
1 change: 0 additions & 1 deletion make/data/hotspot-symbols/symbols-unix
Original file line number Diff line number Diff line change
@@ -154,7 +154,6 @@ JVM_IsRecord
JVM_IsSameClassPackage
JVM_IsSharingEnabled
JVM_IsSupportedJNIVersion
JVM_IsThreadAlive
JVM_IsVMGeneratedMethodIx
JVM_LatestUserDefinedLoader
JVM_LoadLibrary
5 changes: 1 addition & 4 deletions src/hotspot/share/include/jvm.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, 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
@@ -257,9 +257,6 @@ JVM_StartThread(JNIEnv *env, jobject thread);
JNIEXPORT void JNICALL
JVM_StopThread(JNIEnv *env, jobject thread, jobject exception);

JNIEXPORT jboolean JNICALL
JVM_IsThreadAlive(JNIEnv *env, jobject thread);

JNIEXPORT void JNICALL
JVM_SuspendThread(JNIEnv *env, jobject thread);

6 changes: 0 additions & 6 deletions src/hotspot/share/prims/jvm.cpp
Original file line number Diff line number Diff line change
@@ -2976,12 +2976,6 @@ JVM_ENTRY(void, JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable))
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_SuspendThread(JNIEnv* env, jobject jthread))
ThreadsListHandle tlh(thread);
JavaThread* receiver = NULL;
4 changes: 3 additions & 1 deletion src/hotspot/share/runtime/thread.cpp
Original file line number Diff line number Diff line change
@@ -1337,7 +1337,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(), NULL);
lock.notify_all(thread);
// Ignore pending exception (ThreadDeath), since we are exiting anyway
15 changes: 12 additions & 3 deletions src/java.base/share/classes/java/lang/Thread.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2021, 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
@@ -157,7 +157,14 @@ public class Thread implements Runnable {

/* Fields reserved for exclusive use by the JVM */
private boolean stillborn = false;
private long eetop;

/*
* Reserved for exclusive use by the JVM. 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;

/* What will be run. */
private Runnable target;
@@ -1050,7 +1057,9 @@ public boolean isInterrupted() {
* @return {@code true} if this thread is alive;
* {@code false} otherwise.
*/
public final native boolean isAlive();
public final boolean isAlive() {
return eetop != 0;
}

/**
* Suspends this thread.
3 changes: 1 addition & 2 deletions src/java.base/share/native/libjava/Thread.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2019, 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
@@ -43,7 +43,6 @@
static JNINativeMethod methods[] = {
{"start0", "()V", (void *)&JVM_StartThread},
{"stop0", "(" OBJ ")V", (void *)&JVM_StopThread},
{"isAlive", "()Z", (void *)&JVM_IsThreadAlive},
{"suspend0", "()V", (void *)&JVM_SuspendThread},
{"resume0", "()V", (void *)&JVM_ResumeThread},
{"setPriority0", "(I)V", (void *)&JVM_SetThreadPriority},
114 changes: 114 additions & 0 deletions test/jdk/java/lang/Thread/IsAlive.java
Original file line number Diff line number Diff line change
@@ -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");
}
}

1 comment on commit 4fd0ee5

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.