Debian Codegraph @

storage/innobase/include/ut0mpmcbq.h

raw
1 /*****************************************************************************
2 Copyright (c) 2017, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25 *****************************************************************************/
26
27 #ifndef ut0mpmcbq_h
28 #define ut0mpmcbq_h
29
30 #include "ut0cpu_cache.h"
31
32 #include <atomic>
33
34 /** Multiple producer consumer, bounded queue
35 Implementation of Dmitry Vyukov's MPMC algorithm
36 http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue */
37 template <typename T>
38 class mpmc_bq {
39 public:
40 /** Constructor
41 @param[in] n_elems Max number of elements allowed */
42 explicit mpmc_bq(size_t n_elems)
43 : m_ring(reinterpret_cast<Cell *>(ut::new_arr_withkey<Aligned>(
44 UT_NEW_THIS_FILE_PSI_KEY, ut::Count{n_elems}))),
45 m_capacity(n_elems - 1) {
46 /* Should be a power of 2 */
47 ut_a((n_elems >= 2) && ((n_elems & (n_elems - 1)) == 0));
48
49 for (size_t i = 0; i < n_elems; ++i) {
50 m_ring[i].m_pos.store(i, std::memory_order_relaxed);
51 }
52
53 m_enqueue_pos.store(0, std::memory_order_relaxed);
54 m_dequeue_pos.store(0, std::memory_order_relaxed);
55 }
56
57 /** Destructor */
58 ~mpmc_bq() { ut::delete_arr(m_ring); }
59
60 /** Enqueue an element
61 @param[in] data Element to insert, it will be copied
62 @return true on success */
63 [[nodiscard]] bool enqueue(T const &data) {
64 /* m_enqueue_pos only wraps at MAX(m_enqueue_pos), instead
65 we use the capacity to convert the sequence to an array
66 index. This is why the ring buffer must be a size which
67 is a power of 2. This also allows the sequence to double
68 as a ticket/lock. */
69
70 size_t pos = m_enqueue_pos.load(std::memory_order_relaxed);
71
72 Cell *cell;
73
74 for (;;) {
75 cell = &m_ring[pos & m_capacity];
76
77 size_t seq;
78
79 seq = cell->m_pos.load(std::memory_order_acquire);
80
81 intptr_t diff = (intptr_t)seq - (intptr_t)pos;
82
83 /* If they are the same then it means this cell is empty */
84
85 if (diff == 0) {
86 /* Claim our spot by moving head. If head isn't the same as we last
87 checked then that means someone beat us to the punch. Weak compare is
88 faster, but can return spurious results which in this instance is OK,
89 because it's in the loop */
90
91 if (m_enqueue_pos.compare_exchange_weak(pos, pos + 1,
92 std::memory_order_relaxed)) {
93 break;
94 }
95
96 } else if (diff < 0) {
97 /* The queue is full */
98
99 return (false);
100
101 } else {
102 pos = m_enqueue_pos.load(std::memory_order_relaxed);
103 }
104 }
105
106 cell->m_data = data;
107
108 /* Increment the sequence so that the tail knows it's accessible */
109
110 cell->m_pos.store(pos + 1, std::memory_order_release);
111
112 return (true);
113 }
114
115 /** Dequeue an element
116 @param[out] data Element read from the queue
117 @return true on success */
118 [[nodiscard]] bool dequeue(T &data) {
119 Cell *cell;
120 size_t pos = m_dequeue_pos.load(std::memory_order_relaxed);
121
122 for (;;) {
123 cell = &m_ring[pos & m_capacity];
124
125 size_t seq = cell->m_pos.load(std::memory_order_acquire);
126
127 auto diff = (intptr_t)seq - (intptr_t)(pos + 1);
128
129 if (diff == 0) {
130 /* Claim our spot by moving the head. If head isn't the same as we last
131 checked then that means someone beat us to the punch. Weak compare is
132 faster, but can return spurious results. Which in this instance is
133 OK, because it's in the loop. */
134
135 if (m_dequeue_pos.compare_exchange_weak(pos, pos + 1,
136 std::memory_order_relaxed)) {
137 break;
138 }
139
140 } else if (diff < 0) {
141 /* The queue is empty */
142 return (false);
143
144 } else {
145 /* Under normal circumstances this branch should never be taken. */
146 pos = m_dequeue_pos.load(std::memory_order_relaxed);
147 }
148 }
149
150 data = cell->m_data;
151
152 /* Set the sequence to what the head sequence should be next
153 time around */
154
155 cell->m_pos.store(pos + m_capacity + 1, std::memory_order_release);
156
157 return (true);
158 }
159
160 /** @return the capacity of the queue */
161 [[nodiscard]] size_t capacity() const { return (m_capacity + 1); }
162
163 /** @return true if the queue is empty. */
164 [[nodiscard]] bool empty() const {
165 size_t pos = m_dequeue_pos.load(std::memory_order_relaxed);
166
167 for (;;) {
168 auto cell = &m_ring[pos & m_capacity];
169
170 size_t seq = cell->m_pos.load(std::memory_order_acquire);
171
172 auto diff = (intptr_t)seq - (intptr_t)(pos + 1);
173
174 if (diff == 0) {
175 return (false);
176 } else if (diff < 0) {
177 return (true);
178 } else {
179 pos = m_dequeue_pos.load(std::memory_order_relaxed);
180 }
181 }
182 }
183
184 private:
185 using Pad = byte[ut::INNODB_CACHE_LINE_SIZE];
186
187 struct Cell {
188 std::atomic<size_t> m_pos;
189 T m_data;
190 };
191
192 using Aligned =
193 typename std::aligned_storage<sizeof(Cell),
194 std::alignment_of<Cell>::value>::type;
195
196 Pad m_pad0;
197 Cell *const m_ring;
198 size_t const m_capacity;
199 Pad m_pad1;
200 std::atomic<size_t> m_enqueue_pos;
201 Pad m_pad2;
202 std::atomic<size_t> m_dequeue_pos;
203 Pad m_pad3;
204
205 mpmc_bq(mpmc_bq &&) = delete;
206 mpmc_bq(const mpmc_bq &) = delete;
207 mpmc_bq &operator=(mpmc_bq &&) = delete;
208 mpmc_bq &operator=(const mpmc_bq &) = delete;
209 };
210
211 #endif /* ut0mpmcbq_h */