__cxa_thread_atexit_test.cpp revision df79c330d895af31f39ee301dee62731fa586168
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
23extern "C" int __cxa_thread_atexit_impl(void (*fn)(void*), void* arg, void* dso_handle);
24
25static void thread_atexit_fn1(void* arg) {
26  std::string* call_sequence = static_cast<std::string*>(arg);
27  *call_sequence += "one, ";
28}
29
30static void thread_atexit_fn2(void* arg) {
31  std::string* call_sequence = static_cast<std::string*>(arg);
32  *call_sequence += "two, ";
33}
34
35static void thread_atexit_from_atexit(void* arg) {
36  std::string* call_sequence = static_cast<std::string*>(arg);
37  *call_sequence += "oops, ";
38}
39
40static void thread_atexit_fn3(void* arg) {
41  __cxa_thread_atexit_impl(thread_atexit_from_atexit, arg, nullptr);
42  std::string* call_sequence = static_cast<std::string*>(arg);
43  *call_sequence += "three, ";
44}
45
46static void thread_atexit_fn4(void* arg) {
47  std::string* call_sequence = static_cast<std::string*>(arg);
48  *call_sequence += "four, ";
49}
50
51static void thread_atexit_fn5(void* arg) {
52  std::string* call_sequence = static_cast<std::string*>(arg);
53  *call_sequence += "five.";
54}
55
56static void* thread_main(void* arg) {
57  __cxa_thread_atexit_impl(thread_atexit_fn5, arg, nullptr);
58  __cxa_thread_atexit_impl(thread_atexit_fn4, arg, nullptr);
59  __cxa_thread_atexit_impl(thread_atexit_fn3, arg, nullptr);
60  __cxa_thread_atexit_impl(thread_atexit_fn2, arg, nullptr);
61  __cxa_thread_atexit_impl(thread_atexit_fn1, arg, nullptr);
62  return nullptr;
63}
64
65TEST(__cxa_thread_atexit_impl, smoke) {
66  std::string atexit_call_sequence;
67
68  pthread_t t;
69  ASSERT_EQ(0, pthread_create(&t, nullptr, thread_main, &atexit_call_sequence));
70  ASSERT_EQ(0, pthread_join(t, nullptr));
71  ASSERT_EQ("one, two, three, oops, four, five.", atexit_call_sequence);
72}
73
74
75