1 #include <gtest/gtest.h> 2 #include <thread> 3 #include <threads.h> 4 5 using namespace testing::ext; 6 7 class PthreadSpinTest : public testing::Test { 8 protected: 9 pthread_spinlock_t spinLock; 10 SetUp()11 void SetUp() override 12 { 13 EXPECT_EQ(0, pthread_spin_init(&spinLock, 0)); 14 } TearDown()15 void TearDown() override 16 { 17 EXPECT_EQ(0, pthread_spin_destroy(&spinLock)); 18 } 19 }; 20 21 /** 22 * @tc.name: pthread_spinlock_001 23 * @tc.desc: Verify that the trylock sucessful. 24 * @tc.type: FUNC 25 * */ 26 HWTEST_F(PthreadSpinTest, pthread_spinlock_001, TestSize.Level1) 27 { 28 EXPECT_EQ(0, pthread_spin_trylock(&spinLock)); 29 EXPECT_EQ(0, pthread_spin_unlock(&spinLock)); 30 } 31 32 /** 33 * @tc.name: pthread_spinlock_002 34 * @tc.desc: Verify that the trylock interface cannot lock a lock that has already been locked again. 35 * @tc.type: FUNC 36 * */ 37 HWTEST_F(PthreadSpinTest, pthread_spinlock_002, TestSize.Level1) 38 { 39 EXPECT_EQ(0, pthread_spin_lock(&spinLock)); 40 EXPECT_EQ(EBUSY, pthread_spin_trylock(&spinLock)); 41 EXPECT_EQ(0, pthread_spin_unlock(&spinLock)); 42 }