MySQL 26.7.0
Source Code Documentation
locking_queue.h
Go to the documentation of this file.
1// Copyright (c) 2026, Oracle and/or its affiliates.
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of MySQL hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License, version 2.0, for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23
24#ifndef MYSQL_CONCURRENCY_LOCKING_QUEUE
25#define MYSQL_CONCURRENCY_LOCKING_QUEUE
26
27#include <condition_variable>
28#include <mutex>
29#include <queue>
30
34
35/// @addtogroup GroupLibsMysqlConcurrency
36/// @{
37
38namespace mysql::concurrency {
39
40/// @brief Synchronized unbounded FIFO queue for thread-safe producer-consumer
41/// operations, using a single mutex and condition variable.
42/// @details
43/// This queue is unbounded (enqueue always succeeds). For performance, it does
44/// not enforce safe destruction during concurrent access. Ensure the queue
45/// outlives all producers and consumers to avoid undefined behavior.
46template <typename ET>
48 public:
49 using ElemType = ET;
51
52 /// @brief Constructs the queue. The memory_resource is reserved for future
53 /// use (e.g., custom allocation for elements) and currently unused.
54 Locking_queue(Memory_resource memory_resource = {})
57 m_memory_resource(memory_resource) {}
58 // Disable copy-move semantics
63 ~Locking_queue() = default;
64
65 /// @brief Enqueues an element into the queue (called by producers).
66 /// @param elem Element to enqueue (moved).
67 /// @return Always true, as the queue is unbounded and enqueue always
68 /// succeeds.
69 /// @details Acquires the mutex, pushes the element to the back, notifies one
70 /// waiting consumer, then releases the mutex.
71 bool enqueue(ElemType &&elem) {
72 std::lock_guard<concurrency::Mutex> lock(m_mutex_access_queue);
73 m_queue.push(std::move(elem));
75 return true;
76 }
77
78 /// @brief Checks if the queue is empty.
79 /// @details Acquires the mutex for thread-safe access. Due to concurrent
80 /// enqueues/consumes, the result is a snapshot and may change
81 /// immediately after the call.
82 /// @return true if no elements are present at the time of the check.
83 bool empty() const {
84 std::lock_guard<concurrency::Mutex> lock(m_mutex_access_queue);
85 return m_queue.empty();
86 }
87
88 /// @brief Consumes (dequeues) an element from the front of the queue.
89 /// Blocks until an element is available or the stop predicate
90 /// triggers.
91 /// @param pred Stop predicate (callable); if pred() returns true after
92 /// waking, stops waiting and returns without consuming.
93 /// @return std::pair<ElemType, bool>: {consumed element, true} if an element
94 /// was dequeued, or {default-constructed ElemType, false} if stopped by
95 /// pred() without consuming.
96 template <typename P>
97 std::pair<ElemType, bool> dequeue(P &&pred) {
98 std::unique_lock<concurrency::Mutex> lock(m_mutex_access_queue);
99 auto stop_waiting = [this, &pred]() -> bool {
100 return pred() || !m_queue.empty();
101 };
102 m_cv_empty_queue.wait(lock, stop_waiting);
103 if (m_queue.empty()) {
104 assert(stop_waiting());
105 return std::make_pair(ElemType{}, false);
106 }
107 auto elem = m_queue.front();
108 m_queue.pop();
109 return std::make_pair(elem, true);
110 }
111
112 /// @brief Wakes all waiting consumers (e.g., during shutdown to check pred
113 /// and exit). Notifies all on the condition variable, unblocking blocked
114 /// consumes.
116
117 private:
118 /// Underlying unbounded FIFO queue.
119 std::queue<ElemType> m_queue;
120 /// Mutex protecting all queue operations.
122 /// Condition variable for waiting on non-empty queue.
124 /// Reserved for future custom allocation; currently unused.
126};
127
128} // namespace mysql::concurrency
129
130/// @}
131
132#endif // MYSQL_CONCURRENCY_LOCKING_QUEUE
Polymorphism-free memory resource class with custom allocator and deallocator functions.
Definition: memory_resource.h:88
MySQL wrapper for a condition variable, using mysql_cond_t as implementation of a condition variable ...
Definition: condition_variable_wrapper.h:37
void notify_one()
Notify one waiting thread.
Definition: condition_variable_wrapper.cpp:38
void notify_all()
Notify all waiting threads.
Definition: condition_variable_wrapper.cpp:40
void wait(std::unique_lock< Mutex_wrapper > &lock, Predicate pred)
Wait until notified or predicate is false.
Definition: condition_variable_wrapper_impl.hpp:29
Synchronized unbounded FIFO queue for thread-safe producer-consumer operations, using a single mutex ...
Definition: locking_queue.h:47
bool enqueue(ElemType &&elem)
Enqueues an element into the queue (called by producers).
Definition: locking_queue.h:71
Locking_queue & operator=(const Locking_queue< ElemType > &src)=delete
Locking_queue(Memory_resource memory_resource={})
Constructs the queue.
Definition: locking_queue.h:54
concurrency::Mutex m_mutex_access_queue
Mutex protecting all queue operations.
Definition: locking_queue.h:121
void notify_all()
Wakes all waiting consumers (e.g., during shutdown to check pred and exit).
Definition: locking_queue.h:115
Memory_resource m_memory_resource
Reserved for future custom allocation; currently unused.
Definition: locking_queue.h:125
Locking_queue(Locking_queue< ElemType > &&)=delete
ET ElemType
Definition: locking_queue.h:49
std::queue< ElemType > m_queue
Underlying unbounded FIFO queue.
Definition: locking_queue.h:119
Locking_queue(const Locking_queue< ElemType > &)=delete
std::pair< ElemType, bool > dequeue(P &&pred)
Consumes (dequeues) an element from the front of the queue.
Definition: locking_queue.h:97
bool empty() const
Checks if the queue is empty.
Definition: locking_queue.h:83
Locking_queue & operator=(Locking_queue< ElemType > &&src)=delete
concurrency::Condition_variable m_cv_empty_queue
Condition variable for waiting on non-empty queue.
Definition: locking_queue.h:123
MySQL wrapper for a mutex, template which may be specialized with a specific implementation of a mute...
Definition: mutex_wrapper.h:38
#define MYSQL_CONCURRENCY_DEFINE_CV_PSI_KEY(key)
Definition: condition_variable_srv.h:34
#define P
Definition: dtoa.cc:620
Class that wraps resources in a polymorphic manner.
#define MYSQL_CONCURRENCY_DEFINE_MT_PSI_KEY(key)
Definition: mutex_srv.h:35
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
Definition: cache_line_size.h:31
static std::mutex lock
Definition: net_ns.cc:56