From c8b2521bc7fad30706bfdd613850304c7c61ea48 Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Thu, 18 Sep 2025 05:55:28 +0300 Subject: [PATCH] feat(task-executor): make block wait configurable --- docs/TaskExecutor.md | 4 +++- include/logit_cpp/logit/Logger.hpp | 1 + include/logit_cpp/logit/config.hpp | 9 +++++++++ include/logit_cpp/logit/detail/TaskExecutor.hpp | 4 ++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/docs/TaskExecutor.md b/docs/TaskExecutor.md index ba9ef72..f93392f 100644 --- a/docs/TaskExecutor.md +++ b/docs/TaskExecutor.md @@ -49,7 +49,9 @@ lifetime guarantees that logger integrations rely on. * Uses `m_active_tasks` to count in-flight work. If the counter reaches the limit, producers wait. The non-MPSC build waits on `m_queue_condition`. The MPSC build parks on `m_cv` with short sleeps while - the worker drains tasks. This policy avoids loss at the expense of + the worker drains tasks. The sleep interval defaults to + `LOGIT_TASK_EXECUTOR_BLOCK_WAIT_USEC` microseconds (200 by default) and can + be overridden at compile time. This policy avoids loss at the expense of producer-side backpressure. * `DropNewest` * Non-MPSC: the incoming task is discarded when the deque is full. diff --git a/include/logit_cpp/logit/Logger.hpp b/include/logit_cpp/logit/Logger.hpp index a097f0d..0120b14 100644 --- a/include/logit_cpp/logit/Logger.hpp +++ b/include/logit_cpp/logit/Logger.hpp @@ -5,6 +5,7 @@ /// \file Logger.hpp /// \brief Defines the Logger class for managing multiple loggers and formatters. +#include "config.hpp" #include "loggers/ILogger.hpp" #include "formatter.hpp" #include "detail/TaskExecutor.hpp" diff --git a/include/logit_cpp/logit/config.hpp b/include/logit_cpp/logit/config.hpp index 2bccd42..1cb6587 100644 --- a/include/logit_cpp/logit/config.hpp +++ b/include/logit_cpp/logit/config.hpp @@ -22,6 +22,15 @@ #define LOGIT_DEFAULT_COLOR logit::TextColor::LightGray #endif +/// \brief Defines the sleep duration (in microseconds) used by TaskExecutor when blocking producers. +/// +/// When `QueuePolicy::Block` is active, the TaskExecutor periodically waits for +/// capacity to become available. Override this value to tweak the polling +/// cadence in builds where the default wait is not appropriate. +#ifndef LOGIT_TASK_EXECUTOR_BLOCK_WAIT_USEC + #define LOGIT_TASK_EXECUTOR_BLOCK_WAIT_USEC 200 +#endif + /// \name Log Level Colors /// Default colors for each log level. /// \{ diff --git a/include/logit_cpp/logit/detail/TaskExecutor.hpp b/include/logit_cpp/logit/detail/TaskExecutor.hpp index 27166d0..25e42ba 100644 --- a/include/logit_cpp/logit/detail/TaskExecutor.hpp +++ b/include/logit_cpp/logit/detail/TaskExecutor.hpp @@ -242,7 +242,7 @@ namespace logit { namespace detail { m_active_tasks.load(std::memory_order_relaxed) >= m_max_queue_size) { std::unique_lock lk(m_cv_mutex); - m_cv.wait_for(lk, std::chrono::microseconds(200)); + m_cv.wait_for(lk, std::chrono::microseconds(LOGIT_TASK_EXECUTOR_BLOCK_WAIT_USEC)); continue; } @@ -266,7 +266,7 @@ namespace logit { namespace detail { case QueuePolicy::Block: { std::unique_lock lk(m_cv_mutex); - m_cv.wait_for(lk, std::chrono::microseconds(200)); + m_cv.wait_for(lk, std::chrono::microseconds(LOGIT_TASK_EXECUTOR_BLOCK_WAIT_USEC)); break; } }