1 //===-- Unittests for mutex -----------------------------------------------===//
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 "src/__support/CPP/mutex.h"
10 #include "test/UnitTest/Test.h"
12 using LIBC_NAMESPACE::cpp::adopt_lock
;
13 using LIBC_NAMESPACE::cpp::lock_guard
;
15 // Simple struct for testing cpp::lock_guard. It defines methods 'lock' and
16 // 'unlock' which are required for the cpp::lock_guard class template.
18 // Flag to show whether this mutex is locked.
21 // Flag to show if this mutex has been double locked.
22 bool double_locked
= false;
24 // Flag to show if this mutex has been double unlocked.
25 bool double_unlocked
= false;
38 double_unlocked
= true;
44 TEST(LlvmLibcMutexTest
, Basic
) {
46 ASSERT_FALSE(m
.locked
);
47 ASSERT_FALSE(m
.double_locked
);
48 ASSERT_FALSE(m
.double_unlocked
);
52 ASSERT_TRUE(m
.locked
);
53 ASSERT_FALSE(m
.double_locked
);
56 ASSERT_FALSE(m
.locked
);
57 ASSERT_FALSE(m
.double_unlocked
);
60 TEST(LlvmLibcMutexTest
, AcquireLocked
) {
62 ASSERT_FALSE(m
.locked
);
63 ASSERT_FALSE(m
.double_locked
);
64 ASSERT_FALSE(m
.double_unlocked
);
66 // Lock the mutex before placing a lock guard on it.
68 ASSERT_TRUE(m
.locked
);
69 ASSERT_FALSE(m
.double_locked
);
72 lock_guard
lg(m
, adopt_lock
);
73 ASSERT_TRUE(m
.locked
);
74 ASSERT_FALSE(m
.double_locked
);
77 ASSERT_FALSE(m
.locked
);
78 ASSERT_FALSE(m
.double_unlocked
);