[RISCV] Use RISCVSubtarget::is64Bit() instead of hasFeature(RISCV::Feature64Bit)...
[llvm-project.git] / compiler-rt / lib / gwp_asan / tests / mutex_test.cpp
blobf68619cb01c9c87c2bb896153cd068b799422ab2
1 //===-- mutex_test.cpp ------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "gwp_asan/mutex.h"
10 #include "gwp_asan/tests/harness.h"
12 #include <atomic>
13 #include <mutex>
14 #include <thread>
15 #include <vector>
17 using gwp_asan::Mutex;
18 using gwp_asan::ScopedLock;
20 TEST(GwpAsanMutexTest, LockUnlockTest) {
21 Mutex Mu;
23 ASSERT_TRUE(Mu.tryLock());
24 ASSERT_FALSE(Mu.tryLock());
25 Mu.unlock();
27 Mu.lock();
28 Mu.unlock();
30 // Ensure that the mutex actually unlocked.
31 ASSERT_TRUE(Mu.tryLock());
32 Mu.unlock();
35 TEST(GwpAsanMutexTest, ScopedLockUnlockTest) {
36 Mutex Mu;
37 { ScopedLock L(Mu); }
38 // Locking will fail here if the scoped lock failed to unlock.
39 EXPECT_TRUE(Mu.tryLock());
40 Mu.unlock();
43 ScopedLock L(Mu);
44 EXPECT_FALSE(Mu.tryLock()); // Check that the c'tor did lock.
46 // Manually unlock and check that this succeeds.
47 Mu.unlock();
48 EXPECT_TRUE(Mu.tryLock()); // Manually lock.
50 EXPECT_TRUE(Mu.tryLock()); // Assert that the scoped destructor did unlock.
51 Mu.unlock();
54 static void synchronousIncrementTask(std::atomic<bool> *StartingGun, Mutex *Mu,
55 unsigned *Counter,
56 unsigned NumIterations) {
57 while (!StartingGun) {
58 // Wait for starting gun.
60 for (unsigned i = 0; i < NumIterations; ++i) {
61 ScopedLock L(*Mu);
62 (*Counter)++;
66 static void runSynchronisedTest(unsigned NumThreads, unsigned CounterMax) {
67 std::vector<std::thread> Threads;
69 ASSERT_TRUE(CounterMax % NumThreads == 0);
71 std::atomic<bool> StartingGun{false};
72 Mutex Mu;
73 unsigned Counter = 0;
75 for (unsigned i = 0; i < NumThreads; ++i)
76 Threads.emplace_back(synchronousIncrementTask, &StartingGun, &Mu, &Counter,
77 CounterMax / NumThreads);
79 StartingGun = true;
80 for (auto &T : Threads)
81 T.join();
83 EXPECT_EQ(CounterMax, Counter);
86 TEST(GwpAsanMutexTest, SynchronisedCounterTest) {
87 runSynchronisedTest(4, 1000000);
88 runSynchronisedTest(100, 1000000);