45
45
import java .util .concurrent .Executors ;
46
46
import java .util .concurrent .ScheduledExecutorService ;
47
47
import java .util .concurrent .TimeUnit ;
48
- import java .util .concurrent .atomic .AtomicBoolean ;
49
48
import java .util .concurrent .locks .Lock ;
50
49
import java .util .concurrent .locks .ReentrantLock ;
51
50
import java .util .function .Supplier ;
@@ -94,7 +93,7 @@ default int frameIndex() {
94
93
95
94
private final ScheduledExecutorService executorService = Executors .newScheduledThreadPool (2 );
96
95
private final ConcurrentLinkedQueue <AsyncAction > scheduledActions = new ConcurrentLinkedQueue <>();
97
- private final AtomicBoolean hasScheduledAction = new AtomicBoolean ( false ) ;
96
+ private boolean hasScheduledAction = false ;
98
97
private final Lock executingScheduledActions = new ReentrantLock ();
99
98
private static final int ASYNC_ACTION_DELAY = 15 ; // chosen by a fair D20 dice roll
100
99
@@ -113,7 +112,7 @@ public void run() {
113
112
executingScheduledActions .lock ();
114
113
try {
115
114
scheduledActions .add (asyncAction );
116
- hasScheduledAction . set ( true ) ;
115
+ hasScheduledAction = true ;
117
116
} finally {
118
117
executingScheduledActions .unlock ();
119
118
}
@@ -154,7 +153,9 @@ void registerAction(Supplier<AsyncAction> actionSupplier) {
154
153
}
155
154
156
155
void triggerAsyncActions () {
157
- if (hasScheduledAction .compareAndSet (true , false )) {
156
+ // Uses weakCompareAndSet because we just want to do it in a timely manner, but we don't
157
+ // need the ordering guarantees.
158
+ if (hasScheduledAction ) {
158
159
CompilerDirectives .transferToInterpreter ();
159
160
processAsyncActions ();
160
161
}
@@ -171,6 +172,7 @@ private void processAsyncActions() {
171
172
// scheduledActions queue, so we won't have a race between finishing the while loop and
172
173
// returning from this method.
173
174
if (executingScheduledActions .tryLock ()) {
175
+ hasScheduledAction = false ;
174
176
try {
175
177
ConcurrentLinkedQueue <AsyncAction > actions = scheduledActions ;
176
178
AsyncAction action ;
0 commit comments