@@ -106,7 +106,7 @@ final class VirtualThread extends BaseVirtualThread {
106
106
* TIMED_PINNED -> RUNNING // unparked, continue execution on same carrier
107
107
*
108
108
* RUNNABLE -> RUNNING // continue execution
109
-
109
+ *
110
110
* RUNNING -> YIELDING // Thread.yield
111
111
* YIELDING -> RUNNABLE // yield successful
112
112
* YIELDING -> RUNNING // yield failed
@@ -228,9 +228,6 @@ private void runContinuation() {
228
228
return ;
229
229
}
230
230
231
- // notify JVMTI before mount
232
- notifyJvmtiMount (/*hide*/ true );
233
-
234
231
mount ();
235
232
try {
236
233
cont .run ();
@@ -303,7 +300,6 @@ private void submitFailed(RejectedExecutionException ree) {
303
300
/**
304
301
* Runs a task in the context of this virtual thread.
305
302
*/
306
- @ ChangesCurrentThread
307
303
private void run (Runnable task ) {
308
304
assert Thread .currentThread () == this && state == RUNNING ;
309
305
@@ -348,6 +344,9 @@ private void run(Runnable task) {
348
344
@ ChangesCurrentThread
349
345
@ ReservedStackAccess
350
346
private void mount () {
347
+ // notify JVMTI before mount
348
+ notifyJvmtiMount (/*hide*/ true );
349
+
351
350
// sets the carrier thread
352
351
Thread carrier = Thread .currentCarrierThread ();
353
352
setCarrierThread (carrier );
@@ -384,6 +383,9 @@ private void unmount() {
384
383
setCarrierThread (null );
385
384
}
386
385
carrier .clearInterrupt ();
386
+
387
+ // notify JVMTI after unmount
388
+ notifyJvmtiUnmount (/*hide*/ false );
387
389
}
388
390
389
391
/**
@@ -448,13 +450,12 @@ private void afterYield() {
448
450
assert carrierThread == null ;
449
451
450
452
int s = state ();
453
+
454
+ // LockSupport.park/parkNanos
451
455
if (s == PARKING || s == TIMED_PARKING ) {
452
456
int newState = (s == PARKING ) ? PARKED : TIMED_PARKED ;
453
457
setState (newState );
454
458
455
- // notify JVMTI that unmount has completed, thread is parked
456
- notifyJvmtiUnmount (/*hide*/ false );
457
-
458
459
// may have been unparked while parking
459
460
if (parkPermit && compareAndSetState (newState , RUNNABLE )) {
460
461
// lazy submit to continue on the current thread as carrier if possible
@@ -465,45 +466,42 @@ private void afterYield() {
465
466
}
466
467
467
468
}
468
- } else if ( s == YIELDING ) { // Thread.yield
469
- setState ( RUNNABLE );
469
+ return ;
470
+ }
470
471
471
- // notify JVMTI that unmount has completed, thread is runnable
472
- notifyJvmtiUnmount (/*hide*/ false );
472
+ // Thread.yield
473
+ if (s == YIELDING ) {
474
+ setState (RUNNABLE );
473
475
474
476
// external submit if there are no tasks in the local task queue
475
477
if (currentThread () instanceof CarrierThread ct && ct .getQueuedTaskCount () == 0 ) {
476
478
externalSubmitRunContinuation (ct .getPool ());
477
479
} else {
478
480
submitRunContinuation ();
479
481
}
480
- } else {
481
- assert false ;
482
+ return ;
482
483
}
484
+
485
+ assert false ;
483
486
}
484
487
485
488
/**
486
489
* Invoked after the continuation completes.
487
490
*/
488
491
private void afterDone () {
489
- afterDone (true , true );
492
+ afterDone (true );
490
493
}
491
494
492
495
/**
493
496
* Invoked after the continuation completes (or start failed). Sets the thread
494
497
* state to TERMINATED and notifies anyone waiting for the thread to terminate.
495
498
*
496
499
* @param notifyContainer true if its container should be notified
497
- * @param executed true if the thread executed, false if it failed to start
498
500
*/
499
- private void afterDone (boolean notifyContainer , boolean executed ) {
501
+ private void afterDone (boolean notifyContainer ) {
500
502
assert carrierThread == null ;
501
503
setState (TERMINATED );
502
504
503
- if (executed ) {
504
- notifyJvmtiUnmount (/*hide*/ false );
505
- }
506
-
507
505
// notify anyone waiting for this virtual thread to terminate
508
506
CountDownLatch termination = this .termination ;
509
507
if (termination != null ) {
@@ -552,7 +550,7 @@ void start(ThreadContainer container) {
552
550
started = true ;
553
551
} finally {
554
552
if (!started ) {
555
- afterDone (addedToContainer , /*executed*/ false );
553
+ afterDone (addedToContainer );
556
554
}
557
555
}
558
556
}
0 commit comments