Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / scudo / standalone / tests / mutex_test.cpp
blobc3efeab8272d0180e0f576bdf72be5fdaebdd815
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 "tests/scudo_unit_test.h"
11 #include "mutex.h"
13 #include <pthread.h>
14 #include <string.h>
16 class TestData {
17 public:
18 explicit TestData(scudo::HybridMutex &M) : Mutex(M) {
19 for (scudo::u32 I = 0; I < Size; I++)
20 Data[I] = 0;
23 void write() {
24 scudo::ScopedLock L(Mutex);
25 T V0 = Data[0];
26 for (scudo::u32 I = 0; I < Size; I++) {
27 EXPECT_EQ(Data[I], V0);
28 Data[I]++;
32 void tryWrite() {
33 if (!Mutex.tryLock())
34 return;
35 T V0 = Data[0];
36 for (scudo::u32 I = 0; I < Size; I++) {
37 EXPECT_EQ(Data[I], V0);
38 Data[I]++;
40 Mutex.unlock();
43 void backoff() {
44 volatile T LocalData[Size] = {};
45 for (scudo::u32 I = 0; I < Size; I++) {
46 LocalData[I] = LocalData[I] + 1;
47 EXPECT_EQ(LocalData[I], 1U);
51 private:
52 static const scudo::u32 Size = 64U;
53 typedef scudo::u64 T;
54 scudo::HybridMutex &Mutex;
55 alignas(SCUDO_CACHE_LINE_SIZE) T Data[Size];
58 const scudo::u32 NumberOfThreads = 8;
59 #if SCUDO_DEBUG
60 const scudo::u32 NumberOfIterations = 4 * 1024;
61 #else
62 const scudo::u32 NumberOfIterations = 16 * 1024;
63 #endif
65 static void *lockThread(void *Param) {
66 TestData *Data = reinterpret_cast<TestData *>(Param);
67 for (scudo::u32 I = 0; I < NumberOfIterations; I++) {
68 Data->write();
69 Data->backoff();
71 return 0;
74 static void *tryThread(void *Param) {
75 TestData *Data = reinterpret_cast<TestData *>(Param);
76 for (scudo::u32 I = 0; I < NumberOfIterations; I++) {
77 Data->tryWrite();
78 Data->backoff();
80 return 0;
83 TEST(ScudoMutexTest, Mutex) {
84 scudo::HybridMutex M;
85 TestData Data(M);
86 pthread_t Threads[NumberOfThreads];
87 for (scudo::u32 I = 0; I < NumberOfThreads; I++)
88 pthread_create(&Threads[I], 0, lockThread, &Data);
89 for (scudo::u32 I = 0; I < NumberOfThreads; I++)
90 pthread_join(Threads[I], 0);
93 TEST(ScudoMutexTest, MutexTry) {
94 scudo::HybridMutex M;
95 TestData Data(M);
96 pthread_t Threads[NumberOfThreads];
97 for (scudo::u32 I = 0; I < NumberOfThreads; I++)
98 pthread_create(&Threads[I], 0, tryThread, &Data);
99 for (scudo::u32 I = 0; I < NumberOfThreads; I++)
100 pthread_join(Threads[I], 0);
103 TEST(ScudoMutexTest, MutexAssertHeld) {
104 scudo::HybridMutex M;
105 M.lock();
106 M.assertHeld();
107 M.unlock();