Path: blob/master/libs/c++abi/src/cxa_thread_atexit.cpp
12346 views
//===----------------------- cxa_thread_atexit.cpp ------------------------===//1//2// The LLVM Compiler Infrastructure3//4// This file is dual licensed under the MIT and the University of Illinois Open5// Source Licenses. See LICENSE.TXT for details.6//7//===----------------------------------------------------------------------===//89#include "abort_message.h"10#include "cxxabi.h"11#include <__threading_support>12#include <cstdlib>1314namespace __cxxabiv1 {1516using Dtor = void(*)(void*);1718#ifndef HAVE___CXA_THREAD_ATEXIT_IMPL1920namespace {21// This implementation is used if the C library does not provide22// __cxa_thread_atexit_impl() for us. It has a number of limitations that are23// difficult to impossible to address without ..._impl():24//25// - dso_symbol is ignored. This means that a shared library may be unloaded26// (via dlclose()) before its thread_local destructors have run.27//28// - thread_local destructors for the main thread are run by the destructor of29// a static object. This is later than expected; they should run before the30// destructors of any objects with static storage duration.31//32// - thread_local destructors on non-main threads run on the first iteration33// through the __libccpp_tls_key destructors.34// std::notify_all_at_thread_exit() and similar functions must be careful to35// wait until the second iteration to provide their intended ordering36// guarantees.37//38// Another limitation, though one shared with ..._impl(), is that any39// thread_locals that are first initialized after non-thread_local global40// destructors begin to run will not be destroyed. [basic.start.term] states41// that all thread_local destructors are sequenced before the destruction of42// objects with static storage duration, resulting in a contradiction if a43// thread_local is constructed after that point. Thus we consider such44// programs ill-formed, and don't bother to run those destructors. (If the45// program terminates abnormally after such a thread_local is constructed,46// the destructor is not expected to run and thus there is no contradiction.47// So construction still has to work.)4849struct DtorList {50Dtor dtor;51void* obj;52DtorList* next;53};5455// The linked list of thread-local destructors to run56__thread DtorList* dtors = nullptr;57// True if the destructors are currently scheduled to run on this thread58__thread bool dtors_alive = false;59// Used to trigger destructors on thread exit; value is ignored60std::__libcpp_tls_key dtors_key;6162void _LIBCPP_TLS_DESTRUCTOR_CC run_dtors(void*) {63while (auto head = dtors) {64dtors = head->next;65head->dtor(head->obj);66std::free(head);67}6869dtors_alive = false;70}7172struct DtorsManager {73DtorsManager() {74// There is intentionally no matching std::__libcpp_tls_delete call, as75// __cxa_thread_atexit() may be called arbitrarily late (for example, from76// global destructors or atexit() handlers).77if (std::__libcpp_tls_create(&dtors_key, run_dtors) != 0) {78abort_message("std::__libcpp_tls_create() failed in __cxa_thread_atexit()");79}80}8182~DtorsManager() {83// std::__libcpp_tls_key destructors do not run on threads that call exit()84// (including when the main thread returns from main()), so we explicitly85// call the destructor here. This runs at exit time (potentially earlier86// if libc++abi is dlclose()'d). Any thread_locals initialized after this87// point will not be destroyed.88run_dtors(nullptr);89}90};91} // namespace9293#endif // HAVE___CXA_THREAD_ATEXIT_IMPL9495extern "C" {9697_LIBCXXABI_FUNC_VIS int __cxa_thread_atexit(Dtor dtor, void* obj, void* dso_symbol) throw() {98#ifdef HAVE___CXA_THREAD_ATEXIT_IMPL99return __cxa_thread_atexit_impl(dtor, obj, dso_symbol);100#else101// Initialize the dtors std::__libcpp_tls_key (uses __cxa_guard_*() for102// one-time initialization and __cxa_atexit() for destruction)103static DtorsManager manager;104105if (!dtors_alive) {106if (std::__libcpp_tls_set(dtors_key, &dtors_key) != 0) {107return -1;108}109dtors_alive = true;110}111112auto head = static_cast<DtorList*>(std::malloc(sizeof(DtorList)));113if (!head) {114return -1;115}116117head->dtor = dtor;118head->obj = obj;119head->next = dtors;120dtors = head;121122return 0;123#endif // HAVE___CXA_THREAD_ATEXIT_IMPL124}125126} // extern "C"127} // namespace __cxxabiv1128129130