Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
service_registration.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
15#pragma once
16
17#include <memory>
18#include <thread>
19
20#ifdef BUILD_WITH_COMMON_SYSTEM
21
22#include <kcenon/common/di/service_container.h>
23#include <kcenon/common/interfaces/executor_interface.h>
24#include <kcenon/common/interfaces/logger_interface.h>
25
27#include "../core/thread_pool.h"
28
29namespace kcenon::thread::di {
30
34struct executor_registration_config {
36 size_t worker_count = 0;
37
39 common::di::service_lifetime lifetime = common::di::service_lifetime::singleton;
40};
41
65 });
66 * @endcode
67 */
68inline common::VoidResult register_executor_services(
69 common::di::IServiceContainer& container,
70 const executor_registration_config& config = {}) {
71
72 // Check if already registered
73 if (container.is_registered<common::interfaces::IExecutor>()) {
74 return common::make_error<std::monostate>(
75 common::di::di_error_codes::already_registered,
76 "IExecutor is already registered",
77 "thread_system::di"
78 );
79 }
80
81 // Determine worker count
82 size_t workers = config.worker_count;
83 if (workers == 0) {
84 workers = std::thread::hardware_concurrency();
85 if (workers == 0) {
86 workers = 4; // fallback default
87 }
88 }
89
90 // Register executor factory
91 return container.register_factory<common::interfaces::IExecutor>(
92 [workers](common::di::IServiceContainer&) -> std::shared_ptr<common::interfaces::IExecutor> {
93 auto pool = std::make_shared<thread_pool>(workers);
94 return std::make_shared<adapters::thread_pool_executor_adapter>(pool);
95 },
96 config.lifetime
97 );
98}
99
118inline common::VoidResult register_executor_instance(
119 common::di::IServiceContainer& container,
120 std::shared_ptr<thread_pool> pool) {
121
122 if (!pool) {
123 return common::make_error<std::monostate>(
124 common::error_codes::INVALID_ARGUMENT,
125 "Cannot register null thread pool instance",
126 "thread_system::di"
127 );
128 }
129
130 auto adapter = std::make_shared<adapters::thread_pool_executor_adapter>(pool);
131 return container.register_instance<common::interfaces::IExecutor>(adapter);
132}
133
140inline common::VoidResult unregister_executor_services(
141 common::di::IServiceContainer& container) {
142
143 return container.unregister<common::interfaces::IExecutor>();
144}
145
146// ============================================================================
147// ILogger Registration (Issue #336)
148// ============================================================================
149
175inline common::VoidResult register_logger_instance(
176 common::di::IServiceContainer& container,
177 std::shared_ptr<common::interfaces::ILogger> logger) {
178
179 if (!logger) {
180 return common::make_error<std::monostate>(
181 common::error_codes::INVALID_ARGUMENT,
182 "Cannot register null logger instance",
183 "thread_system::di"
184 );
185 }
186
187 return container.register_instance<common::interfaces::ILogger>(logger);
188}
189
207inline common::VoidResult register_logger_factory(
208 common::di::IServiceContainer& container,
209 std::function<std::shared_ptr<common::interfaces::ILogger>()> factory,
210 common::di::service_lifetime lifetime = common::di::service_lifetime::singleton) {
211
212 if (!factory) {
213 return common::make_error<std::monostate>(
214 common::error_codes::INVALID_ARGUMENT,
215 "Cannot register null logger factory",
216 "thread_system::di"
217 );
218 }
219
220 return container.register_factory<common::interfaces::ILogger>(
221 [factory = std::move(factory)](common::di::IServiceContainer&) {
222 return factory();
223 },
224 lifetime
225 );
226}
227
234inline bool is_logger_registered(
235 const common::di::IServiceContainer& container) {
236
237 return container.is_registered<common::interfaces::ILogger>();
238}
239
246inline common::VoidResult unregister_logger(
247 common::di::IServiceContainer& container) {
248
249 return container.unregister<common::interfaces::ILogger>();
250}
251
261inline common::VoidResult register_thread_services(
262 common::di::IServiceContainer& container,
263 const executor_registration_config& executor_config = {}) {
264
265 // Register IExecutor
266 auto result = register_executor_services(container, executor_config);
267 if (result.is_err()) {
268 return result;
269 }
270
271 return common::VoidResult::ok({});
272}
273
274} // namespace kcenon::thread::di
275
276#endif // BUILD_WITH_COMMON_SYSTEM
Adapter to bridge thread_system pools with common IExecutor interface.
Core thread pool implementation with work stealing and auto-scaling.