Skip to content

Commit 02c2051

Browse files
committed
with the lock for keeping out duplicate executions of async actions, allow races for the action flag
1 parent 634ffb0 commit 02c2051

File tree

1 file changed

+6
-4
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime

1 file changed

+6
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/AsyncHandler.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import java.util.concurrent.Executors;
4646
import java.util.concurrent.ScheduledExecutorService;
4747
import java.util.concurrent.TimeUnit;
48-
import java.util.concurrent.atomic.AtomicBoolean;
4948
import java.util.concurrent.locks.Lock;
5049
import java.util.concurrent.locks.ReentrantLock;
5150
import java.util.function.Supplier;
@@ -94,7 +93,7 @@ default int frameIndex() {
9493

9594
private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
9695
private final ConcurrentLinkedQueue<AsyncAction> scheduledActions = new ConcurrentLinkedQueue<>();
97-
private final AtomicBoolean hasScheduledAction = new AtomicBoolean(false);
96+
private boolean hasScheduledAction = false;
9897
private final Lock executingScheduledActions = new ReentrantLock();
9998
private static final int ASYNC_ACTION_DELAY = 15; // chosen by a fair D20 dice roll
10099

@@ -113,7 +112,7 @@ public void run() {
113112
executingScheduledActions.lock();
114113
try {
115114
scheduledActions.add(asyncAction);
116-
hasScheduledAction.set(true);
115+
hasScheduledAction = true;
117116
} finally {
118117
executingScheduledActions.unlock();
119118
}
@@ -154,7 +153,9 @@ void registerAction(Supplier<AsyncAction> actionSupplier) {
154153
}
155154

156155
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) {
158159
CompilerDirectives.transferToInterpreter();
159160
processAsyncActions();
160161
}
@@ -171,6 +172,7 @@ private void processAsyncActions() {
171172
// scheduledActions queue, so we won't have a race between finishing the while loop and
172173
// returning from this method.
173174
if (executingScheduledActions.tryLock()) {
175+
hasScheduledAction = false;
174176
try {
175177
ConcurrentLinkedQueue<AsyncAction> actions = scheduledActions;
176178
AsyncAction action;

0 commit comments

Comments
 (0)