• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gtest/gtest.h>
2 #include <pthread.h>
3 
4 using namespace testing::ext;
5 constexpr int USLEEP_TIME = 1000;
6 
7 class PthreadCleanupTest : public testing::Test {
SetUp()8     void SetUp() override {}
TearDown()9     void TearDown() override {}
10 };
11 
CleanupFunc(void * arg)12 void CleanupFunc(void* arg)
13 {
14     if (arg == nullptr) {
15         return;
16     }
17     EXPECT_EQ(1, *((int*)(arg)));
18 }
19 
ThreadFn(void * arg)20 void* ThreadFn(void* arg)
21 {
22     if (arg == nullptr) {
23         return nullptr;
24     }
25     int cleanupArg = 1;
26     struct __ptcb cleanup;
27     cleanup.__f = &CleanupFunc;
28     cleanup.__x = reinterpret_cast<void*>(&cleanupArg);
29     cleanup.__next = nullptr;
30     _pthread_cleanup_push(&cleanup, cleanup.__f, cleanup.__x);
31     usleep(USLEEP_TIME);
32 
33     _pthread_cleanup_pop(&cleanup, 1);
34     return arg;
35 }
36 
37 /**
38  * @tc.name: pthread_cleanup_001
39  * @tc.desc: Verify whether the function can perform the clear operation correctly in a multithreaded environment.
40  * @tc.type: FUNC
41  * */
42 HWTEST_F(PthreadCleanupTest, pthread_cleanup_001, TestSize.Level1)
43 {
44     pthread_t pt;
45     EXPECT_EQ(0, pthread_create(&pt, nullptr, ThreadFn, nullptr));
46     EXPECT_EQ(0, pthread_join(pt, nullptr));
47 }