Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
thread_adapter.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
51#pragma once
52
54#include "../../core/concepts.h"
55
56#include <chrono>
57#include <cstddef>
58#include <functional>
59#include <future>
60#include <memory>
61#include <type_traits>
62
63// Use common Result pattern from shared header
65
66// Forward declare backend interface
68{
69 class thread_backend;
70}
71
72namespace database
73{
74namespace integrated
75{
76namespace adapters
77{
78
83{
85 fallback,
86 null
87};
88
99{
100public:
106 explicit thread_adapter(
107 const db_thread_config& config,
109
114
115 // Non-copyable
118
119 // Move constructor only (const reference member prevents move assignment)
121 thread_adapter& operator=(thread_adapter&&) = delete;
122
128
138
143 bool is_initialized() const;
144
145 // ═══════════════════════════════════════════════════════════════
146 // Task Execution
147 // ═══════════════════════════════════════════════════════════════
148
154 common::VoidResult execute(std::function<void()> task);
155
169 template <typename F, typename... Args>
170 requires concepts::SubmittableTask<F, Args...>
171 auto submit(F&& f, Args&&... args) -> std::future<std::invoke_result_t<F, Args...>>;
172
173 // ═══════════════════════════════════════════════════════════════
174 // Work Completion & Statistics
175 // ═══════════════════════════════════════════════════════════════
176
182 void wait_for_completion();
183
189 bool wait_for_completion_timeout(std::chrono::milliseconds timeout);
190
195 std::size_t worker_count() const;
196
201 std::size_t queue_size() const;
202
207 bool is_idle() const;
208
209private:
213 static std::unique_ptr<backends::thread_backend> create_backend(
214 const db_thread_config& config,
216
218 std::unique_ptr<backends::thread_backend> backend_;
219};
220
221// ═══════════════════════════════════════════════════════════════
222// Template Implementations
223// ═══════════════════════════════════════════════════════════════
224
225template <typename F, typename... Args>
226 requires concepts::SubmittableTask<F, Args...>
227auto thread_adapter::submit(F&& f, Args&&... args) -> std::future<std::invoke_result_t<F, Args...>>
228{
229 using return_type = std::invoke_result_t<F, Args...>;
230
231 auto task = std::make_shared<std::packaged_task<return_type()>>(
232 std::bind(std::forward<F>(f), std::forward<Args>(args)...));
233
234 auto result = task->get_future();
235
236 execute([task = std::move(task)]() { (*task)(); });
237
238 return result;
239}
240
241} // namespace adapters
242} // namespace integrated
243} // namespace database
Thread pool adapter for async database operations.
auto submit(F &&f, Args &&... args) -> std::future< std::invoke_result_t< F, Args... > >
Submit a task and get a future.
common::VoidResult shutdown()
Shutdown thread pool gracefully.
bool wait_for_completion_timeout(std::chrono::milliseconds timeout)
Wait for tasks with timeout.
common::VoidResult initialize()
Initialize thread pool.
thread_adapter(const thread_adapter &)=delete
void wait_for_completion()
Wait for all pending tasks to complete.
std::unique_ptr< backends::thread_backend > backend_
Thread backend implementation.
~thread_adapter()
Destructor - ensures graceful shutdown.
std::size_t queue_size() const
Get current queue size.
thread_adapter(const db_thread_config &config, thread_backend_type backend_type=thread_backend_type::auto_select)
Construct thread adapter with configuration.
std::size_t worker_count() const
Get number of worker threads.
bool is_initialized() const
Check if thread pool is initialized.
common::VoidResult execute(std::function< void()> task)
Execute a task (fire-and-forget)
static std::unique_ptr< backends::thread_backend > create_backend(const db_thread_config &config, thread_backend_type backend_type)
Create appropriate backend based on type.
thread_adapter & operator=(const thread_adapter &)=delete
bool is_idle() const
Check if thread pool is idle.
thread_adapter(thread_adapter &&) noexcept
Common Result<T> pattern for integrated database system.
C++20 concepts for database_system type validation.
Unified configuration for integrated database system.
thread_backend_type
Thread backend type selection.
@ auto_select
Automatically select best available backend.
@ null
No-op backend (discard all logs)
@ auto_select
Automatically select best available backend.
backend_type
Database backend type enumeration.
kcenon::common::VoidResult VoidResult
Primary VoidResult type - use this for void operations.
Definition result.h:33
Thread pool configuration for async operations.