Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
io_context_thread_manager.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
18#include <asio.hpp>
19#include <functional>
20#include <future>
21#include <memory>
22#include <mutex>
23#include <string>
24#include <unordered_map>
25
27
29
62class io_context_thread_manager {
63public:
69
83 std::future<void> run_io_context(
84 std::shared_ptr<asio::io_context> io_context,
85 const std::string& component_name = ""
86 );
87
96 void stop_io_context(std::shared_ptr<asio::io_context> io_context);
97
104 void stop_all();
105
112 void wait_all();
113
118 size_t active_count() const;
119
125 bool is_active(std::shared_ptr<asio::io_context> io_context) const;
126
135 void set_thread_pool(std::shared_ptr<thread_pool_interface> pool);
136
140 struct metrics {
141 size_t active_contexts = 0;
142 size_t total_started = 0;
143 size_t total_completed = 0;
144 };
145
150 metrics get_metrics() const;
151
152private:
155
156 // Non-copyable, non-movable
161
162 class impl;
163
181 std::shared_ptr<impl> pimpl_;
182};
183
184} // namespace kcenon::network::integration
185
186// Backward compatibility - use the existing namespace alias from thread_integration.h
187// The alias `namespace kcenon::network::integration = kcenon::network::integration;` is
188// already defined in thread_integration.h, which is included before this header.
Manages io_context execution on shared thread pools.
Definition core.cppm:180
std::shared_ptr< impl > pimpl_
PIMPL pointer with intentional leak pattern.
bool is_active(std::shared_ptr< asio::io_context > io_context) const
Check if an io_context is managed and running.
size_t active_count() const
Get the number of active io_contexts.
io_context_thread_manager(const io_context_thread_manager &)=delete
io_context_thread_manager & operator=(io_context_thread_manager &&)=delete
void stop_io_context(std::shared_ptr< asio::io_context > io_context)
Stop an io_context managed by this manager.
static io_context_thread_manager & instance()
Get the singleton instance.
void set_thread_pool(std::shared_ptr< thread_pool_interface > pool)
Set a custom thread pool.
void wait_all()
Wait for all managed io_contexts to complete.
io_context_thread_manager(io_context_thread_manager &&)=delete
io_context_thread_manager & operator=(const io_context_thread_manager &)=delete
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.
Thread system integration interface for network_system.