|
| 1 | +/* |
| 2 | + * Copyright 2026 Datadog, Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | +#include "spinLock.h" |
| 19 | +#include "../../main/cpp/gtest_crash_handler.h" |
| 20 | +#include <atomic> |
| 21 | +#include <thread> |
| 22 | + |
| 23 | +static constexpr char SPINLOCK_TEST_NAME[] = "SpinLockTest"; |
| 24 | + |
| 25 | +class SpinLockTest : public ::testing::Test { |
| 26 | +protected: |
| 27 | + void SetUp() override { |
| 28 | + installGtestCrashHandler<SPINLOCK_TEST_NAME>(); |
| 29 | + } |
| 30 | + void TearDown() override { |
| 31 | + restoreDefaultSignalHandlers(); |
| 32 | + } |
| 33 | + |
| 34 | + SpinLock lock; |
| 35 | +}; |
| 36 | + |
| 37 | +// OptionalSharedLockGuard acquires on a free lock and releases on destruction. |
| 38 | +TEST_F(SpinLockTest, OptionalGuard_UncontendedAcquire) { |
| 39 | + { |
| 40 | + OptionalSharedLockGuard g(&lock); |
| 41 | + EXPECT_TRUE(g.ownsLock()); |
| 42 | + } |
| 43 | + // After destruction the lock must be back to 0 (unlocked). |
| 44 | + // Verify by taking an exclusive lock — would spin forever if still shared. |
| 45 | + EXPECT_TRUE(lock.tryLock()); |
| 46 | + lock.unlock(); |
| 47 | +} |
| 48 | + |
| 49 | +// When an exclusive lock is held, tryLockShared returns false immediately |
| 50 | +// (first load sees _lock == 1 > 0, exits without spinning). |
| 51 | +TEST_F(SpinLockTest, OptionalGuard_ExclusiveHeld_ImmediateReturn) { |
| 52 | + lock.lock(); |
| 53 | + OptionalSharedLockGuard g(&lock, 1000000); |
| 54 | + EXPECT_FALSE(g.ownsLock()); |
| 55 | + lock.unlock(); |
| 56 | +} |
| 57 | + |
| 58 | +// The spin budget must be honoured: even with a tiny budget the constructor |
| 59 | +// returns (does not hang) when readers are continuously racing the CAS. |
| 60 | +TEST_F(SpinLockTest, OptionalGuard_SpinBudgetBound) { |
| 61 | + // A background contender thread rapidly locks/unlocks shared to create |
| 62 | + // CAS contention on _lock; the guard must return without hanging. |
| 63 | + std::atomic<bool> stop{false}; |
| 64 | + |
| 65 | + // Background thread hammers shared lock/unlock to create CAS contention. |
| 66 | + std::thread contender([&] { |
| 67 | + while (!stop.load(std::memory_order_relaxed)) { |
| 68 | + lock.lockShared(); |
| 69 | + lock.unlockShared(); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + // With a very small budget the guard must return promptly, not hang. |
| 74 | + for (int i = 0; i < 1000; ++i) { |
| 75 | + OptionalSharedLockGuard g(&lock, 8); |
| 76 | + // ownsLock() may be true or false depending on timing — we only assert |
| 77 | + // the constructor returned (i.e. we reach here without hanging). |
| 78 | + (void)g.ownsLock(); |
| 79 | + } |
| 80 | + |
| 81 | + stop.store(true, std::memory_order_relaxed); |
| 82 | + contender.join(); |
| 83 | +} |
| 84 | + |
| 85 | +// Verifies that the budget is enforced: with an exclusive lock held, |
| 86 | +// tryLockShared(N) must return false regardless of N. |
| 87 | +TEST_F(SpinLockTest, OptionalGuard_BudgetEnforced_ExclusivePath) { |
| 88 | + lock.lock(); |
| 89 | + // With any budget, exclusive lock causes immediate false return. |
| 90 | + EXPECT_FALSE(lock.tryLockShared(1)); |
| 91 | + EXPECT_FALSE(lock.tryLockShared(1000)); |
| 92 | + EXPECT_FALSE(lock.tryLockShared(SpinLock::DEFAULT_SHARED_SPIN_BUDGET)); |
| 93 | + lock.unlock(); |
| 94 | +} |
| 95 | + |
| 96 | +// Multiple shared guards can be held simultaneously (readers don't starve each other). |
| 97 | +TEST_F(SpinLockTest, OptionalGuard_SharedReentrancy) { |
| 98 | + OptionalSharedLockGuard g1(&lock); |
| 99 | + OptionalSharedLockGuard g2(&lock); |
| 100 | + EXPECT_TRUE(g1.ownsLock()); |
| 101 | + EXPECT_TRUE(g2.ownsLock()); |
| 102 | + // Exclusive try must fail while shared locks are held. |
| 103 | + EXPECT_FALSE(lock.tryLock()); |
| 104 | +} |
| 105 | + |
| 106 | +// tryLockShared() (unbounded) still works correctly alongside the bounded overload. |
| 107 | +TEST_F(SpinLockTest, TryLockShared_ExclusiveHeld_ReturnsFalse) { |
| 108 | + lock.lock(); |
| 109 | + EXPECT_FALSE(lock.tryLockShared()); |
| 110 | + lock.unlock(); |
| 111 | +} |
| 112 | + |
| 113 | +TEST_F(SpinLockTest, TryLockShared_Free_ReturnsTrue) { |
| 114 | + EXPECT_TRUE(lock.tryLockShared()); |
| 115 | + lock.unlockShared(); |
| 116 | +} |
0 commit comments