__cxa_thread_atexit_test.cpp revision 66aa0b61f736678e97a0cfaf975052881a23651b
1/* 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 23static 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 38static thread_local ClassWithDtor class_with_dtor; 39 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 53extern "C" int __cxa_thread_atexit_impl(void (*fn)(void*), void* arg, void* dso_handle); 54 55static void thread_atexit_fn1(void* arg) { 56 std::string* call_sequence = static_cast<std::string*>(arg); 57 *call_sequence += "one, "; 58} 59 60static void thread_atexit_fn2(void* arg) { 61 std::string* call_sequence = static_cast<std::string*>(arg); 62 *call_sequence += "two, "; 63} 64 65static void thread_atexit_from_atexit(void* arg) { 66 std::string* call_sequence = static_cast<std::string*>(arg); 67 *call_sequence += "oops, "; 68} 69 70static void thread_atexit_fn3(void* arg) { 71 __cxa_thread_atexit_impl(thread_atexit_from_atexit, arg, nullptr); 72 std::string* call_sequence = static_cast<std::string*>(arg); 73 *call_sequence += "three, "; 74} 75 76static void thread_atexit_fn4(void* arg) { 77 std::string* call_sequence = static_cast<std::string*>(arg); 78 *call_sequence += "four, "; 79} 80 81static void thread_atexit_fn5(void* arg) { 82 std::string* call_sequence = static_cast<std::string*>(arg); 83 *call_sequence += "five."; 84} 85 86static void* thread_main(void* arg) { 87 __cxa_thread_atexit_impl(thread_atexit_fn5, arg, nullptr); 88 __cxa_thread_atexit_impl(thread_atexit_fn4, arg, nullptr); 89 __cxa_thread_atexit_impl(thread_atexit_fn3, arg, nullptr); 90 __cxa_thread_atexit_impl(thread_atexit_fn2, arg, nullptr); 91 __cxa_thread_atexit_impl(thread_atexit_fn1, arg, nullptr); 92 return nullptr; 93} 94 95TEST(__cxa_thread_atexit_impl, smoke) { 96 std::string atexit_call_sequence; 97 98 pthread_t t; 99 ASSERT_EQ(0, pthread_create(&t, nullptr, thread_main, &atexit_call_sequence)); 100 ASSERT_EQ(0, pthread_join(t, nullptr)); 101 ASSERT_EQ("one, two, three, oops, four, five.", atexit_call_sequence); 102} 103 104 105