1 //===-- sanitizer_posix_test.cc -------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Tests for POSIX-specific code.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_platform.h"
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "gtest/gtest.h"
22 namespace __sanitizer
{
24 static pthread_key_t key
;
25 static bool destructor_executed
;
28 void destructor(void *arg
) {
29 uptr iter
= reinterpret_cast<uptr
>(arg
);
31 ASSERT_EQ(0, pthread_setspecific(key
, reinterpret_cast<void *>(iter
- 1)));
34 destructor_executed
= true;
38 void *thread_func(void *arg
) {
39 return reinterpret_cast<void*>(pthread_setspecific(key
, arg
));
42 static void SpawnThread(uptr iteration
) {
43 destructor_executed
= false;
45 ASSERT_EQ(0, pthread_create(&tid
, 0, &thread_func
,
46 reinterpret_cast<void *>(iteration
)));
48 ASSERT_EQ(0, pthread_join(tid
, &retval
));
52 TEST(SanitizerCommon
, PthreadDestructorIterations
) {
53 ASSERT_EQ(0, pthread_key_create(&key
, &destructor
));
54 SpawnThread(kPthreadDestructorIterations
);
55 EXPECT_TRUE(destructor_executed
);
56 SpawnThread(kPthreadDestructorIterations
+ 1);
57 EXPECT_FALSE(destructor_executed
);
60 } // namespace __sanitizer
62 #endif // SANITIZER_POSIX