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

Per-instance single-thread executor for isolated async logging. More...

#include <SingleThreadExecutor.hpp>

Public Member Functions

 SingleThreadExecutor ()
 Construct and immediately start the worker thread.
 
 ~SingleThreadExecutor ()
 Destructor drains and joins the worker thread.
 
 SingleThreadExecutor (const SingleThreadExecutor &)=delete
 
SingleThreadExecutor & operator= (const SingleThreadExecutor &)=delete
 
 SingleThreadExecutor (SingleThreadExecutor &&)=delete
 
SingleThreadExecutor & operator= (SingleThreadExecutor &&)=delete
 
void add_task (std::function< void()> task)
 Enqueue a task for asynchronous execution.
 
void wait ()
 Block until the queue is empty and no active tasks remain.
 
void shutdown ()
 Stop accepting new tasks, drain remaining, and join the worker thread.
 
void set_max_queue_size (std::size_t size)
 Change the maximum queue size (0 disables the limit).
 
void set_queue_policy (QueuePolicy policy)
 Change the queue overflow policy.
 
std::size_t dropped_tasks () const noexcept
 Return the number of tasks dropped by the overflow policy.
 
void reset_dropped_tasks () noexcept
 Reset the drop counter to zero.
 

Private Member Functions

void worker_loop ()
 

Private Attributes

std::deque< std::function< void()> > m_queue
 
std::mutex m_mutex
 
std::condition_variable m_cv
 
std::thread m_worker
 
std::atomic< bool > m_stop
 
std::atomic< bool > m_shutdown_done
 
std::size_t m_max_queue_size
 
QueuePolicy m_overflow_policy
 
std::atomic< std::size_t > m_dropped_tasks
 
std::atomic< std::size_t > m_active_tasks
 

Detailed Description

Per-instance single-thread executor for isolated async logging.

Provides the same public API as TaskExecutor so loggers can use either executor interchangeably. Unlike the global TaskExecutor singleton, each native instance owns its own worker thread, providing isolation between loggers.

Lifecycle guarantees (native builds):

Single-threaded Emscripten builds use a per-instance cooperative queue drained through the browser event loop instead of a worker thread. See docs/TaskExecutor.md for full semantics.

Definition at line 41 of file SingleThreadExecutor.hpp.

Constructor & Destructor Documentation

◆ SingleThreadExecutor() [1/3]

logit::detail::SingleThreadExecutor::SingleThreadExecutor ( )
inline

Construct and immediately start the worker thread.

Definition at line 44 of file SingleThreadExecutor.hpp.

◆ ~SingleThreadExecutor()

logit::detail::SingleThreadExecutor::~SingleThreadExecutor ( )
inline

Destructor drains and joins the worker thread.

Definition at line 56 of file SingleThreadExecutor.hpp.

◆ SingleThreadExecutor() [2/3]

logit::detail::SingleThreadExecutor::SingleThreadExecutor ( const SingleThreadExecutor & )
delete

◆ SingleThreadExecutor() [3/3]

logit::detail::SingleThreadExecutor::SingleThreadExecutor ( SingleThreadExecutor && )
delete

Member Function Documentation

◆ add_task()

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

Enqueue a task for asynchronous execution.

Definition at line 66 of file SingleThreadExecutor.hpp.

◆ dropped_tasks()

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

Return the number of tasks dropped by the overflow policy.

Definition at line 153 of file SingleThreadExecutor.hpp.

◆ operator=() [1/2]

SingleThreadExecutor & logit::detail::SingleThreadExecutor::operator= ( const SingleThreadExecutor & )
delete

◆ operator=() [2/2]

SingleThreadExecutor & logit::detail::SingleThreadExecutor::operator= ( SingleThreadExecutor && )
delete

◆ reset_dropped_tasks()

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

Reset the drop counter to zero.

Definition at line 158 of file SingleThreadExecutor.hpp.

◆ set_max_queue_size()

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

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

No-op if shutdown() has already been called.

Definition at line 134 of file SingleThreadExecutor.hpp.

◆ set_queue_policy()

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

Change the queue overflow policy.

No-op if shutdown() has already been called.

Definition at line 146 of file SingleThreadExecutor.hpp.

◆ shutdown()

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

Stop accepting new tasks, drain remaining, and join the worker thread.

After shutdown() returns, add_task() is a no-op and the worker thread has been joined (native) or the queue has been drained (Emscripten). Safe to call multiple times; subsequent calls are no-ops.

Definition at line 113 of file SingleThreadExecutor.hpp.

◆ wait()

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

Block until the queue is empty and no active tasks remain.

Returns once every task that was already accepted by add_task() has completed. Does not prevent new tasks from arriving while waiting.

Definition at line 100 of file SingleThreadExecutor.hpp.

◆ worker_loop()

void logit::detail::SingleThreadExecutor::worker_loop ( )
inlineprivate

Definition at line 174 of file SingleThreadExecutor.hpp.

Member Data Documentation

◆ m_active_tasks

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

Definition at line 172 of file SingleThreadExecutor.hpp.

◆ m_cv

std::condition_variable logit::detail::SingleThreadExecutor::m_cv
private

Definition at line 165 of file SingleThreadExecutor.hpp.

◆ m_dropped_tasks

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

Definition at line 171 of file SingleThreadExecutor.hpp.

◆ m_max_queue_size

std::size_t logit::detail::SingleThreadExecutor::m_max_queue_size
private

Definition at line 169 of file SingleThreadExecutor.hpp.

◆ m_mutex

std::mutex logit::detail::SingleThreadExecutor::m_mutex
mutableprivate

Definition at line 164 of file SingleThreadExecutor.hpp.

◆ m_overflow_policy

QueuePolicy logit::detail::SingleThreadExecutor::m_overflow_policy
private

Definition at line 170 of file SingleThreadExecutor.hpp.

◆ m_queue

std::deque<std::function<void()> > logit::detail::SingleThreadExecutor::m_queue
private

Definition at line 163 of file SingleThreadExecutor.hpp.

◆ m_shutdown_done

std::atomic<bool> logit::detail::SingleThreadExecutor::m_shutdown_done
private

Definition at line 168 of file SingleThreadExecutor.hpp.

◆ m_stop

std::atomic<bool> logit::detail::SingleThreadExecutor::m_stop
private

Definition at line 167 of file SingleThreadExecutor.hpp.

◆ m_worker

std::thread logit::detail::SingleThreadExecutor::m_worker
private

Definition at line 166 of file SingleThreadExecutor.hpp.


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