Skip to content

Commit c46a54e

Browse files
author
Alan Bateman
committed
8312777: notifyJvmtiMount before notifyJvmtiUnmount
Reviewed-by: mli, sspitsyn
1 parent 8f5f440 commit c46a54e

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/java.base/share/classes/java/lang/VirtualThread.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ final class VirtualThread extends BaseVirtualThread {
106106
* TIMED_PINNED -> RUNNING // unparked, continue execution on same carrier
107107
*
108108
* RUNNABLE -> RUNNING // continue execution
109-
109+
*
110110
* RUNNING -> YIELDING // Thread.yield
111111
* YIELDING -> RUNNABLE // yield successful
112112
* YIELDING -> RUNNING // yield failed
@@ -228,9 +228,6 @@ private void runContinuation() {
228228
return;
229229
}
230230

231-
// notify JVMTI before mount
232-
notifyJvmtiMount(/*hide*/true);
233-
234231
mount();
235232
try {
236233
cont.run();
@@ -303,7 +300,6 @@ private void submitFailed(RejectedExecutionException ree) {
303300
/**
304301
* Runs a task in the context of this virtual thread.
305302
*/
306-
@ChangesCurrentThread
307303
private void run(Runnable task) {
308304
assert Thread.currentThread() == this && state == RUNNING;
309305

@@ -348,6 +344,9 @@ private void run(Runnable task) {
348344
@ChangesCurrentThread
349345
@ReservedStackAccess
350346
private void mount() {
347+
// notify JVMTI before mount
348+
notifyJvmtiMount(/*hide*/true);
349+
351350
// sets the carrier thread
352351
Thread carrier = Thread.currentCarrierThread();
353352
setCarrierThread(carrier);
@@ -384,6 +383,9 @@ private void unmount() {
384383
setCarrierThread(null);
385384
}
386385
carrier.clearInterrupt();
386+
387+
// notify JVMTI after unmount
388+
notifyJvmtiUnmount(/*hide*/false);
387389
}
388390

389391
/**
@@ -448,13 +450,12 @@ private void afterYield() {
448450
assert carrierThread == null;
449451

450452
int s = state();
453+
454+
// LockSupport.park/parkNanos
451455
if (s == PARKING || s == TIMED_PARKING) {
452456
int newState = (s == PARKING) ? PARKED : TIMED_PARKED;
453457
setState(newState);
454458

455-
// notify JVMTI that unmount has completed, thread is parked
456-
notifyJvmtiUnmount(/*hide*/false);
457-
458459
// may have been unparked while parking
459460
if (parkPermit && compareAndSetState(newState, RUNNABLE)) {
460461
// lazy submit to continue on the current thread as carrier if possible
@@ -465,45 +466,42 @@ private void afterYield() {
465466
}
466467

467468
}
468-
} else if (s == YIELDING) { // Thread.yield
469-
setState(RUNNABLE);
469+
return;
470+
}
470471

471-
// notify JVMTI that unmount has completed, thread is runnable
472-
notifyJvmtiUnmount(/*hide*/false);
472+
// Thread.yield
473+
if (s == YIELDING) {
474+
setState(RUNNABLE);
473475

474476
// external submit if there are no tasks in the local task queue
475477
if (currentThread() instanceof CarrierThread ct && ct.getQueuedTaskCount() == 0) {
476478
externalSubmitRunContinuation(ct.getPool());
477479
} else {
478480
submitRunContinuation();
479481
}
480-
} else {
481-
assert false;
482+
return;
482483
}
484+
485+
assert false;
483486
}
484487

485488
/**
486489
* Invoked after the continuation completes.
487490
*/
488491
private void afterDone() {
489-
afterDone(true, true);
492+
afterDone(true);
490493
}
491494

492495
/**
493496
* Invoked after the continuation completes (or start failed). Sets the thread
494497
* state to TERMINATED and notifies anyone waiting for the thread to terminate.
495498
*
496499
* @param notifyContainer true if its container should be notified
497-
* @param executed true if the thread executed, false if it failed to start
498500
*/
499-
private void afterDone(boolean notifyContainer, boolean executed) {
501+
private void afterDone(boolean notifyContainer) {
500502
assert carrierThread == null;
501503
setState(TERMINATED);
502504

503-
if (executed) {
504-
notifyJvmtiUnmount(/*hide*/false);
505-
}
506-
507505
// notify anyone waiting for this virtual thread to terminate
508506
CountDownLatch termination = this.termination;
509507
if (termination != null) {
@@ -552,7 +550,7 @@ void start(ThreadContainer container) {
552550
started = true;
553551
} finally {
554552
if (!started) {
555-
afterDone(addedToContainer, /*executed*/false);
553+
afterDone(addedToContainer);
556554
}
557555
}
558556
}

0 commit comments

Comments
 (0)