Skip to content

Commit

Permalink
Use request state to select a choosen Thread
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Jul 25, 2024
1 parent b380847 commit a797ca1
Showing 1 changed file with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,29 +177,34 @@ private final class ProcessItems extends ThreadLocalAction implements Runnable {
*/
@Override
protected void perform(ThreadLocalAction.Access access) {
var isMyThreadChoosen = false;
for (; ; ) {
Item[] toProcess;
synchronized (pendingItems) {
request.cancel(false);
if (!isMyThreadChoosen) {
if (request == null || request.isCancelled() || request.isDone()) {
// some thread is already handing the request
return;
} else {
// I am choosen and I will loop and process pendingItems
// until they are available
isMyThreadChoosen = true;
// signal others this request has choosen thread
request.cancel(false);
}
}
if (pendingItems.isEmpty()) {
// nothing to process,
// signal request is finished
// nothing to process anymore,
// signal request is finished and new one shall be scheduled
request = null;
return;
}
toProcess = pendingItems.toArray(Item[]::new);
// mark as being processed
pendingItems.set(0, null);
pendingItems.clear();
}
try {
for (var it : toProcess) {
it.finalizeNow(context);
removeFromItems(it);
}
} finally {
synchronized (pendingItems) {
pendingItems.subList(0, toProcess.length).clear();
}
for (var it : toProcess) {
it.finalizeNow(context);
removeFromItems(it);
}
}
}
Expand Down

0 comments on commit a797ca1

Please sign in to comment.