Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::thread_context Class Reference

Context object that provides access to optional services. More...

#include <thread_context.h>

Collaboration diagram for kcenon::thread::thread_context:
Collaboration graph

Public Member Functions

 thread_context ()
 Default constructor - resolves services from global registry.
 
 thread_context (std::shared_ptr< ILogger > logger, std::shared_ptr< IMonitor > monitoring=nullptr)
 Constructor with explicit service injection.
 
std::shared_ptr< ILoggerlogger () const
 Get the logger service.
 
std::shared_ptr< IMonitormonitoring () const
 Get the monitoring service.
 
void log (common::interfaces::log_level level, const std::string &message) const
 Log a message if logger is available.
 
void log (log_level_v2 level, const std::string &message) const
 Log a message if logger is available (v2 API with conversion)
 
void log (common::interfaces::log_level level, std::string_view message, const common::source_location &loc=common::source_location::current()) const
 Log a message with source location if logger is available.
 
void update_system_metrics (const common::interfaces::system_metrics &metrics) const
 Update system metrics if monitoring is available.
 
void update_thread_pool_metrics (const common::interfaces::thread_pool_metrics &metrics) const
 Update thread pool metrics if monitoring is available.
 
void update_thread_pool_metrics (const std::string &pool_name, std::uint32_t pool_instance_id, const common::interfaces::thread_pool_metrics &metrics) const
 Update thread pool metrics with pool identifier.
 
void update_worker_metrics (std::size_t worker_id, const common::interfaces::worker_metrics &metrics) const
 Update worker metrics if monitoring is available.
 
thread_context create_child () const
 Create a child context with the same services.
 
auto get_context_name () const -> std::string
 Get context name.
 
auto set_context_name (const std::string &name) -> bool
 Set context name.
 
auto has_logger () const -> bool
 Check if logger is available.
 
auto has_monitoring () const -> bool
 Check if monitoring is available.
 

Static Private Member Functions

static common::interfaces::log_level to_common_level (log_level_v2 level)
 Convert log_level_v2 to common::interfaces::log_level.
 

Private Attributes

std::shared_ptr< ILoggerlogger_
 
std::shared_ptr< IMonitormonitoring_
 
std::string context_name_
 

Detailed Description

Context object that provides access to optional services.

This class uses composition to provide thread system components with optional access to logger and monitoring services.

Note
Issue #261: Migrated to use common_system's ILogger interface.
Issue #312: Migrated to use common_system's IMonitor interface. The context now uses GlobalLoggerRegistry for default logger resolution.
Examples
composition_example.cpp, integration_example.cpp, minimal_thread_pool.cpp, and multi_process_monitoring_integration.cpp.

Definition at line 40 of file thread_context.h.

Constructor & Destructor Documentation

◆ thread_context() [1/2]

kcenon::thread::thread_context::thread_context ( )
inline

Default constructor - resolves services from global registry.

Definition at line 45 of file thread_context.h.

46 : logger_(common::interfaces::GlobalLoggerRegistry::instance().get_default_logger())
47 , monitoring_(service_container::global().resolve<common::interfaces::IMonitor>()) {
48 }
static service_container & global()
Get the global service container instance.
std::shared_ptr< ILogger > logger_
std::shared_ptr< IMonitor > monitoring_

Referenced by create_child().

Here is the caller graph for this function:

◆ thread_context() [2/2]

kcenon::thread::thread_context::thread_context ( std::shared_ptr< ILogger > logger,
std::shared_ptr< IMonitor > monitoring = nullptr )
inlineexplicit

Constructor with explicit service injection.

Parameters
loggerOptional logger service (ILogger from common_system)
monitoringOptional monitoring service (IMonitor from common_system)

Definition at line 55 of file thread_context.h.

58 : logger_(std::move(logger))
59 , monitoring_(std::move(monitoring)) {
60 }
std::shared_ptr< ILogger > logger() const
Get the logger service.
std::shared_ptr< IMonitor > monitoring() const
Get the monitoring service.

Member Function Documentation

◆ create_child()

thread_context kcenon::thread::thread_context::create_child ( ) const
inline

Create a child context with the same services.

Returns
New context with shared services

Definition at line 244 of file thread_context.h.

244 {
246 }
thread_context()
Default constructor - resolves services from global registry.

References logger_, monitoring_, and thread_context().

Here is the call graph for this function:

◆ get_context_name()

auto kcenon::thread::thread_context::get_context_name ( ) const -> std::string
inline

Get context name.

Returns
Context name

Definition at line 252 of file thread_context.h.

252 {
253 return context_name_;
254 }

References context_name_.

◆ has_logger()

auto kcenon::thread::thread_context::has_logger ( ) const -> bool
inline

Check if logger is available.

Returns
True if logger is set

Definition at line 270 of file thread_context.h.

270 {
271 return logger_ != nullptr;
272 }

References logger_.

◆ has_monitoring()

auto kcenon::thread::thread_context::has_monitoring ( ) const -> bool
inline

Check if monitoring is available.

Returns
True if monitoring is set

Definition at line 278 of file thread_context.h.

278 {
279 return monitoring_ != nullptr;
280 }

References monitoring_.

◆ log() [1/3]

void kcenon::thread::thread_context::log ( common::interfaces::log_level level,
const std::string & message ) const
inline

Log a message if logger is available.

Parameters
levelLog level (common::interfaces::log_level)
messageLog message
Note
Issue #261: Now uses common_system's ILogger interface.
Issue #295: Skips logging during static destruction to prevent SDOF.

Definition at line 86 of file thread_context.h.

86 {
87 // Skip logging during static destruction to prevent SDOF
89 return;
90 }
91 if (logger_) {
92 logger_->log(level, message);
93 }
94 }
static bool is_shutting_down()
Check if shutdown is in progress.

References kcenon::thread::thread_logger::is_shutting_down(), and logger_.

Here is the call graph for this function:

◆ log() [2/3]

void kcenon::thread::thread_context::log ( common::interfaces::log_level level,
std::string_view message,
const common::source_location & loc = common::source_location::current() ) const
inline

Log a message with source location if logger is available.

Parameters
levelLog level (common::interfaces::log_level)
messageLog message
locSource location (automatically captured)
Note
Issue #261: Now uses common_system's ILogger with source_location support.
Issue #295: Skips logging during static destruction to prevent SDOF.

Definition at line 123 of file thread_context.h.

125 {
126 // Skip logging during static destruction to prevent SDOF
128 return;
129 }
130 if (logger_) {
131 logger_->log(level, message, loc);
132 }
133 }

References kcenon::thread::thread_logger::is_shutting_down(), and logger_.

Here is the call graph for this function:

◆ log() [3/3]

void kcenon::thread::thread_context::log ( log_level_v2 level,
const std::string & message ) const
inline

Log a message if logger is available (v2 API with conversion)

Parameters
levelLog level (log_level_v2 with ascending order)
messageLog message
Note
Provides backward compatibility for code using log_level_v2.
Issue #295: Skips logging during static destruction to prevent SDOF.

Definition at line 104 of file thread_context.h.

104 {
105 // Skip logging during static destruction to prevent SDOF
107 return;
108 }
109 if (logger_) {
110 logger_->log(to_common_level(level), message);
111 }
112 }
static common::interfaces::log_level to_common_level(log_level_v2 level)
Convert log_level_v2 to common::interfaces::log_level.

References kcenon::thread::thread_logger::is_shutting_down(), logger_, and to_common_level().

Here is the call graph for this function:

◆ logger()

std::shared_ptr< ILogger > kcenon::thread::thread_context::logger ( ) const
inline

Get the logger service.

Returns
Logger service or nullptr if not available

Definition at line 66 of file thread_context.h.

66 {
67 return logger_;
68 }

References logger_.

◆ monitoring()

std::shared_ptr< IMonitor > kcenon::thread::thread_context::monitoring ( ) const
inline

Get the monitoring service.

Returns
Monitoring service or nullptr if not available

Definition at line 74 of file thread_context.h.

74 {
75 return monitoring_;
76 }

References monitoring_.

Referenced by kcenon::thread::thread_pool::report_metrics(), kcenon::thread::thread_pool::thread_pool(), kcenon::thread::thread_pool::thread_pool(), and kcenon::thread::thread_pool::thread_pool().

Here is the caller graph for this function:

◆ set_context_name()

auto kcenon::thread::thread_context::set_context_name ( const std::string & name) -> bool
inline

Set context name.

Parameters
nameNew context name
Returns
True if successful

Definition at line 261 of file thread_context.h.

261 {
262 context_name_ = name;
263 return true;
264 }

References context_name_.

◆ to_common_level()

static common::interfaces::log_level kcenon::thread::thread_context::to_common_level ( log_level_v2 level)
inlinestaticprivate

Convert log_level_v2 to common::interfaces::log_level.

Definition at line 290 of file thread_context.h.

290 {
291 switch (level) {
292 case log_level_v2::trace: return common::interfaces::log_level::trace;
293 case log_level_v2::debug: return common::interfaces::log_level::debug;
294 case log_level_v2::info: return common::interfaces::log_level::info;
295 case log_level_v2::warn: return common::interfaces::log_level::warning;
296 case log_level_v2::error: return common::interfaces::log_level::error;
297 case log_level_v2::critical: return common::interfaces::log_level::critical;
298 case log_level_v2::off: return common::interfaces::log_level::off;
299 default: return common::interfaces::log_level::info;
300 }
301 }
@ trace
Finest-grained informational events.
@ warn
Potentially harmful situations.
@ off
Special level to disable logging.
@ critical
Severe error events that lead to termination.
@ debug
Fine-grained informational events for debugging.
@ info
Informational messages highlighting progress.
@ error
Error events that might still allow continuation.

References kcenon::thread::critical, kcenon::thread::debug, kcenon::thread::error, kcenon::thread::info, kcenon::thread::off, kcenon::thread::trace, and kcenon::thread::warn.

Referenced by log().

Here is the caller graph for this function:

◆ update_system_metrics()

void kcenon::thread::thread_context::update_system_metrics ( const common::interfaces::system_metrics & metrics) const
inline

Update system metrics if monitoring is available.

Parameters
metricsSystem metrics to record
Note
Issue #295: Skips monitoring during static destruction to prevent SDOF.
Issue #312: Now uses common::interfaces::IMonitor::record_metric().

Definition at line 142 of file thread_context.h.

142 {
143 // Skip monitoring during static destruction to prevent SDOF
145 return;
146 }
147 if (monitoring_) {
148 std::unordered_map<std::string, std::string> tags{{"component", "system"}};
149 monitoring_->record_metric("cpu_usage_percent", metrics.cpu_usage_percent.value, tags);
150 monitoring_->record_metric("memory_usage_bytes", metrics.memory_usage_bytes.value, tags);
151 monitoring_->record_metric("active_threads", metrics.active_threads.value, tags);
152 monitoring_->record_metric("total_allocations", metrics.total_allocations.value, tags);
153 }
154 }

References kcenon::thread::thread_logger::is_shutting_down(), and monitoring_.

Here is the call graph for this function:

◆ update_thread_pool_metrics() [1/2]

void kcenon::thread::thread_context::update_thread_pool_metrics ( const common::interfaces::thread_pool_metrics & metrics) const
inline

Update thread pool metrics if monitoring is available.

Parameters
metricsThread pool metrics to record
Note
Issue #295: Skips monitoring during static destruction to prevent SDOF.
Issue #312: Now uses common::interfaces::IMonitor::record_metric().

Definition at line 163 of file thread_context.h.

163 {
164 // Skip monitoring during static destruction to prevent SDOF
166 return;
167 }
168 if (monitoring_) {
169 std::unordered_map<std::string, std::string> tags{
170 {"component", "thread_pool"},
171 {"pool_name", metrics.pool_name},
172 {"pool_instance_id", std::to_string(metrics.pool_instance_id)}
173 };
174 monitoring_->record_metric("jobs_completed", metrics.jobs_completed.value, tags);
175 monitoring_->record_metric("jobs_pending", metrics.jobs_pending.value, tags);
176 monitoring_->record_metric("worker_threads", metrics.worker_threads.value, tags);
177 monitoring_->record_metric("idle_threads", metrics.idle_threads.value, tags);
178 monitoring_->record_metric("average_latency_ns", metrics.average_latency_ns.value, tags);
179 monitoring_->record_metric("total_execution_time_ns", metrics.total_execution_time_ns.value, tags);
180 }
181 }

References kcenon::thread::thread_logger::is_shutting_down(), and monitoring_.

Referenced by kcenon::thread::thread_pool::report_metrics().

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

◆ update_thread_pool_metrics() [2/2]

void kcenon::thread::thread_context::update_thread_pool_metrics ( const std::string & pool_name,
std::uint32_t pool_instance_id,
const common::interfaces::thread_pool_metrics & metrics ) const
inline

Update thread pool metrics with pool identifier.

Parameters
pool_nameName of the thread pool
pool_instance_idInstance ID for multiple pools
metricsThread pool metrics to record
Note
Issue #295: Skips monitoring during static destruction to prevent SDOF.
Issue #312: Now uses common::interfaces::IMonitor::record_metric().

Definition at line 192 of file thread_context.h.

194 {
195 // Skip monitoring during static destruction to prevent SDOF
197 return;
198 }
199 if (monitoring_) {
200 std::unordered_map<std::string, std::string> tags{
201 {"component", "thread_pool"},
202 {"pool_name", pool_name},
203 {"pool_instance_id", std::to_string(pool_instance_id)}
204 };
205 monitoring_->record_metric("jobs_completed", metrics.jobs_completed.value, tags);
206 monitoring_->record_metric("jobs_pending", metrics.jobs_pending.value, tags);
207 monitoring_->record_metric("worker_threads", metrics.worker_threads.value, tags);
208 monitoring_->record_metric("idle_threads", metrics.idle_threads.value, tags);
209 monitoring_->record_metric("average_latency_ns", metrics.average_latency_ns.value, tags);
210 monitoring_->record_metric("total_execution_time_ns", metrics.total_execution_time_ns.value, tags);
211 }
212 }

References kcenon::thread::thread_logger::is_shutting_down(), and monitoring_.

Here is the call graph for this function:

◆ update_worker_metrics()

void kcenon::thread::thread_context::update_worker_metrics ( std::size_t worker_id,
const common::interfaces::worker_metrics & metrics ) const
inline

Update worker metrics if monitoring is available.

Parameters
worker_idWorker identifier
metricsWorker metrics to record
Note
Issue #295: Skips monitoring during static destruction to prevent SDOF.
Issue #312: Now uses common::interfaces::IMonitor::record_metric().

Definition at line 222 of file thread_context.h.

223 {
224 // Skip monitoring during static destruction to prevent SDOF
226 return;
227 }
228 if (monitoring_) {
229 std::unordered_map<std::string, std::string> tags{
230 {"component", "worker"},
231 {"worker_id", std::to_string(worker_id)}
232 };
233 monitoring_->record_metric("jobs_processed", metrics.jobs_processed.value, tags);
234 monitoring_->record_metric("total_processing_time_ns", metrics.total_processing_time_ns.value, tags);
235 monitoring_->record_metric("idle_time_ns", metrics.idle_time_ns.value, tags);
236 monitoring_->record_metric("context_switches", metrics.context_switches.value, tags);
237 }
238 }

References kcenon::thread::thread_logger::is_shutting_down(), and monitoring_.

Here is the call graph for this function:

Member Data Documentation

◆ context_name_

std::string kcenon::thread::thread_context::context_name_
mutableprivate

Definition at line 285 of file thread_context.h.

Referenced by get_context_name(), and set_context_name().

◆ logger_

std::shared_ptr<ILogger> kcenon::thread::thread_context::logger_
private

Definition at line 283 of file thread_context.h.

Referenced by create_child(), has_logger(), log(), log(), log(), and logger().

◆ monitoring_

std::shared_ptr<IMonitor> kcenon::thread::thread_context::monitoring_
private

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