Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::integration::io_context_thread_manager Class Referenceexport

Manages io_context execution on shared thread pools. More...

#include <io_context_thread_manager.h>

Collaboration diagram for kcenon::network::integration::io_context_thread_manager:
Collaboration graph

Classes

class  impl
 
struct  metrics
 Get current metrics. More...
 

Public Member Functions

std::future< void > run_io_context (std::shared_ptr< asio::io_context > io_context, const std::string &component_name="")
 Run an io_context on the shared thread pool.
 
void stop_io_context (std::shared_ptr< asio::io_context > io_context)
 Stop an io_context managed by this manager.
 
void stop_all ()
 Stop all managed io_contexts.
 
void wait_all ()
 Wait for all managed io_contexts to complete.
 
size_t active_count () const
 Get the number of active io_contexts.
 
bool is_active (std::shared_ptr< asio::io_context > io_context) const
 Check if an io_context is managed and running.
 
void set_thread_pool (std::shared_ptr< thread_pool_interface > pool)
 Set a custom thread pool.
 
metrics get_metrics () const
 Get current metrics.
 
 io_context_thread_manager (size_t worker_count=0)
 Construct a manager with specified worker count.
 
 ~io_context_thread_manager ()
 Destructor - stops the manager if running.
 
 io_context_thread_manager (const io_context_thread_manager &)=delete
 
io_context_thread_manageroperator= (const io_context_thread_manager &)=delete
 
 io_context_thread_manager (io_context_thread_manager &&) noexcept
 
io_context_thread_manageroperator= (io_context_thread_manager &&) noexcept
 
bool start ()
 Start the io_context worker threads.
 
void stop ()
 Stop the io_context and join worker threads.
 
bool is_running () const noexcept
 Check if the manager is running.
 
asio::io_context & get_io_context () noexcept
 Get the managed io_context.
 
size_t worker_count () const noexcept
 Get the number of worker threads.
 

Static Public Member Functions

static io_context_thread_managerinstance ()
 Get the singleton instance.
 

Private Member Functions

 io_context_thread_manager ()
 
 ~io_context_thread_manager ()
 
 io_context_thread_manager (const io_context_thread_manager &)=delete
 
io_context_thread_manageroperator= (const io_context_thread_manager &)=delete
 
 io_context_thread_manager (io_context_thread_manager &&)=delete
 
io_context_thread_manageroperator= (io_context_thread_manager &&)=delete
 

Private Attributes

std::shared_ptr< implpimpl_
 PIMPL pointer with intentional leak pattern.
 
std::unique_ptr< asio::io_context > io_context_
 
std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > work_guard_
 
std::vector< std::thread > workers_
 
std::atomic< bool > running_ {false}
 
size_t worker_count_
 
std::mutex mutex_
 

Detailed Description

Manages io_context execution on shared thread pools.

Manages ASIO io_context lifecycle and worker threads.

This class provides unified thread management for all asio::io_context instances in the network system. Instead of each component managing its own threads, this manager provides a centralized approach.

Benefits

  • Unified thread resource management
  • Consistent shutdown behavior across all components
  • Reduced total thread count
  • Simplified component implementation

Thread Safety

All public methods are thread-safe.

Usage Example

// Run io_context on the shared thread pool
auto io_ctx = std::make_shared<asio::io_context>();
auto future = manager.run_io_context(io_ctx, "my_component");
// ... use io_context for async operations ...
// Stop when done
manager.stop_io_context(io_ctx);
future.wait();
static io_context_thread_manager & instance()
Get the singleton instance.

This class provides a thread-safe wrapper around asio::io_context, managing worker threads for asynchronous I/O operations.

Definition at line 180 of file core.cppm.

Constructor & Destructor Documentation

◆ io_context_thread_manager() [1/6]

kcenon::network::integration::io_context_thread_manager::io_context_thread_manager ( )
private

Definition at line 233 of file io_context_thread_manager.cpp.

238 : pimpl_(new impl(), [](impl*) { /* no-op deleter - intentional leak */ }) {
239}
std::shared_ptr< impl > pimpl_
PIMPL pointer with intentional leak pattern.

◆ ~io_context_thread_manager() [1/2]

kcenon::network::integration::io_context_thread_manager::~io_context_thread_manager ( )
privatedefault

◆ io_context_thread_manager() [2/6]

kcenon::network::integration::io_context_thread_manager::io_context_thread_manager ( const io_context_thread_manager & )
privatedelete

◆ io_context_thread_manager() [3/6]

kcenon::network::integration::io_context_thread_manager::io_context_thread_manager ( io_context_thread_manager && )
privatedelete

◆ io_context_thread_manager() [4/6]

kcenon::network::integration::io_context_thread_manager::io_context_thread_manager ( size_t worker_count = 0)
explicitexport

Construct a manager with specified worker count.

Parameters
worker_countNumber of worker threads (0 = hardware concurrency)

◆ ~io_context_thread_manager() [2/2]

kcenon::network::integration::io_context_thread_manager::~io_context_thread_manager ( )
export

Destructor - stops the manager if running.

◆ io_context_thread_manager() [5/6]

kcenon::network::integration::io_context_thread_manager::io_context_thread_manager ( const io_context_thread_manager & )
exportdelete

◆ io_context_thread_manager() [6/6]

kcenon::network::integration::io_context_thread_manager::io_context_thread_manager ( io_context_thread_manager && )
exportnoexcept

Member Function Documentation

◆ active_count()

size_t kcenon::network::integration::io_context_thread_manager::active_count ( ) const

Get the number of active io_contexts.

Returns
Number of io_contexts currently running

Definition at line 264 of file io_context_thread_manager.cpp.

264 {
265 return pimpl_->active_count();
266}

References pimpl_.

◆ get_io_context()

asio::io_context & kcenon::network::integration::io_context_thread_manager::get_io_context ( )
exportnoexcept

Get the managed io_context.

Returns
Reference to the io_context

◆ get_metrics()

io_context_thread_manager::metrics kcenon::network::integration::io_context_thread_manager::get_metrics ( ) const

Get current metrics.

Returns
Current metrics

Definition at line 280 of file io_context_thread_manager.cpp.

280 {
281 return pimpl_->get_metrics();
282}

References pimpl_.

◆ instance()

io_context_thread_manager & kcenon::network::integration::io_context_thread_manager::instance ( )
static

Get the singleton instance.

Returns
Reference to the singleton instance

Definition at line 228 of file io_context_thread_manager.cpp.

References instance().

Referenced by kcenon::network::core::messaging_client::do_start_impl(), kcenon::network::core::messaging_server::do_start_impl(), kcenon::network::core::messaging_client::do_stop_impl(), kcenon::network::core::messaging_server::do_stop_impl(), and instance().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_active()

bool kcenon::network::integration::io_context_thread_manager::is_active ( std::shared_ptr< asio::io_context > io_context) const

Check if an io_context is managed and running.

Parameters
io_contextThe io_context to check
Returns
true if the io_context is active, false otherwise

Definition at line 268 of file io_context_thread_manager.cpp.

270 {
271 return pimpl_->is_active(io_context);
272}

References pimpl_.

◆ is_running()

bool kcenon::network::integration::io_context_thread_manager::is_running ( ) const
exportnoexcept

Check if the manager is running.

Returns
true if running, false otherwise

◆ operator=() [1/4]

io_context_thread_manager & kcenon::network::integration::io_context_thread_manager::operator= ( const io_context_thread_manager & )
privatedelete

◆ operator=() [2/4]

io_context_thread_manager & kcenon::network::integration::io_context_thread_manager::operator= ( const io_context_thread_manager & )
exportdelete

◆ operator=() [3/4]

io_context_thread_manager & kcenon::network::integration::io_context_thread_manager::operator= ( io_context_thread_manager && )
exportnoexcept

◆ operator=() [4/4]

io_context_thread_manager & kcenon::network::integration::io_context_thread_manager::operator= ( io_context_thread_manager && )
privatedelete

◆ run_io_context()

std::future< void > kcenon::network::integration::io_context_thread_manager::run_io_context ( std::shared_ptr< asio::io_context > io_context,
const std::string & component_name = "" )

Run an io_context on the shared thread pool.

Submits the io_context::run() call as a task to the thread pool. The io_context will run until stopped or an error occurs.

Parameters
io_contextThe io_context to run
component_nameOptional name for logging/identification
Returns
Future that completes when io_context::run() returns
Note
The io_context must have work posted to it or use a work_guard, otherwise io_context::run() will return immediately.

Definition at line 243 of file io_context_thread_manager.cpp.

246 {
247 return pimpl_->run_io_context(io_context, component_name);
248}

References pimpl_.

Referenced by kcenon::network::core::messaging_client::do_start_impl(), and kcenon::network::core::messaging_server::do_start_impl().

Here is the caller graph for this function:

◆ set_thread_pool()

void kcenon::network::integration::io_context_thread_manager::set_thread_pool ( std::shared_ptr< thread_pool_interface > pool)

Set a custom thread pool.

By default, uses the thread pool from network_context. This allows using a different thread pool if needed.

Parameters
poolThe thread pool to use

Definition at line 274 of file io_context_thread_manager.cpp.

276 {
277 pimpl_->set_thread_pool(pool);
278}

References pimpl_.

◆ start()

bool kcenon::network::integration::io_context_thread_manager::start ( )
export

Start the io_context worker threads.

Returns
true if started successfully, false if already running

◆ stop()

void kcenon::network::integration::io_context_thread_manager::stop ( )
export

Stop the io_context and join worker threads.

◆ stop_all()

void kcenon::network::integration::io_context_thread_manager::stop_all ( )

Stop all managed io_contexts.

Stops all io_contexts that were started via run_io_context(). Useful for application shutdown.

Definition at line 256 of file io_context_thread_manager.cpp.

256 {
257 pimpl_->stop_all();
258}

References pimpl_.

◆ stop_io_context()

void kcenon::network::integration::io_context_thread_manager::stop_io_context ( std::shared_ptr< asio::io_context > io_context)

Stop an io_context managed by this manager.

Calls io_context::stop() to terminate the run loop. The future returned by run_io_context() will complete after this.

Parameters
io_contextThe io_context to stop

Definition at line 250 of file io_context_thread_manager.cpp.

252 {
253 pimpl_->stop_io_context(io_context);
254}

References pimpl_.

Referenced by kcenon::network::core::messaging_client::do_stop_impl(), and kcenon::network::core::messaging_server::do_stop_impl().

Here is the caller graph for this function:

◆ wait_all()

void kcenon::network::integration::io_context_thread_manager::wait_all ( )

Wait for all managed io_contexts to complete.

Blocks until all io_context::run() calls have returned. Should be called after stop_all() for clean shutdown.

Definition at line 260 of file io_context_thread_manager.cpp.

260 {
261 pimpl_->wait_all();
262}

References pimpl_.

◆ worker_count()

size_t kcenon::network::integration::io_context_thread_manager::worker_count ( ) const
exportnoexcept

Get the number of worker threads.

Returns
Number of worker threads

Member Data Documentation

◆ io_context_

std::unique_ptr<asio::io_context> kcenon::network::integration::io_context_thread_manager::io_context_
exportprivate

Definition at line 229 of file core.cppm.

◆ mutex_

std::mutex kcenon::network::integration::io_context_thread_manager::mutex_
mutableexportprivate

Definition at line 234 of file core.cppm.

◆ pimpl_

std::shared_ptr<impl> kcenon::network::integration::io_context_thread_manager::pimpl_
private

PIMPL pointer with intentional leak pattern.

Uses shared_ptr with no-op deleter to prevent heap corruption during static destruction. This is intentional - see docs/DESIGN_DECISIONS.md for detailed rationale.

Note
Memory is intentionally not freed. This is safe because:
  • Only ~100-200 bytes per singleton instance
  • Process terminates immediately after static destruction
  • OS reclaims all process memory on exit
Warning
Do not "fix" this by changing to unique_ptr or adding a real deleter. Doing so will cause heap corruption on shutdown.
See also
docs/DESIGN_DECISIONS.md#intentional-leak-pattern

Definition at line 181 of file io_context_thread_manager.h.

Referenced by active_count(), get_metrics(), is_active(), run_io_context(), set_thread_pool(), stop_all(), stop_io_context(), and wait_all().

◆ running_

std::atomic<bool> kcenon::network::integration::io_context_thread_manager::running_ {false}
exportprivate

Definition at line 232 of file core.cppm.

232{false};

◆ work_guard_

std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type> > kcenon::network::integration::io_context_thread_manager::work_guard_
exportprivate

Definition at line 230 of file core.cppm.

◆ worker_count_

size_t kcenon::network::integration::io_context_thread_manager::worker_count_
exportprivate

Definition at line 233 of file core.cppm.

◆ workers_

std::vector<std::thread> kcenon::network::integration::io_context_thread_manager::workers_
exportprivate

Definition at line 231 of file core.cppm.


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