24#ifndef MYSQL_CONCURRENCY_SYNC_BOUNDED_QUEUE
25#define MYSQL_CONCURRENCY_SYNC_BOUNDED_QUEUE
29#include <condition_variable>
82template <
typename T, u
int64_t capacity_tp>
85 static_assert(std::has_single_bit(capacity_tp),
86 "Capacity must be a power of 2");
115 template <
typename P>
138 template <
typename P>
139 [[nodiscard]] std::pair<Element_type, bool>
dequeue(
P &&stop_predicate);
162 [[nodiscard]] std::size_t
size()
const;
177 std::array<detail::Padded_slot<Element_type, Mutex_type, Cv_type>,
Allocator using a Memory_resource to do the allocation.
Definition: allocator.h:52
Polymorphism-free memory resource class with custom allocator and deallocator functions.
Definition: memory_resource.h:88
Bounded concurrent queue supporting multiple producers and consumers.
Definition: sync_bounded_queue.h:84
std::atomic< std::size_t > m_tail
Current tail idx, when at the end, wraps to 0.
Definition: sync_bounded_queue.h:183
void notify_all()
Signal shutdown: Notifies all blocked consumers.
std::size_t get_next_index(std::atomic< std::size_t > ¤t)
Atomically increments the given position (m_head for producers, m_tail for consumers) and returns the...
bool enqueue(Element_type &&element, P &&stop_predicate)
Enqueue (push) an element into the queue.
static constexpr uint64_t m_capacity
Definition: sync_bounded_queue.h:94
Sync_bounded_queue(Memory_resource={})
Construct the queue.
void reset()
Reset the queue: Clears all slots (sets validity=false, elements to default), resets m_head and m_tai...
virtual ~Sync_bounded_queue()=default
Destructor Before deleting the queue, the caller must ensure that no thread is or will be using the q...
std::pair< Element_type, bool > dequeue(P &&stop_predicate)
Dequeue an element from the queue.
bool enqueue(Element_type &&element)
Overload without stop_predicate, using default that always returns false.
std::size_t size() const
Estimates the queue size based on m_head and m_tail positions.
std::condition_variable Cv_type
Definition: sync_bounded_queue.h:93
bool empty()
Check if the queue is empty.
std::atomic< std::size_t > m_head
Current head idx, when at the end, wraps to 0.
Definition: sync_bounded_queue.h:181
std::mutex Mutex_type
Definition: sync_bounded_queue.h:92
std::array< detail::Padded_slot< Element_type, Mutex_type, Cv_type >, capacity_tp > m_slots
Padded slots containing elements and synchronization primitives.
Definition: sync_bounded_queue.h:179
T Element_type
Definition: sync_bounded_queue.h:89
Definition: sync_bounded_queue.h:44
#define P
Definition: dtoa.cc:620
#define T
Definition: jit_executor_value.cc:373
Allocator class that uses a polymorphic Memory_resource to allocate memory.
std::atomic< Type > N
Definition: ut0counter.h:225
Definition: cache_line_size.h:31