1 //===-- mutex_test.cpp ------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "tests/scudo_unit_test.h"
18 explicit TestData(scudo::HybridMutex
&M
) : Mutex(M
) {
19 for (scudo::u32 I
= 0; I
< Size
; I
++)
24 scudo::ScopedLock
L(Mutex
);
26 for (scudo::u32 I
= 0; I
< Size
; I
++) {
27 EXPECT_EQ(Data
[I
], V0
);
36 for (scudo::u32 I
= 0; I
< Size
; I
++) {
37 EXPECT_EQ(Data
[I
], V0
);
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);
52 static const scudo::u32 Size
= 64U;
54 scudo::HybridMutex
&Mutex
;
55 alignas(SCUDO_CACHE_LINE_SIZE
) T Data
[Size
];
58 const scudo::u32 NumberOfThreads
= 8;
60 const scudo::u32 NumberOfIterations
= 4 * 1024;
62 const scudo::u32 NumberOfIterations
= 16 * 1024;
65 static void *lockThread(void *Param
) {
66 TestData
*Data
= reinterpret_cast<TestData
*>(Param
);
67 for (scudo::u32 I
= 0; I
< NumberOfIterations
; I
++) {
74 static void *tryThread(void *Param
) {
75 TestData
*Data
= reinterpret_cast<TestData
*>(Param
);
76 for (scudo::u32 I
= 0; I
< NumberOfIterations
; I
++) {
83 TEST(ScudoMutexTest
, Mutex
) {
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
) {
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
;