Skip to content

Commit 0a2e2bb

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 f21d6b0 commit 0a2e2bb

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
@@ -2277,6 +2277,31 @@ class VM_VMHelpers
22772277
targetThread->privateFlags2 |= J9_PRIVATE_FLAGS2_REENTER_INTERPRETER;
22782278
indicateAsyncMessagePending(targetThread);
22792279
}
2280+
2281+
2282+
static U_32
2283+
setThreadState(J9VMThread *currentThread, U_32 state)
2284+
{
2285+
U_32 oldState = 0;
2286+
#if JAVA_SPEC_VERSION >= 19
2287+
j9object_t receiverObject = currentThread->carrierThreadObject;
2288+
if (NULL != receiverObject) {
2289+
/* Platform threads must have a non-null FieldHolder object. */
2290+
j9object_t threadHolder = J9VMJAVALANGTHREAD_HOLDER(currentThread, receiverObject);
2291+
if (NULL != threadHolder) {
2292+
oldState = (U_32)J9VMJAVALANGTHREADFIELDHOLDER_THREADSTATUS(currentThread, threadHolder);
2293+
J9VMJAVALANGTHREADFIELDHOLDER_SET_THREADSTATUS(currentThread, threadHolder, state);
2294+
}
2295+
}
2296+
#else /* JAVA_SPEC_VERSION >= 19 */
2297+
j9object_t receiverObject = currentThread->threadObject;
2298+
if (NULL != receiverObject) {
2299+
oldState = (U_32)J9VMJAVALANGTHREAD_THREADSTATUS(currentThread, receiverObject);
2300+
J9VMJAVALANGTHREAD_SET_THREADSTATUS(currentThread, receiverObject, state);
2301+
}
2302+
#endif /* JAVA_SPEC_VERSION >= 19 */
2303+
return oldState;
2304+
}
22802305
};
22812306

22822307
#endif /* VMHELPERS_HPP_ */

runtime/oti/vmconstantpool.xml

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

266267
<fieldref class="java/lang/VirtualThread" name="state" signature="I" versions="19-"/>
267268
<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
@@ -185,6 +185,8 @@ objectMonitorEnterBlocking(J9VMThread *currentThread)
185185

186186
if (J9_EVENT_IS_HOOKED(vm->hookInterface, J9HOOK_VM_MONITOR_CONTENDED_ENTER)) {
187187
bool frameBuilt = saveBlockingEnterObject(currentThread);
188+
/* Set j.l.Thread status to BLOCKED. */
189+
VM_VMHelpers::setThreadState(currentThread, J9VMTHREAD_STATE_BLOCKED);
188190
VM_VMAccess::setPublicFlags(currentThread, J9_PUBLIC_FLAGS_THREAD_BLOCKED);
189191
ALWAYS_TRIGGER_J9HOOK_VM_MONITOR_CONTENDED_ENTER(vm->hookInterface, currentThread, monitor);
190192
restoreBlockingEnterObject(currentThread, frameBuilt);
@@ -197,6 +199,8 @@ objectMonitorEnterBlocking(J9VMThread *currentThread)
197199
goto releasedAccess;
198200
}
199201
restart:
202+
/* Set j.l.Thread status to BLOCKED. */
203+
VM_VMHelpers::setThreadState(currentThread, J9VMTHREAD_STATE_BLOCKED);
200204
internalReleaseVMAccessSetStatus(currentThread, J9_PUBLIC_FLAGS_THREAD_BLOCKED);
201205
releasedAccess:
202206
omrthread_monitor_enter_using_threadId(monitor, osThread);
@@ -297,6 +301,9 @@ objectMonitorEnterBlocking(J9VMThread *currentThread)
297301
}
298302
done:
299303
clearEventFlag(currentThread, J9_PUBLIC_FLAGS_THREAD_BLOCKED);
304+
/* Set j.l.Thread status to RUNNING state. */
305+
VM_VMHelpers::setThreadState(currentThread, J9VMTHREAD_STATE_RUNNING);
306+
300307
/* Clear the SUPPRESS_CONTENDED_EXITS bit in the monitor saying that CONTENDED EXIT can be sent again */
301308
((J9ThreadMonitor*)monitor)->flags &= ~(UDATA)J9THREAD_MONITOR_SUPPRESS_CONTENDED_EXIT;
302309
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
@@ -118,13 +118,19 @@ monitorWaitImpl(J9VMThread *vmThread, j9object_t object, I_64 millis, I_32 nanos
118118
#endif
119119
J9VMTHREAD_SET_BLOCKINGENTEROBJECT(vmThread, vmThread, object);
120120
object = NULL;
121+
/* Set j.l.Thread status to WAITING. */
122+
U_32 oldState = J9_ARE_ANY_BITS_SET(thrstate, J9_PUBLIC_FLAGS_THREAD_TIMED)
123+
? VM_VMHelpers::setThreadState(vmThread, J9VMTHREAD_STATE_WAITING_TIMED)
124+
: VM_VMHelpers::setThreadState(vmThread, J9VMTHREAD_STATE_WAITING);
121125
#if JAVA_SPEC_VERSION >= 24
122126
J9VM_SEND_VIRTUAL_UNBLOCKER_THREAD_SIGNAL(javaVM);
123127
#endif /* JAVA_SPEC_VERSION >= 24 */
124128
internalReleaseVMAccessSetStatus(vmThread, thrstate);
125129
rc = timeCompensationHelper(vmThread,
126130
interruptable ? HELPER_TYPE_MONITOR_WAIT_INTERRUPTABLE : HELPER_TYPE_MONITOR_WAIT_TIMED, monitor, millis, nanos);
127131
internalAcquireVMAccessClearStatus(vmThread, thrstate);
132+
/* Set j.l.Thread status to oldState. */
133+
VM_VMHelpers::setThreadState(vmThread, oldState);
128134
J9VMTHREAD_SET_BLOCKINGENTEROBJECT(vmThread, vmThread, NULL);
129135
omrthread_monitor_unpin(monitor, vmThread->osThread);
130136
#if JAVA_SPEC_VERSION >= 24
@@ -197,9 +203,13 @@ threadSleepImpl(J9VMThread *vmThread, I_64 millis, I_32 nanos)
197203
#endif
198204
if (0 == rc) {
199205
TRIGGER_J9HOOK_VM_SLEEP(javaVM->hookInterface, vmThread, millis, nanos);
206+
/* Set j.l.Thread status to SLEEPING. */
207+
U_32 oldState = VM_VMHelpers::setThreadState(vmThread, J9VMTHREAD_STATE_SLEEPING);
200208
internalReleaseVMAccessSetStatus(vmThread, J9_PUBLIC_FLAGS_THREAD_SLEEPING);
201209
rc = timeCompensationHelper(vmThread, HELPER_TYPE_THREAD_SLEEP, NULL, millis, nanos);
202210
internalAcquireVMAccessClearStatus(vmThread, J9_PUBLIC_FLAGS_THREAD_SLEEPING);
211+
/* Set j.l.Thread status to oldState. */
212+
VM_VMHelpers::setThreadState(vmThread, oldState);
203213
TRIGGER_J9HOOK_VM_SLEPT(javaVM->hookInterface, vmThread, millis, nanos, startTicks);
204214
}
205215

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_32 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 */
@@ -2086,6 +2089,9 @@ startJavaThreadInternal(J9VMThread * currentThread, UDATA privateFlags, UDATA os
20862089
}
20872090
J9VMJAVALANGTHREAD_SET_THREADREF(currentThread, threadObject, newThread);
20882091

2092+
/* Set j.l.Thread status to RUNNABLE. */
2093+
VM_VMHelpers::setThreadState(currentThread, J9VMTHREAD_STATE_RUNNING);
2094+
20892095
#if (JAVA_SPEC_VERSION >= 14)
20902096
/* If thread was interrupted before start, make sure interrupt flag is set for running thread. */
20912097
if (J9VMJAVALANGTHREAD_DEADINTERRUPT(currentThread, threadObject)) {

0 commit comments

Comments
 (0)