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

Bridge for thread pool integration implementing INetworkBridge. More...

#include <thread_pool_bridge.h>

Inheritance diagram for kcenon::network::integration::ThreadPoolBridge:
Inheritance graph
Collaboration diagram for kcenon::network::integration::ThreadPoolBridge:
Collaboration graph

Public Types

enum class  BackendType { ThreadSystem , CommonSystem , Custom }
 Type of thread pool backend. More...
 

Public Member Functions

 ~ThreadPoolBridge () override
 Destructor.
 
VoidResult initialize (const BridgeConfig &config) override
 Initialize the bridge with configuration.
 
VoidResult shutdown () override
 Shutdown the bridge.
 
bool is_initialized () const override
 Check if the bridge is initialized.
 
BridgeMetrics get_metrics () const override
 Get current metrics.
 
std::shared_ptr< thread_pool_interface > get_thread_pool () const
 Get the underlying thread pool.
 
BackendType get_backend_type () const
 Get the backend type.
 
- Public Member Functions inherited from kcenon::network::integration::INetworkBridge
virtual ~INetworkBridge ()=default
 

Static Public Member Functions

static Result< std::shared_ptr< ThreadPoolBridge > > create (std::shared_ptr< thread_pool_interface > pool, BackendType backend_type=BackendType::Custom)
 Construct bridge with custom thread pool.
 
static Result< std::shared_ptr< ThreadPoolBridge > > from_thread_system (const std::string &pool_name="network_pool")
 Create bridge from thread_system.
 

Private Member Functions

 ThreadPoolBridge (std::shared_ptr< thread_pool_interface > pool, BackendType backend_type)
 

Private Attributes

std::shared_ptr< thread_pool_interface > pool_
 
BackendType backend_type_
 
std::atomic< bool > initialized_ {false}
 
std::mutex metrics_mutex_
 
BridgeMetrics cached_metrics_
 

Detailed Description

Bridge for thread pool integration implementing INetworkBridge.

This class consolidates thread_system and common_system thread pool integrations into a single, unified bridge. It provides factory methods for creating bridges from different backend types.

Backend Types:

  • ThreadSystem: Uses thread_system's thread pool directly
  • CommonSystem: Adapts common_system's IExecutor
  • Custom: Uses user-provided thread_pool_interface

Lifecycle:

  1. Create using factory method (from_thread_system, from_common_system) or direct constructor
  2. Call initialize() with configuration
  3. Use get_thread_pool() to access the underlying pool
  4. Call shutdown() before destruction

Thread Safety:

Definition at line 81 of file thread_pool_bridge.h.

Member Enumeration Documentation

◆ BackendType

Type of thread pool backend.

Enumerator
ThreadSystem 

Uses thread_system's thread pool.

CommonSystem 

Uses common_system's IExecutor.

Custom 

Uses custom thread_pool_interface.

Definition at line 87 of file thread_pool_bridge.h.

87 {
90 Custom
91 };

Constructor & Destructor Documentation

◆ ~ThreadPoolBridge()

kcenon::network::integration::ThreadPoolBridge::~ThreadPoolBridge ( )
override

Destructor.

Automatically calls shutdown() if initialized

Definition at line 31 of file thread_pool_bridge.cpp.

31 {
32 if (initialized_.load()) {
33 shutdown();
34 }
35}
VoidResult shutdown() override
Shutdown the bridge.

References initialized_, and shutdown().

Here is the call graph for this function:

◆ ThreadPoolBridge()

kcenon::network::integration::ThreadPoolBridge::ThreadPoolBridge ( std::shared_ptr< thread_pool_interface > pool,
BackendType backend_type )
private

Definition at line 25 of file thread_pool_bridge.cpp.

28 : pool_(std::move(pool)), backend_type_(backend_type) {
29}
std::shared_ptr< thread_pool_interface > pool_

Referenced by create().

Here is the caller graph for this function:

Member Function Documentation

◆ create()

Result< std::shared_ptr< ThreadPoolBridge > > kcenon::network::integration::ThreadPoolBridge::create ( std::shared_ptr< thread_pool_interface > pool,
BackendType backend_type = BackendType::Custom )
staticnodiscard

Construct bridge with custom thread pool.

Parameters
poolThread pool implementation
backend_typeType of backend (default: Custom)
Exceptions
std::invalid_argumentif pool is nullptr

Example:

auto pool = std::make_shared<basic_thread_pool>(8);
auto bridge = std::make_shared<ThreadPoolBridge>(pool);

Definition at line 13 of file thread_pool_bridge.cpp.

14 {
15 if (!pool) {
18 "ThreadPoolBridge requires non-null thread pool",
19 "ThreadPoolBridge::create");
20 }
21 return ok(std::shared_ptr<ThreadPoolBridge>(
22 new ThreadPoolBridge(std::move(pool), backend_type)));
23}
ThreadPoolBridge(std::shared_ptr< thread_pool_interface > pool, BackendType backend_type)
VoidResult ok()

References kcenon::network::integration::error, kcenon::network::error_codes::common_errors::invalid_argument, kcenon::network::ok(), and ThreadPoolBridge().

Referenced by from_thread_system(), and kcenon::network::integration::NetworkSystemBridge::with_custom().

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

◆ from_thread_system()

Result< std::shared_ptr< ThreadPoolBridge > > kcenon::network::integration::ThreadPoolBridge::from_thread_system ( const std::string & pool_name = "network_pool")
staticnodiscard

Create bridge from thread_system.

Parameters
pool_nameThread pool name (default: "network_pool")
Returns
Shared pointer to ThreadPoolBridge

Creates a bridge using thread_system's thread pool via thread_integration_manager.

Example:

config.integration_name = "thread_system";
bridge->initialize(config);
static Result< std::shared_ptr< ThreadPoolBridge > > from_thread_system(const std::string &pool_name="network_pool")
Create bridge from thread_system.
tracing_config config
Definition exporters.cpp:29
Configuration for bridge initialization.

Definition at line 132 of file thread_pool_bridge.cpp.

133 {
134 (void)pool_name;
136 if (!pool) {
139 "Failed to get thread pool from thread_integration_manager",
140 "ThreadPoolBridge::from_thread_system");
141 }
142 return create(pool, BackendType::ThreadSystem);
143}
static Result< std::shared_ptr< ThreadPoolBridge > > create(std::shared_ptr< thread_pool_interface > pool, BackendType backend_type=BackendType::Custom)
Construct bridge with custom thread pool.
static thread_integration_manager & instance()
Get the singleton instance.
std::shared_ptr< thread_pool_interface > get_thread_pool()
Get the current thread pool.

References create(), kcenon::network::integration::error, kcenon::network::integration::thread_integration_manager::get_thread_pool(), kcenon::network::integration::thread_integration_manager::instance(), kcenon::network::error_codes::common_errors::not_initialized, and ThreadSystem.

Referenced by kcenon::network::integration::NetworkSystemBridge::create_default(), and kcenon::network::integration::NetworkSystemBridge::with_thread_system().

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

◆ get_backend_type()

ThreadPoolBridge::BackendType kcenon::network::integration::ThreadPoolBridge::get_backend_type ( ) const

Get the backend type.

Returns
Type of backend this bridge uses

Definition at line 128 of file thread_pool_bridge.cpp.

128 {
129 return backend_type_;
130}

References backend_type_.

◆ get_metrics()

BridgeMetrics kcenon::network::integration::ThreadPoolBridge::get_metrics ( ) const
overridevirtual

Get current metrics.

Returns
Bridge metrics including thread pool statistics

Custom Metrics:

  • "worker_threads": Number of worker threads
  • "pending_tasks": Number of queued tasks
  • "backend_type": Backend type (0=ThreadSystem, 1=CommonSystem, 2=Custom)

Thread Safety: Safe to call concurrently

Implements kcenon::network::integration::INetworkBridge.

Definition at line 100 of file thread_pool_bridge.cpp.

100 {
101 std::lock_guard<std::mutex> lock(metrics_mutex_);
102
103 if (!initialized_.load() || !pool_) {
104 BridgeMetrics metrics;
105 metrics.is_healthy = false;
106 metrics.last_activity = cached_metrics_.last_activity;
107 return metrics;
108 }
109
110 // Update metrics with current thread pool state
111 BridgeMetrics metrics = cached_metrics_;
112 metrics.is_healthy = pool_->is_running();
113 metrics.last_activity = std::chrono::steady_clock::now();
114 metrics.custom_metrics["worker_threads"] = static_cast<double>(pool_->worker_count());
115 metrics.custom_metrics["pending_tasks"] = static_cast<double>(pool_->pending_tasks());
116 metrics.custom_metrics["backend_type"] = static_cast<double>(backend_type_);
117
118 // Update cached metrics for next call
119 cached_metrics_ = metrics;
120
121 return metrics;
122}
std::chrono::steady_clock::time_point last_activity
Timestamp of last activity or health check.
bool is_healthy
Overall health status of the bridge.

References backend_type_, cached_metrics_, kcenon::network::integration::BridgeMetrics::custom_metrics, initialized_, kcenon::network::integration::BridgeMetrics::is_healthy, kcenon::network::integration::BridgeMetrics::last_activity, metrics_mutex_, and pool_.

◆ get_thread_pool()

std::shared_ptr< thread_pool_interface > kcenon::network::integration::ThreadPoolBridge::get_thread_pool ( ) const

Get the underlying thread pool.

Returns
Shared pointer to thread pool, or nullptr if not initialized

Thread Safety: Safe to call after initialization

Example:

if (auto pool = bridge->get_thread_pool()) {
pool->submit([]{ // task implementation here });
}

Definition at line 124 of file thread_pool_bridge.cpp.

124 {
125 return pool_;
126}

References pool_.

◆ initialize()

VoidResult kcenon::network::integration::ThreadPoolBridge::initialize ( const BridgeConfig & config)
overridevirtual

Initialize the bridge with configuration.

Parameters
configConfiguration parameters
Returns
ok() on success, error_info on failure

Configuration Properties:

  • "enabled": "true" or "false" (default: "true")
  • "worker_count": Number of worker threads (informational)
  • "pool_name": Thread pool identifier (informational)

Error Conditions:

  • Already initialized
  • Underlying thread pool not running
  • Invalid configuration

Example:

config.integration_name = "thread_system";
config.properties["pool_name"] = "network_pool";
config.properties["worker_count"] = "8";
auto result = bridge->initialize(config);
if (result.is_err()) {
std::cerr << "Init failed: " << result.error().message << std::endl;
}

Implements kcenon::network::integration::INetworkBridge.

Definition at line 37 of file thread_pool_bridge.cpp.

37 {
38 if (initialized_.load()) {
39 return error_void(
41 "ThreadPoolBridge already initialized",
42 "ThreadPoolBridge::initialize");
43 }
44
45 if (!pool_) {
46 return error_void(
48 "Thread pool is null",
49 "ThreadPoolBridge::initialize");
50 }
51
52 if (!pool_->is_running()) {
53 return error_void(
55 "Thread pool is not running",
56 "ThreadPoolBridge::initialize");
57 }
58
59 // Check if bridge is enabled (default: true)
60 auto enabled_it = config.properties.find("enabled");
61 if (enabled_it != config.properties.end() && enabled_it->second == "false") {
62 return error_void(
64 "Bridge is disabled in configuration",
65 "ThreadPoolBridge::initialize");
66 }
67
68 // Initialize metrics
69 std::lock_guard<std::mutex> lock(metrics_mutex_);
71 cached_metrics_.last_activity = std::chrono::steady_clock::now();
72 cached_metrics_.custom_metrics["worker_threads"] = static_cast<double>(pool_->worker_count());
73 cached_metrics_.custom_metrics["pending_tasks"] = static_cast<double>(pool_->pending_tasks());
74 cached_metrics_.custom_metrics["backend_type"] = static_cast<double>(backend_type_);
75
76 initialized_.store(true);
77 return ok();
78}
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
std::map< std::string, double > custom_metrics
Bridge-specific custom metrics.

References kcenon::network::error_codes::common_errors::already_exists, backend_type_, cached_metrics_, config, kcenon::network::integration::BridgeMetrics::custom_metrics, kcenon::network::error_void(), initialized_, kcenon::network::error_codes::common_errors::invalid_argument, kcenon::network::integration::BridgeMetrics::is_healthy, kcenon::network::integration::BridgeMetrics::last_activity, metrics_mutex_, kcenon::network::error_codes::common_errors::not_initialized, kcenon::network::ok(), and pool_.

Here is the call graph for this function:

◆ is_initialized()

bool kcenon::network::integration::ThreadPoolBridge::is_initialized ( ) const
overridevirtual

Check if the bridge is initialized.

Returns
true if initialized and ready, false otherwise

Implements kcenon::network::integration::INetworkBridge.

Definition at line 96 of file thread_pool_bridge.cpp.

96 {
97 return initialized_.load() && pool_ && pool_->is_running();
98}

References initialized_, and pool_.

◆ shutdown()

VoidResult kcenon::network::integration::ThreadPoolBridge::shutdown ( )
overridevirtual

Shutdown the bridge.

Returns
ok() on success, error_info on failure

Shuts down the bridge but does not shut down the underlying thread pool. Thread pool lifecycle is managed externally.

This method is idempotent - multiple calls are safe.

Implements kcenon::network::integration::INetworkBridge.

Definition at line 80 of file thread_pool_bridge.cpp.

80 {
81 if (!initialized_.load()) {
82 return ok(); // Idempotent: already shut down
83 }
84
85 // Update metrics to reflect shutdown state
86 {
87 std::lock_guard<std::mutex> lock(metrics_mutex_);
89 cached_metrics_.last_activity = std::chrono::steady_clock::now();
90 }
91
92 initialized_.store(false);
93 return ok();
94}

References cached_metrics_, initialized_, kcenon::network::integration::BridgeMetrics::is_healthy, kcenon::network::integration::BridgeMetrics::last_activity, metrics_mutex_, and kcenon::network::ok().

Referenced by ~ThreadPoolBridge().

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

Member Data Documentation

◆ backend_type_

BackendType kcenon::network::integration::ThreadPoolBridge::backend_type_
private

Definition at line 248 of file thread_pool_bridge.h.

Referenced by get_backend_type(), get_metrics(), and initialize().

◆ cached_metrics_

BridgeMetrics kcenon::network::integration::ThreadPoolBridge::cached_metrics_
mutableprivate

Definition at line 251 of file thread_pool_bridge.h.

Referenced by get_metrics(), initialize(), and shutdown().

◆ initialized_

std::atomic<bool> kcenon::network::integration::ThreadPoolBridge::initialized_ {false}
private

Definition at line 249 of file thread_pool_bridge.h.

249{false};

Referenced by get_metrics(), initialize(), is_initialized(), shutdown(), and ~ThreadPoolBridge().

◆ metrics_mutex_

std::mutex kcenon::network::integration::ThreadPoolBridge::metrics_mutex_
mutableprivate

Definition at line 250 of file thread_pool_bridge.h.

Referenced by get_metrics(), initialize(), and shutdown().

◆ pool_

std::shared_ptr<thread_pool_interface> kcenon::network::integration::ThreadPoolBridge::pool_
private

Definition at line 247 of file thread_pool_bridge.h.

Referenced by get_metrics(), get_thread_pool(), initialize(), and is_initialized().


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