Home | History | Annotate | Download | only in tests
      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 
     23 static std::string class_with_dtor_output;
     24 
     25 class 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 
     38 static thread_local ClassWithDtor class_with_dtor;
     39 
     40 static void* thread_nop(void* arg) {
     41   class_with_dtor.set_message(*static_cast<std::string*>(arg));
     42   return nullptr;
     43 }
     44 
     45 TEST(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 
     53 class 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 
     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.");
     69   exit(0);
     70 }
     71 
     72 TEST(thread_local, dtor_for_main_thread) {
     73   ASSERT_EXIT(thread_atexit_main(), testing::ExitedWithCode(0), "d-tor for main thread called.");
     74 }
     75 
     76 extern "C" int __cxa_thread_atexit_impl(void (*fn)(void*), void* arg, void* dso_handle);
     77 
     78 static void thread_atexit_fn1(void* arg) {
     79   std::string* call_sequence = static_cast<std::string*>(arg);
     80   *call_sequence += "one, ";
     81 }
     82 
     83 static void thread_atexit_fn2(void* arg) {
     84   std::string* call_sequence = static_cast<std::string*>(arg);
     85   *call_sequence += "two, ";
     86 }
     87 
     88 static void thread_atexit_from_atexit(void* arg) {
     89   std::string* call_sequence = static_cast<std::string*>(arg);
     90   *call_sequence += "oops, ";
     91 }
     92 
     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, ";
     97 }
     98 
     99 static void thread_atexit_fn4(void* arg) {
    100   std::string* call_sequence = static_cast<std::string*>(arg);
    101   *call_sequence += "four, ";
    102 }
    103 
    104 static void thread_atexit_fn5(void* arg) {
    105   std::string* call_sequence = static_cast<std::string*>(arg);
    106   *call_sequence += "five.";
    107 }
    108 
    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);
    115   return nullptr;
    116 }
    117 
    118 TEST(__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 
    128