1 //===-- Tests for pthread_equal -------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "src/__support/CPP/string_view.h" 10 #include "src/errno/libc_errno.h" 11 #include "src/pthread/pthread_create.h" 12 #include "src/pthread/pthread_getname_np.h" 13 #include "src/pthread/pthread_join.h" 14 #include "src/pthread/pthread_mutex_destroy.h" 15 #include "src/pthread/pthread_mutex_init.h" 16 #include "src/pthread/pthread_mutex_lock.h" 17 #include "src/pthread/pthread_mutex_unlock.h" 18 #include "src/pthread/pthread_self.h" 19 #include "src/pthread/pthread_setname_np.h" 20 21 #include "test/IntegrationTest/test.h" 22 23 #include <pthread.h> 24 #include <stdint.h> // uintptr_t 25 26 using string_view = LIBC_NAMESPACE::cpp::string_view; 27 28 char child_thread_name_buffer[16]; 29 pthread_mutex_t mutex; 30 31 static void *child_func(void *) { 32 LIBC_NAMESPACE::pthread_mutex_lock(&mutex); 33 auto self = LIBC_NAMESPACE::pthread_self(); 34 LIBC_NAMESPACE::pthread_getname_np(self, child_thread_name_buffer, 16); 35 LIBC_NAMESPACE::pthread_mutex_unlock(&mutex); 36 return nullptr; 37 } 38 39 TEST_MAIN() { 40 // We init and lock the mutex so that we guarantee that the child thread is 41 // waiting after startup. 42 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_init(&mutex, nullptr), 0); 43 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_lock(&mutex), 0); 44 45 auto main_thread = LIBC_NAMESPACE::pthread_self(); 46 const char MAIN_THREAD_NAME[] = "main_thread"; 47 char thread_name_buffer[16]; 48 ASSERT_EQ(LIBC_NAMESPACE::pthread_setname_np(main_thread, MAIN_THREAD_NAME), 49 0); 50 ASSERT_EQ( 51 LIBC_NAMESPACE::pthread_getname_np(main_thread, thread_name_buffer, 16), 52 0); 53 ASSERT_EQ(string_view(MAIN_THREAD_NAME), 54 string_view(reinterpret_cast<const char *>(thread_name_buffer))); 55 56 pthread_t th; 57 ASSERT_EQ(LIBC_NAMESPACE::pthread_create(&th, nullptr, child_func, nullptr), 58 0); 59 // This new thread should of course not be equal to the main thread. 60 const char CHILD_THREAD_NAME[] = "child_thread"; 61 ASSERT_EQ(LIBC_NAMESPACE::pthread_setname_np(th, CHILD_THREAD_NAME), 0); 62 ASSERT_EQ(LIBC_NAMESPACE::pthread_getname_np(th, thread_name_buffer, 16), 0); 63 ASSERT_EQ(string_view(CHILD_THREAD_NAME), 64 string_view(reinterpret_cast<const char *>(thread_name_buffer))); 65 66 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_unlock(&mutex), 0); 67 68 void *retval; 69 ASSERT_EQ(LIBC_NAMESPACE::pthread_join(th, &retval), 0); 70 ASSERT_EQ(uintptr_t(retval), uintptr_t(nullptr)); 71 // Make sure that the child thread saw it name correctly. 72 ASSERT_EQ( 73 string_view(CHILD_THREAD_NAME), 74 string_view(reinterpret_cast<const char *>(child_thread_name_buffer))); 75 76 LIBC_NAMESPACE::pthread_mutex_destroy(&mutex); 77 78 ASSERT_EQ(LIBC_NAMESPACE::pthread_setname_np( 79 main_thread, "a really long name for a thread"), 80 ERANGE); 81 char smallbuf[1]; 82 ASSERT_EQ(LIBC_NAMESPACE::pthread_getname_np(main_thread, smallbuf, 1), 83 ERANGE); 84 85 return 0; 86 } 87