1*8dc42802SSiva Chandra Reddy //===-- Tests for pthread_equal -------------------------------------------===//
2*8dc42802SSiva Chandra Reddy //
3*8dc42802SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*8dc42802SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
5*8dc42802SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*8dc42802SSiva Chandra Reddy //
7*8dc42802SSiva Chandra Reddy //===----------------------------------------------------------------------===//
8*8dc42802SSiva Chandra Reddy
9*8dc42802SSiva Chandra Reddy #include "src/pthread/pthread_create.h"
10*8dc42802SSiva Chandra Reddy #include "src/pthread/pthread_equal.h"
11*8dc42802SSiva Chandra Reddy #include "src/pthread/pthread_join.h"
12*8dc42802SSiva Chandra Reddy #include "src/pthread/pthread_mutex_destroy.h"
13*8dc42802SSiva Chandra Reddy #include "src/pthread/pthread_mutex_init.h"
14*8dc42802SSiva Chandra Reddy #include "src/pthread/pthread_mutex_lock.h"
15*8dc42802SSiva Chandra Reddy #include "src/pthread/pthread_mutex_unlock.h"
16*8dc42802SSiva Chandra Reddy #include "src/pthread/pthread_self.h"
17*8dc42802SSiva Chandra Reddy
18*8dc42802SSiva Chandra Reddy #include "utils/IntegrationTest/test.h"
19*8dc42802SSiva Chandra Reddy
20*8dc42802SSiva Chandra Reddy #include <pthread.h>
21*8dc42802SSiva Chandra Reddy
22*8dc42802SSiva Chandra Reddy pthread_t child_thread;
23*8dc42802SSiva Chandra Reddy pthread_mutex_t mutex;
24*8dc42802SSiva Chandra Reddy
child_func(void * arg)25*8dc42802SSiva Chandra Reddy static void *child_func(void *arg) {
26*8dc42802SSiva Chandra Reddy __llvm_libc::pthread_mutex_lock(&mutex);
27*8dc42802SSiva Chandra Reddy int *ret = reinterpret_cast<int *>(arg);
28*8dc42802SSiva Chandra Reddy auto self = __llvm_libc::pthread_self();
29*8dc42802SSiva Chandra Reddy *ret = __llvm_libc::pthread_equal(child_thread, self);
30*8dc42802SSiva Chandra Reddy __llvm_libc::pthread_mutex_unlock(&mutex);
31*8dc42802SSiva Chandra Reddy return nullptr;
32*8dc42802SSiva Chandra Reddy }
33*8dc42802SSiva Chandra Reddy
main()34*8dc42802SSiva Chandra Reddy int main() {
35*8dc42802SSiva Chandra Reddy // We init and lock the mutex so that we guarantee that the child thread is
36*8dc42802SSiva Chandra Reddy // waiting after startup.
37*8dc42802SSiva Chandra Reddy ASSERT_EQ(__llvm_libc::pthread_mutex_init(&mutex, nullptr), 0);
38*8dc42802SSiva Chandra Reddy ASSERT_EQ(__llvm_libc::pthread_mutex_lock(&mutex), 0);
39*8dc42802SSiva Chandra Reddy
40*8dc42802SSiva Chandra Reddy auto main_thread = __llvm_libc::pthread_self();
41*8dc42802SSiva Chandra Reddy
42*8dc42802SSiva Chandra Reddy // The idea here is that, we start a child thread which will immediately
43*8dc42802SSiva Chandra Reddy // wait on |mutex|. The main thread will update the global |child_thread| var
44*8dc42802SSiva Chandra Reddy // and unlock |mutex|. This will give the child thread a chance to compare
45*8dc42802SSiva Chandra Reddy // the result of pthread_self with the |child_thread|. The result of the
46*8dc42802SSiva Chandra Reddy // comparison is returned in the thread arg.
47*8dc42802SSiva Chandra Reddy int result = 0;
48*8dc42802SSiva Chandra Reddy pthread_t th;
49*8dc42802SSiva Chandra Reddy ASSERT_EQ(__llvm_libc::pthread_create(&th, nullptr, child_func, &result), 0);
50*8dc42802SSiva Chandra Reddy // This new thread should of course not be equal to the main thread.
51*8dc42802SSiva Chandra Reddy ASSERT_EQ(__llvm_libc::pthread_equal(th, main_thread), 0);
52*8dc42802SSiva Chandra Reddy
53*8dc42802SSiva Chandra Reddy // Set the |child_thread| global var and unlock to allow the child to perform
54*8dc42802SSiva Chandra Reddy // the comparison.
55*8dc42802SSiva Chandra Reddy child_thread = th;
56*8dc42802SSiva Chandra Reddy ASSERT_EQ(__llvm_libc::pthread_mutex_unlock(&mutex), 0);
57*8dc42802SSiva Chandra Reddy
58*8dc42802SSiva Chandra Reddy void *retval;
59*8dc42802SSiva Chandra Reddy ASSERT_EQ(__llvm_libc::pthread_join(th, &retval), 0);
60*8dc42802SSiva Chandra Reddy ASSERT_EQ(uintptr_t(retval), uintptr_t(nullptr));
61*8dc42802SSiva Chandra Reddy // The child thread should see that pthread_self return value is the same as
62*8dc42802SSiva Chandra Reddy // |child_thread|.
63*8dc42802SSiva Chandra Reddy ASSERT_NE(result, 0);
64*8dc42802SSiva Chandra Reddy
65*8dc42802SSiva Chandra Reddy __llvm_libc::pthread_mutex_destroy(&mutex);
66*8dc42802SSiva Chandra Reddy return 0;
67*8dc42802SSiva Chandra Reddy }
68