Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Android/bionic/bionic/tests/   (Android Betriebssystem Version 17©)  Datei vom 26.5.2026 mit Größe 3 kB image not shown  

Quelle  __cxa_thread_atexit_test.cpp

  Sprache: 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);
}

class ClassWithDtorForMainThread {
 public:
  void set_message(const std::string& msg) {
    message = msg;
  }

  ~ClassWithDtorForMainThread() {
    fprintf(stderr, "%s", message.c_str());
  }
 private:
  std::string message;
};

static void thread_atexit_main() {
  static thread_local ClassWithDtorForMainThread class_with_dtor_for_main_thread;
  class_with_dtor_for_main_thread.set_message("d-tor for main thread called.");
  exit(0);
}

TEST(thread_local, dtor_for_main_thread) {
  ASSERT_EXIT(thread_atexit_main(), testing::ExitedWithCode(0), "d-tor for main thread called.");
}

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);
}



Messung V0.5 in Prozent
C=84 H=96 G=90

¤ Dauer der Verarbeitung: 0.10 Sekunden  (vorverarbeitet am  2026-06-28) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.