xref: /llvm-project/libc/test/integration/src/threads/thrd_equal_test.cpp (revision b6bc9d72f65a5086f310f321e969d96e9a559e75)
18dc42802SSiva Chandra Reddy //===-- Tests for thrd_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/threads/mtx_destroy.h"
108dc42802SSiva Chandra Reddy #include "src/threads/mtx_init.h"
118dc42802SSiva Chandra Reddy #include "src/threads/mtx_lock.h"
128dc42802SSiva Chandra Reddy #include "src/threads/mtx_unlock.h"
138dc42802SSiva Chandra Reddy #include "src/threads/thrd_create.h"
148dc42802SSiva Chandra Reddy #include "src/threads/thrd_current.h"
158dc42802SSiva Chandra Reddy #include "src/threads/thrd_equal.h"
168dc42802SSiva Chandra Reddy #include "src/threads/thrd_join.h"
178dc42802SSiva Chandra Reddy 
18af1315c2SSiva Chandra Reddy #include "test/IntegrationTest/test.h"
198dc42802SSiva Chandra Reddy 
208dc42802SSiva Chandra Reddy #include <threads.h>
218dc42802SSiva Chandra Reddy 
228dc42802SSiva Chandra Reddy thrd_t child_thread;
238dc42802SSiva Chandra Reddy mtx_t mutex;
248dc42802SSiva Chandra Reddy 
child_func(void * arg)258dc42802SSiva Chandra Reddy static int child_func(void *arg) {
26*b6bc9d72SGuillaume Chatelet   LIBC_NAMESPACE::mtx_lock(&mutex);
278dc42802SSiva Chandra Reddy   int *ret = reinterpret_cast<int *>(arg);
28*b6bc9d72SGuillaume Chatelet   auto self = LIBC_NAMESPACE::thrd_current();
29*b6bc9d72SGuillaume Chatelet   *ret = LIBC_NAMESPACE::thrd_equal(child_thread, self);
30*b6bc9d72SGuillaume Chatelet   LIBC_NAMESPACE::mtx_unlock(&mutex);
318dc42802SSiva Chandra Reddy   return 0;
328dc42802SSiva Chandra Reddy }
338dc42802SSiva Chandra Reddy 
TEST_MAIN()3412df3080SSiva Chandra Reddy TEST_MAIN() {
358dc42802SSiva Chandra Reddy   // We init and lock the mutex so that we guarantee that the child thread is
368dc42802SSiva Chandra Reddy   // waiting after startup.
37*b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::mtx_init(&mutex, mtx_plain), int(thrd_success));
38*b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::mtx_lock(&mutex), int(thrd_success));
398dc42802SSiva Chandra Reddy 
40*b6bc9d72SGuillaume Chatelet   auto main_thread = LIBC_NAMESPACE::thrd_current();
418dc42802SSiva Chandra Reddy 
428dc42802SSiva Chandra Reddy   // The idea here is that, we start a child thread which will immediately
438dc42802SSiva Chandra Reddy   // wait on |mutex|. The main thread will update the global |child_thread| var
448dc42802SSiva Chandra Reddy   // and unlock |mutex|. This will give the child thread a chance to compare
458dc42802SSiva Chandra Reddy   // the result of thrd_self with the |child_thread|. The result of the
468dc42802SSiva Chandra Reddy   // comparison is returned in the thread arg.
478dc42802SSiva Chandra Reddy   int result = 0;
488dc42802SSiva Chandra Reddy   thrd_t th;
49*b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::thrd_create(&th, child_func, &result),
508dc42802SSiva Chandra Reddy             int(thrd_success));
518dc42802SSiva Chandra Reddy   // This new thread should of course not be equal to the main thread.
52*b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::thrd_equal(th, main_thread), 0);
538dc42802SSiva Chandra Reddy 
548dc42802SSiva Chandra Reddy   // Set the |child_thread| global var and unlock to allow the child to perform
558dc42802SSiva Chandra Reddy   // the comparison.
568dc42802SSiva Chandra Reddy   child_thread = th;
57*b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::mtx_unlock(&mutex), int(thrd_success));
588dc42802SSiva Chandra Reddy 
598dc42802SSiva Chandra Reddy   int retval;
60*b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::thrd_join(th, &retval), int(thrd_success));
618dc42802SSiva Chandra Reddy   ASSERT_EQ(retval, 0);
628dc42802SSiva Chandra Reddy   // The child thread should see that thrd_current return value is the same as
638dc42802SSiva Chandra Reddy   // |child_thread|.
648dc42802SSiva Chandra Reddy   ASSERT_NE(result, 0);
658dc42802SSiva Chandra Reddy 
66*b6bc9d72SGuillaume Chatelet   LIBC_NAMESPACE::mtx_destroy(&mutex);
678dc42802SSiva Chandra Reddy   return 0;
688dc42802SSiva Chandra Reddy }
69