Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
network_context.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
15
16#include <thread>
17#include <mutex>
18
19#if KCENON_WITH_THREAD_SYSTEM
21#endif
22
23namespace kcenon::network::core {
24
25// Use namespace alias for integration types
27
29public:
30 impl() = default;
31
32 std::shared_ptr<integration::thread_pool_interface> thread_pool_;
33 std::shared_ptr<integration::logger_interface> logger_;
34 std::shared_ptr<integration::monitoring_interface> monitoring_;
35
36 bool initialized_ = false;
37 bool owns_thread_pool_ = false;
38 mutable std::mutex mutex_;
39};
40
42 // Intentional Leak pattern: Use no-op deleter to prevent destruction
43 // during static destruction phase. This avoids heap corruption when
44 // io_context_thread_manager tasks still reference the thread pool.
45 // Memory impact: ~few KB (reclaimed by OS on process termination)
46 : pimpl_(new impl(), [](impl*) { /* no-op deleter - intentional leak */ }) {
47}
48
50 // Note: With Intentional Leak pattern, shutdown() is never called
51 // during static destruction. The OS reclaims all resources on exit.
52 // This prevents heap corruption from thread pool destruction order issues.
53}
54
56 static network_context ctx;
57 return ctx;
58}
59
60void network_context::initialize(size_t thread_count) {
61 std::lock_guard<std::mutex> lock(pimpl_->mutex_);
62
63 if (pimpl_->initialized_) {
64 return;
65 }
66
67 // Initialize thread pool if not already set
68 if (!pimpl_->thread_pool_) {
69 if (thread_count == 0) {
70 thread_count = std::thread::hardware_concurrency();
71 if (thread_count == 0) {
72 thread_count = 2; // Fallback
73 }
74 }
75
76#if KCENON_WITH_THREAD_SYSTEM
77 // Use thread_system's thread_pool via adapter
78 try {
79 auto adapter = integration::thread_system_pool_adapter::from_service_or_default("network_pool");
80 if (adapter && adapter->is_running()) {
81 pimpl_->thread_pool_ = adapter;
82 pimpl_->owns_thread_pool_ = false; // Managed by thread_system
83 } else {
84 // Fallback to basic pool if thread_system pool is not available
85 pimpl_->thread_pool_ = std::make_shared<integration::basic_thread_pool>(thread_count);
86 pimpl_->owns_thread_pool_ = true;
87 }
88 } catch (...) {
89 // If thread_system integration fails, use basic pool
90 pimpl_->thread_pool_ = std::make_shared<integration::basic_thread_pool>(thread_count);
91 pimpl_->owns_thread_pool_ = true;
92 }
93#else
94 // Use basic thread pool when thread_system is not available
95 pimpl_->thread_pool_ = std::make_shared<integration::basic_thread_pool>(thread_count);
96 pimpl_->owns_thread_pool_ = true;
97#endif
98 }
99
100 // Initialize logger if not already set
101 // Issue #285: Uses common_system_logger_adapter when available, basic_logger otherwise
102 if (!pimpl_->logger_) {
103#if KCENON_WITH_COMMON_SYSTEM
104 pimpl_->logger_ = std::make_shared<integration::common_system_logger_adapter>();
105#else
106 pimpl_->logger_ = std::make_shared<integration::basic_logger>(
108#endif
110 }
111
112 // Initialize monitoring if not already set
113 // Note: Uses basic_monitoring by default. Metrics are also published via EventBus.
114 if (!pimpl_->monitoring_) {
116 }
117
118 pimpl_->initialized_ = true;
119
120 if (pimpl_->logger_) {
121 pimpl_->logger_->log(
123 "network_context initialized with " + std::to_string(thread_count) + " threads"
124 );
125 }
126}
127
129 std::lock_guard<std::mutex> lock(pimpl_->mutex_);
130
131 if (!pimpl_->initialized_) {
132 return;
133 }
134
135 if (pimpl_->logger_) {
136 pimpl_->logger_->log(integration::log_level::info, "network_context shutting down");
137 }
138
139 if (pimpl_->monitoring_) {
140 pimpl_->monitoring_.reset();
141 }
142
143 // Only stop thread pool if we own it
144 if (pimpl_->owns_thread_pool_ && pimpl_->thread_pool_) {
145 // Try to stop the thread pool if it's a basic_thread_pool
146 if (auto* basic_pool = dynamic_cast<integration::basic_thread_pool*>(pimpl_->thread_pool_.get())) {
147 basic_pool->stop(true);
148 }
149 pimpl_->thread_pool_.reset();
150 }
151
152 // Don't reset logger as it may be used during shutdown logging
153 // Just mark as not initialized
154 pimpl_->initialized_ = false;
155}
156
158 std::lock_guard<std::mutex> lock(pimpl_->mutex_);
159 return pimpl_->initialized_;
160}
161
162void network_context::set_thread_pool(std::shared_ptr<integration::thread_pool_interface> pool) {
163 std::lock_guard<std::mutex> lock(pimpl_->mutex_);
164 pimpl_->thread_pool_ = pool;
165 pimpl_->owns_thread_pool_ = false;
166}
167
168std::shared_ptr<integration::thread_pool_interface> network_context::get_thread_pool() {
169 std::lock_guard<std::mutex> lock(pimpl_->mutex_);
170 return pimpl_->thread_pool_;
171}
172
173void network_context::set_logger(std::shared_ptr<integration::logger_interface> logger) {
174 std::lock_guard<std::mutex> lock(pimpl_->mutex_);
175 pimpl_->logger_ = logger;
177}
178
179std::shared_ptr<integration::logger_interface> network_context::get_logger() {
180 std::lock_guard<std::mutex> lock(pimpl_->mutex_);
181 return pimpl_->logger_;
182}
183
184void network_context::set_monitoring(std::shared_ptr<integration::monitoring_interface> monitoring) {
185 std::lock_guard<std::mutex> lock(pimpl_->mutex_);
186 pimpl_->monitoring_ = monitoring;
187 // Also update the integration manager for backward compatibility
189}
190
191std::shared_ptr<integration::monitoring_interface> network_context::get_monitoring() {
192 std::lock_guard<std::mutex> lock(pimpl_->mutex_);
193 if (!pimpl_->monitoring_) {
194 // Return the default from integration manager
196 }
197 return pimpl_->monitoring_;
198}
199
200} // namespace kcenon::network::core
std::shared_ptr< integration::thread_pool_interface > thread_pool_
std::shared_ptr< integration::monitoring_interface > monitoring_
std::shared_ptr< integration::logger_interface > logger_
Global context for shared network system resources.
Definition core.cppm:252
std::shared_ptr< kcenon::network::integration::monitoring_interface > get_monitoring()
Get current monitoring system.
void set_monitoring(std::shared_ptr< kcenon::network::integration::monitoring_interface > monitoring)
Set custom monitoring system.
std::shared_ptr< impl > pimpl_
PIMPL pointer with intentional leak pattern.
std::shared_ptr< kcenon::network::integration::logger_interface > get_logger()
Get current logger.
void initialize(size_t thread_count=0)
Initialize all systems.
void set_logger(std::shared_ptr< kcenon::network::integration::logger_interface > logger)
Set custom logger.
void set_thread_pool(std::shared_ptr< kcenon::network::integration::thread_pool_interface > pool)
Set custom thread pool.
std::shared_ptr< kcenon::network::integration::thread_pool_interface > get_thread_pool()
Get current thread pool.
static network_context & instance()
Get the singleton instance.
bool is_initialized() const
Check if context is initialized.
Basic thread pool implementation for standalone use.
static logger_integration_manager & instance()
Get the singleton instance.
void set_logger(std::shared_ptr< logger_interface > logger)
Set the logger implementation.
std::shared_ptr< monitoring_interface > get_monitoring()
Get the current monitoring implementation.
static monitoring_integration_manager & instance()
Get the singleton instance.
void set_monitoring(std::shared_ptr< monitoring_interface > monitoring)
Set the monitoring implementation.
Feature flags for network_system.
Global context for shared network system resources.
Adapter that bridges thread_system::thread_pool to thread_pool_interface.