2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <gtest/gtest.h>
23 static std::string class_with_dtor_output;
27 void set_message(const std::string& msg) {
32 class_with_dtor_output += message;
38 #if defined(__clang__) && defined(__aarch64__)
39 // b/25642296, aarch64 clang compiled "thread_local" does not link.
40 static ClassWithDtor class_with_dtor;
42 static thread_local ClassWithDtor class_with_dtor;
45 static void* thread_nop(void* arg) {
46 class_with_dtor.set_message(*static_cast<std::string*>(arg));
50 TEST(thread_local, smoke) {
51 std::string msg("dtor called.");
53 ASSERT_EQ(0, pthread_create(&t, nullptr, thread_nop, &msg));
54 ASSERT_EQ(0, pthread_join(t, nullptr));
55 #if defined(__clang__) && defined(__aarch64__)
56 GTEST_LOG_(INFO) << "Skipping test, b/25642296, "
57 << "thread_local does not work with aarch64 clang/llvm.\n";
59 ASSERT_EQ("dtor called.", class_with_dtor_output);
63 class ClassWithDtorForMainThread {
65 void set_message(const std::string& msg) {
69 ~ClassWithDtorForMainThread() {
70 fprintf(stderr, "%s", message.c_str());
76 static void thread_atexit_main() {
77 #if defined(__clang__) && defined(__aarch64__)
78 static ClassWithDtorForMainThread class_with_dtor_for_main_thread;
79 GTEST_LOG_(INFO) << "Skipping test, b/25642296, "
80 << "thread_local does not work with aarch64 clang/llvm.\n";
82 static thread_local ClassWithDtorForMainThread class_with_dtor_for_main_thread;
84 class_with_dtor_for_main_thread.set_message("d-tor for main thread called.");
88 TEST(thread_local, dtor_for_main_thread) {
89 ASSERT_EXIT(thread_atexit_main(), testing::ExitedWithCode(0), "d-tor for main thread called.");
92 extern "C" int __cxa_thread_atexit_impl(void (*fn)(void*), void* arg, void* dso_handle);
94 static void thread_atexit_fn1(void* arg) {
95 std::string* call_sequence = static_cast<std::string*>(arg);
96 *call_sequence += "one, ";
99 static void thread_atexit_fn2(void* arg) {
100 std::string* call_sequence = static_cast<std::string*>(arg);
101 *call_sequence += "two, ";
104 static void thread_atexit_from_atexit(void* arg) {
105 std::string* call_sequence = static_cast<std::string*>(arg);
106 *call_sequence += "oops, ";
109 static void thread_atexit_fn3(void* arg) {
110 __cxa_thread_atexit_impl(thread_atexit_from_atexit, arg, nullptr);
111 std::string* call_sequence = static_cast<std::string*>(arg);
112 *call_sequence += "three, ";
115 static void thread_atexit_fn4(void* arg) {
116 std::string* call_sequence = static_cast<std::string*>(arg);
117 *call_sequence += "four, ";
120 static void thread_atexit_fn5(void* arg) {
121 std::string* call_sequence = static_cast<std::string*>(arg);
122 *call_sequence += "five.";
125 static void* thread_main(void* arg) {
126 __cxa_thread_atexit_impl(thread_atexit_fn5, arg, nullptr);
127 __cxa_thread_atexit_impl(thread_atexit_fn4, arg, nullptr);
128 __cxa_thread_atexit_impl(thread_atexit_fn3, arg, nullptr);
129 __cxa_thread_atexit_impl(thread_atexit_fn2, arg, nullptr);
130 __cxa_thread_atexit_impl(thread_atexit_fn1, arg, nullptr);
134 TEST(__cxa_thread_atexit_impl, smoke) {
135 std::string atexit_call_sequence;
138 ASSERT_EQ(0, pthread_create(&t, nullptr, thread_main, &atexit_call_sequence));
139 ASSERT_EQ(0, pthread_join(t, nullptr));
140 ASSERT_EQ("one, two, three, oops, four, five.", atexit_call_sequence);