Skip to content

Commit 386b6f5

Browse files
committed
Add support for JEP491 on JDK24+
- Add missing setThreadState - Fix thread state on notified blocked threads - Refactor comments Signed-off-by: Jack Lu <Jack.S.Lu@ibm.com>
1 parent 8275d36 commit 386b6f5

File tree

7 files changed

+43
-34
lines changed

7 files changed

+43
-34
lines changed

runtime/oti/VMHelpers.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,7 +2278,6 @@ class VM_VMHelpers
22782278
indicateAsyncMessagePending(targetThread);
22792279
}
22802280

2281-
22822281
static U_32
22832282
setThreadState(J9VMThread *currentThread, U_32 state)
22842283
{
@@ -2289,14 +2288,14 @@ class VM_VMHelpers
22892288
/* Platform threads must have a non-null FieldHolder object. */
22902289
j9object_t threadHolder = J9VMJAVALANGTHREAD_HOLDER(currentThread, receiverObject);
22912290
if (NULL != threadHolder) {
2292-
oldState = (U_32)J9VMJAVALANGTHREADFIELDHOLDER_THREADSTATUS(currentThread, threadHolder);
2291+
oldState = J9VMJAVALANGTHREADFIELDHOLDER_THREADSTATUS(currentThread, threadHolder);
22932292
J9VMJAVALANGTHREADFIELDHOLDER_SET_THREADSTATUS(currentThread, threadHolder, state);
22942293
}
22952294
}
22962295
#else /* JAVA_SPEC_VERSION >= 19 */
22972296
j9object_t receiverObject = currentThread->threadObject;
22982297
if (NULL != receiverObject) {
2299-
oldState = (U_32)J9VMJAVALANGTHREAD_THREADSTATUS(currentThread, receiverObject);
2298+
oldState = J9VMJAVALANGTHREAD_THREADSTATUS(currentThread, receiverObject);
23002299
J9VMJAVALANGTHREAD_SET_THREADSTATUS(currentThread, receiverObject, state);
23012300
}
23022301
#endif /* JAVA_SPEC_VERSION >= 19 */

runtime/oti/j9nonbuilder.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5881,18 +5881,25 @@ typedef struct J9VMThread {
58815881
#endif
58825882
#define J9VMTHREAD_ALIGNMENT 0x100
58835883
#define J9VMTHREAD_RESERVED_C_STACK_FRACTION 8
5884-
#define J9VMTHREAD_STATE_RUNNING 1
5885-
#define J9VMTHREAD_STATE_BLOCKED 2
5886-
#define J9VMTHREAD_STATE_WAITING 4
5887-
#define J9VMTHREAD_STATE_SLEEPING 8
5888-
#define J9VMTHREAD_STATE_SUSPENDED 16
5889-
#define J9VMTHREAD_STATE_DEAD 32
5890-
#define J9VMTHREAD_STATE_WAITING_TIMED 64
5891-
#define J9VMTHREAD_STATE_PARKED 0x80
5884+
5885+
/* J9VMTHREAD_STATE_* bit flags are used to map vmthread state to
5886+
* j.l.Thread.State in Thread.translateJ9VMThreadStateToThreadState().
5887+
* Any updates to these value must also update the value used in JCL.
5888+
*
5889+
* Note: 0 is reserved for uninitialized case and cannot be used.
5890+
*/
5891+
#define J9VMTHREAD_STATE_RUNNING 0x1
5892+
#define J9VMTHREAD_STATE_BLOCKED 0x2
5893+
#define J9VMTHREAD_STATE_WAITING 0x4
5894+
#define J9VMTHREAD_STATE_SLEEPING 0x8
5895+
#define J9VMTHREAD_STATE_SUSPENDED 0x10
5896+
#define J9VMTHREAD_STATE_DEAD 0x20
5897+
#define J9VMTHREAD_STATE_WAITING_TIMED 0x40
5898+
#define J9VMTHREAD_STATE_PARKED 0x80
58925899
#define J9VMTHREAD_STATE_PARKED_TIMED 0x100
5893-
#define J9VMTHREAD_STATE_INTERRUPTED 0x200
5894-
#define J9VMTHREAD_STATE_UNKNOWN 0x400
5895-
#define J9VMTHREAD_STATE_UNREADABLE 0x800
5900+
#define J9VMTHREAD_STATE_INTERRUPTED 0x200
5901+
#define J9VMTHREAD_STATE_UNKNOWN 0x400
5902+
#define J9VMTHREAD_STATE_UNREADABLE 0x800
58965903
#define J9VMSTATE_MAJOR 0xFFFF0000
58975904
#define J9VMSTATE_MINOR 0xFFFF
58985905
#define J9VMSTATE_INTERPRETER 0x10000

runtime/vm/ObjectMonitor.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ 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. */
189188
VM_VMHelpers::setThreadState(currentThread, J9VMTHREAD_STATE_BLOCKED);
190189
VM_VMAccess::setPublicFlags(currentThread, J9_PUBLIC_FLAGS_THREAD_BLOCKED);
191190
ALWAYS_TRIGGER_J9HOOK_VM_MONITOR_CONTENDED_ENTER(vm->hookInterface, currentThread, monitor);
@@ -199,7 +198,6 @@ objectMonitorEnterBlocking(J9VMThread *currentThread)
199198
goto releasedAccess;
200199
}
201200
restart:
202-
/* Set j.l.Thread status to BLOCKED. */
203201
VM_VMHelpers::setThreadState(currentThread, J9VMTHREAD_STATE_BLOCKED);
204202
internalReleaseVMAccessSetStatus(currentThread, J9_PUBLIC_FLAGS_THREAD_BLOCKED);
205203
releasedAccess:
@@ -290,6 +288,7 @@ objectMonitorEnterBlocking(J9VMThread *currentThread)
290288
J9_STORE_LOCKWORD(currentThread, lwEA, lock);
291289
goto done;
292290
}
291+
VM_VMHelpers::setThreadState(currentThread, J9VMTHREAD_STATE_BLOCKED);
293292
internalReleaseVMAccessSetStatus(currentThread, J9_PUBLIC_FLAGS_THREAD_BLOCKED);
294293
SET_IGNORE_ENTER(monitor);
295294
omrthread_monitor_wait_timed(monitor, (I_64)waitTime, 0);
@@ -301,7 +300,6 @@ objectMonitorEnterBlocking(J9VMThread *currentThread)
301300
}
302301
done:
303302
clearEventFlag(currentThread, J9_PUBLIC_FLAGS_THREAD_BLOCKED);
304-
/* Set j.l.Thread status to RUNNING state. */
305303
VM_VMHelpers::setThreadState(currentThread, J9VMTHREAD_STATE_RUNNING);
306304

307305
/* Clear the SUPPRESS_CONTENDED_EXITS bit in the monitor saying that CONTENDED EXIT can be sent again */

runtime/vm/callin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,6 @@ initializeAttachedThreadImpl(J9VMThread *currentThread, const char *name, j9obje
589589
currentThread->returnValue = J9_BCLOOP_RUN_METHOD;
590590
currentThread->returnValue2 = (UDATA)J9VMJAVALANGTHREAD_INIT_METHOD(vm);
591591
c_cInterpreter(currentThread);
592-
J9VMJAVALANGTHREAD_SET_STARTED(currentThread, initializee->threadObject, JNI_TRUE);
593592
#if JAVA_SPEC_VERSION >= 19
594593
j9object_t threadHolder = J9VMJAVALANGTHREAD_HOLDER(currentThread, initializee->threadObject);
595594
if (NULL != threadHolder) {
@@ -598,6 +597,8 @@ initializeAttachedThreadImpl(J9VMThread *currentThread, const char *name, j9obje
598597
#else /* JAVA_SPEC_VERSION >= 19 */
599598
J9VMJAVALANGTHREAD_SET_THREADSTATUS(currentThread, initializee->threadObject, J9VMTHREAD_STATE_RUNNING);
600599
#endif /* JAVA_SPEC_VERSION >= 19 */
600+
/* Thread.started must be set after Thread.threadStatus to avoid timing issue in Thread.getState(). */
601+
J9VMJAVALANGTHREAD_SET_STARTED(currentThread, initializee->threadObject, JNI_TRUE);
601602
}
602603
}
603604
done:

runtime/vm/threadhelp.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +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);
125121
#if JAVA_SPEC_VERSION >= 24
126122
J9VM_SEND_VIRTUAL_UNBLOCKER_THREAD_SIGNAL(javaVM);
127123
#endif /* JAVA_SPEC_VERSION >= 24 */
124+
/* Set j.l.Thread status to UNKNOWN since the wait can be notified/interrupted
125+
* but blocked on re-acquiring the monitor.
126+
* In this case, the thread state will have to be determined by looking at
127+
* the vmThread->publicFlags field.
128+
*/
129+
U_32 oldState = VM_VMHelpers::setThreadState(vmThread, J9VMTHREAD_STATE_UNKNOWN);
128130
internalReleaseVMAccessSetStatus(vmThread, thrstate);
129131
rc = timeCompensationHelper(vmThread,
130132
interruptable ? HELPER_TYPE_MONITOR_WAIT_INTERRUPTABLE : HELPER_TYPE_MONITOR_WAIT_TIMED, monitor, millis, nanos);
131133
internalAcquireVMAccessClearStatus(vmThread, thrstate);
132-
/* Set j.l.Thread status to oldState. */
133134
VM_VMHelpers::setThreadState(vmThread, oldState);
134135
J9VMTHREAD_SET_BLOCKINGENTEROBJECT(vmThread, vmThread, NULL);
135136
omrthread_monitor_unpin(monitor, vmThread->osThread);
@@ -203,12 +204,10 @@ threadSleepImpl(J9VMThread *vmThread, I_64 millis, I_32 nanos)
203204
#endif
204205
if (0 == rc) {
205206
TRIGGER_J9HOOK_VM_SLEEP(javaVM->hookInterface, vmThread, millis, nanos);
206-
/* Set j.l.Thread status to SLEEPING. */
207207
U_32 oldState = VM_VMHelpers::setThreadState(vmThread, J9VMTHREAD_STATE_SLEEPING);
208208
internalReleaseVMAccessSetStatus(vmThread, J9_PUBLIC_FLAGS_THREAD_SLEEPING);
209209
rc = timeCompensationHelper(vmThread, HELPER_TYPE_THREAD_SLEEP, NULL, millis, nanos);
210210
internalAcquireVMAccessClearStatus(vmThread, J9_PUBLIC_FLAGS_THREAD_SLEEPING);
211-
/* Set j.l.Thread status to oldState. */
212211
VM_VMHelpers::setThreadState(vmThread, oldState);
213212
TRIGGER_J9HOOK_VM_SLEPT(javaVM->hookInterface, vmThread, millis, nanos, startTicks);
214213
}

runtime/vm/threadpark.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,9 @@ 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. */
102101
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);
102+
? VM_VMHelpers::setThreadState(vmThread, J9VMTHREAD_STATE_PARKED_TIMED)
103+
: VM_VMHelpers::setThreadState(vmThread, J9VMTHREAD_STATE_PARKED);
105104
internalReleaseVMAccessSetStatus(vmThread, thrstate);
106105

107106
while (1) {
@@ -120,7 +119,6 @@ threadParkImpl(J9VMThread *vmThread, BOOLEAN timeoutIsEpochRelative, I_64 timeou
120119
}
121120

122121
internalAcquireVMAccessClearStatus(vmThread, thrstate);
123-
/* Set j.l.Thread status to oldState. */
124122
VM_VMHelpers::setThreadState(vmThread, oldState);
125123
TRIGGER_J9HOOK_VM_UNPARKED(vm->hookInterface, vmThread, millis, nanos, startTicks, (UDATA) parkedAddress, VM_VMHelpers::currentClass(parkedClass));
126124
J9VMTHREAD_SET_BLOCKINGENTEROBJECT(vmThread, vmThread, NULL);

runtime/vm/vmthread.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,6 @@ void threadCleanup(J9VMThread * vmThread, UDATA forkedByVM)
460460
/* Safe to call this whether handleUncaughtException clears the exception or not */
461461
internalExceptionDescribe(vmThread);
462462
}
463-
/* Set j.l.Thread status to TERMINATED. */
464463
VM_VMHelpers::setThreadState(vmThread, J9VMTHREAD_STATE_DEAD);
465464
releaseVMAccess(vmThread);
466465

@@ -1924,8 +1923,19 @@ startJavaThread(J9VMThread * currentThread, j9object_t threadObject, UDATA priva
19241923
privateFlags &= ~J9_PRIVATE_FLAGS_NO_EXCEPTION_IN_START_JAVA_THREAD;
19251924

19261925
#ifndef J9VM_IVE_RAW_BUILD /* J9VM_IVE_RAW_BUILD is not enabled by default */
1926+
#if JAVA_SPEC_VERSION >= 19
1927+
j9object_t threadHolder = J9VMJAVALANGTHREAD_HOLDER(currentThread, threadObject);
1928+
if (NULL != threadHolder) {
1929+
J9VMJAVALANGTHREADFIELDHOLDER_SET_THREADSTATUS(currentThread, threadHolder, J9VMTHREAD_STATE_RUNNING);
1930+
}
1931+
#else /* JAVA_SPEC_VERSION >= 19 */
1932+
J9VMJAVALANGTHREAD_SET_THREADSTATUS(currentThread, threadObject, J9VMTHREAD_STATE_RUNNING);
1933+
#endif /* JAVA_SPEC_VERSION >= 19 */
19271934
/* Any attempt to start a Thread makes it illegal to attempt to start it again.
1928-
* Oracle class libraries don't have the 'started' field */
1935+
* Oracle class libraries don't have the 'started' field.
1936+
*
1937+
* Thread.started must be set after Thread.threadStatus to avoid timing issue in Thread.getState().
1938+
*/
19291939
J9VMJAVALANGTHREAD_SET_STARTED(currentThread, threadObject, TRUE);
19301940
#endif /* !J9VM_IVE_RAW_BUILD */
19311941

@@ -2089,9 +2099,6 @@ startJavaThreadInternal(J9VMThread * currentThread, UDATA privateFlags, UDATA os
20892099
}
20902100
J9VMJAVALANGTHREAD_SET_THREADREF(currentThread, threadObject, newThread);
20912101

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

0 commit comments

Comments
 (0)