Skip to content

Commit 35cb303

Browse files
David Holmesshipilev
andcommitted
8305425: Thread.isAlive0 doesn't need to call into the VM
Co-authored-by: Aleksey Shipilev <shade@openjdk.org> Reviewed-by: shade, coleenp, alanb
1 parent b5d204c commit 35cb303

File tree

7 files changed

+130
-19
lines changed

7 files changed

+130
-19
lines changed

make/data/hotspot-symbols/symbols-unix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ JVM_IsRecord
162162
JVM_IsSameClassPackage
163163
JVM_IsSharingEnabled
164164
JVM_IsSupportedJNIVersion
165-
JVM_IsThreadAlive
166165
JVM_IsVMGeneratedMethodIx
167166
JVM_LatestUserDefinedLoader
168167
JVM_LoadZipLibrary

src/hotspot/share/include/jvm.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* 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
266266
JNIEXPORT void JNICALL
267267
JVM_StartThread(JNIEnv *env, jobject thread);
268268

269-
JNIEXPORT jboolean JNICALL
270-
JVM_IsThreadAlive(JNIEnv *env, jobject thread);
271-
272269
JNIEXPORT void JNICALL
273270
JVM_SetThreadPriority(JNIEnv *env, jobject thread, jint prio);
274271

src/hotspot/share/prims/jvm.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3020,12 +3020,6 @@ JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
30203020
JVM_END
30213021

30223022

3023-
JVM_ENTRY(jboolean, JVM_IsThreadAlive(JNIEnv* env, jobject jthread))
3024-
oop thread_oop = JNIHandles::resolve_non_null(jthread);
3025-
return java_lang_Thread::is_alive(thread_oop);
3026-
JVM_END
3027-
3028-
30293023
JVM_ENTRY(void, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio))
30303024
ThreadsListHandle tlh(thread);
30313025
oop java_thread = nullptr;

src/hotspot/share/runtime/javaThread.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,9 @@ static void ensure_join(JavaThread* thread) {
739739
// Thread is exiting. So set thread_status field in java.lang.Thread class to TERMINATED.
740740
java_lang_Thread::set_thread_status(threadObj(), JavaThreadStatus::TERMINATED);
741741
// Clear the native thread instance - this makes isAlive return false and allows the join()
742-
// to complete once we've done the notify_all below
742+
// to complete once we've done the notify_all below. Needs a release() to obey Java Memory Model
743+
// requirements.
744+
OrderAccess::release();
743745
java_lang_Thread::set_thread(threadObj(), nullptr);
744746
lock.notify_all(thread);
745747
// Ignore pending exception, since we are exiting anyway

src/java.base/share/classes/java/lang/Thread.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -226,8 +226,15 @@ public class Thread implements Runnable {
226226
registerNatives();
227227
}
228228

229-
/* Reserved for exclusive use by the JVM, maybe move to FieldHolder */
230-
private long eetop;
229+
/*
230+
* Reserved for exclusive use by the JVM. Cannot be moved to the FieldHolder
231+
* as it needs to be set by the VM for JNI attaching threads, before executing
232+
* the constructor that will create the FieldHolder. The historically named
233+
* `eetop` holds the address of the underlying VM JavaThread, and is set to
234+
* non-zero when the thread is started, and reset to zero when the thread terminates.
235+
* A non-zero value indicates this thread isAlive().
236+
*/
237+
private volatile long eetop;
231238

232239
// thread id
233240
private final long tid;
@@ -1840,9 +1847,8 @@ public final boolean isAlive() {
18401847
* This method is non-final so it can be overridden.
18411848
*/
18421849
boolean alive() {
1843-
return isAlive0();
1850+
return eetop != 0;
18441851
}
1845-
private native boolean isAlive0();
18461852

18471853
/**
18481854
* Throws {@code UnsupportedOperationException}.

src/java.base/share/native/libjava/Thread.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -37,7 +37,6 @@
3737

3838
static JNINativeMethod methods[] = {
3939
{"start0", "()V", (void *)&JVM_StartThread},
40-
{"isAlive0", "()Z", (void *)&JVM_IsThreadAlive},
4140
{"setPriority0", "(I)V", (void *)&JVM_SetThreadPriority},
4241
{"yield0", "()V", (void *)&JVM_Yield},
4342
{"sleep0", "(J)V", (void *)&JVM_Sleep},
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2023, Amazon.com Inc. or its affiliates. All Rights Reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8305425
27+
* @summary Check Thread.isAlive
28+
* @run main/othervm/timeout=10 IsAlive
29+
*/
30+
31+
public class IsAlive {
32+
33+
static boolean spinnerDone;
34+
35+
private static void spin() {
36+
try {
37+
while (!Thread.currentThread().isInterrupted()) {
38+
Thread.sleep(100);
39+
}
40+
} catch (InterruptedException ie) {
41+
// Do nothing, just exit
42+
}
43+
spinnerDone = true;
44+
}
45+
46+
static volatile boolean checkerReady;
47+
48+
private static void check(Thread t) {
49+
while (!t.isAlive()) {
50+
// Burn hard, without any sleeps.
51+
// Check that we discover the thread is alive eventually.
52+
}
53+
54+
checkerReady = true;
55+
56+
while (t.isAlive()) {
57+
// Burn hard, without any sleeps.
58+
// Check that we discover the thread is not alive eventually.
59+
}
60+
61+
if (!spinnerDone) {
62+
throw new RuntimeException("Last write of terminated thread was not seen!");
63+
}
64+
}
65+
66+
private static void assertAlive(Thread t) {
67+
if (!t.isAlive()) {
68+
throw new IllegalStateException("Thread " + t + " is not alive, but it should be");
69+
}
70+
}
71+
72+
private static void assertNotAlive(Thread t) {
73+
if (t.isAlive()) {
74+
throw new IllegalStateException("Thread " + t + " is alive, but it should not be");
75+
}
76+
}
77+
78+
public static void main(String args[]) throws Exception {
79+
Thread spinner = new Thread(IsAlive::spin);
80+
spinner.setName("Spinner");
81+
spinner.setDaemon(true);
82+
83+
Thread checker = new Thread(() -> check(spinner));
84+
checker.setName("Checker");
85+
checker.setDaemon(true);
86+
87+
assertNotAlive(spinner);
88+
assertNotAlive(checker);
89+
90+
System.out.println("Starting spinner");
91+
spinner.start();
92+
assertAlive(spinner);
93+
94+
System.out.println("Starting checker");
95+
checker.start();
96+
assertAlive(checker);
97+
98+
System.out.println("Waiting for checker to catch up");
99+
while (!checkerReady) {
100+
Thread.sleep(100);
101+
}
102+
103+
System.out.println("Interrupting and joining spinner");
104+
spinner.interrupt();
105+
spinner.join();
106+
assertNotAlive(spinner);
107+
108+
System.out.println("Joining checker");
109+
checker.join();
110+
assertNotAlive(checker);
111+
112+
System.out.println("Complete");
113+
}
114+
}

0 commit comments

Comments
 (0)