platform_bionic/tests/__cxa_thread_atexit_test.cpp
Dmitriy Ivanov 66aa0b61f7 Hide emutls* symbols in libc.so
Also make thread_local in test static to avoid ld.bfd
 warning for arm64.

Change-Id: I09a3f2aa9b73a4fafa3f3bbc64ddc2a128ad50ee
2015-04-15 14:23:00 -07:00

104 lines
2.9 KiB
C++

/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <stdint.h>
#include <string>
static std::string class_with_dtor_output;
class ClassWithDtor {
public:
void set_message(const std::string& msg) {
message = msg;
}
~ClassWithDtor() {
class_with_dtor_output += message;
}
private:
std::string message;
};
static thread_local ClassWithDtor class_with_dtor;
static void* thread_nop(void* arg) {
class_with_dtor.set_message(*static_cast<std::string*>(arg));
return nullptr;
}
TEST(thread_local, smoke) {
std::string msg("dtor called.");
pthread_t t;
ASSERT_EQ(0, pthread_create(&t, nullptr, thread_nop, &msg));
ASSERT_EQ(0, pthread_join(t, nullptr));
ASSERT_EQ("dtor called.", class_with_dtor_output);
}
extern "C" int __cxa_thread_atexit_impl(void (*fn)(void*), void* arg, void* dso_handle);
static void thread_atexit_fn1(void* arg) {
std::string* call_sequence = static_cast<std::string*>(arg);
*call_sequence += "one, ";
}
static void thread_atexit_fn2(void* arg) {
std::string* call_sequence = static_cast<std::string*>(arg);
*call_sequence += "two, ";
}
static void thread_atexit_from_atexit(void* arg) {
std::string* call_sequence = static_cast<std::string*>(arg);
*call_sequence += "oops, ";
}
static void thread_atexit_fn3(void* arg) {
__cxa_thread_atexit_impl(thread_atexit_from_atexit, arg, nullptr);
std::string* call_sequence = static_cast<std::string*>(arg);
*call_sequence += "three, ";
}
static void thread_atexit_fn4(void* arg) {
std::string* call_sequence = static_cast<std::string*>(arg);
*call_sequence += "four, ";
}
static void thread_atexit_fn5(void* arg) {
std::string* call_sequence = static_cast<std::string*>(arg);
*call_sequence += "five.";
}
static void* thread_main(void* arg) {
__cxa_thread_atexit_impl(thread_atexit_fn5, arg, nullptr);
__cxa_thread_atexit_impl(thread_atexit_fn4, arg, nullptr);
__cxa_thread_atexit_impl(thread_atexit_fn3, arg, nullptr);
__cxa_thread_atexit_impl(thread_atexit_fn2, arg, nullptr);
__cxa_thread_atexit_impl(thread_atexit_fn1, arg, nullptr);
return nullptr;
}
TEST(__cxa_thread_atexit_impl, smoke) {
std::string atexit_call_sequence;
pthread_t t;
ASSERT_EQ(0, pthread_create(&t, nullptr, thread_main, &atexit_call_sequence));
ASSERT_EQ(0, pthread_join(t, nullptr));
ASSERT_EQ("one, two, three, oops, four, five.", atexit_call_sequence);
}