Skip to content

Commit 53ea8f0

Browse files
fengxue-ISNathan Henderson
andcommitted
Support using threadStatus field in JVM native
Update thread status changes to j.l.Thread fields so getState() API can be called without needing to halt the target thread. Co-authored-by: Nathan Henderson <nathan.henderson@ibm.com> Signed-off-by: Jack Lu <Jack.S.Lu@ibm.com>
1 parent 8bbc086 commit 53ea8f0

File tree

7 files changed

+63
-0
lines changed

7 files changed

+63
-0
lines changed

runtime/oti/VMHelpers.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,6 +2275,31 @@ class VM_VMHelpers
22752275
targetThread->privateFlags2 |= J9_PRIVATE_FLAGS2_REENTER_INTERPRETER;
22762276
indicateAsyncMessagePending(targetThread);
22772277
}
2278+
2279+
2280+
static U_64
2281+
setThreadState(J9VMThread *currentThread, U_64 state)
2282+
{
2283+
U_64 oldState = 0;
2284+
#if JAVA_SPEC_VERSION >= 19
2285+
j9object_t receiverObject = currentThread->carrierThreadObject;
2286+
if (NULL != receiverObject) {
2287+
/* Platform threads must have a non-null FieldHolder object. */
2288+
j9object_t threadHolder = J9VMJAVALANGTHREAD_HOLDER(currentThread, receiverObject);
2289+
if (NULL != threadHolder) {
2290+
oldState = J9VMJAVALANGTHREADFIELDHOLDER_THREADSTATUS(currentThread, threadHolder);
2291+
J9VMJAVALANGTHREADFIELDHOLDER_SET_THREADSTATUS(currentThread, threadHolder, state);
2292+
}
2293+
}
2294+
#else /* JAVA_SPEC_VERSION >= 19 */
2295+
j9object_t receiverObject = currentThread->threadObject;
2296+
if (NULL != receiverObject) {
2297+
oldState = J9VMJAVALANGTHREAD_THREADSTATUS(currentThread, receiverObject);
2298+
J9VMJAVALANGTHREAD_SET_THREADSTATUS(currentThread, receiverObject, state);
2299+
}
2300+
#endif /* JAVA_SPEC_VERSION >= 19 */
2301+
return oldState;
2302+
}
22782303
};
22792304

22802305
#endif /* VMHELPERS_HPP_ */

runtime/oti/vmconstantpool.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-ex
260260
<fieldref class="java/lang/Thread" name="priority" signature="I" versions="8-18"/>
261261
<fieldref class="java/lang/Thread" name="isDaemon" signature="Z" versions="8-18"/>
262262
<fieldref class="java/lang/Thread" name="group" signature="Ljava/lang/ThreadGroup;" versions="8-18"/>
263+
<fieldref class="java/lang/Thread" name="threadStatus" signature="I" versions="8-18"/>
263264

264265
<fieldref class="java/lang/VirtualThread" name="state" signature="I" versions="19-"/>
265266
<fieldref class="java/lang/VirtualThread" name="carrierThread" signature="Ljava/lang/Thread;" versions="19-"/>

runtime/vm/ObjectMonitor.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ objectMonitorEnterBlocking(J9VMThread *currentThread)
173173
IDATA waitTime = 1;
174174
if (J9_EVENT_IS_HOOKED(vm->hookInterface, J9HOOK_VM_MONITOR_CONTENDED_ENTER)) {
175175
bool frameBuilt = saveBlockingEnterObject(currentThread);
176+
/* Set j.l.Thread status to BLOCKED. */
177+
VM_VMHelpers::setThreadState(currentThread, J9VMTHREAD_STATE_BLOCKED);
176178
VM_VMAccess::setPublicFlags(currentThread, J9_PUBLIC_FLAGS_THREAD_BLOCKED);
177179
ALWAYS_TRIGGER_J9HOOK_VM_MONITOR_CONTENDED_ENTER(vm->hookInterface, currentThread, monitor);
178180
restoreBlockingEnterObject(currentThread, frameBuilt);
@@ -185,6 +187,8 @@ objectMonitorEnterBlocking(J9VMThread *currentThread)
185187
goto releasedAccess;
186188
}
187189
restart:
190+
/* Set j.l.Thread status to BLOCKED. */
191+
VM_VMHelpers::setThreadState(currentThread, J9VMTHREAD_STATE_BLOCKED);
188192
internalReleaseVMAccessSetStatus(currentThread, J9_PUBLIC_FLAGS_THREAD_BLOCKED);
189193
releasedAccess:
190194
omrthread_monitor_enter_using_threadId(monitor, osThread);
@@ -285,6 +289,9 @@ objectMonitorEnterBlocking(J9VMThread *currentThread)
285289
}
286290
done:
287291
clearEventFlag(currentThread, J9_PUBLIC_FLAGS_THREAD_BLOCKED);
292+
/* Set j.l.Thread status to RUNNING state. */
293+
VM_VMHelpers::setThreadState(currentThread, J9VMTHREAD_STATE_RUNNING);
294+
288295
/* Clear the SUPPRESS_CONTENDED_EXITS bit in the monitor saying that CONTENDED EXIT can be sent again */
289296
((J9ThreadMonitor*)monitor)->flags &= ~(UDATA)J9THREAD_MONITOR_SUPPRESS_CONTENDED_EXIT;
290297
VM_AtomicSupport::subtract(&monitor->pinCount, 1);

runtime/vm/callin.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,14 @@ initializeAttachedThreadImpl(J9VMThread *currentThread, const char *name, j9obje
590590
currentThread->returnValue2 = (UDATA)J9VMJAVALANGTHREAD_INIT_METHOD(vm);
591591
c_cInterpreter(currentThread);
592592
J9VMJAVALANGTHREAD_SET_STARTED(currentThread, initializee->threadObject, JNI_TRUE);
593+
#if JAVA_SPEC_VERSION >= 19
594+
j9object_t threadHolder = J9VMJAVALANGTHREAD_HOLDER(currentThread, initializee->threadObject);
595+
if (NULL != threadHolder) {
596+
J9VMJAVALANGTHREADFIELDHOLDER_SET_THREADSTATUS(currentThread, threadHolder, J9VMTHREAD_STATE_RUNNING);
597+
}
598+
#else /* JAVA_SPEC_VERSION >= 19 */
599+
J9VMJAVALANGTHREAD_SET_THREADSTATUS(currentThread, initializee->threadObject, J9VMTHREAD_STATE_RUNNING);
600+
#endif /* JAVA_SPEC_VERSION >= 19 */
593601
}
594602
}
595603
done:

runtime/vm/threadhelp.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,16 @@ monitorWaitImpl(J9VMThread *vmThread, j9object_t object, I_64 millis, I_32 nanos
106106
#endif
107107
J9VMTHREAD_SET_BLOCKINGENTEROBJECT(vmThread, vmThread, object);
108108
object = NULL;
109+
/* Set j.l.Thread status to WAITING. */
110+
U_64 oldState = J9_ARE_ANY_BITS_SET(thrstate, J9_PUBLIC_FLAGS_THREAD_TIMED)
111+
? VM_VMHelpers::setThreadState(vmThread, J9VMTHREAD_STATE_WAITING_TIMED)
112+
: VM_VMHelpers::setThreadState(vmThread, J9VMTHREAD_STATE_WAITING);
109113
internalReleaseVMAccessSetStatus(vmThread, thrstate);
110114
rc = timeCompensationHelper(vmThread,
111115
interruptable ? HELPER_TYPE_MONITOR_WAIT_INTERRUPTABLE : HELPER_TYPE_MONITOR_WAIT_TIMED, monitor, millis, nanos);
112116
internalAcquireVMAccessClearStatus(vmThread, thrstate);
117+
/* Set j.l.Thread status to oldState. */
118+
VM_VMHelpers::setThreadState(vmThread, oldState);
113119
J9VMTHREAD_SET_BLOCKINGENTEROBJECT(vmThread, vmThread, NULL);
114120
omrthread_monitor_unpin(monitor, vmThread->osThread);
115121
TRIGGER_J9HOOK_VM_MONITOR_WAITED(javaVM->hookInterface, vmThread, monitor, millis, nanos, rc, startTicks, (UDATA) monitor, VM_VMHelpers::currentClass(monitorClass));
@@ -179,9 +185,13 @@ threadSleepImpl(J9VMThread *vmThread, I_64 millis, I_32 nanos)
179185
#endif
180186
if (0 == rc) {
181187
TRIGGER_J9HOOK_VM_SLEEP(javaVM->hookInterface, vmThread, millis, nanos);
188+
/* Set j.l.Thread status to SLEEPING. */
189+
U_64 oldState = VM_VMHelpers::setThreadState(vmThread, J9VMTHREAD_STATE_SLEEPING);
182190
internalReleaseVMAccessSetStatus(vmThread, J9_PUBLIC_FLAGS_THREAD_SLEEPING);
183191
rc = timeCompensationHelper(vmThread, HELPER_TYPE_THREAD_SLEEP, NULL, millis, nanos);
184192
internalAcquireVMAccessClearStatus(vmThread, J9_PUBLIC_FLAGS_THREAD_SLEEPING);
193+
/* Set j.l.Thread status to oldState. */
194+
VM_VMHelpers::setThreadState(vmThread, oldState);
185195
TRIGGER_J9HOOK_VM_SLEPT(javaVM->hookInterface, vmThread, millis, nanos, startTicks);
186196
}
187197

runtime/vm/threadpark.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ threadParkImpl(J9VMThread *vmThread, BOOLEAN timeoutIsEpochRelative, I_64 timeou
9898
/* vmThread->threadObject != NULL because vmThread must be the current thread */
9999
J9VMTHREAD_SET_BLOCKINGENTEROBJECT(vmThread, vmThread, J9VMJAVALANGTHREAD_PARKBLOCKER(vmThread, vmThread->threadObject));
100100
TRIGGER_J9HOOK_VM_PARK(vm->hookInterface, vmThread, millis, nanos);
101+
/* Set j.l.Thread status to WAITING. */
102+
U_64 oldState = J9_ARE_ANY_BITS_SET(thrstate, J9_PUBLIC_FLAGS_THREAD_TIMED)
103+
? VM_VMHelpers::setThreadState(vmThread, J9VMTHREAD_STATE_WAITING_TIMED)
104+
: VM_VMHelpers::setThreadState(vmThread, J9VMTHREAD_STATE_WAITING);
101105
internalReleaseVMAccessSetStatus(vmThread, thrstate);
102106

103107
while (1) {
@@ -116,6 +120,8 @@ threadParkImpl(J9VMThread *vmThread, BOOLEAN timeoutIsEpochRelative, I_64 timeou
116120
}
117121

118122
internalAcquireVMAccessClearStatus(vmThread, thrstate);
123+
/* Set j.l.Thread status to oldState. */
124+
VM_VMHelpers::setThreadState(vmThread, oldState);
119125
TRIGGER_J9HOOK_VM_UNPARKED(vm->hookInterface, vmThread, millis, nanos, startTicks, (UDATA) parkedAddress, VM_VMHelpers::currentClass(parkedClass));
120126
J9VMTHREAD_SET_BLOCKINGENTEROBJECT(vmThread, vmThread, NULL);
121127
}

runtime/vm/vmthread.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "ut_j9vm.h"
4545
#include "vm_internal.h"
4646
#include "vmaccess.h"
47+
#include "VMHelpers.hpp"
4748
#include "vmhook_internal.h"
4849

4950
#include "HeapIteratorAPI.h"
@@ -459,6 +460,8 @@ void threadCleanup(J9VMThread * vmThread, UDATA forkedByVM)
459460
/* Safe to call this whether handleUncaughtException clears the exception or not */
460461
internalExceptionDescribe(vmThread);
461462
}
463+
/* Set j.l.Thread status to TERMINATED. */
464+
VM_VMHelpers::setThreadState(vmThread, J9VMTHREAD_STATE_DEAD);
462465
releaseVMAccess(vmThread);
463466

464467
/* Mark this thread as dead */
@@ -2010,6 +2013,9 @@ startJavaThreadInternal(J9VMThread * currentThread, UDATA privateFlags, UDATA os
20102013
}
20112014
J9VMJAVALANGTHREAD_SET_THREADREF(currentThread, threadObject, newThread);
20122015

2016+
/* Set j.l.Thread status to RUNNABLE. */
2017+
VM_VMHelpers::setThreadState(currentThread, J9VMTHREAD_STATE_RUNNING);
2018+
20132019
#if (JAVA_SPEC_VERSION >= 14)
20142020
/* If thread was interrupted before start, make sure interrupt flag is set for running thread. */
20152021
if (J9VMJAVALANGTHREAD_DEADINTERRUPT(currentThread, threadObject)) {

0 commit comments

Comments
 (0)