xref: /llvm-project/libc/test/src/__support/CPP/mutex_test.cpp (revision c4a3d184db5fdffe798208b8281dfe944616f9ed)
1*c4a3d184SVlad Mishel //===-- Unittests for mutex -----------------------------------------------===//
2*c4a3d184SVlad Mishel //
3*c4a3d184SVlad Mishel // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*c4a3d184SVlad Mishel // See https://llvm.org/LICENSE.txt for license information.
5*c4a3d184SVlad Mishel // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*c4a3d184SVlad Mishel //
7*c4a3d184SVlad Mishel //===----------------------------------------------------------------------===//
8*c4a3d184SVlad Mishel 
9*c4a3d184SVlad Mishel #include "src/__support/CPP/mutex.h"
10*c4a3d184SVlad Mishel #include "test/UnitTest/Test.h"
11*c4a3d184SVlad Mishel 
12*c4a3d184SVlad Mishel using LIBC_NAMESPACE::cpp::adopt_lock;
13*c4a3d184SVlad Mishel using LIBC_NAMESPACE::cpp::lock_guard;
14*c4a3d184SVlad Mishel 
15*c4a3d184SVlad Mishel // Simple struct for testing cpp::lock_guard. It defines methods 'lock' and
16*c4a3d184SVlad Mishel // 'unlock' which are required for the cpp::lock_guard class template.
17*c4a3d184SVlad Mishel struct Mutex {
18*c4a3d184SVlad Mishel   // Flag to show whether this mutex is locked.
19*c4a3d184SVlad Mishel   bool locked = false;
20*c4a3d184SVlad Mishel 
21*c4a3d184SVlad Mishel   // Flag to show if this mutex has been double locked.
22*c4a3d184SVlad Mishel   bool double_locked = false;
23*c4a3d184SVlad Mishel 
24*c4a3d184SVlad Mishel   // Flag to show if this mutex has been double unlocked.
25*c4a3d184SVlad Mishel   bool double_unlocked = false;
26*c4a3d184SVlad Mishel 
MutexMutex27*c4a3d184SVlad Mishel   Mutex() {}
28*c4a3d184SVlad Mishel 
lockMutex29*c4a3d184SVlad Mishel   void lock() {
30*c4a3d184SVlad Mishel     if (locked)
31*c4a3d184SVlad Mishel       double_locked = true;
32*c4a3d184SVlad Mishel 
33*c4a3d184SVlad Mishel     locked = true;
34*c4a3d184SVlad Mishel   }
35*c4a3d184SVlad Mishel 
unlockMutex36*c4a3d184SVlad Mishel   void unlock() {
37*c4a3d184SVlad Mishel     if (!locked)
38*c4a3d184SVlad Mishel       double_unlocked = true;
39*c4a3d184SVlad Mishel 
40*c4a3d184SVlad Mishel     locked = false;
41*c4a3d184SVlad Mishel   }
42*c4a3d184SVlad Mishel };
43*c4a3d184SVlad Mishel 
TEST(LlvmLibcMutexTest,Basic)44*c4a3d184SVlad Mishel TEST(LlvmLibcMutexTest, Basic) {
45*c4a3d184SVlad Mishel   Mutex m;
46*c4a3d184SVlad Mishel   ASSERT_FALSE(m.locked);
47*c4a3d184SVlad Mishel   ASSERT_FALSE(m.double_locked);
48*c4a3d184SVlad Mishel   ASSERT_FALSE(m.double_unlocked);
49*c4a3d184SVlad Mishel 
50*c4a3d184SVlad Mishel   {
51*c4a3d184SVlad Mishel     lock_guard lg(m);
52*c4a3d184SVlad Mishel     ASSERT_TRUE(m.locked);
53*c4a3d184SVlad Mishel     ASSERT_FALSE(m.double_locked);
54*c4a3d184SVlad Mishel   }
55*c4a3d184SVlad Mishel 
56*c4a3d184SVlad Mishel   ASSERT_FALSE(m.locked);
57*c4a3d184SVlad Mishel   ASSERT_FALSE(m.double_unlocked);
58*c4a3d184SVlad Mishel }
59*c4a3d184SVlad Mishel 
TEST(LlvmLibcMutexTest,AcquireLocked)60*c4a3d184SVlad Mishel TEST(LlvmLibcMutexTest, AcquireLocked) {
61*c4a3d184SVlad Mishel   Mutex m;
62*c4a3d184SVlad Mishel   ASSERT_FALSE(m.locked);
63*c4a3d184SVlad Mishel   ASSERT_FALSE(m.double_locked);
64*c4a3d184SVlad Mishel   ASSERT_FALSE(m.double_unlocked);
65*c4a3d184SVlad Mishel 
66*c4a3d184SVlad Mishel   // Lock the mutex before placing a lock guard on it.
67*c4a3d184SVlad Mishel   m.lock();
68*c4a3d184SVlad Mishel   ASSERT_TRUE(m.locked);
69*c4a3d184SVlad Mishel   ASSERT_FALSE(m.double_locked);
70*c4a3d184SVlad Mishel 
71*c4a3d184SVlad Mishel   {
72*c4a3d184SVlad Mishel     lock_guard lg(m, adopt_lock);
73*c4a3d184SVlad Mishel     ASSERT_TRUE(m.locked);
74*c4a3d184SVlad Mishel     ASSERT_FALSE(m.double_locked);
75*c4a3d184SVlad Mishel   }
76*c4a3d184SVlad Mishel 
77*c4a3d184SVlad Mishel   ASSERT_FALSE(m.locked);
78*c4a3d184SVlad Mishel   ASSERT_FALSE(m.double_unlocked);
79*c4a3d184SVlad Mishel }
80