1 //===-- Tests for pthread_equal -------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
21 #include "test/IntegrationTest/test.h"
24 #include <stdint.h> // uintptr_t
26 using string_view
= LIBC_NAMESPACE::cpp::string_view
;
28 char child_thread_name_buffer
[16];
29 pthread_mutex_t mutex
;
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
);
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);
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
),
51 LIBC_NAMESPACE::pthread_getname_np(main_thread
, thread_name_buffer
, 16),
53 ASSERT_EQ(string_view(MAIN_THREAD_NAME
),
54 string_view(reinterpret_cast<const char *>(thread_name_buffer
)));
57 ASSERT_EQ(LIBC_NAMESPACE::pthread_create(&th
, nullptr, child_func
, nullptr),
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
)));
66 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_unlock(&mutex
), 0);
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.
73 string_view(CHILD_THREAD_NAME
),
74 string_view(reinterpret_cast<const char *>(child_thread_name_buffer
)));
76 LIBC_NAMESPACE::pthread_mutex_destroy(&mutex
);
78 ASSERT_EQ(LIBC_NAMESPACE::pthread_setname_np(
79 main_thread
, "a really long name for a thread"),
82 ASSERT_EQ(LIBC_NAMESPACE::pthread_getname_np(main_thread
, smallbuf
, 1),