PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
kcenon::pacs::integration::thread_pool_adapter Class Referencefinal

Concrete implementation of thread_pool_interface. More...

#include <thread_pool_adapter.h>

Inheritance diagram for kcenon::pacs::integration::thread_pool_adapter:
Inheritance graph
Collaboration diagram for kcenon::pacs::integration::thread_pool_adapter:
Collaboration graph

Public Member Functions

 thread_pool_adapter (const thread_pool_config &config)
 Construct adapter with configuration.
 
 thread_pool_adapter (std::shared_ptr< kcenon::thread::thread_pool > pool)
 Construct adapter with an existing thread pool.
 
 ~thread_pool_adapter () override
 Destructor.
 
 thread_pool_adapter (const thread_pool_adapter &)=delete
 
thread_pool_adapteroperator= (const thread_pool_adapter &)=delete
 
 thread_pool_adapter (thread_pool_adapter &&)=delete
 
thread_pool_adapteroperator= (thread_pool_adapter &&)=delete
 
auto start () -> bool override
 Start the thread pool.
 
auto is_running () const noexcept -> bool override
 Check if the thread pool is running.
 
void shutdown (bool wait_for_completion=true) override
 Shutdown the thread pool.
 
auto submit (std::function< void()> task) -> std::future< void > override
 Submit a task for execution.
 
auto submit_with_priority (job_priority priority, std::function< void()> task) -> std::future< void > override
 Submit a task with a specific priority level.
 
void submit_fire_and_forget (std::function< void()> task) override
 Submit a task without waiting for completion.
 
auto get_thread_count () const -> std::size_t override
 Get the current number of worker threads.
 
auto get_pending_task_count () const -> std::size_t override
 Get the number of pending tasks in the queue.
 
auto get_idle_worker_count () const -> std::size_t override
 Get the number of idle workers.
 
auto get_underlying_pool () const -> std::shared_ptr< kcenon::thread::thread_pool >
 Get the underlying thread pool.
 
auto get_config () const noexcept -> const thread_pool_config &
 Get the current configuration.
 
- Public Member Functions inherited from kcenon::pacs::integration::thread_pool_interface
virtual ~thread_pool_interface ()=default
 Virtual destructor for proper polymorphic destruction.
 

Private Member Functions

void submit_internal (std::function< void()> task, job_priority priority)
 Internal task submission with priority.
 

Private Attributes

std::shared_ptr< kcenon::thread::thread_pool > pool_
 
thread_pool_config config_
 
std::mutex mutex_
 
bool initialized_ {false}
 

Additional Inherited Members

- Protected Member Functions inherited from kcenon::pacs::integration::thread_pool_interface
 thread_pool_interface ()=default
 Protected default constructor for derived classes.
 
 thread_pool_interface (const thread_pool_interface &)=delete
 Non-copyable.
 
thread_pool_interfaceoperator= (const thread_pool_interface &)=delete
 
 thread_pool_interface (thread_pool_interface &&)=default
 Movable.
 
thread_pool_interfaceoperator= (thread_pool_interface &&)=default
 

Detailed Description

Concrete implementation of thread_pool_interface.

This class adapts kcenon::thread::thread_pool to the thread_pool_interface, enabling dependency injection for thread pool operations in PACS components.

Key features:

  • Lock-free Job Queue: Uses thread_system's lock-free queue
  • Priority Scheduling: Critical operations get priority
  • Automatic Scaling: Thread pool grows/shrinks based on workload
  • Injectable: Can be passed to components via constructor

Thread Safety: All public methods are thread-safe.

Definition at line 65 of file thread_pool_adapter.h.

Constructor & Destructor Documentation

◆ thread_pool_adapter() [1/4]

kcenon::pacs::integration::thread_pool_adapter::thread_pool_adapter ( const thread_pool_config & config)
explicit

Construct adapter with configuration.

Creates a new thread pool with the specified configuration. The pool is not started until start() is called.

Parameters
configConfiguration options for the thread pool
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/integration/thread_pool_adapter.h.

Definition at line 28 of file thread_pool_adapter.cpp.

29 : config_(config) {
30 // Validate configuration
31 if (config_.min_threads == 0) {
33 }
36 }
37
38 // Create the pool
39 kcenon::thread::thread_context context;
40 pool_ = std::make_shared<kcenon::thread::thread_pool>(config_.pool_name, context);
41}
std::shared_ptr< kcenon::thread::thread_pool > pool_
std::string pool_name
Thread pool name for logging.
std::size_t min_threads
Minimum number of worker threads.
std::size_t max_threads
Maximum number of worker threads.

References config_, if(), kcenon::pacs::integration::thread_pool_config::max_threads, kcenon::pacs::integration::thread_pool_config::min_threads, pool_, and kcenon::pacs::integration::thread_pool_config::pool_name.

Here is the call graph for this function:

◆ thread_pool_adapter() [2/4]

kcenon::pacs::integration::thread_pool_adapter::thread_pool_adapter ( std::shared_ptr< kcenon::thread::thread_pool > pool)
explicit

Construct adapter with an existing thread pool.

Wraps an existing thread pool instance. This is useful for sharing a pool across multiple components or for testing.

Parameters
poolExisting thread pool to wrap (must not be null)

Definition at line 43 of file thread_pool_adapter.cpp.

45 : pool_(std::move(pool)), initialized_(true) {
46 if (!pool_) {
47 throw std::invalid_argument("Thread pool cannot be null");
48 }
49}

References pool_.

◆ ~thread_pool_adapter()

kcenon::pacs::integration::thread_pool_adapter::~thread_pool_adapter ( )
override

Destructor.

Shuts down the thread pool if it's still running.

Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/integration/thread_pool_adapter.h.

Definition at line 51 of file thread_pool_adapter.cpp.

51 {
52 shutdown(true);
53}
void shutdown(bool wait_for_completion=true) override
Shutdown the thread pool.

References shutdown().

Here is the call graph for this function:

◆ thread_pool_adapter() [3/4]

kcenon::pacs::integration::thread_pool_adapter::thread_pool_adapter ( const thread_pool_adapter & )
delete

◆ thread_pool_adapter() [4/4]

kcenon::pacs::integration::thread_pool_adapter::thread_pool_adapter ( thread_pool_adapter && )
delete

Member Function Documentation

◆ get_config()

auto kcenon::pacs::integration::thread_pool_adapter::get_config ( ) const -> const thread_pool_config&
nodiscardnoexcept

Get the current configuration.

Returns
Current thread pool configuration
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/integration/thread_pool_adapter.h.

Definition at line 225 of file thread_pool_adapter.cpp.

225 {
226 return config_;
227}

References config_.

◆ get_idle_worker_count()

auto kcenon::pacs::integration::thread_pool_adapter::get_idle_worker_count ( ) const -> std::size_t
nodiscardoverridevirtual

Get the number of idle workers.

Returns
Number of workers not currently processing tasks

Implements kcenon::pacs::integration::thread_pool_interface.

Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/integration/thread_pool_adapter.h.

Definition at line 210 of file thread_pool_adapter.cpp.

210 {
211 std::lock_guard<std::mutex> lock(mutex_);
212 return pool_ ? pool_->get_idle_worker_count() : 0;
213}

References mutex_, and pool_.

◆ get_pending_task_count()

auto kcenon::pacs::integration::thread_pool_adapter::get_pending_task_count ( ) const -> std::size_t
nodiscardoverridevirtual

Get the number of pending tasks in the queue.

Returns
Number of tasks waiting to be processed

Implements kcenon::pacs::integration::thread_pool_interface.

Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/integration/thread_pool_adapter.h.

Definition at line 205 of file thread_pool_adapter.cpp.

205 {
206 std::lock_guard<std::mutex> lock(mutex_);
207 return pool_ ? pool_->get_pending_task_count() : 0;
208}

References mutex_, and pool_.

◆ get_thread_count()

auto kcenon::pacs::integration::thread_pool_adapter::get_thread_count ( ) const -> std::size_t
nodiscardoverridevirtual

Get the current number of worker threads.

Returns
Number of active worker threads

Implements kcenon::pacs::integration::thread_pool_interface.

Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/integration/thread_pool_adapter.h.

Definition at line 198 of file thread_pool_adapter.cpp.

198 {
199 std::lock_guard<std::mutex> lock(mutex_);
200 // Return configured thread count if pool is initialized
201 // This reflects the expected worker count rather than transient active state
202 return initialized_ ? config_.min_threads : 0;
203}

References config_, initialized_, kcenon::pacs::integration::thread_pool_config::min_threads, and mutex_.

◆ get_underlying_pool()

auto kcenon::pacs::integration::thread_pool_adapter::get_underlying_pool ( ) const -> std::shared_ptr<kcenon::thread::thread_pool>
nodiscard

Get the underlying thread pool.

Provides access to the underlying kcenon::thread::thread_pool for advanced use cases. Use with caution.

Returns
Shared pointer to the underlying thread pool
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/integration/thread_pool_adapter.h.

Definition at line 219 of file thread_pool_adapter.cpp.

220 {
221 std::lock_guard<std::mutex> lock(mutex_);
222 return pool_;
223}

References mutex_, and pool_.

◆ is_running()

auto kcenon::pacs::integration::thread_pool_adapter::is_running ( ) const -> bool
nodiscardoverridevirtualnoexcept

Check if the thread pool is running.

Returns
true if the pool is active and accepting tasks

Implements kcenon::pacs::integration::thread_pool_interface.

Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/integration/thread_pool_adapter.h.

Definition at line 97 of file thread_pool_adapter.cpp.

97 {
98 std::lock_guard<std::mutex> lock(mutex_);
99 return pool_ && pool_->is_running();
100}

References mutex_, and pool_.

Referenced by submit_internal().

Here is the caller graph for this function:

◆ operator=() [1/2]

thread_pool_adapter & kcenon::pacs::integration::thread_pool_adapter::operator= ( const thread_pool_adapter & )
delete

◆ operator=() [2/2]

thread_pool_adapter & kcenon::pacs::integration::thread_pool_adapter::operator= ( thread_pool_adapter && )
delete

◆ shutdown()

void kcenon::pacs::integration::thread_pool_adapter::shutdown ( bool wait_for_completion = true)
overridevirtual

Shutdown the thread pool.

Stops accepting new tasks and optionally waits for pending tasks.

Parameters
wait_for_completionIf true, waits for all pending tasks to complete

Implements kcenon::pacs::integration::thread_pool_interface.

Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/integration/thread_pool_adapter.h.

Definition at line 102 of file thread_pool_adapter.cpp.

102 {
103 std::lock_guard<std::mutex> lock(mutex_);
104
105 if (pool_) {
106 pool_->stop(!wait_for_completion);
107 }
108
109 initialized_ = false;
110}

References initialized_, mutex_, and pool_.

Referenced by ~thread_pool_adapter().

Here is the caller graph for this function:

◆ start()

auto kcenon::pacs::integration::thread_pool_adapter::start ( ) -> bool
nodiscardoverridevirtual

Start the thread pool.

Initializes worker threads and begins accepting tasks. Safe to call multiple times; subsequent calls are no-ops if running.

Returns
true if started successfully or already running, false otherwise

Implements kcenon::pacs::integration::thread_pool_interface.

Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/integration/thread_pool_adapter.h.

Definition at line 59 of file thread_pool_adapter.cpp.

59 {
60 std::lock_guard<std::mutex> lock(mutex_);
61
62 if (initialized_ && pool_ && pool_->is_running()) {
63 return true; // Already running
64 }
65
66 // Create pool if not exists
67 if (!pool_) {
68 kcenon::thread::thread_context context;
69 pool_ = std::make_shared<kcenon::thread::thread_pool>(config_.pool_name, context);
70 }
71
72 // Create worker threads
73 std::vector<std::unique_ptr<kcenon::thread::thread_worker>> workers;
74 workers.reserve(config_.min_threads);
75
76 kcenon::thread::thread_context context;
77 for (std::size_t i = 0; i < config_.min_threads; ++i) {
78 workers.push_back(std::make_unique<kcenon::thread::thread_worker>(false, context));
79 }
80
81 // Enqueue workers
82 auto enqueue_result = pool_->enqueue_batch(std::move(workers));
83 if (enqueue_result.is_err()) {
84 return false;
85 }
86
87 // Start the pool
88 auto start_result = pool_->start();
89 if (start_result.is_err()) {
90 return false;
91 }
92
93 initialized_ = true;
94 return true;
95}

Referenced by submit_internal().

Here is the caller graph for this function:

◆ submit()

auto kcenon::pacs::integration::thread_pool_adapter::submit ( std::function< void()> task) -> std::future< void >
nodiscardoverridevirtual

Submit a task for execution.

Submits a void-returning task to the thread pool for asynchronous execution with normal priority.

Parameters
taskThe task to execute (must be callable with no arguments)
Returns
Future that completes when the task finishes
Exceptions
std::runtime_errorif pool is not running

Implements kcenon::pacs::integration::thread_pool_interface.

Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/integration/thread_pool_adapter.h.

Definition at line 116 of file thread_pool_adapter.cpp.

117 {
118 auto promise = std::make_shared<std::promise<void>>();
119 auto future = promise->get_future();
120
122 [task = std::move(task), promise]() mutable {
123 try {
124 task();
125 promise->set_value();
126 } catch (...) {
127 promise->set_exception(std::current_exception());
128 }
129 },
131
132 return future;
133}
void submit_internal(std::function< void()> task, job_priority priority)
Internal task submission with priority.

References kcenon::pacs::integration::normal.

◆ submit_fire_and_forget()

void kcenon::pacs::integration::thread_pool_adapter::submit_fire_and_forget ( std::function< void()> task)
overridevirtual

Submit a task without waiting for completion.

Fire-and-forget submission for tasks where the result is not needed. Errors are logged but not propagated.

Parameters
taskThe task to execute

Implements kcenon::pacs::integration::thread_pool_interface.

Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/integration/thread_pool_adapter.h.

Definition at line 155 of file thread_pool_adapter.cpp.

155 {
156 submit_internal(std::move(task), job_priority::low);
157}
@ low
Background tasks (cleanup, maintenance)

References kcenon::pacs::integration::low, and submit_internal().

Here is the call graph for this function:

◆ submit_internal()

void kcenon::pacs::integration::thread_pool_adapter::submit_internal ( std::function< void()> task,
job_priority priority )
private

Internal task submission with priority.

Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/integration/thread_pool_adapter.h.

Definition at line 159 of file thread_pool_adapter.cpp.

161 {
162 // Ensure pool is started
163 if (!is_running()) {
164 if (!start()) {
165 throw std::runtime_error("Failed to start thread pool");
166 }
167 }
168
169 std::lock_guard<std::mutex> lock(mutex_);
170 if (pool_) {
171 // Use job_builder for modern API (replaces deprecated future_job)
172 // Note: Priority is not currently used as the base thread_pool
173 // doesn't support it. For future implementation, consider using
174 // typed_thread_pool or a priority queue wrapper.
175 try {
176 auto job = kcenon::thread::job_builder()
177 .name("pacs_pool_task")
178 .work([task = std::move(task)]() -> kcenon::common::VoidResult {
179 task();
180 return kcenon::common::ok();
181 })
182 .build();
183
184 auto result = pool_->enqueue(std::move(job));
185 if (result.is_err()) {
186 throw std::runtime_error("Failed to enqueue task to thread pool");
187 }
188 } catch (const std::exception&) {
189 throw std::runtime_error("Failed to enqueue task to thread pool");
190 }
191 }
192}
auto start() -> bool override
Start the thread pool.
auto is_running() const noexcept -> bool override
Check if the thread pool is running.

References is_running(), mutex_, pool_, and start().

Referenced by submit_fire_and_forget().

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

◆ submit_with_priority()

auto kcenon::pacs::integration::thread_pool_adapter::submit_with_priority ( job_priority priority,
std::function< void()> task ) -> std::future< void >
nodiscardoverridevirtual

Submit a task with a specific priority level.

Jobs with higher priority (lower enum value) are processed first.

Parameters
priorityThe priority level for this task
taskThe task to execute
Returns
Future that completes when the task finishes
Exceptions
std::runtime_errorif pool is not running

Implements kcenon::pacs::integration::thread_pool_interface.

Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/integration/thread_pool_adapter.h.

Definition at line 135 of file thread_pool_adapter.cpp.

137 {
138 auto promise = std::make_shared<std::promise<void>>();
139 auto future = promise->get_future();
140
142 [task = std::move(task), promise]() mutable {
143 try {
144 task();
145 promise->set_value();
146 } catch (...) {
147 promise->set_exception(std::current_exception());
148 }
149 },
150 priority);
151
152 return future;
153}
constexpr dicom_tag priority
Priority.

Member Data Documentation

◆ config_

thread_pool_config kcenon::pacs::integration::thread_pool_adapter::config_
private

◆ initialized_

bool kcenon::pacs::integration::thread_pool_adapter::initialized_ {false}
private

◆ mutex_

◆ pool_

std::shared_ptr<kcenon::thread::thread_pool> kcenon::pacs::integration::thread_pool_adapter::pool_
private

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