xref: /aosp_15_r20/external/llvm-libc/test/integration/src/threads/thrd_exit_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Tests for thrd_exit -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/threads/thrd_create.h"
10 #include "src/threads/thrd_exit.h"
11 #include "src/threads/thrd_join.h"
12 #include "test/IntegrationTest/test.h"
13 
14 #include <threads.h>
15 
16 bool dtor_called = false;
17 
18 class A {
19   int val;
20 
21 public:
A(int i)22   A(int i) { val = i; }
23 
set(int i)24   void set(int i) { val = i; }
25 
~A()26   ~A() {
27     val = 0;
28     dtor_called = true;
29   }
30 };
31 
32 thread_local A thread_local_a(123);
33 
func(void *)34 int func(void *) {
35   thread_local_a.set(321);
36   LIBC_NAMESPACE::thrd_exit(0);
37   return 0;
38 }
39 
TEST_MAIN()40 TEST_MAIN() {
41   thrd_t th;
42   int retval;
43 
44   ASSERT_EQ(LIBC_NAMESPACE::thrd_create(&th, func, nullptr), thrd_success);
45   ASSERT_EQ(LIBC_NAMESPACE::thrd_join(th, &retval), thrd_success);
46 
47   ASSERT_TRUE(dtor_called);
48   LIBC_NAMESPACE::thrd_exit(0);
49   return 0;
50 }
51 
52 extern "C" {
53 
54 using Destructor = void(void *);
55 
56 int __cxa_thread_atexit_impl(Destructor *, void *, void *);
57 
58 // We do not link integration tests to C++ runtime pieces like the libcxxabi.
59 // So, we provide our own simple __cxa_thread_atexit implementation.
__cxa_thread_atexit(Destructor * dtor,void * obj,void *)60 int __cxa_thread_atexit(Destructor *dtor, void *obj, void *) {
61   return __cxa_thread_atexit_impl(dtor, obj, nullptr);
62 }
63 }
64