| 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 <atomic> |
| 26 | #include <thread> |
| 27 | |
| 28 | #include "sql/locks/shared_spin_lock.h" |
| 29 | |
| 30 | #include <gmock/gmock.h> |
| 31 | #include <gtest/gtest.h> |
| 32 | |
| 33 | namespace lock { |
| 34 | namespace unittests { |
| 35 | |
| 36 | class Shared_spin_lock_test : public ::testing::Test { |
| 37 | protected: |
| 38 | Shared_spin_lock_test() = default; |
| 39 | virtual void SetUp() {} |
| 40 | virtual void TearDown() {} |
| 41 | }; |
| 42 | |
| 43 | TEST_F(Shared_spin_lock_test, Lock_unlock_test) { |
| 44 | lock::Shared_spin_lock lock1; |
| 45 | lock::Shared_spin_lock lock2; |
| 46 | std::atomic_flag t1_sync{true}; |
| 47 | std::atomic_flag t2_sync{true}; |
| 48 | |
| 49 | lock1.try_exclusive(); |
| 50 | lock2.acquire_shared(); |
| 51 | |
| 52 | std::thread s1([&]() -> void { |
| 53 | t1_sync.clear(); // Unblock main thread |
| 54 | |
| 55 | EXPECT_EQ(lock1.acquire_shared().is_shared_acquisition(), |
| 56 | true); // Spin to acquire shared access until main |
| 57 | // thread releases exclusive access |
| 58 | lock1.release_shared(); |
| 59 | |
| 60 | t1_sync.clear(); // Unblock main thread |
| 61 | |
| 62 | EXPECT_EQ(lock2.acquire_exclusive().is_exclusive_acquisition(), |
| 63 | true); // Spin to acquire exclusive access until main |
| 64 | // thread releases shared access |
| 65 | lock2.release_exclusive(); |
| 66 | }); |
| 67 | |
| 68 | while (t1_sync.test_and_set()) std::this_thread::yield(); |
| 69 | lock1.release_exclusive(); |
| 70 | while (t1_sync.test_and_set()) std::this_thread::yield(); |
| 71 | lock1.acquire_exclusive(); |
| 72 | lock2.release_shared(); |
| 73 | |
| 74 | s1.join(); |
| 75 | |
| 76 | lock2.acquire_shared(); |
| 77 | |
| 78 | std::thread t1([&]() -> void { |
| 79 | EXPECT_EQ(lock1.try_shared().is_shared_acquisition(), |
| 80 | false); // Trying to acquire shared access fails since main |
| 81 | // thread has exclusive access |
| 82 | |
| 83 | EXPECT_EQ(lock1.try_exclusive().is_exclusive_acquisition(), |
| 84 | false); // Trying to acquire exclusive access fails since main |
| 85 | // thread has exclusive access |
| 86 | |
| 87 | t1_sync.clear(); // Unblock main thread |
| 88 | |
| 89 | EXPECT_EQ(lock1.acquire_exclusive().is_exclusive_acquisition(), |
| 90 | true); // Acquiring exclusive mode succeeds |
| 91 | |
| 92 | EXPECT_EQ(lock1.try_exclusive().is_exclusive_acquisition(), |
| 93 | true); // Trying to acquire exclusive mode while already acquired |
| 94 | // succeeds (reentrance is supported) |
| 95 | |
| 96 | EXPECT_EQ( |
| 97 | lock1.release_exclusive().is_exclusive_acquisition(), |
| 98 | true); // Testing for exclusive access after releasing exclusivity is |
| 99 | // still true since it was acquire twice, previously |
| 100 | |
| 101 | EXPECT_EQ(lock1.release_exclusive().is_exclusive_acquisition(), |
| 102 | false); // Testing for exclusive access after releasing |
| 103 | // exclusivity is now false |
| 104 | }); |
| 105 | |
| 106 | std::thread t2([&]() -> void { |
| 107 | EXPECT_EQ(lock2.try_shared().is_shared_acquisition(), |
| 108 | true); // Trying to acquire shared access fails since main thread |
| 109 | // has exclusive access |
| 110 | |
| 111 | EXPECT_EQ(lock2.try_exclusive().is_exclusive_acquisition(), |
| 112 | false); // Trying to acquire exclusive access fails since main |
| 113 | // thread has exclusive access |
| 114 | |
| 115 | EXPECT_EQ(lock2.release_shared().is_shared_acquisition(), |
| 116 | false); // Testing for exclusive access after releasing share is |
| 117 | // now false |
| 118 | |
| 119 | EXPECT_EQ(lock2.try_exclusive().is_exclusive_acquisition(), |
| 120 | false); // Trying to acquire exclusive access fails since main |
| 121 | // thread has exclusive access |
| 122 | |
| 123 | t2_sync.clear(); // Unblock main thread |
| 124 | |
| 125 | EXPECT_EQ(lock2.acquire_exclusive().is_exclusive_acquisition(), |
| 126 | true); // Acquiring exclusive mode succeeds |
| 127 | |
| 128 | EXPECT_EQ(lock2.try_exclusive().is_exclusive_acquisition(), |
| 129 | true); // Trying to acquire exclusive mode while already acquired |
| 130 | // succeeds (reentrance is supported) |
| 131 | |
| 132 | EXPECT_EQ( |
| 133 | lock2.release_exclusive().is_exclusive_acquisition(), |
| 134 | true); // Testing for exclusive access after releasing exclusivity is |
| 135 | // still true since it was acquire twice, previously |
| 136 | |
| 137 | EXPECT_EQ(lock2.release_exclusive().is_exclusive_acquisition(), |
| 138 | false); // Testing for exclusive access after releasing |
| 139 | // exclusivity is now false |
| 140 | }); |
| 141 | |
| 142 | while (t1_sync.test_and_set()) std::this_thread::yield(); |
| 143 | lock1.release_exclusive(); |
| 144 | while (t2_sync.test_and_set()) std::this_thread::yield(); |
| 145 | lock2.release_shared(); |
| 146 | |
| 147 | t1.join(); |
| 148 | t2.join(); |
| 149 | } |
| 150 | |
| 151 | TEST_F(Shared_spin_lock_test, Starvation_test) { |
| 152 | lock::Shared_spin_lock lock; |
| 153 | std::atomic_flag sync{true}; |
| 154 | |
| 155 | lock.acquire_shared(); |
| 156 | EXPECT_EQ(lock.acquire_exclusive().is_shared_acquisition(), |
| 157 | true); // Acquiring shared mode succeeds |
| 158 | |
| 159 | std::thread t1([&]() -> void { |
| 160 | EXPECT_EQ(lock.try_shared().is_shared_acquisition(), |
| 161 | true); // Acquiring shared mode succeeds even with another |
| 162 | // thread acquiring in shared mode |
| 163 | lock.release_shared(); |
| 164 | sync.clear(); // Unblock main thread |
| 165 | }); |
| 166 | |
| 167 | while (sync.test_and_set()) std::this_thread::yield(); |
| 168 | |
| 169 | std::thread t2([&]() -> void { |
| 170 | lock.acquire_exclusive(); |
| 171 | EXPECT_EQ(lock.is_exclusive_acquisition(), |
| 172 | true); // Acquiring exclusive mode succeeds |
| 173 | |
| 174 | sync.clear(); // Unblock main thread |
| 175 | }); |
| 176 | |
| 177 | std::thread t3([&]() -> void { |
| 178 | while (lock.try_shared().is_shared_acquisition()) lock.release_shared(); |
| 179 | EXPECT_EQ(lock.is_shared_acquisition(), |
| 180 | false); // Acquiring shared mode fails because t2 is already |
| 181 | // waiting on the exclusive lock |
| 182 | |
| 183 | sync.clear(); // Unblock main thread |
| 184 | }); |
| 185 | |
| 186 | while (sync.test_and_set()) std::this_thread::yield(); |
| 187 | lock.release_shared(); |
| 188 | while (sync.test_and_set()) std::this_thread::yield(); |
| 189 | |
| 190 | t1.join(); |
| 191 | t2.join(); |
| 192 | t3.join(); |
| 193 | } |
| 194 | |
| 195 | TEST_F(Shared_spin_lock_test, Sentry_class_test) { |
| 196 | lock::Shared_spin_lock lock1; |
| 197 | std::atomic_flag t1_sync{true}; |
| 198 | std::atomic_flag t2_sync{true}; |
| 199 | |
| 200 | std::thread t1([&]() -> void { |
| 201 | lock::Shared_spin_lock::Guard sentry1{ |
| 202 | lock1, |
| 203 | lock::Shared_spin_lock::enum_lock_acquisition::SL_NO_ACQUISITION}; |
| 204 | sentry1.acquire(lock::Shared_spin_lock::enum_lock_acquisition::SL_SHARED); |
| 205 | EXPECT_EQ(sentry1->is_shared_acquisition(), |
| 206 | true); // Testing for shared mode access succeeds |
| 207 | |
| 208 | { |
| 209 | lock::Shared_spin_lock::Guard sentry2{ |
| 210 | lock1, |
| 211 | lock::Shared_spin_lock::enum_lock_acquisition::SL_NO_ACQUISITION}; |
| 212 | sentry2.acquire( |
| 213 | lock::Shared_spin_lock::enum_lock_acquisition::SL_EXCLUSIVE, |
| 214 | true /* try and exit*/ |
| 215 | ); |
| 216 | EXPECT_EQ((*sentry2).is_exclusive_acquisition(), |
| 217 | false); // Testing for exclusive mode access fails |
| 218 | } |
| 219 | |
| 220 | t1_sync.clear(); // Unblock main thread |
| 221 | while (t2_sync.test_and_set()) std::this_thread::yield(); |
| 222 | }); |
| 223 | |
| 224 | while (t1_sync.test_and_set()) std::this_thread::yield(); |
| 225 | |
| 226 | { |
| 227 | lock::Shared_spin_lock::Guard sentry{ |
| 228 | lock1, lock::Shared_spin_lock::enum_lock_acquisition::SL_EXCLUSIVE, |
| 229 | true /* try and exit*/}; |
| 230 | EXPECT_EQ(sentry->is_exclusive_acquisition(), |
| 231 | false); // Exclusivity won't be achieved until t1 exits |
| 232 | } |
| 233 | { |
| 234 | lock::Shared_spin_lock::Guard sentry{ |
| 235 | lock1, lock::Shared_spin_lock::enum_lock_acquisition::SL_SHARED}; |
| 236 | EXPECT_EQ(sentry->is_shared_acquisition(), |
| 237 | true); // Shared access is allowed |
| 238 | } |
| 239 | t2_sync.clear(); // Allow t1 to exit |
| 240 | |
| 241 | { |
| 242 | lock::Shared_spin_lock::Guard sentry{ |
| 243 | lock1, lock::Shared_spin_lock::enum_lock_acquisition::SL_EXCLUSIVE}; |
| 244 | EXPECT_EQ(sentry->is_exclusive_acquisition(), |
| 245 | true); // Exclusivity acquired successfullly |
| 246 | { |
| 247 | lock::Shared_spin_lock::Guard sentry2{ |
| 248 | lock1, |
| 249 | lock::Shared_spin_lock::enum_lock_acquisition::SL_NO_ACQUISITION}; |
| 250 | sentry2.acquire(lock::Shared_spin_lock::enum_lock_acquisition::SL_SHARED, |
| 251 | true /* try and exit*/ |
| 252 | ); |
| 253 | EXPECT_EQ((*sentry2).is_shared_acquisition(), |
| 254 | false); // Testing for exclusive mode access fails |
| 255 | } |
| 256 | } |
| 257 | { |
| 258 | lock::Shared_spin_lock::Guard sentry{ |
| 259 | lock1, lock::Shared_spin_lock::enum_lock_acquisition::SL_SHARED, |
| 260 | true /* try and exit*/}; |
| 261 | EXPECT_EQ((*sentry).is_shared_acquisition(), |
| 262 | true); // Shared mode acquired successfully |
| 263 | } |
| 264 | |
| 265 | t1.join(); |
| 266 | |
| 267 | EXPECT_EQ(lock1.is_exclusive_acquisition(), |
| 268 | false); // All access has been cleared |
| 269 | EXPECT_EQ(lock1.is_shared_acquisition(), |
| 270 | false); // All access has been cleared |
| 271 | |
| 272 | { |
| 273 | lock::Shared_spin_lock::Guard sentry{ |
| 274 | lock1, lock::Shared_spin_lock::enum_lock_acquisition::SL_SHARED}; |
| 275 | EXPECT_EQ(sentry->is_shared_acquisition(), |
| 276 | true); // Shared access acquired successfullly |
| 277 | sentry.release(); |
| 278 | sentry.release(); |
| 279 | EXPECT_EQ(sentry->is_shared_acquisition(), |
| 280 | false); // Shared access is not acquired anymore |
| 281 | } |
| 282 | EXPECT_EQ(lock1.is_shared_acquisition(), |
| 283 | false); // Shared access is not acquired anymore |
| 284 | |
| 285 | lock1.acquire_shared(); |
| 286 | { |
| 287 | lock::Shared_spin_lock::Guard sentry{ |
| 288 | lock1, lock::Shared_spin_lock::enum_lock_acquisition::SL_EXCLUSIVE, |
| 289 | true}; // Try to acquire in exclusive mode |
| 290 | EXPECT_EQ(sentry->is_exclusive_acquisition(), |
| 291 | false); // Exclusive access isn't granted |
| 292 | |
| 293 | if (lock1.is_shared_acquisition()) { |
| 294 | sentry.acquire(lock::Shared_spin_lock::enum_lock_acquisition::SL_SHARED); |
| 295 | lock1.release_shared(); |
| 296 | } |
| 297 | EXPECT_EQ(sentry->is_shared_acquisition(), |
| 298 | true); // Shared access is acquired by sentry |
| 299 | } |
| 300 | EXPECT_EQ(lock1.is_shared_acquisition(), |
| 301 | false); // Shared access is not acquired anymore |
| 302 | |
| 303 | { |
| 304 | lock::Shared_spin_lock::Guard sentry{ |
| 305 | lock1, lock::Shared_spin_lock::enum_lock_acquisition:: |
| 306 | SL_NO_ACQUISITION}; // Create the sentry but don't acquire |
| 307 | EXPECT_EQ(sentry->is_exclusive_acquisition(), |
| 308 | false); // Exclusive access isn't granted |
| 309 | EXPECT_EQ(sentry->is_shared_acquisition(), |
| 310 | false); // Shsared access isn't granted |
| 311 | sentry.release(); |
| 312 | |
| 313 | sentry.acquire(lock::Shared_spin_lock::enum_lock_acquisition::SL_EXCLUSIVE); |
| 314 | EXPECT_EQ(sentry->is_exclusive_acquisition(), |
| 315 | true); // Exclusive access is acquired by sentry |
| 316 | } |
| 317 | EXPECT_EQ(lock1.is_exclusive_acquisition(), |
| 318 | false); // Exclusive access is not acquired anymore |
| 319 | } // namespace unittests |
| 320 | |
| 321 | } // namespace unittests |
| 322 | } // namespace lock |