Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
thread_pool_bridge.cpp
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
6
7#if KCENON_WITH_COMMON_SYSTEM
9#endif
10
11#include <stdexcept>
12
14
16 std::shared_ptr<thread_pool_interface> pool,
17 BackendType backend_type)
18 : pool_(std::move(pool)), backend_type_(backend_type) {
19 if (!pool_) {
20 throw std::invalid_argument("ThreadPoolBridge requires non-null thread pool");
21 }
22}
23
25 if (initialized_.load()) {
26 shutdown();
27 }
28}
29
31 if (initialized_.load()) {
32 return error_void(
34 "ThreadPoolBridge already initialized",
35 "ThreadPoolBridge::initialize");
36 }
37
38 if (!pool_) {
39 return error_void(
41 "Thread pool is null",
42 "ThreadPoolBridge::initialize");
43 }
44
45 if (!pool_->is_running()) {
46 return error_void(
48 "Thread pool is not running",
49 "ThreadPoolBridge::initialize");
50 }
51
52 // Check if bridge is enabled (default: true)
53 auto enabled_it = config.properties.find("enabled");
54 if (enabled_it != config.properties.end() && enabled_it->second == "false") {
55 return error_void(
57 "Bridge is disabled in configuration",
58 "ThreadPoolBridge::initialize");
59 }
60
61 // Initialize metrics
62 std::lock_guard<std::mutex> lock(metrics_mutex_);
64 cached_metrics_.last_activity = std::chrono::steady_clock::now();
65 cached_metrics_.custom_metrics["worker_threads"] = static_cast<double>(pool_->worker_count());
66 cached_metrics_.custom_metrics["pending_tasks"] = static_cast<double>(pool_->pending_tasks());
67 cached_metrics_.custom_metrics["backend_type"] = static_cast<double>(backend_type_);
68
69 initialized_.store(true);
70 return ok();
71}
72
74 if (!initialized_.load()) {
75 return ok(); // Idempotent: already shut down
76 }
77
78 // Update metrics to reflect shutdown state
79 {
80 std::lock_guard<std::mutex> lock(metrics_mutex_);
82 cached_metrics_.last_activity = std::chrono::steady_clock::now();
83 }
84
85 initialized_.store(false);
86 return ok();
87}
88
90 return initialized_.load() && pool_ && pool_->is_running();
91}
92
94 std::lock_guard<std::mutex> lock(metrics_mutex_);
95
96 if (!initialized_.load() || !pool_) {
97 BridgeMetrics metrics;
98 metrics.is_healthy = false;
100 return metrics;
101 }
102
103 // Update metrics with current thread pool state
105 metrics.is_healthy = pool_->is_running();
106 metrics.last_activity = std::chrono::steady_clock::now();
107 metrics.custom_metrics["worker_threads"] = static_cast<double>(pool_->worker_count());
108 metrics.custom_metrics["pending_tasks"] = static_cast<double>(pool_->pending_tasks());
109 metrics.custom_metrics["backend_type"] = static_cast<double>(backend_type_);
110
111 // Update cached metrics for next call
112 cached_metrics_ = metrics;
113
114 return metrics;
115}
116
117std::shared_ptr<thread_pool_interface> ThreadPoolBridge::get_thread_pool() const {
118 return pool_;
119}
120
124
125std::shared_ptr<ThreadPoolBridge> ThreadPoolBridge::from_thread_system(
126 const std::string& pool_name) {
127 (void)pool_name; // Pool name is informational only in current implementation
128
130 if (!pool) {
131 throw std::runtime_error("Failed to get thread pool from thread_integration_manager");
132 }
133
134 return std::make_shared<ThreadPoolBridge>(pool, BackendType::ThreadSystem);
135}
136
137#if KCENON_WITH_COMMON_SYSTEM
138std::shared_ptr<ThreadPoolBridge> ThreadPoolBridge::from_common_system(
139 std::shared_ptr<::kcenon::common::interfaces::IExecutor> executor) {
140 if (!executor) {
141 throw std::invalid_argument("ThreadPoolBridge::from_common_system requires non-null executor");
142 }
143
144 // Adapt common_system executor to thread_pool_interface
145 auto adapted_pool = std::make_shared<common_to_network_thread_adapter>(std::move(executor));
146
147 return std::make_shared<ThreadPoolBridge>(adapted_pool, BackendType::CommonSystem);
148}
149#endif
150
151} // namespace kcenon::network::integration
BackendType get_backend_type() const
Get the backend type.
VoidResult shutdown() override
Shutdown the bridge.
static std::shared_ptr< ThreadPoolBridge > from_thread_system(const std::string &pool_name="network_pool")
Create bridge from thread_system.
ThreadPoolBridge(std::shared_ptr< thread_pool_interface > pool, BackendType backend_type=BackendType::Custom)
Construct bridge with custom thread pool.
bool is_initialized() const override
Check if the bridge is initialized.
std::shared_ptr< thread_pool_interface > pool_
std::shared_ptr< thread_pool_interface > get_thread_pool() const
Get the underlying thread pool.
BridgeMetrics get_metrics() const override
Get current metrics.
VoidResult initialize(const BridgeConfig &config) override
Initialize the bridge with configuration.
static thread_integration_manager & instance()
Get the singleton instance.
std::shared_ptr< thread_pool_interface > get_thread_pool()
Get the current thread pool.
tracing_config config
Definition exporters.cpp:29
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
VoidResult ok()
Configuration for bridge initialization.
Metrics and health information for a bridge.
std::chrono::steady_clock::time_point last_activity
Timestamp of last activity or health check.
std::map< std::string, double > custom_metrics
Bridge-specific custom metrics.
bool is_healthy
Overall health status of the bridge.
Bidirectional adapters between network_system's thread_pool_interface and common_system's IExecutor/I...
Thread pool integration bridge for network_system.