brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.5 KiB · 0156979 Raw
69 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/pthread/pthread_create.h"11#include "src/pthread/pthread_equal.h"12#include "src/pthread/pthread_join.h"13#include "src/pthread/pthread_mutex_destroy.h"14#include "src/pthread/pthread_mutex_init.h"15#include "src/pthread/pthread_mutex_lock.h"16#include "src/pthread/pthread_mutex_unlock.h"17#include "src/pthread/pthread_self.h"18#include "test/IntegrationTest/test.h"19 20#include <pthread.h>21 22pthread_t child_thread;23pthread_mutex_t mutex;24 25static void *child_func(void *arg) {26  LIBC_NAMESPACE::pthread_mutex_lock(&mutex);27  int *ret = reinterpret_cast<int *>(arg);28  auto self = LIBC_NAMESPACE::pthread_self();29  *ret = LIBC_NAMESPACE::pthread_equal(child_thread, self);30  LIBC_NAMESPACE::pthread_mutex_unlock(&mutex);31  return nullptr;32}33 34TEST_MAIN() {35  // We init and lock the mutex so that we guarantee that the child thread is36  // waiting after startup.37  ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_init(&mutex, nullptr), 0);38  ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_lock(&mutex), 0);39 40  auto main_thread = LIBC_NAMESPACE::pthread_self();41 42  // The idea here is that, we start a child thread which will immediately43  // wait on |mutex|. The main thread will update the global |child_thread| var44  // and unlock |mutex|. This will give the child thread a chance to compare45  // the result of pthread_self with the |child_thread|. The result of the46  // comparison is returned in the thread arg.47  int result = 0;48  pthread_t th;49  ASSERT_EQ(LIBC_NAMESPACE::pthread_create(&th, nullptr, child_func, &result),50            0);51  // This new thread should of course not be equal to the main thread.52  ASSERT_EQ(LIBC_NAMESPACE::pthread_equal(th, main_thread), 0);53 54  // Set the |child_thread| global var and unlock to allow the child to perform55  // the comparison.56  child_thread = th;57  ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_unlock(&mutex), 0);58 59  void *retval;60  ASSERT_EQ(LIBC_NAMESPACE::pthread_join(th, &retval), 0);61  ASSERT_EQ(uintptr_t(retval), uintptr_t(nullptr));62  // The child thread should see that pthread_self return value is the same as63  // |child_thread|.64  ASSERT_NE(result, 0);65 66  LIBC_NAMESPACE::pthread_mutex_destroy(&mutex);67  return 0;68}69