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 static thread_local ClassWithDtor class_with_dtor;
40 static void* thread_nop(void* arg) {
41 class_with_dtor.set_message(*static_cast<std::string*>(arg));
45 TEST(thread_local, smoke) {
46 std::string msg("dtor called.");
48 ASSERT_EQ(0, pthread_create(&t, nullptr, thread_nop, &msg));
49 ASSERT_EQ(0, pthread_join(t, nullptr));
50 ASSERT_EQ("dtor called.", class_with_dtor_output);
53 class ClassWithDtorForMainThread {
55 void set_message(const std::string& msg) {
59 ~ClassWithDtorForMainThread() {
60 fprintf(stderr, "%s", message.c_str());
66 static void thread_atexit_main() {
67 static thread_local ClassWithDtorForMainThread class_with_dtor_for_main_thread;
68 class_with_dtor_for_main_thread.set_message("d-tor for main thread called.");
72 TEST(thread_local, dtor_for_main_thread) {
73 ASSERT_EXIT(thread_atexit_main(), testing::ExitedWithCode(0), "d-tor for main thread called.");
76 extern "C" int __cxa_thread_atexit_impl(void (*fn)(void*), void* arg, void* dso_handle);
78 static void thread_atexit_fn1(void* arg) {
79 std::string* call_sequence = static_cast<std::string*>(arg);
80 *call_sequence += "one, ";
83 static void thread_atexit_fn2(void* arg) {
84 std::string* call_sequence = static_cast<std::string*>(arg);
85 *call_sequence += "two, ";
88 static void thread_atexit_from_atexit(void* arg) {
89 std::string* call_sequence = static_cast<std::string*>(arg);
90 *call_sequence += "oops, ";
93 static void thread_atexit_fn3(void* arg) {
94 __cxa_thread_atexit_impl(thread_atexit_from_atexit, arg, nullptr);
95 std::string* call_sequence = static_cast<std::string*>(arg);
96 *call_sequence += "three, ";
99 static void thread_atexit_fn4(void* arg) {
100 std::string* call_sequence = static_cast<std::string*>(arg);
101 *call_sequence += "four, ";
104 static void thread_atexit_fn5(void* arg) {
105 std::string* call_sequence = static_cast<std::string*>(arg);
106 *call_sequence += "five.";
109 static void* thread_main(void* arg) {
110 __cxa_thread_atexit_impl(thread_atexit_fn5, arg, nullptr);
111 __cxa_thread_atexit_impl(thread_atexit_fn4, arg, nullptr);
112 __cxa_thread_atexit_impl(thread_atexit_fn3, arg, nullptr);
113 __cxa_thread_atexit_impl(thread_atexit_fn2, arg, nullptr);
114 __cxa_thread_atexit_impl(thread_atexit_fn1, arg, nullptr);
118 TEST(__cxa_thread_atexit_impl, smoke) {
119 std::string atexit_call_sequence;
122 ASSERT_EQ(0, pthread_create(&t, nullptr, thread_main, &atexit_call_sequence));
123 ASSERT_EQ(0, pthread_join(t, nullptr));
124 ASSERT_EQ("one, two, three, oops, four, five.", atexit_call_sequence);