Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
thread_system_integration.cpp
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
13
14#include <thread>
15
16#ifdef LOGGER_HAS_THREAD_SYSTEM
17
18#include <kcenon/thread/core/callback_job.h>
19
21
22// Static member initialization
23std::atomic<async_backend_type> thread_system_integration::current_backend_{
25};
26std::shared_ptr<kcenon::thread::thread_pool> thread_system_integration::thread_pool_{nullptr};
27std::mutex thread_system_integration::pool_mutex_{};
28
29void thread_system_integration::enable(std::shared_ptr<kcenon::thread::thread_pool> pool) {
30 std::lock_guard<std::mutex> lock(pool_mutex_);
31
32 if (pool) {
33 thread_pool_ = std::move(pool);
34 } else if (!thread_pool_) {
35 // Create default pool if none provided and none exists
36 thread_pool_ = create_default_pool();
37 }
38
39 current_backend_.store(async_backend_type::thread_pool, std::memory_order_release);
40}
41
43 std::lock_guard<std::mutex> lock(pool_mutex_);
44
45 current_backend_.store(async_backend_type::standalone, std::memory_order_release);
46 // Note: We don't stop the pool here - let it be managed by its owner
47 // Just release our reference
48 thread_pool_.reset();
49}
50
52 return current_backend_.load(std::memory_order_acquire) == async_backend_type::thread_pool;
53}
54
56 return current_backend_.load(std::memory_order_acquire);
57}
58
59void thread_system_integration::set_thread_pool(
60 std::shared_ptr<kcenon::thread::thread_pool> pool) {
61 std::lock_guard<std::mutex> lock(pool_mutex_);
62
63 if (pool) {
64 thread_pool_ = std::move(pool);
65 current_backend_.store(async_backend_type::thread_pool, std::memory_order_release);
66 } else {
67 thread_pool_.reset();
68 current_backend_.store(async_backend_type::standalone, std::memory_order_release);
69 }
70}
71
72std::shared_ptr<kcenon::thread::thread_pool> thread_system_integration::get_thread_pool() noexcept {
73 std::lock_guard<std::mutex> lock(pool_mutex_);
74 return thread_pool_;
75}
76
77bool thread_system_integration::submit_task(std::function<void()> task) {
78 if (!is_enabled()) {
79 return false;
80 }
81
82 std::shared_ptr<kcenon::thread::thread_pool> pool;
83 {
84 std::lock_guard<std::mutex> lock(pool_mutex_);
85 pool = thread_pool_;
86 }
87
88 if (!pool || !pool->is_running()) {
89 return false;
90 }
91
92 // Create a callback_job that wraps the task
93 auto job = std::make_unique<kcenon::thread::callback_job>(
94 [task = std::move(task)]() -> kcenon::common::VoidResult {
95 task();
96 return kcenon::common::ok();
97 },
98 "logger_async_task"
99 );
100
101 auto result = pool->enqueue(std::move(job));
102 return result.is_ok();
103}
104
105std::string thread_system_integration::get_backend_name() noexcept {
106 return is_enabled() ? "thread_pool" : "standalone";
107}
108
109std::shared_ptr<kcenon::thread::thread_pool> thread_system_integration::create_default_pool() {
110 auto pool = std::make_shared<kcenon::thread::thread_pool>(
111 "logger_async_pool"
112 );
113
114 // Start the pool
115 auto result = pool->start();
116 if (!result.is_ok()) {
117 // Failed to start pool - return nullptr to signal failure
118 return nullptr;
119 }
120
121 return pool;
122}
123
124} // namespace kcenon::logger::integration
125
126#endif // LOGGER_HAS_THREAD_SYSTEM
static std::string get_backend_name() noexcept
Always returns "standalone" when thread_system is not available.
static void disable() noexcept
No-op when thread_system is not available.
static void enable() noexcept
No-op when thread_system is not available.
static constexpr async_backend_type get_backend() noexcept
Always returns standalone when thread_system is not available.
static constexpr bool is_enabled() noexcept
Always returns false when thread_system is not available.
static bool submit_task(std::function< void()>) noexcept
Always returns false when thread_system is not available.
VoidResult ok()
async_backend_type
Backend type enumeration for async processing.
@ standalone
Standalone backend using std::jthread.
@ thread_pool
Thread pool backend using thread_system.
Optional thread_system integration for advanced async processing.