1*8dc42802SSiva Chandra Reddy //===-- Tests for thrd_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/threads/mtx_destroy.h"
10*8dc42802SSiva Chandra Reddy #include "src/threads/mtx_init.h"
11*8dc42802SSiva Chandra Reddy #include "src/threads/mtx_lock.h"
12*8dc42802SSiva Chandra Reddy #include "src/threads/mtx_unlock.h"
13*8dc42802SSiva Chandra Reddy #include "src/threads/thrd_create.h"
14*8dc42802SSiva Chandra Reddy #include "src/threads/thrd_current.h"
15*8dc42802SSiva Chandra Reddy #include "src/threads/thrd_equal.h"
16*8dc42802SSiva Chandra Reddy #include "src/threads/thrd_join.h"
17*8dc42802SSiva Chandra Reddy
18*8dc42802SSiva Chandra Reddy #include "utils/IntegrationTest/test.h"
19*8dc42802SSiva Chandra Reddy
20*8dc42802SSiva Chandra Reddy #include <threads.h>
21*8dc42802SSiva Chandra Reddy
22*8dc42802SSiva Chandra Reddy thrd_t child_thread;
23*8dc42802SSiva Chandra Reddy mtx_t mutex;
24*8dc42802SSiva Chandra Reddy
child_func(void * arg)25*8dc42802SSiva Chandra Reddy static int child_func(void *arg) {
26*8dc42802SSiva Chandra Reddy __llvm_libc::mtx_lock(&mutex);
27*8dc42802SSiva Chandra Reddy int *ret = reinterpret_cast<int *>(arg);
28*8dc42802SSiva Chandra Reddy auto self = __llvm_libc::thrd_current();
29*8dc42802SSiva Chandra Reddy *ret = __llvm_libc::thrd_equal(child_thread, self);
30*8dc42802SSiva Chandra Reddy __llvm_libc::mtx_unlock(&mutex);
31*8dc42802SSiva Chandra Reddy return 0;
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::mtx_init(&mutex, mtx_plain), int(thrd_success));
38*8dc42802SSiva Chandra Reddy ASSERT_EQ(__llvm_libc::mtx_lock(&mutex), int(thrd_success));
39*8dc42802SSiva Chandra Reddy
40*8dc42802SSiva Chandra Reddy auto main_thread = __llvm_libc::thrd_current();
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 thrd_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 thrd_t th;
49*8dc42802SSiva Chandra Reddy ASSERT_EQ(__llvm_libc::thrd_create(&th, child_func, &result),
50*8dc42802SSiva Chandra Reddy int(thrd_success));
51*8dc42802SSiva Chandra Reddy // This new thread should of course not be equal to the main thread.
52*8dc42802SSiva Chandra Reddy ASSERT_EQ(__llvm_libc::thrd_equal(th, main_thread), 0);
53*8dc42802SSiva Chandra Reddy
54*8dc42802SSiva Chandra Reddy // Set the |child_thread| global var and unlock to allow the child to perform
55*8dc42802SSiva Chandra Reddy // the comparison.
56*8dc42802SSiva Chandra Reddy child_thread = th;
57*8dc42802SSiva Chandra Reddy ASSERT_EQ(__llvm_libc::mtx_unlock(&mutex), int(thrd_success));
58*8dc42802SSiva Chandra Reddy
59*8dc42802SSiva Chandra Reddy int retval;
60*8dc42802SSiva Chandra Reddy ASSERT_EQ(__llvm_libc::thrd_join(&th, &retval), int(thrd_success));
61*8dc42802SSiva Chandra Reddy ASSERT_EQ(retval, 0);
62*8dc42802SSiva Chandra Reddy // The child thread should see that thrd_current return value is the same as
63*8dc42802SSiva Chandra Reddy // |child_thread|.
64*8dc42802SSiva Chandra Reddy ASSERT_NE(result, 0);
65*8dc42802SSiva Chandra Reddy
66*8dc42802SSiva Chandra Reddy __llvm_libc::mtx_destroy(&mutex);
67*8dc42802SSiva Chandra Reddy return 0;
68*8dc42802SSiva Chandra Reddy }
69