35
35
#include " VMHelpers.hpp"
36
36
#include " ContinuationHelpers.hpp"
37
37
#endif /* JAVA_SPEC_VERSION >= 19 */
38
+ #if JAVA_SPEC_VERSION >= 24
39
+ #include " j9protos.h"
40
+ #endif /* JAVA_SPEC_VERSION >= 24 */
38
41
39
42
extern " C" {
40
43
@@ -260,89 +263,27 @@ JVM_IsPreviewEnabled(void)
260
263
return isPreviewEnabled;
261
264
}
262
265
263
- static void
264
- enterVThreadTransitionCritical (J9VMThread *currentThread, jobject thread)
265
- {
266
- J9JavaVM *vm = currentThread->javaVM ;
267
- J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions ;
268
- MM_ObjectAccessBarrierAPI objectAccessBarrier = MM_ObjectAccessBarrierAPI (currentThread);
269
- j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE (thread);
270
-
271
- retry:
272
- if (!VM_VMHelpers::isThreadSuspended (currentThread, threadObj)) {
273
- while (!objectAccessBarrier.inlineMixedObjectCompareAndSwapU64 (currentThread, threadObj, vm->virtualThreadInspectorCountOffset , 0 , ~(U_64)0 )) {
274
- /* Thread is being inspected or unmounted, wait. */
275
- vmFuncs->internalReleaseVMAccess (currentThread);
276
- VM_AtomicSupport::yieldCPU ();
277
- /* After wait, the thread may suspend here. */
278
- vmFuncs->internalAcquireVMAccess (currentThread);
279
- threadObj = J9_JNI_UNWRAP_REFERENCE (thread);
280
- }
281
-
282
- /* Now we have locked access to virtualThreadInspectorCount, check if the vthread is suspended.
283
- * If suspended, release the access and spin-wait until the vthread is resumed.
284
- * If not suspended, link the current J9VMThread with the virtual thread object.
285
- */
286
- if (!VM_VMHelpers::isThreadSuspended (currentThread, threadObj)
287
- && objectAccessBarrier.inlineMixedObjectCompareAndSwapU64 (currentThread, threadObj, vm->internalSuspendStateOffset , J9_VIRTUALTHREAD_INTERNAL_STATE_NONE, (U_64)currentThread)
288
- ) {
289
- return ;
290
- }
291
- J9OBJECT_I64_STORE (currentThread, threadObj, vm->virtualThreadInspectorCountOffset , 0 );
292
- }
293
- vmFuncs->internalReleaseVMAccess (currentThread);
294
- /* Spin is used instead of the halt flag as we cannot guarantee suspend flag is still set now.
295
- *
296
- * TODO: Dynamically increase the sleep time to a bounded maximum.
297
- */
298
- f_threadSleep (10 );
299
- /* After wait, the thread may suspend here. */
300
- vmFuncs->internalAcquireVMAccess (currentThread);
301
- threadObj = J9_JNI_UNWRAP_REFERENCE (thread);
302
- goto retry;
303
- }
304
-
305
- static void
306
- exitVThreadTransitionCritical (J9VMThread *currentThread, jobject thread)
307
- {
308
- J9JavaVM *vm = currentThread->javaVM ;
309
- J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions ;
310
- j9object_t vthread = J9_JNI_UNWRAP_REFERENCE (thread);
311
- MM_ObjectAccessBarrierAPI objectAccessBarrier = MM_ObjectAccessBarrierAPI (currentThread);
312
-
313
- /* Remove J9VMThread address from internalSuspendedState field, as the thread state is no longer in a transition. */
314
- while (!objectAccessBarrier.inlineMixedObjectCompareAndSwapU64 (currentThread, vthread, vm->internalSuspendStateOffset , (U_64)currentThread, J9_VIRTUALTHREAD_INTERNAL_STATE_NONE)) {
315
- /* Wait if the suspend flag is set. */
316
- vmFuncs->internalReleaseVMAccess (currentThread);
317
- VM_AtomicSupport::yieldCPU ();
318
- /* After wait, the thread may suspend here. */
319
- vmFuncs->internalAcquireVMAccess (currentThread);
320
- vthread = J9_JNI_UNWRAP_REFERENCE (thread);
321
- }
322
-
323
- /* Update to virtualThreadInspectorCount must be after clearing isSuspendedInternal field to retain sync ordering. */
324
- Assert_SC_true (-1 == J9OBJECT_I64_LOAD (currentThread, vthread, vm->virtualThreadInspectorCountOffset ));
325
- J9OBJECT_I64_STORE (currentThread, vthread, vm->virtualThreadInspectorCountOffset , 0 );
326
- }
327
-
328
266
static void
329
267
setContinuationStateToLastUnmount (J9VMThread *currentThread, jobject thread)
330
268
{
331
- enterVThreadTransitionCritical (currentThread, thread);
269
+ J9InternalVMFunctions const * const vmFuncs = currentThread->javaVM ->internalVMFunctions ;
270
+ vmFuncs->enterVThreadTransitionCritical (currentThread, thread);
271
+
332
272
/* Re-fetch reference as enterVThreadTransitionCritical may release VMAccess. */
333
273
j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE (thread);
334
274
j9object_t continuationObj = J9VMJAVALANGVIRTUALTHREAD_CONT (currentThread, threadObj);
335
275
ContinuationState volatile *continuationStatePtr = VM_ContinuationHelpers::getContinuationStateAddress (currentThread, continuationObj);
336
276
/* Used in JVMTI to not suspend the virtual thread once it enters the last unmount phase. */
337
277
VM_ContinuationHelpers::setLastUnmount (continuationStatePtr);
338
- exitVThreadTransitionCritical (currentThread, thread);
278
+ vmFuncs-> exitVThreadTransitionCritical (currentThread, thread);
339
279
}
340
280
341
281
/* Caller must have VMAccess. */
342
282
static void
343
283
virtualThreadMountBegin (JNIEnv *env, jobject thread)
344
284
{
345
285
J9VMThread *currentThread = (J9VMThread *)env;
286
+ J9InternalVMFunctions const * const vmFuncs = currentThread->javaVM ->internalVMFunctions ;
346
287
347
288
j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE (thread);
348
289
Assert_SC_true (IS_JAVA_LANG_VIRTUALTHREAD (currentThread, threadObj));
@@ -361,7 +302,7 @@ virtualThreadMountBegin(JNIEnv *env, jobject thread)
361
302
continuation);
362
303
}
363
304
364
- enterVThreadTransitionCritical (currentThread, thread);
305
+ vmFuncs-> enterVThreadTransitionCritical (currentThread, thread);
365
306
366
307
VM_VMHelpers::virtualThreadHideFrames (currentThread, JNI_TRUE);
367
308
}
@@ -372,6 +313,7 @@ virtualThreadMountEnd(JNIEnv *env, jobject thread)
372
313
{
373
314
J9VMThread *currentThread = (J9VMThread *)env;
374
315
J9JavaVM *vm = currentThread->javaVM ;
316
+ J9InternalVMFunctions const * const vmFuncs = vm->internalVMFunctions ;
375
317
j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE (thread);
376
318
377
319
Assert_SC_true (IS_JAVA_LANG_VIRTUALTHREAD (currentThread, threadObj));
@@ -391,7 +333,7 @@ virtualThreadMountEnd(JNIEnv *env, jobject thread)
391
333
VM_VMHelpers::virtualThreadHideFrames (currentThread, JNI_FALSE);
392
334
393
335
/* Allow thread to be inspected again. */
394
- exitVThreadTransitionCritical (currentThread, thread);
336
+ vmFuncs-> exitVThreadTransitionCritical (currentThread, thread);
395
337
396
338
TRIGGER_J9HOOK_VM_VIRTUAL_THREAD_MOUNT (vm->hookInterface , currentThread);
397
339
}
@@ -402,6 +344,7 @@ virtualThreadUnmountBegin(JNIEnv *env, jobject thread)
402
344
{
403
345
J9VMThread *currentThread = (J9VMThread *)env;
404
346
J9JavaVM *vm = currentThread->javaVM ;
347
+ J9InternalVMFunctions const * const vmFuncs = vm->internalVMFunctions ;
405
348
406
349
j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE (thread);
407
350
@@ -421,16 +364,15 @@ virtualThreadUnmountBegin(JNIEnv *env, jobject thread)
421
364
422
365
TRIGGER_J9HOOK_VM_VIRTUAL_THREAD_UNMOUNT (vm->hookInterface , currentThread);
423
366
424
- enterVThreadTransitionCritical (currentThread, thread);
367
+ vmFuncs-> enterVThreadTransitionCritical (currentThread, thread);
425
368
426
- J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions ;
427
369
j9object_t carrierThreadObject = currentThread->carrierThreadObject ;
428
370
/* Virtual thread is being umounted. If its carrier thread is suspended, spin until
429
371
* the carrier thread is resumed. The carrier thread should not be mounted until it
430
372
* is resumed.
431
373
*/
432
374
while (VM_VMHelpers::isThreadSuspended (currentThread, carrierThreadObject)) {
433
- exitVThreadTransitionCritical (currentThread, thread);
375
+ vmFuncs-> exitVThreadTransitionCritical (currentThread, thread);
434
376
vmFuncs->internalReleaseVMAccess (currentThread);
435
377
/* Spin is used instead of the halt flag; otherwise, the virtual thread will
436
378
* show as suspended.
@@ -439,7 +381,7 @@ virtualThreadUnmountBegin(JNIEnv *env, jobject thread)
439
381
*/
440
382
f_threadSleep (10 );
441
383
vmFuncs->internalAcquireVMAccess (currentThread);
442
- enterVThreadTransitionCritical (currentThread, thread);
384
+ vmFuncs-> enterVThreadTransitionCritical (currentThread, thread);
443
385
carrierThreadObject = currentThread->carrierThreadObject ;
444
386
}
445
387
@@ -452,7 +394,7 @@ virtualThreadUnmountEnd(JNIEnv *env, jobject thread)
452
394
{
453
395
J9VMThread *currentThread = (J9VMThread *)env;
454
396
J9JavaVM *vm = currentThread->javaVM ;
455
- J9InternalVMFunctions * vmFuncs = vm->internalVMFunctions ;
397
+ J9InternalVMFunctions const * const vmFuncs = vm->internalVMFunctions ;
456
398
457
399
j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE (thread);
458
400
j9object_t continuationObj = J9VMJAVALANGVIRTUALTHREAD_CONT (currentThread, threadObj);
@@ -478,7 +420,7 @@ virtualThreadUnmountEnd(JNIEnv *env, jobject thread)
478
420
VM_VMHelpers::virtualThreadHideFrames (currentThread, JNI_FALSE);
479
421
480
422
/* Allow thread to be inspected again. */
481
- exitVThreadTransitionCritical (currentThread, thread);
423
+ vmFuncs-> exitVThreadTransitionCritical (currentThread, thread);
482
424
}
483
425
#endif /* JAVA_SPEC_VERSION >= 19 */
484
426
@@ -534,14 +476,14 @@ JVM_VirtualThreadHideFrames(
534
476
#if JAVA_SPEC_VERSION < 23
535
477
Assert_SC_true (vThreadObj == J9_JNI_UNWRAP_REFERENCE (vthread));
536
478
#endif /* JAVA_SPEC_VERSION < 23 */
537
- enterVThreadTransitionCritical (currentThread, (jobject)¤tThread->threadObject );
479
+ vmFuncs-> enterVThreadTransitionCritical (currentThread, (jobject)¤tThread->threadObject );
538
480
}
539
481
540
482
VM_VMHelpers::virtualThreadHideFrames (currentThread, hide);
541
483
542
484
if (!hide) {
543
485
Assert_SC_true (hiddenFrames);
544
- exitVThreadTransitionCritical (currentThread, (jobject)¤tThread->threadObject );
486
+ vmFuncs-> exitVThreadTransitionCritical (currentThread, (jobject)¤tThread->threadObject );
545
487
}
546
488
547
489
vmFuncs->internalExitVMToJNI (currentThread);
@@ -567,7 +509,7 @@ JVM_VirtualThreadMount(JNIEnv *env, jobject vthread, jboolean hide)
567
509
{
568
510
J9VMThread *currentThread = (J9VMThread *)env;
569
511
J9JavaVM *vm = currentThread->javaVM ;
570
- J9InternalVMFunctions * vmFuncs = vm->internalVMFunctions ;
512
+ J9InternalVMFunctions const * const vmFuncs = vm->internalVMFunctions ;
571
513
572
514
Trc_SC_VirtualThreadMount_Entry (currentThread, vthread, hide);
573
515
@@ -589,7 +531,7 @@ JVM_VirtualThreadUnmount(JNIEnv *env, jobject vthread, jboolean hide)
589
531
{
590
532
J9VMThread *currentThread = (J9VMThread *)env;
591
533
J9JavaVM *vm = currentThread->javaVM ;
592
- J9InternalVMFunctions * vmFuncs = vm->internalVMFunctions ;
534
+ J9InternalVMFunctions const * const vmFuncs = vm->internalVMFunctions ;
593
535
594
536
Trc_SC_VirtualThreadUnmount_Entry (currentThread, vthread, hide);
595
537
@@ -617,7 +559,7 @@ JVM_VirtualThreadStart(JNIEnv *env, jobject vthread)
617
559
{
618
560
J9VMThread *currentThread = (J9VMThread *)env;
619
561
J9JavaVM *vm = currentThread->javaVM ;
620
- J9InternalVMFunctions * vmFuncs = vm->internalVMFunctions ;
562
+ J9InternalVMFunctions const * const vmFuncs = vm->internalVMFunctions ;
621
563
622
564
Trc_SC_VirtualThreadStart_Entry (currentThread, vthread);
623
565
@@ -636,7 +578,7 @@ JVM_VirtualThreadEnd(JNIEnv *env, jobject vthread)
636
578
{
637
579
J9VMThread *currentThread = (J9VMThread *)env;
638
580
J9JavaVM *vm = currentThread->javaVM ;
639
- J9InternalVMFunctions * vmFuncs = vm->internalVMFunctions ;
581
+ J9InternalVMFunctions const * const vmFuncs = vm->internalVMFunctions ;
640
582
641
583
Trc_SC_VirtualThreadEnd_Entry (currentThread, vthread);
642
584
@@ -720,17 +662,19 @@ JVM_IsStaticallyLinked(void)
720
662
}
721
663
722
664
JNIEXPORT void JNICALL
723
- JVM_VirtualThreadPinnedEvent (JNIEnv* env, jclass clazz, jstring op)
665
+ JVM_VirtualThreadPinnedEvent (JNIEnv * env, jclass clazz, jstring op)
724
666
{
725
667
// TODO: emit JFR Event
726
668
return ;
727
669
}
728
670
729
671
JNIEXPORT jobject JNICALL
730
- JVM_TakeVirtualThreadListToUnblock (JNIEnv* env, jclass ignored)
672
+ JVM_TakeVirtualThreadListToUnblock (JNIEnv * env, jclass ignored)
731
673
{
732
- // TODO: return the unblocked list
733
- return NULL ;
674
+ J9VMThread *currentThread = (J9VMThread *)env;
675
+ J9JavaVM *vm = currentThread->javaVM ;
676
+
677
+ return vm->internalVMFunctions ->takeVirtualThreadListToUnblock (currentThread, vm);
734
678
}
735
679
#endif /* JAVA_SPEC_VERSION >= 24 */
736
680
0 commit comments