1 /** 2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 17 #include "libpandabase/os/mutex.h" 18 #include <gtest/gtest.h> 19 #include <thread> 20 21 namespace panda { 22 23 class LockHolderBasicTest : public testing::Test {}; 24 25 HWTEST_F(LockHolderBasicTest, LockHolderConstructorDestructorTest, testing::ext::TestSize.Level0) 26 { 27 panda::os::memory::DummyLock lock; 28 { 29 panda::os::memory::LockHolder<panda::os::memory::DummyLock> lock_holder(lock); 30 } 31 } 32 33 HWTEST_F(LockHolderBasicTest, ReadLockHolderConstructorDestructorTest, testing::ext::TestSize.Level0) 34 { 35 panda::os::memory::DummyLock lock; 36 { 37 panda::os::memory::ReadLockHolder<panda::os::memory::DummyLock> read_lock_holder(lock); 38 } 39 } 40 41 HWTEST_F(LockHolderBasicTest, WriteLockHolderConstructorDestructorTest, testing::ext::TestSize.Level0) 42 { 43 panda::os::memory::DummyLock lock; 44 { 45 panda::os::memory::WriteLockHolder<panda::os::memory::DummyLock> write_lock_holder(lock); 46 } 47 } 48 49 HWTEST_F(LockHolderBasicTest, LockHolderExclusivityTest, testing::ext::TestSize.Level0) 50 { 51 panda::os::memory::Mutex lock; 52 bool try_lock_success = false; 53 54 { 55 panda::os::memory::LockHolder<panda::os::memory::Mutex> lock_holder(lock); 56 __anon9809558c0102() 57 std::thread t([&lock, &try_lock_success]() NO_THREAD_SAFETY_ANALYSIS { 58 try_lock_success = lock.TryLock(); 59 if (try_lock_success) { 60 lock.Unlock(); 61 } 62 }); 63 t.join(); 64 65 EXPECT_FALSE(try_lock_success); 66 } 67 68 EXPECT_TRUE(lock.TryLock()); __anon9809558c0202() 69 [&]() NO_THREAD_SAFETY_ANALYSIS { 70 lock.Unlock(); 71 }(); 72 } 73 74 HWTEST_F(LockHolderBasicTest, ReadLockPreventsWriteTest, testing::ext::TestSize.Level0) 75 { 76 panda::os::memory::RWLock lock; 77 bool try_read_lock_success = false; 78 79 { 80 panda::os::memory::ReadLockHolder<panda::os::memory::RWLock> read_lock_holder(lock); 81 __anon9809558c0302() 82 std::thread t([&lock, &try_read_lock_success]() NO_THREAD_SAFETY_ANALYSIS { 83 try_read_lock_success = lock.TryWriteLock(); 84 if (try_read_lock_success) { 85 lock.Unlock(); 86 } 87 }); 88 t.join(); 89 90 EXPECT_FALSE(try_read_lock_success); 91 } 92 93 EXPECT_TRUE(lock.TryWriteLock()); __anon9809558c0402() 94 [&]() NO_THREAD_SAFETY_ANALYSIS { 95 lock.Unlock(); 96 }(); 97 } 98 99 HWTEST_F(LockHolderBasicTest, WriteLockPreventsReadAndWriteTest, testing::ext::TestSize.Level0) 100 { 101 panda::os::memory::RWLock lock; 102 bool try_read_lock_success = false; 103 bool try_write_lock_success = false; 104 105 { 106 panda::os::memory::WriteLockHolder<panda::os::memory::RWLock> write_lock_holder(lock); 107 __anon9809558c0502() 108 std::thread t([&]() NO_THREAD_SAFETY_ANALYSIS { 109 try_read_lock_success = lock.TryReadLock(); 110 try_write_lock_success = lock.TryWriteLock(); 111 if (try_read_lock_success) { 112 lock.Unlock(); 113 } 114 if (try_write_lock_success) { 115 lock.Unlock(); 116 } 117 }); 118 t.join(); 119 120 EXPECT_FALSE(try_read_lock_success); 121 EXPECT_FALSE(try_write_lock_success); 122 } 123 124 EXPECT_TRUE(lock.TryWriteLock()); __anon9809558c0602() 125 [&]() NO_THREAD_SAFETY_ANALYSIS { 126 lock.Unlock(); 127 }(); 128 } 129 } // namespace ark::os::memory::test 130