Debian Codegraph @

unittest/gunit/containers/integrals_lockfree_queue-t.cc

raw symbols SCIP
1 /* Copyright (c) 2020, 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
25 #include "sql/containers/atomics_array_index_interleaved.h"
26 #include "sql/containers/integrals_lockfree_queue.h"
27
28 #include <gmock/gmock.h>
29 #include <gtest/gtest.h>
30
31 namespace containers {
32 namespace lf {
33 namespace unittests {
34
35 static std::atomic<int> pushed{0};
36 static std::atomic<int> popped{0};
37 static std::atomic<int> removed{0};
38
39 class Integrals_lockfree_queue_test : public ::testing::Test {
40 public:
41 using value_type = long;
42
43 static constexpr int Threads = 8;
44 static constexpr value_type Null = -1;
45 static constexpr value_type Erased = -2;
46 static constexpr value_type Workload = 32;
47
48 template <typename Queue>
49 static void test_queue(Queue &queue) {
50 for (value_type idx = 0; idx != Workload + 1; ++idx) {
51 queue.push(idx);
52 }
53 EXPECT_EQ(queue.array().to_string(),
54 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, "
55 "18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, EOF");
56 EXPECT_EQ(queue.to_string(),
57 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, "
58 "18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, EOF");
59 EXPECT_EQ(queue.array().find(31), 31);
60 EXPECT_EQ(queue.is_full(), true);
61 EXPECT_EQ(queue.front(), 0);
62 EXPECT_EQ(queue.back(), 31);
63 EXPECT_EQ(queue.head(), 0);
64 EXPECT_EQ(queue.tail(), 32);
65 EXPECT_EQ(queue.get_state(),
66 Queue::enum_queue_state::
67 NO_SPACE_AVAILABLE); // Pushing one more item, above the
68 // capacity, makes the queue state to
69 // report no more space
70
71 for (value_type idx = 0; idx != Workload / 2; ++idx) {
72 queue.pop();
73 }
74
75 for (value_type idx = 0;
76 queue.get_state() != Queue::enum_queue_state::NO_SPACE_AVAILABLE;
77 ++idx) {
78 queue.push(idx);
79 }
80 EXPECT_EQ(queue.head(), 16);
81 EXPECT_EQ(queue.tail(), 48);
82 EXPECT_EQ(queue.array().to_string(),
83 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, "
84 "18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, EOF");
85 EXPECT_EQ(queue.to_string(),
86 "16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, "
87 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, EOF");
88 EXPECT_EQ(queue.array().find(31), 31);
89
90 queue.clear();
91 queue.pop();
92 EXPECT_EQ(
93 queue.get_state(),
94 Queue::enum_queue_state::NO_MORE_ELEMENTS); // Poping on more item than
95 // the ones available maked
96 // the queue state to
97 // report no more elements
98
99 pushed = 0;
100 popped = 0;
101 removed = 0;
102
103 size_t total_threads{Threads * 4};
104 std::vector<std::thread> threads;
105
106 for (size_t idx = 0; idx != total_threads; ++idx) {
107 threads.emplace_back(
108 [&](int n_thread) -> void {
109 switch (n_thread % 4) {
110 case 0: { // Producer threads
111 for (size_t k = 0; k != Workload;) {
112 value_type value = n_thread * Workload + k;
113 queue << value; // Using `push` or `<<` is the same
114 if (queue.get_state() == Queue::enum_queue_state::SUCCESS) {
115 ++pushed;
116 ++k;
117 }
118 std::this_thread::yield();
119 }
120 break;
121 }
122 case 1: { // Remover thread
123 for (; popped.load() + removed.load() != Workload * Threads;) {
124 int n_success =
125 queue.erase_if([=](value_type item) mutable -> bool {
126 return item >= ((n_thread - 1) * Workload) &&
127 item < (n_thread * Workload);
128 });
129 if (n_success > 0) {
130 removed += n_success;
131 }
132 std::this_thread::yield();
133 }
134 break;
135 }
136 case 2: { // Finder thread
137 size_t start{0};
138 for (; popped.load() + removed.load() != Workload * Threads;) {
139 value_type found{Null};
140 std::tie(found, start) = queue.array().find_if(
141 [=](value_type item, size_t) mutable -> bool {
142 return item >= ((n_thread - 2) * Workload) &&
143 item < ((n_thread - 1) * Workload);
144 },
145 start);
146 if (start == queue.capacity()) {
147 start = 0;
148 }
149 std::this_thread::yield();
150 }
151 break;
152 }
153 case 3: { // Consumer thread
154 for (; popped.load() + removed.load() != Workload * Threads;) {
155 value_type value{Null};
156 queue >> value; // Using `pop` or `>>` is the same
157 if (queue.get_state() == Queue::enum_queue_state::SUCCESS) {
158 ++popped;
159 }
160 std::this_thread::yield();
161 }
162 }
163 }
164 },
165 idx);
166 }
167
168 for (size_t idx = 0; idx != total_threads; ++idx) {
169 threads[idx].join();
170 }
171
172 EXPECT_EQ(pushed.load(), Workload * Threads);
173 EXPECT_EQ(popped.load() + removed.load(), pushed.load());
174 }
175
176 protected:
177 Integrals_lockfree_queue_test() = default;
178 virtual void SetUp() {}
179 virtual void TearDown() {}
180 };
181
182 TEST_F(Integrals_lockfree_queue_test, Padding_indexing_test) {
183 size_t size = Workload;
184 container::Integrals_lockfree_queue<Integrals_lockfree_queue_test::value_type,
185 Null, Erased>
186 queue{size};
187 EXPECT_EQ(queue.capacity(), size);
188 EXPECT_EQ(queue.allocated_size(),
189 queue.capacity() * memory::cache_line_size());
190 Integrals_lockfree_queue_test::test_queue(queue);
191 }
192
193 TEST_F(Integrals_lockfree_queue_test, Interleaved_indexing_test) {
194 size_t size = Workload;
195 container::Integrals_lockfree_queue<
196 Integrals_lockfree_queue_test::value_type, Null, Erased,
197 container::Interleaved_indexing<
198 Integrals_lockfree_queue_test::value_type>>
199 queue{size};
200 EXPECT_EQ(queue.capacity(), size);
201 EXPECT_EQ(queue.allocated_size(),
202 queue.capacity() *
203 sizeof(std::atomic<Integrals_lockfree_queue_test::value_type>));
204 Integrals_lockfree_queue_test::test_queue(queue);
205 }
206
207 } // namespace unittests
208 } // namespace lf
209 } // namespace containers