From e2b00bd4e00fdd1e8f40b109e68a8b8f10eb6616 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 2 Nov 2022 11:19:08 +0100 Subject: [PATCH] Improved non-blocking algorithm of AdaptiveExecutionStrategy. Now only starting one pending producer also in case of CAS failures. Signed-off-by: Simone Bordet --- .../strategy/AdaptiveExecutionStrategy.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/thread/strategy/AdaptiveExecutionStrategy.java b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/thread/strategy/AdaptiveExecutionStrategy.java index 712a2f84ed7f..5e81cb940370 100644 --- a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/thread/strategy/AdaptiveExecutionStrategy.java +++ b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/thread/strategy/AdaptiveExecutionStrategy.java @@ -98,9 +98,9 @@ public class AdaptiveExecutionStrategy extends ContainerLifeCycle implements Exe /** * The production state of the strategy. */ - static final int IDLE = 0; // No tasks or producers. - static final int PRODUCING = 1; // There is an active producing thread. - static final int REPRODUCING = 2; // There is an active producing thread and demand for more production. + private static final int IDLE = 0; // No tasks or producers. + private static final int PRODUCING = 1; // There is an active producing thread. + private static final int REPRODUCING = 2; // There is an active producing thread and demand for more production. /** * The sub-strategies used by the strategy to consume tasks that are produced. @@ -327,7 +327,7 @@ private SubStrategy selectSubStrategy(Runnable task, boolean nonBlocking) return SubStrategy.PRODUCE_CONSUME; // check if a pending producer is available. - int executed = 0; + boolean tryExecuted = false; while (true) { long biState = _state.get(); @@ -335,10 +335,9 @@ private SubStrategy selectSubStrategy(Runnable task, boolean nonBlocking) int pending = AtomicBiInteger.getHi(biState); // If a pending producer is available or one can be started - pending += executed; - if (pending <= 0 && _tryExecutor.tryExecute(_runPendingProducer)) + if (tryExecuted || pending <= 0 && _tryExecutor.tryExecute(_runPendingProducer)) { - executed++; + tryExecuted = true; pending++; } @@ -369,7 +368,7 @@ private SubStrategy selectSubStrategy(Runnable task, boolean nonBlocking) if (!nonBlocking) { // check if a pending producer is available. - int executed = 0; + boolean tryExecuted = false; while (true) { long biState = _state.get(); @@ -377,10 +376,9 @@ private SubStrategy selectSubStrategy(Runnable task, boolean nonBlocking) int pending = AtomicBiInteger.getHi(biState); // If a pending producer is available or one can be started - pending += executed; - if (pending <= 0 && _tryExecutor.tryExecute(_runPendingProducer)) + if (tryExecuted || pending <= 0 && _tryExecutor.tryExecute(_runPendingProducer)) { - executed++; + tryExecuted = true; pending++; }