Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
database::integrated::adapters::thread_adapter Class Reference

Thread pool adapter for async database operations. More...

#include <thread_adapter.h>

Collaboration diagram for database::integrated::adapters::thread_adapter:
Collaboration graph

Public Member Functions

 thread_adapter (const db_thread_config &config, thread_backend_type backend_type=thread_backend_type::auto_select)
 Construct thread adapter with configuration.
 
 ~thread_adapter ()
 Destructor - ensures graceful shutdown.
 
 thread_adapter (const thread_adapter &)=delete
 
thread_adapteroperator= (const thread_adapter &)=delete
 
 thread_adapter (thread_adapter &&) noexcept
 
thread_adapteroperator= (thread_adapter &&)=delete
 
common::VoidResult initialize ()
 Initialize thread pool.
 
common::VoidResult shutdown ()
 Shutdown thread pool gracefully.
 
bool is_initialized () const
 Check if thread pool is initialized.
 
common::VoidResult execute (std::function< void()> task)
 Execute a task (fire-and-forget)
 
template<typename F , typename... Args>
requires concepts::SubmittableTask<F, Args...>
auto submit (F &&f, Args &&... args) -> std::future< std::invoke_result_t< F, Args... > >
 Submit a task and get a future.
 
void wait_for_completion ()
 Wait for all pending tasks to complete.
 
bool wait_for_completion_timeout (std::chrono::milliseconds timeout)
 Wait for tasks with timeout.
 
std::size_t worker_count () const
 Get number of worker threads.
 
std::size_t queue_size () const
 Get current queue size.
 
bool is_idle () const
 Check if thread pool is idle.
 

Static Private Member Functions

static std::unique_ptr< backends::thread_backendcreate_backend (const db_thread_config &config, thread_backend_type backend_type)
 Create appropriate backend based on type.
 

Private Attributes

const db_thread_configconfig_
 
std::unique_ptr< backends::thread_backendbackend_
 Thread backend implementation.
 

Detailed Description

Thread pool adapter for async database operations.

Provides unified async execution with runtime backend selection. No longer uses conditional compilation - backend is selected at runtime.

Thread Safety: All methods are thread-safe Exception Safety: Strong guarantee - failed operations don't affect pool state

Examples
/home/runner/work/database_system/database_system/database/integrated/core/database_coordinator.h.

Definition at line 98 of file thread_adapter.h.

Constructor & Destructor Documentation

◆ thread_adapter() [1/3]

database::integrated::adapters::thread_adapter::thread_adapter ( const db_thread_config & config,
thread_backend_type backend_type = thread_backend_type::auto_select )
explicit

Construct thread adapter with configuration.

Parameters
configThread pool configuration
backend_typeBackend type to use (default: auto_select)
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/thread_adapter.h.

Definition at line 43 of file thread_adapter.cpp.

46 : config_(config)
48{
49}
std::unique_ptr< backends::thread_backend > backend_
Thread backend implementation.
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.
backend_type
Database backend type enumeration.

◆ ~thread_adapter()

database::integrated::adapters::thread_adapter::~thread_adapter ( )

Destructor - ensures graceful shutdown.

Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/thread_adapter.h.

Definition at line 51 of file thread_adapter.cpp.

52{
53 if (backend_ && backend_->is_initialized())
54 {
55 backend_->shutdown();
56 }
57}

References backend_.

◆ thread_adapter() [2/3]

database::integrated::adapters::thread_adapter::thread_adapter ( const thread_adapter & )
delete

◆ thread_adapter() [3/3]

database::integrated::adapters::thread_adapter::thread_adapter ( thread_adapter && )
defaultnoexcept

Member Function Documentation

◆ create_backend()

std::unique_ptr< backends::thread_backend > database::integrated::adapters::thread_adapter::create_backend ( const db_thread_config & config,
thread_backend_type backend_type )
staticprivate

Create appropriate backend based on type.

Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/thread_adapter.h.

Definition at line 21 of file thread_adapter.cpp.

24{
25 switch (backend_type)
26 {
29 return std::make_unique<backends::fallback_thread_backend>(config);
30
32 return std::make_unique<backends::null_thread_backend>(config);
33
34 default:
35 return std::make_unique<backends::fallback_thread_backend>(config);
36 }
37}
@ null
Synchronous execution (no threading)
@ auto_select
Automatically select best available backend.

References database::integrated::adapters::auto_select, database::integrated::adapters::fallback, and database::integrated::adapters::null.

◆ execute()

common::VoidResult database::integrated::adapters::thread_adapter::execute ( std::function< void()> task)

Execute a task (fire-and-forget)

Parameters
taskTask to execute
Returns
Ok on successful submission
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/thread_adapter.h.

Definition at line 91 of file thread_adapter.cpp.

92{
93 if (!backend_)
94 {
95 return common::VoidResult(
96 common::error_info{ -1, "Backend not initialized", "" });
97 }
98
99 return backend_->execute(std::move(task));
100}
Result< std::monostate > VoidResult

References backend_.

Referenced by submit().

Here is the caller graph for this function:

◆ initialize()

common::VoidResult database::integrated::adapters::thread_adapter::initialize ( )

Initialize thread pool.

Returns
Ok on success, error otherwise
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/thread_adapter.h.

Definition at line 61 of file thread_adapter.cpp.

62{
63 if (!backend_)
64 {
65 return common::VoidResult(
66 common::error_info{ -1, "Backend not created", "" });
67 }
68
69 return backend_->initialize();
70}

References initialize().

Referenced by initialize(), test_api_availability_priority(), test_api_availability_submit(), test_destructor_safety(), test_double_initialization(), and test_move_semantics().

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

◆ is_idle()

bool database::integrated::adapters::thread_adapter::is_idle ( ) const

Check if thread pool is idle.

Returns
true if no tasks are running or pending
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/thread_adapter.h.

Definition at line 148 of file thread_adapter.cpp.

149{
150 if (!backend_)
151 {
152 return true;
153 }
154
155 return backend_->is_idle();
156}

References backend_.

◆ is_initialized()

bool database::integrated::adapters::thread_adapter::is_initialized ( ) const

Check if thread pool is initialized.

Returns
true if initialized and ready to accept tasks
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/thread_adapter.h.

Definition at line 82 of file thread_adapter.cpp.

83{
84 return backend_ && backend_->is_initialized();
85}

References backend_.

◆ operator=() [1/2]

thread_adapter & database::integrated::adapters::thread_adapter::operator= ( const thread_adapter & )
delete

◆ operator=() [2/2]

thread_adapter & database::integrated::adapters::thread_adapter::operator= ( thread_adapter && )
delete

◆ queue_size()

std::size_t database::integrated::adapters::thread_adapter::queue_size ( ) const

Get current queue size.

Returns
Number of pending tasks
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/thread_adapter.h.

Definition at line 138 of file thread_adapter.cpp.

139{
140 if (!backend_)
141 {
142 return 0;
143 }
144
145 return backend_->queue_size();
146}

References backend_.

Referenced by test_api_availability_stats().

Here is the caller graph for this function:

◆ shutdown()

common::VoidResult database::integrated::adapters::thread_adapter::shutdown ( )

Shutdown thread pool gracefully.

Waits for all pending tasks to complete before shutting down. After shutdown, no new tasks can be submitted.

Returns
Ok on success
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/thread_adapter.h.

Definition at line 72 of file thread_adapter.cpp.

73{
74 if (!backend_)
75 {
76 return common::ok();
77 }
78
79 return backend_->shutdown();
80}
VoidResult ok()

References backend_, and common::ok().

Referenced by test_api_availability_priority(), test_api_availability_submit(), test_double_initialization(), test_move_semantics(), and test_shutdown_without_init().

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

◆ submit()

template<typename F , typename... Args>
requires concepts::SubmittableTask<F, Args...>
auto database::integrated::adapters::thread_adapter::submit ( F && f,
Args &&... args ) -> std::future<std::invoke_result_t<F, Args...>>

Submit a task and get a future.

Template Parameters
FFunction type - constrained by SubmittableTask concept
ArgsArgument types
Parameters
fFunction to execute
argsArguments to pass to function
Returns
Future containing the result

Uses SubmittableTask concept for compile-time validation:

  • Ensures F is invocable with Args...
  • Ensures F is move-constructible for async storage
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/thread_adapter.h.

Definition at line 227 of file thread_adapter.h.

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}
common::VoidResult execute(std::function< void()> task)
Execute a task (fire-and-forget)

References execute().

Referenced by test_api_availability_priority(), test_api_availability_submit(), and test_destructor_safety().

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

◆ wait_for_completion()

void database::integrated::adapters::thread_adapter::wait_for_completion ( )

Wait for all pending tasks to complete.

Blocks until all submitted tasks finish execution.

Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/thread_adapter.h.

Definition at line 106 of file thread_adapter.cpp.

107{
108 if (backend_)
109 {
110 backend_->wait_for_completion();
111 }
112}

References backend_.

◆ wait_for_completion_timeout()

bool database::integrated::adapters::thread_adapter::wait_for_completion_timeout ( std::chrono::milliseconds timeout)

Wait for tasks with timeout.

Parameters
timeoutMaximum time to wait
Returns
true if all tasks completed, false if timeout
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/thread_adapter.h.

Definition at line 114 of file thread_adapter.cpp.

115{
116 if (!backend_)
117 {
118 return true;
119 }
120
121 return backend_->wait_for_completion_timeout(timeout);
122}

References backend_, and database::timeout.

◆ worker_count()

std::size_t database::integrated::adapters::thread_adapter::worker_count ( ) const

Get number of worker threads.

Returns
Thread count
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/thread_adapter.h.

Definition at line 128 of file thread_adapter.cpp.

129{
130 if (!backend_)
131 {
132 return 0;
133 }
134
135 return backend_->worker_count();
136}

References backend_.

Referenced by test_api_availability_stats().

Here is the caller graph for this function:

Member Data Documentation

◆ backend_

std::unique_ptr<backends::thread_backend> database::integrated::adapters::thread_adapter::backend_
private

◆ config_

const db_thread_config& database::integrated::adapters::thread_adapter::config_
private

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