| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //===-- Implementation of the thrd_equal function -------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/threads/thrd_equal.h" | ||
| #include "src/__support/common.h" | ||
| #include "src/__support/threads/thread.h" | ||
|
|
||
| #include <threads.h> // For thrd_* type definitions. | ||
|
|
||
| namespace __llvm_libc { | ||
|
|
||
| static_assert(sizeof(thrd_t) == sizeof(__llvm_libc::Thread), | ||
| "Mismatch between thrd_t and internal Thread."); | ||
|
|
||
| LLVM_LIBC_FUNCTION(int, thrd_equal, (thrd_t lhs, thrd_t rhs)) { | ||
| auto *lhs_internal = reinterpret_cast<Thread *>(&lhs); | ||
| auto *rhs_internal = reinterpret_cast<Thread *>(&rhs); | ||
| return *lhs_internal == *rhs_internal; | ||
| } | ||
|
|
||
| } // namespace __llvm_libc |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| //===-- Implementation header for thrd_equal function -----------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_LIBC_SRC_THREADS_THRD_EQUAL_H | ||
| #define LLVM_LIBC_SRC_THREADS_THRD_EQUAL_H | ||
|
|
||
| #include "include/threads.h" | ||
|
|
||
| namespace __llvm_libc { | ||
|
|
||
| int thrd_equal(thrd_t lhs, thrd_t rhs); | ||
|
|
||
| } // namespace __llvm_libc | ||
|
|
||
| #endif // LLVM_LIBC_SRC_THREADS_THRD_EQUAL_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| //===-- Tests for pthread_equal -------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/pthread/pthread_create.h" | ||
| #include "src/pthread/pthread_equal.h" | ||
| #include "src/pthread/pthread_join.h" | ||
| #include "src/pthread/pthread_mutex_destroy.h" | ||
| #include "src/pthread/pthread_mutex_init.h" | ||
| #include "src/pthread/pthread_mutex_lock.h" | ||
| #include "src/pthread/pthread_mutex_unlock.h" | ||
| #include "src/pthread/pthread_self.h" | ||
|
|
||
| #include "utils/IntegrationTest/test.h" | ||
|
|
||
| #include <pthread.h> | ||
|
|
||
| pthread_t child_thread; | ||
| pthread_mutex_t mutex; | ||
|
|
||
| static void *child_func(void *arg) { | ||
| __llvm_libc::pthread_mutex_lock(&mutex); | ||
| int *ret = reinterpret_cast<int *>(arg); | ||
| auto self = __llvm_libc::pthread_self(); | ||
| *ret = __llvm_libc::pthread_equal(child_thread, self); | ||
| __llvm_libc::pthread_mutex_unlock(&mutex); | ||
| return nullptr; | ||
| } | ||
|
|
||
| int main() { | ||
| // We init and lock the mutex so that we guarantee that the child thread is | ||
| // waiting after startup. | ||
| ASSERT_EQ(__llvm_libc::pthread_mutex_init(&mutex, nullptr), 0); | ||
| ASSERT_EQ(__llvm_libc::pthread_mutex_lock(&mutex), 0); | ||
|
|
||
| auto main_thread = __llvm_libc::pthread_self(); | ||
|
|
||
| // The idea here is that, we start a child thread which will immediately | ||
| // wait on |mutex|. The main thread will update the global |child_thread| var | ||
| // and unlock |mutex|. This will give the child thread a chance to compare | ||
| // the result of pthread_self with the |child_thread|. The result of the | ||
| // comparison is returned in the thread arg. | ||
| int result = 0; | ||
| pthread_t th; | ||
| ASSERT_EQ(__llvm_libc::pthread_create(&th, nullptr, child_func, &result), 0); | ||
| // This new thread should of course not be equal to the main thread. | ||
| ASSERT_EQ(__llvm_libc::pthread_equal(th, main_thread), 0); | ||
|
|
||
| // Set the |child_thread| global var and unlock to allow the child to perform | ||
| // the comparison. | ||
| child_thread = th; | ||
| ASSERT_EQ(__llvm_libc::pthread_mutex_unlock(&mutex), 0); | ||
|
|
||
| void *retval; | ||
| ASSERT_EQ(__llvm_libc::pthread_join(th, &retval), 0); | ||
| ASSERT_EQ(uintptr_t(retval), uintptr_t(nullptr)); | ||
| // The child thread should see that pthread_self return value is the same as | ||
| // |child_thread|. | ||
| ASSERT_NE(result, 0); | ||
|
|
||
| __llvm_libc::pthread_mutex_destroy(&mutex); | ||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| //===-- Tests for thrd_equal ----------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/threads/mtx_destroy.h" | ||
| #include "src/threads/mtx_init.h" | ||
| #include "src/threads/mtx_lock.h" | ||
| #include "src/threads/mtx_unlock.h" | ||
| #include "src/threads/thrd_create.h" | ||
| #include "src/threads/thrd_current.h" | ||
| #include "src/threads/thrd_equal.h" | ||
| #include "src/threads/thrd_join.h" | ||
|
|
||
| #include "utils/IntegrationTest/test.h" | ||
|
|
||
| #include <threads.h> | ||
|
|
||
| thrd_t child_thread; | ||
| mtx_t mutex; | ||
|
|
||
| static int child_func(void *arg) { | ||
| __llvm_libc::mtx_lock(&mutex); | ||
| int *ret = reinterpret_cast<int *>(arg); | ||
| auto self = __llvm_libc::thrd_current(); | ||
| *ret = __llvm_libc::thrd_equal(child_thread, self); | ||
| __llvm_libc::mtx_unlock(&mutex); | ||
| return 0; | ||
| } | ||
|
|
||
| int main() { | ||
| // We init and lock the mutex so that we guarantee that the child thread is | ||
| // waiting after startup. | ||
| ASSERT_EQ(__llvm_libc::mtx_init(&mutex, mtx_plain), int(thrd_success)); | ||
| ASSERT_EQ(__llvm_libc::mtx_lock(&mutex), int(thrd_success)); | ||
|
|
||
| auto main_thread = __llvm_libc::thrd_current(); | ||
|
|
||
| // The idea here is that, we start a child thread which will immediately | ||
| // wait on |mutex|. The main thread will update the global |child_thread| var | ||
| // and unlock |mutex|. This will give the child thread a chance to compare | ||
| // the result of thrd_self with the |child_thread|. The result of the | ||
| // comparison is returned in the thread arg. | ||
| int result = 0; | ||
| thrd_t th; | ||
| ASSERT_EQ(__llvm_libc::thrd_create(&th, child_func, &result), | ||
| int(thrd_success)); | ||
| // This new thread should of course not be equal to the main thread. | ||
| ASSERT_EQ(__llvm_libc::thrd_equal(th, main_thread), 0); | ||
|
|
||
| // Set the |child_thread| global var and unlock to allow the child to perform | ||
| // the comparison. | ||
| child_thread = th; | ||
| ASSERT_EQ(__llvm_libc::mtx_unlock(&mutex), int(thrd_success)); | ||
|
|
||
| int retval; | ||
| ASSERT_EQ(__llvm_libc::thrd_join(&th, &retval), int(thrd_success)); | ||
| ASSERT_EQ(retval, 0); | ||
| // The child thread should see that thrd_current return value is the same as | ||
| // |child_thread|. | ||
| ASSERT_NE(result, 0); | ||
|
|
||
| __llvm_libc::mtx_destroy(&mutex); | ||
| return 0; | ||
| } |