blob: 143296843b27f9dc2ace47d07f65d29835c8cc76 [file] [log] [blame]
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <gtest/gtest.h>
18
19#include <stdint.h>
20
21#include <string>
22
Dmitriy Ivanov947aded2015-03-26 11:07:04 -070023static std::string class_with_dtor_output;
24
25class ClassWithDtor {
26 public:
27 void set_message(const std::string& msg) {
28 message = msg;
29 }
30
31 ~ClassWithDtor() {
32 class_with_dtor_output += message;
33 }
34 private:
35 std::string message;
36};
37
Chih-Hung Hsiehf688c5d2015-11-20 14:13:24 -080038#if defined(__clang__) && defined(__aarch64__)
39// b/25642296, aarch64 clang compiled "thread_local" does not link.
40static ClassWithDtor class_with_dtor;
41#else
Dmitriy Ivanov66aa0b62015-04-15 14:07:28 -070042static thread_local ClassWithDtor class_with_dtor;
Chih-Hung Hsiehf688c5d2015-11-20 14:13:24 -080043#endif
Dmitriy Ivanov947aded2015-03-26 11:07:04 -070044
45static void* thread_nop(void* arg) {
46 class_with_dtor.set_message(*static_cast<std::string*>(arg));
47 return nullptr;
48}
49
50TEST(thread_local, smoke) {
51 std::string msg("dtor called.");
52 pthread_t t;
53 ASSERT_EQ(0, pthread_create(&t, nullptr, thread_nop, &msg));
54 ASSERT_EQ(0, pthread_join(t, nullptr));
Chih-Hung Hsiehf688c5d2015-11-20 14:13:24 -080055#if defined(__clang__) && defined(__aarch64__)
56 GTEST_LOG_(INFO) << "Skipping test, b/25642296, "
57 << "thread_local does not work with aarch64 clang/llvm.\n";
58#else
Dmitriy Ivanov947aded2015-03-26 11:07:04 -070059 ASSERT_EQ("dtor called.", class_with_dtor_output);
Chih-Hung Hsiehf688c5d2015-11-20 14:13:24 -080060#endif
Dmitriy Ivanov947aded2015-03-26 11:07:04 -070061}
62
Dmitriy Ivanov163ab8b2015-04-15 15:31:51 -070063class ClassWithDtorForMainThread {
64 public:
65 void set_message(const std::string& msg) {
66 message = msg;
67 }
68
69 ~ClassWithDtorForMainThread() {
70 fprintf(stderr, "%s", message.c_str());
71 }
72 private:
73 std::string message;
74};
75
76static void thread_atexit_main() {
Chih-Hung Hsiehf688c5d2015-11-20 14:13:24 -080077#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";
81#else
Dmitriy Ivanov163ab8b2015-04-15 15:31:51 -070082 static thread_local ClassWithDtorForMainThread class_with_dtor_for_main_thread;
Chih-Hung Hsiehf688c5d2015-11-20 14:13:24 -080083#endif
Dmitriy Ivanov163ab8b2015-04-15 15:31:51 -070084 class_with_dtor_for_main_thread.set_message("d-tor for main thread called.");
85 exit(0);
86}
87
88TEST(thread_local, dtor_for_main_thread) {
89 ASSERT_EXIT(thread_atexit_main(), testing::ExitedWithCode(0), "d-tor for main thread called.");
90}
91
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -070092extern "C" int __cxa_thread_atexit_impl(void (*fn)(void*), void* arg, void* dso_handle);
93
94static void thread_atexit_fn1(void* arg) {
95 std::string* call_sequence = static_cast<std::string*>(arg);
96 *call_sequence += "one, ";
97}
98
99static void thread_atexit_fn2(void* arg) {
100 std::string* call_sequence = static_cast<std::string*>(arg);
101 *call_sequence += "two, ";
102}
103
104static void thread_atexit_from_atexit(void* arg) {
105 std::string* call_sequence = static_cast<std::string*>(arg);
106 *call_sequence += "oops, ";
107}
108
109static 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, ";
113}
114
115static void thread_atexit_fn4(void* arg) {
116 std::string* call_sequence = static_cast<std::string*>(arg);
117 *call_sequence += "four, ";
118}
119
120static void thread_atexit_fn5(void* arg) {
121 std::string* call_sequence = static_cast<std::string*>(arg);
122 *call_sequence += "five.";
123}
124
125static 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);
131 return nullptr;
132}
133
134TEST(__cxa_thread_atexit_impl, smoke) {
135 std::string atexit_call_sequence;
136
137 pthread_t t;
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);
141}
142
143