@@ -193,11 +193,15 @@ private void runContinuation() {
193
193
194
194
// set state to ST_RUNNING
195
195
short initialState = stateGet ();
196
- assert initialState == ST_STARTED || initialState == ST_RUNNABLE ;
197
- stateGetAndSet (ST_RUNNING );
198
- if (initialState == ST_RUNNABLE ) {
196
+ if (initialState == ST_STARTED
197
+ && stateCompareAndSet (ST_STARTED , ST_RUNNING )) {
198
+ // first run
199
+ } else if (initialState == ST_RUNNABLE
200
+ && stateCompareAndSet (ST_RUNNABLE , ST_RUNNING )) {
199
201
// consume parking permit
200
- parkPermitGetAndSet (false );
202
+ parkPermitSet (false );
203
+ } else {
204
+ throw new RuntimeException ();
201
205
}
202
206
203
207
boolean firstRun = (initialState == ST_STARTED );
@@ -369,7 +373,7 @@ private void yieldFailed() {
369
373
throw new InternalError ();
370
374
371
375
// consume parking permit
372
- parkPermitGetAndSet (false );
376
+ parkPermitSet (false );
373
377
374
378
// switch back to virtual thread
375
379
thread .setVirtualThread (this );
@@ -436,7 +440,7 @@ static void parkNanos(long nanos) {
436
440
} else {
437
441
// consume permit when not parking
438
442
vthread .tryYield ();
439
- vthread .parkPermitGetAndSet (false );
443
+ vthread .parkPermitSet (false );
440
444
}
441
445
}
442
446
@@ -891,7 +895,7 @@ private StackTraceElement[] tryGetStackTrace() {
891
895
// -- wrappers for VarHandle methods --
892
896
893
897
private short stateGet () {
894
- return ( short ) STATE . get ( this ) ;
898
+ return state ;
895
899
}
896
900
897
901
private short stateGetAndSet (short newValue ) {
@@ -902,6 +906,12 @@ private boolean stateCompareAndSet(short expectedValue, short newValue) {
902
906
return STATE .compareAndSet (this , expectedValue , newValue );
903
907
}
904
908
909
+ private void parkPermitSet (boolean newValue ) {
910
+ if (parkPermit != newValue ) {
911
+ parkPermit = newValue ;
912
+ }
913
+ }
914
+
905
915
private boolean parkPermitGetAndSet (boolean newValue ) {
906
916
return (boolean ) PARK_PERMIT .getAndSet (this , newValue );
907
917
}
0 commit comments