18dc42802SSiva Chandra Reddy //===-- Tests for pthread_equal -------------------------------------------===//
28dc42802SSiva Chandra Reddy //
38dc42802SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
48dc42802SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
58dc42802SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68dc42802SSiva Chandra Reddy //
78dc42802SSiva Chandra Reddy //===----------------------------------------------------------------------===//
88dc42802SSiva Chandra Reddy
98dc42802SSiva Chandra Reddy #include "src/pthread/pthread_create.h"
108dc42802SSiva Chandra Reddy #include "src/pthread/pthread_equal.h"
118dc42802SSiva Chandra Reddy #include "src/pthread/pthread_join.h"
128dc42802SSiva Chandra Reddy #include "src/pthread/pthread_mutex_destroy.h"
138dc42802SSiva Chandra Reddy #include "src/pthread/pthread_mutex_init.h"
148dc42802SSiva Chandra Reddy #include "src/pthread/pthread_mutex_lock.h"
158dc42802SSiva Chandra Reddy #include "src/pthread/pthread_mutex_unlock.h"
168dc42802SSiva Chandra Reddy #include "src/pthread/pthread_self.h"
178dc42802SSiva Chandra Reddy
18af1315c2SSiva Chandra Reddy #include "test/IntegrationTest/test.h"
198dc42802SSiva Chandra Reddy
208dc42802SSiva Chandra Reddy #include <pthread.h>
21d9c135cfSGuillaume Chatelet #include <stdint.h> // uintptr_t
228dc42802SSiva Chandra Reddy
238dc42802SSiva Chandra Reddy pthread_t child_thread;
248dc42802SSiva Chandra Reddy pthread_mutex_t mutex;
258dc42802SSiva Chandra Reddy
child_func(void * arg)268dc42802SSiva Chandra Reddy static void *child_func(void *arg) {
27*b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::pthread_mutex_lock(&mutex);
288dc42802SSiva Chandra Reddy int *ret = reinterpret_cast<int *>(arg);
29*b6bc9d72SGuillaume Chatelet auto self = LIBC_NAMESPACE::pthread_self();
30*b6bc9d72SGuillaume Chatelet *ret = LIBC_NAMESPACE::pthread_equal(child_thread, self);
31*b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::pthread_mutex_unlock(&mutex);
328dc42802SSiva Chandra Reddy return nullptr;
338dc42802SSiva Chandra Reddy }
348dc42802SSiva Chandra Reddy
TEST_MAIN()3512df3080SSiva Chandra Reddy TEST_MAIN() {
368dc42802SSiva Chandra Reddy // We init and lock the mutex so that we guarantee that the child thread is
378dc42802SSiva Chandra Reddy // waiting after startup.
38*b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_init(&mutex, nullptr), 0);
39*b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_lock(&mutex), 0);
408dc42802SSiva Chandra Reddy
41*b6bc9d72SGuillaume Chatelet auto main_thread = LIBC_NAMESPACE::pthread_self();
428dc42802SSiva Chandra Reddy
438dc42802SSiva Chandra Reddy // The idea here is that, we start a child thread which will immediately
448dc42802SSiva Chandra Reddy // wait on |mutex|. The main thread will update the global |child_thread| var
458dc42802SSiva Chandra Reddy // and unlock |mutex|. This will give the child thread a chance to compare
468dc42802SSiva Chandra Reddy // the result of pthread_self with the |child_thread|. The result of the
478dc42802SSiva Chandra Reddy // comparison is returned in the thread arg.
488dc42802SSiva Chandra Reddy int result = 0;
498dc42802SSiva Chandra Reddy pthread_t th;
50*b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::pthread_create(&th, nullptr, child_func, &result),
51*b6bc9d72SGuillaume Chatelet 0);
528dc42802SSiva Chandra Reddy // This new thread should of course not be equal to the main thread.
53*b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::pthread_equal(th, main_thread), 0);
548dc42802SSiva Chandra Reddy
558dc42802SSiva Chandra Reddy // Set the |child_thread| global var and unlock to allow the child to perform
568dc42802SSiva Chandra Reddy // the comparison.
578dc42802SSiva Chandra Reddy child_thread = th;
58*b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_unlock(&mutex), 0);
598dc42802SSiva Chandra Reddy
608dc42802SSiva Chandra Reddy void *retval;
61*b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::pthread_join(th, &retval), 0);
628dc42802SSiva Chandra Reddy ASSERT_EQ(uintptr_t(retval), uintptr_t(nullptr));
638dc42802SSiva Chandra Reddy // The child thread should see that pthread_self return value is the same as
648dc42802SSiva Chandra Reddy // |child_thread|.
658dc42802SSiva Chandra Reddy ASSERT_NE(result, 0);
668dc42802SSiva Chandra Reddy
67*b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::pthread_mutex_destroy(&mutex);
688dc42802SSiva Chandra Reddy return 0;
698dc42802SSiva Chandra Reddy }
70