brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.1 KiB · d2a5ffc Raw
86 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "hdr/stdint_proxy.h" // uintptr_t10#include "src/__support/CPP/string_view.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#include "test/IntegrationTest/test.h"21 22#include <errno.h>23#include <pthread.h>24 25using string_view = LIBC_NAMESPACE::cpp::string_view;26 27char child_thread_name_buffer[16];28pthread_mutex_t mutex;29 30static void *child_func(void *) {31  LIBC_NAMESPACE::pthread_mutex_lock(&mutex);32  auto self = LIBC_NAMESPACE::pthread_self();33  LIBC_NAMESPACE::pthread_getname_np(self, child_thread_name_buffer, 16);34  LIBC_NAMESPACE::pthread_mutex_unlock(&mutex);35  return nullptr;36}37 38TEST_MAIN() {39  // We init and lock the mutex so that we guarantee that the child thread is40  // waiting after startup.41  ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_init(&mutex, nullptr), 0);42  ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_lock(&mutex), 0);43 44  auto main_thread = LIBC_NAMESPACE::pthread_self();45  const char MAIN_THREAD_NAME[] = "main_thread";46  char thread_name_buffer[16];47  ASSERT_EQ(LIBC_NAMESPACE::pthread_setname_np(main_thread, MAIN_THREAD_NAME),48            0);49  ASSERT_EQ(50      LIBC_NAMESPACE::pthread_getname_np(main_thread, thread_name_buffer, 16),51      0);52  ASSERT_EQ(string_view(MAIN_THREAD_NAME),53            string_view(reinterpret_cast<const char *>(thread_name_buffer)));54 55  pthread_t th;56  ASSERT_EQ(LIBC_NAMESPACE::pthread_create(&th, nullptr, child_func, nullptr),57            0);58  // This new thread should of course not be equal to the main thread.59  const char CHILD_THREAD_NAME[] = "child_thread";60  ASSERT_EQ(LIBC_NAMESPACE::pthread_setname_np(th, CHILD_THREAD_NAME), 0);61  ASSERT_EQ(LIBC_NAMESPACE::pthread_getname_np(th, thread_name_buffer, 16), 0);62  ASSERT_EQ(string_view(CHILD_THREAD_NAME),63            string_view(reinterpret_cast<const char *>(thread_name_buffer)));64 65  ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_unlock(&mutex), 0);66 67  void *retval;68  ASSERT_EQ(LIBC_NAMESPACE::pthread_join(th, &retval), 0);69  ASSERT_EQ(uintptr_t(retval), uintptr_t(nullptr));70  // Make sure that the child thread saw it name correctly.71  ASSERT_EQ(72      string_view(CHILD_THREAD_NAME),73      string_view(reinterpret_cast<const char *>(child_thread_name_buffer)));74 75  LIBC_NAMESPACE::pthread_mutex_destroy(&mutex);76 77  ASSERT_EQ(LIBC_NAMESPACE::pthread_setname_np(78                main_thread, "a really long name for a thread"),79            ERANGE);80  char smallbuf[1];81  ASSERT_EQ(LIBC_NAMESPACE::pthread_getname_np(main_thread, smallbuf, 1),82            ERANGE);83 84  return 0;85}86