Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
fallback_thread_backend.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
19#pragma once
20
21#include "thread_backend.h"
22
23#include <atomic>
24#include <condition_variable>
25#include <mutex>
26#include <queue>
27#include <thread>
28#include <vector>
29
30namespace database
31{
32namespace integrated
33{
34namespace adapters
35{
36namespace backends
37{
38
47{
48public:
49 explicit fallback_thread_backend(const db_thread_config& config);
50 ~fallback_thread_backend() override;
51
52 // Non-copyable, non-movable
57
60 bool is_initialized() const override;
61
62 common::VoidResult execute(std::function<void()> task) override;
63 void wait_for_completion() override;
64 bool wait_for_completion_timeout(std::chrono::milliseconds timeout) override;
65
66 std::size_t worker_count() const override;
67 std::size_t queue_size() const override;
68 bool is_idle() const override;
69
70private:
74 void worker_thread();
75
78 std::atomic<bool> shutdown_requested_;
79
80 std::vector<std::thread> workers_;
81 std::queue<std::function<void()>> task_queue_;
82
83 mutable std::mutex queue_mutex_;
84 std::condition_variable queue_cv_;
85
86 std::atomic<std::size_t> active_tasks_;
87 mutable std::mutex completion_mutex_;
88 std::condition_variable completion_cv_;
89};
90
91} // namespace backends
92} // namespace adapters
93} // namespace integrated
94} // namespace database
common::VoidResult execute(std::function< void()> task) override
Execute a task (fire-and-forget)
std::size_t queue_size() const override
Get current queue size.
fallback_thread_backend & operator=(const fallback_thread_backend &)=delete
std::size_t worker_count() const override
Get number of worker threads.
bool is_initialized() const override
Check if backend is initialized.
fallback_thread_backend & operator=(fallback_thread_backend &&)=delete
common::VoidResult initialize() override
Initialize the thread backend.
common::VoidResult shutdown() override
Shutdown the thread backend gracefully.
fallback_thread_backend(const fallback_thread_backend &)=delete
bool wait_for_completion_timeout(std::chrono::milliseconds timeout) override
Wait for completion with timeout.
void wait_for_completion() override
Wait for all pending tasks to complete.
Abstract base class for thread pool backends.
Thread pool configuration for async operations.
Abstract interface for thread pool backends.