LogIt++
Header-only, macro-first C++ logging library
Loading...
Searching...
No Matches
logit::detail::TaskExecutor Class Reference

Thread-safe task executor backed by a dedicated worker thread. More...

#include <TaskExecutor.hpp>

Public Member Functions

void add_task (std::function< void()> task)
 Enqueue a task for asynchronous execution.
 
void wait ()
 Block until the queue is empty or a shutdown is requested.
 
void shutdown ()
 Stop the worker thread and drain outstanding tasks.
 
void set_max_queue_size (std::size_t size)
 Update the maximum queue size (0 disables the limit).
 
void set_queue_policy (QueuePolicy policy)
 Change the overflow policy for newly submitted tasks.
 
std::size_t dropped_tasks () const noexcept
 Return the number of tasks dropped by the overflow policy.
 
void reset_dropped_tasks () noexcept
 Reset the overflow counter to zero.
 

Static Public Member Functions

static TaskExecutor & get_instance ()
 Returns the global executor instance.
 

Private Member Functions

void worker_function ()
 
 TaskExecutor ()
 
 ~TaskExecutor ()
 
 TaskExecutor (const TaskExecutor &)=delete
 
TaskExecutor & operator= (const TaskExecutor &)=delete
 
 TaskExecutor (TaskExecutor &&)=delete
 
TaskExecutor & operator= (TaskExecutor &&)=delete
 

Private Attributes

std::mutex m_lifecycle_mutex
 Serializes shutdown with lifecycle-changing operations.
 
std::deque< std::function< void()> > m_tasks_queue
 
std::mutex m_queue_mutex
 
std::condition_variable m_queue_condition
 
std::thread m_worker_thread
 
std::atomic< bool > m_stop_flag
 
std::size_t m_max_queue_size
 
std::atomic< QueuePolicy > m_overflow_policy
 
std::atomic< std::size_t > m_dropped_tasks
 
std::atomic< std::size_t > m_active_tasks
 

Detailed Description

Thread-safe task executor backed by a dedicated worker thread.

The full design, including backpressure semantics and hot resizing, is described in docs/TaskExecutor.md.

Note
Thread-safe on native builds; Emscripten without pthreads is single-threaded and uses cooperative draining.

Definition at line 171 of file TaskExecutor.hpp.

Constructor & Destructor Documentation

◆ TaskExecutor() [1/3]

logit::detail::TaskExecutor::TaskExecutor ( )
inlineprivate

Definition at line 548 of file TaskExecutor.hpp.

◆ ~TaskExecutor()

logit::detail::TaskExecutor::~TaskExecutor ( )
inlineprivate

Definition at line 570 of file TaskExecutor.hpp.

◆ TaskExecutor() [2/3]

logit::detail::TaskExecutor::TaskExecutor ( const TaskExecutor & )
privatedelete

◆ TaskExecutor() [3/3]

logit::detail::TaskExecutor::TaskExecutor ( TaskExecutor && )
privatedelete

Member Function Documentation

◆ add_task()

void logit::detail::TaskExecutor::add_task ( std::function< void()> task)
inline

Enqueue a task for asynchronous execution.

Note
QueuePolicy::DropOldest drops the incoming task when LOGIT_USE_MPSC_RING is defined. See docs/TaskExecutor.md for the rationale.

Definition at line 186 of file TaskExecutor.hpp.

◆ dropped_tasks()

std::size_t logit::detail::TaskExecutor::dropped_tasks ( ) const
inlinenoexcept

Return the number of tasks dropped by the overflow policy.

Definition at line 389 of file TaskExecutor.hpp.

◆ get_instance()

static TaskExecutor & logit::detail::TaskExecutor::get_instance ( )
inlinestatic

Returns the global executor instance.

Note
The pointer-based singleton intentionally outlives static logger destructors so logging remains available during process shutdown.

Definition at line 177 of file TaskExecutor.hpp.

◆ operator=() [1/2]

TaskExecutor & logit::detail::TaskExecutor::operator= ( const TaskExecutor & )
privatedelete

◆ operator=() [2/2]

TaskExecutor & logit::detail::TaskExecutor::operator= ( TaskExecutor && )
privatedelete

◆ reset_dropped_tasks()

void logit::detail::TaskExecutor::reset_dropped_tasks ( )
inlinenoexcept

Reset the overflow counter to zero.

Definition at line 393 of file TaskExecutor.hpp.

◆ set_max_queue_size()

void logit::detail::TaskExecutor::set_max_queue_size ( std::size_t size)
inline

Update the maximum queue size (0 disables the limit).

On MPSC builds this performs the "hot" resize described in docs/TaskExecutor.md.

Definition at line 318 of file TaskExecutor.hpp.

◆ set_queue_policy()

void logit::detail::TaskExecutor::set_queue_policy ( QueuePolicy policy)
inline

Change the overflow policy for newly submitted tasks.

Definition at line 381 of file TaskExecutor.hpp.

◆ shutdown()

void logit::detail::TaskExecutor::shutdown ( )
inline

Stop the worker thread and drain outstanding tasks.

Definition at line 292 of file TaskExecutor.hpp.

◆ wait()

void logit::detail::TaskExecutor::wait ( )
inline

Block until the queue is empty or a shutdown is requested.

Definition at line 273 of file TaskExecutor.hpp.

◆ worker_function()

void logit::detail::TaskExecutor::worker_function ( )
inlineprivate

Definition at line 431 of file TaskExecutor.hpp.

Member Data Documentation

◆ m_active_tasks

std::atomic<std::size_t> logit::detail::TaskExecutor::m_active_tasks
private

Definition at line 408 of file TaskExecutor.hpp.

◆ m_dropped_tasks

std::atomic<std::size_t> logit::detail::TaskExecutor::m_dropped_tasks
private

Definition at line 407 of file TaskExecutor.hpp.

◆ m_lifecycle_mutex

std::mutex logit::detail::TaskExecutor::m_lifecycle_mutex
mutableprivate

Serializes shutdown with lifecycle-changing operations.

Definition at line 398 of file TaskExecutor.hpp.

◆ m_max_queue_size

std::size_t logit::detail::TaskExecutor::m_max_queue_size
private

Definition at line 405 of file TaskExecutor.hpp.

◆ m_overflow_policy

std::atomic<QueuePolicy> logit::detail::TaskExecutor::m_overflow_policy
private

Definition at line 406 of file TaskExecutor.hpp.

◆ m_queue_condition

std::condition_variable logit::detail::TaskExecutor::m_queue_condition
private

Definition at line 402 of file TaskExecutor.hpp.

◆ m_queue_mutex

std::mutex logit::detail::TaskExecutor::m_queue_mutex
mutableprivate

Definition at line 401 of file TaskExecutor.hpp.

◆ m_stop_flag

std::atomic<bool> logit::detail::TaskExecutor::m_stop_flag
private

Definition at line 404 of file TaskExecutor.hpp.

◆ m_tasks_queue

std::deque<std::function<void()> > logit::detail::TaskExecutor::m_tasks_queue
private

Definition at line 400 of file TaskExecutor.hpp.

◆ m_worker_thread

std::thread logit::detail::TaskExecutor::m_worker_thread
private

Definition at line 403 of file TaskExecutor.hpp.


The documentation for this class was generated from the following file: