This is an automated email from the ASF dual-hosted git repository.

swebb2066 pushed a commit to branch asyncappender_robustness
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git

commit 23d5811201a312308515870281e9b95cb6f7ac45
Author: Stephen Webb <swebb2...@gmail.com>
AuthorDate: Fri Jul 26 09:58:56 2024 +1000

    Improve asyncappender robustness
---
 src/main/cpp/asyncappender.cpp | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/main/cpp/asyncappender.cpp b/src/main/cpp/asyncappender.cpp
index 69533ff6..99e7b9db 100644
--- a/src/main/cpp/asyncappender.cpp
+++ b/src/main/cpp/asyncappender.cpp
@@ -223,6 +223,11 @@ struct AsyncAppender::AsyncAppenderPriv : public 
AppenderSkeleton::AppenderSkele
                std::lock_guard<std::mutex> lock(this->bufferMutex);
                this->closed = true;
        }
+
+       /**
+        * Used to ensure the dispatch thread does not wait when a logging 
thread is waiting.
+       */
+       int blockedCount{0};
 };
 
 
@@ -328,10 +333,12 @@ void AsyncAppender::append(const spi::LoggingEventPtr& 
event, Pool& p)
                        && !priv->closed
                        && (priv->dispatcher.get_id() != 
std::this_thread::get_id()) )
                {
+                       ++priv->blockedCount;
                        priv->bufferNotFull.wait(lock, [this]()
                        {
                                return priv->eventCount - priv->dispatchedCount 
< priv->bufferSize;
                        });
+                       --priv->blockedCount;
                        discard = false;
                }
 
@@ -519,7 +526,7 @@ void AsyncAppender::dispatch()
                {
                        std::unique_lock<std::mutex> lock(priv->bufferMutex);
                        priv->bufferNotEmpty.wait(lock, [this]() -> bool
-                               { return priv->dispatchedCount != 
priv->commitCount || priv->closed; }
+                               { return 0 < priv->blockedCount || 
priv->dispatchedCount != priv->commitCount || priv->closed; }
                        );
                }
                isActive = !priv->isClosed();

Reply via email to