Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
queue_factory.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
13#pragma once
14
21
22#include <memory>
23#include <type_traits>
24
25namespace kcenon::thread {
26
65public:
72 struct requirements {
73 bool need_exact_size = false;
74 bool need_atomic_empty = false;
75 bool prefer_lock_free = false;
76 bool need_batch_operations = false;
77 bool need_blocking_wait = false;
78 };
79
80 // ============================================
81 // Convenience factory methods
82 // ============================================
83
100 [[nodiscard]] static auto create_standard_queue() -> std::shared_ptr<job_queue> {
101 return std::make_shared<job_queue>();
102 }
103
120 [[nodiscard]] static auto create_adaptive_queue(
122 -> std::unique_ptr<adaptive_job_queue>
123 {
124 return std::make_unique<adaptive_job_queue>(policy);
125 }
126
127 // ============================================
128 // Requirements-based factory method
129 // ============================================
130
142 [[nodiscard]] static auto create_for_requirements(const requirements& reqs)
143 -> std::unique_ptr<scheduler_interface>;
144
145 // ============================================
146 // Environment-based auto-selection
147 // ============================================
148
163 [[nodiscard]] static auto create_optimal() -> std::unique_ptr<scheduler_interface>;
164
165 // ============================================
166 // Policy-based queue factory methods
167 // ============================================
168
185 [[nodiscard]] static auto create_policy_queue()
186 -> std::unique_ptr<standard_queue>;
187
201 [[nodiscard]] static auto create_lockfree_policy_queue()
202 -> std::unique_ptr<policy_lockfree_queue>;
203
222 [[nodiscard]] static auto create_bounded_policy_queue(std::size_t max_size)
223 -> std::unique_ptr<policy_queue<
227
248 template<typename SyncPolicy, typename BoundPolicy, typename OverflowPolicy>
249 [[nodiscard]] static auto create_custom_policy_queue(
250 SyncPolicy sync_policy,
251 BoundPolicy bound_policy,
252 OverflowPolicy overflow_policy)
253 -> std::unique_ptr<policy_queue<SyncPolicy, BoundPolicy, OverflowPolicy>>
254 {
255 static_assert(is_sync_policy_v<SyncPolicy>,
256 "SyncPolicy must be a valid sync policy type");
257 static_assert(is_bound_policy_v<BoundPolicy>,
258 "BoundPolicy must be a valid bound policy type");
260 "OverflowPolicy must be a valid overflow policy type");
261
262 return std::make_unique<policy_queue<SyncPolicy, BoundPolicy, OverflowPolicy>>(
263 std::move(sync_policy),
264 std::move(bound_policy),
265 std::move(overflow_policy)
266 );
267 }
268};
269
270// ============================================
271// Compile-time selection utilities
272// ============================================
273
292template<bool NeedExactSize, bool PreferLockFree = false>
294 static_assert(!(NeedExactSize && PreferLockFree),
295 "Cannot require both exact size AND lock-free. These are mutually exclusive. "
296 "Lock-free queues cannot provide exact size due to concurrent modifications.");
297
298 // Both PreferLockFree and default case now use adaptive_job_queue
299 // (lockfree_job_queue is an internal implementation detail)
300 using type = std::conditional_t<
301 NeedExactSize,
302 job_queue,
304 >;
305};
306
312template<bool NeedExactSize, bool PreferLockFree = false>
314
315// ============================================
316// Pre-defined type aliases
317// ============================================
318
321
325
328
329} // namespace kcenon::thread
Adaptive queue that auto-switches between mutex and lock-free modes.
Adaptive queue that switches between mutex and lock-free modes.
@ balanced
Auto-switch based on usage.
A thread-safe job queue for managing and dispatching work items.
Definition job_queue.h:65
Policy that limits queue size to a maximum.
Synchronization policy using mutex and condition variable.
Policy that rejects new items when queue is full.
Policy-based queue template.
Factory for creating queue instances.
static auto create_optimal() -> std::unique_ptr< scheduler_interface >
Create optimal queue for current environment.
static auto create_standard_queue() -> std::shared_ptr< job_queue >
Create standard job_queue.
static auto create_custom_policy_queue(SyncPolicy sync_policy, BoundPolicy bound_policy, OverflowPolicy overflow_policy) -> std::unique_ptr< policy_queue< SyncPolicy, BoundPolicy, OverflowPolicy > >
Create a policy_queue with custom policies.
static auto create_policy_queue() -> std::unique_ptr< standard_queue >
Create a standard policy_queue (mutex-based, unbounded)
static auto create_adaptive_queue(adaptive_job_queue::policy policy=adaptive_job_queue::policy::balanced) -> std::unique_ptr< adaptive_job_queue >
Create adaptive queue (RECOMMENDED for most use cases)
static auto create_bounded_policy_queue(std::size_t max_size) -> std::unique_ptr< policy_queue< policies::mutex_sync_policy, policies::bounded_policy, policies::overflow_reject_policy > >
Create a bounded policy_queue with specified size.
static auto create_lockfree_policy_queue() -> std::unique_ptr< policy_lockfree_queue >
Create a lock-free policy_queue.
static auto create_for_requirements(const requirements &reqs) -> std::unique_ptr< scheduler_interface >
Create queue based on requirements.
Thread-safe FIFO job queue with optional bounded size.
Lock-free MPMC job queue using Michael-Scott algorithm with hazard pointers.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
queue_t< false, false > balanced_queue_t
Queue type for balanced performance (adaptive_job_queue)
queue_t< false, true > fast_queue_t
Queue type for maximum throughput (adaptive_job_queue with performance_first policy)
queue_t< true, false > accurate_queue_t
Queue type for accurate size/empty operations (job_queue)
constexpr bool is_overflow_policy_v
Check if type is an overflow policy.
typename queue_type_selector< NeedExactSize, PreferLockFree >::type queue_t
Convenience alias for queue type selection.
constexpr bool is_bound_policy_v
Check if type is a bound policy.
constexpr bool is_sync_policy_v
Check if type is a sync policy.
Policy-based job queue template with customizable sync, bound, and overflow.
Type traits for detecting queue synchronization policy tags.
Abstract scheduler interface for queuing and retrieving jobs.
bool need_blocking_wait
Require blocking dequeue.
bool prefer_lock_free
Prefer lock-free if possible.
bool need_atomic_empty
Require atomic empty()
bool need_batch_operations
Require batch enqueue/dequeue.
Compile-time queue type selector.
std::conditional_t< NeedExactSize, job_queue, adaptive_job_queue > type