blob: e388f3b62df669d405c3530d11af1e373b264a99 [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
Dmitriy Ivanov66aa0b62015-04-15 14:07:28 -070038static thread_local ClassWithDtor class_with_dtor;
Dmitriy Ivanov947aded2015-03-26 11:07:04 -070039
40static void* thread_nop(void* arg) {
41 class_with_dtor.set_message(*static_cast<std::string*>(arg));
42 return nullptr;
43}
44
45TEST(thread_local, smoke) {
46 std::string msg("dtor called.");
47 pthread_t t;
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);
51}
52
Dmitriy Ivanov163ab8b2015-04-15 15:31:51 -070053class ClassWithDtorForMainThread {
54 public:
55 void set_message(const std::string& msg) {
56 message = msg;
57 }
58
59 ~ClassWithDtorForMainThread() {
60 fprintf(stderr, "%s", message.c_str());
61 }
62 private:
63 std::string message;
64};
65
66static 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.");
69 exit(0);
70}
71
72TEST(thread_local, dtor_for_main_thread) {
73 ASSERT_EXIT(thread_atexit_main(), testing::ExitedWithCode(0), "d-tor for main thread called.");
74}
75
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -070076extern "C" int __cxa_thread_atexit_impl(void (*fn)(void*), void* arg, void* dso_handle);
77
78static void thread_atexit_fn1(void* arg) {
79 std::string* call_sequence = static_cast<std::string*>(arg);
80 *call_sequence += "one, ";
81}
82
83static void thread_atexit_fn2(void* arg) {
84 std::string* call_sequence = static_cast<std::string*>(arg);
85 *call_sequence += "two, ";
86}
87
88static void thread_atexit_from_atexit(void* arg) {
89 std::string* call_sequence = static_cast<std::string*>(arg);
90 *call_sequence += "oops, ";
91}
92
93static 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, ";
97}
98
99static void thread_atexit_fn4(void* arg) {
100 std::string* call_sequence = static_cast<std::string*>(arg);
101 *call_sequence += "four, ";
102}
103
104static void thread_atexit_fn5(void* arg) {
105 std::string* call_sequence = static_cast<std::string*>(arg);
106 *call_sequence += "five.";
107}
108
109static 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);
115 return nullptr;
116}
117
118TEST(__cxa_thread_atexit_impl, smoke) {
119 std::string atexit_call_sequence;
120
121 pthread_t t;
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);
125}
126
127