@@ -194,8 +194,11 @@ public void run() {
194
194
}
195
195
196
196
/**
197
- * Runs or continues execution of the continuation on the current thread.
197
+ * Runs or continues execution on the current thread. The virtual thread is mounted
198
+ * on the current thread before the task runs or continues. It unmounts when the
199
+ * task completes or yields.
198
200
*/
201
+ @ ChangesCurrentThread
199
202
private void runContinuation () {
200
203
// the carrier must be a platform thread
201
204
if (Thread .currentThread ().isVirtual ()) {
@@ -217,11 +220,13 @@ private void runContinuation() {
217
220
// notify JVMTI before mount
218
221
notifyJvmtiMount (/*hide*/ true );
219
222
223
+ mount ();
220
224
try {
221
225
cont .run ();
222
226
} finally {
227
+ unmount ();
223
228
if (cont .isDone ()) {
224
- afterTerminate ();
229
+ afterDone ();
225
230
} else {
226
231
afterYield ();
227
232
}
@@ -285,16 +290,13 @@ private void submitFailed(RejectedExecutionException ree) {
285
290
}
286
291
287
292
/**
288
- * Runs a task in the context of this virtual thread. The virtual thread is
289
- * mounted on the current (carrier) thread before the task runs. It unmounts
290
- * from its carrier thread when the task completes.
293
+ * Runs a task in the context of this virtual thread.
291
294
*/
292
295
@ ChangesCurrentThread
293
296
private void run (Runnable task ) {
294
- assert state == RUNNING ;
297
+ assert Thread . currentThread () == this && state == RUNNING ;
295
298
296
- // first mount
297
- mount ();
299
+ // notify JVMTI, may post VirtualThreadStart event
298
300
notifyJvmtiStart ();
299
301
300
302
// emit JFR event if enabled
@@ -322,12 +324,8 @@ private void run(Runnable task) {
322
324
}
323
325
324
326
} finally {
325
- // last unmount
327
+ // notify JVMTI, may post VirtualThreadEnd event
326
328
notifyJvmtiEnd ();
327
- unmount ();
328
-
329
- // final state
330
- setState (TERMINATED );
331
329
}
332
330
}
333
331
}
@@ -417,21 +415,15 @@ <V> V executeOnCarrierThread(Callable<V> task) throws Exception {
417
415
}
418
416
419
417
/**
420
- * Unmounts this virtual thread, invokes Continuation.yield, and re-mounts the
421
- * thread when continued. When enabled, JVMTI must be notified from this method.
422
- * @return true if the yield was successful
418
+ * Invokes Continuation.yield, notifying JVMTI (if enabled) to hide frames until
419
+ * the continuation continues.
423
420
*/
424
421
@ Hidden
425
- @ ChangesCurrentThread
426
422
private boolean yieldContinuation () {
427
- // unmount
428
423
notifyJvmtiUnmount (/*hide*/ true );
429
- unmount ();
430
424
try {
431
425
return Continuation .yield (VTHREAD_SCOPE );
432
426
} finally {
433
- // re-mount
434
- mount ();
435
427
notifyJvmtiMount (/*hide*/ false );
436
428
}
437
429
}
@@ -477,22 +469,22 @@ private void afterYield() {
477
469
}
478
470
479
471
/**
480
- * Invoked after the thread terminates execution. It notifies anyone
481
- * waiting for the thread to terminate.
472
+ * Invoked after the continuation completes.
482
473
*/
483
- private void afterTerminate () {
484
- afterTerminate (true , true );
474
+ private void afterDone () {
475
+ afterDone (true , true );
485
476
}
486
477
487
478
/**
488
- * Invoked after the thread terminates (or start failed). This method
489
- * notifies anyone waiting for the thread to terminate.
479
+ * Invoked after the continuation completes (or start failed). Sets the thread
480
+ * state to TERMINATED and notifies anyone waiting for the thread to terminate.
490
481
*
491
482
* @param notifyContainer true if its container should be notified
492
483
* @param executed true if the thread executed, false if it failed to start
493
484
*/
494
- private void afterTerminate (boolean notifyContainer , boolean executed ) {
495
- assert (state () == TERMINATED ) && (carrierThread == null );
485
+ private void afterDone (boolean notifyContainer , boolean executed ) {
486
+ assert carrierThread == null ;
487
+ setState (TERMINATED );
496
488
497
489
if (executed ) {
498
490
notifyJvmtiUnmount (/*hide*/ false );
@@ -546,8 +538,7 @@ void start(ThreadContainer container) {
546
538
started = true ;
547
539
} finally {
548
540
if (!started ) {
549
- setState (TERMINATED );
550
- afterTerminate (addedToContainer , /*executed*/ false );
541
+ afterDone (addedToContainer , /*executed*/ false );
551
542
}
552
543
}
553
544
}
0 commit comments