146 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "abort_message.h"10#include "cxxabi.h"11#include <__thread/support.h>12#ifndef _LIBCXXABI_HAS_NO_THREADS13#if defined(__ELF__) && defined(_LIBCXXABI_LINK_PTHREAD_LIB)14#pragma comment(lib, "pthread")15#endif16#endif17 18#include <stdlib.h>19 20namespace __cxxabiv1 {21 22 using Dtor = void(*)(void*);23 24 extern "C"25#ifndef HAVE___CXA_THREAD_ATEXIT_IMPL26 // A weak symbol is used to detect this function's presence in the C library27 // at runtime, even if libc++ is built against an older libc28 _LIBCXXABI_WEAK29#endif30 int __cxa_thread_atexit_impl(Dtor, void*, void*);31 32#ifndef HAVE___CXA_THREAD_ATEXIT_IMPL33 34namespace {35 // This implementation is used if the C library does not provide36 // __cxa_thread_atexit_impl() for us. It has a number of limitations that are37 // difficult to impossible to address without ..._impl():38 //39 // - dso_symbol is ignored. This means that a shared library may be unloaded40 // (via dlclose()) before its thread_local destructors have run.41 //42 // - thread_local destructors for the main thread are run by the destructor of43 // a static object. This is later than expected; they should run before the44 // destructors of any objects with static storage duration.45 //46 // - thread_local destructors on non-main threads run on the first iteration47 // through the __libccpp_tls_key destructors.48 // std::notify_all_at_thread_exit() and similar functions must be careful to49 // wait until the second iteration to provide their intended ordering50 // guarantees.51 //52 // Another limitation, though one shared with ..._impl(), is that any53 // thread_locals that are first initialized after non-thread_local global54 // destructors begin to run will not be destroyed. [basic.start.term] states55 // that all thread_local destructors are sequenced before the destruction of56 // objects with static storage duration, resulting in a contradiction if a57 // thread_local is constructed after that point. Thus we consider such58 // programs ill-formed, and don't bother to run those destructors. (If the59 // program terminates abnormally after such a thread_local is constructed,60 // the destructor is not expected to run and thus there is no contradiction.61 // So construction still has to work.)62 63 struct DtorList {64 Dtor dtor;65 void* obj;66 DtorList* next;67 };68 69 // The linked list of thread-local destructors to run70 __thread DtorList* dtors = nullptr;71 // True if the destructors are currently scheduled to run on this thread72 __thread bool dtors_alive = false;73 // Used to trigger destructors on thread exit; value is ignored74 std::__libcpp_tls_key dtors_key;75 76 void run_dtors(void*) {77 while (auto head = dtors) {78 dtors = head->next;79 head->dtor(head->obj);80 ::free(head);81 }82 83 dtors_alive = false;84 }85 86 struct DtorsManager {87 DtorsManager() {88 // There is intentionally no matching std::__libcpp_tls_delete call, as89 // __cxa_thread_atexit() may be called arbitrarily late (for example, from90 // global destructors or atexit() handlers).91 if (std::__libcpp_tls_create(&dtors_key, run_dtors) != 0) {92 __abort_message("std::__libcpp_tls_create() failed in __cxa_thread_atexit()");93 }94 }95 96 ~DtorsManager() {97 // std::__libcpp_tls_key destructors do not run on threads that call exit()98 // (including when the main thread returns from main()), so we explicitly99 // call the destructor here. This runs at exit time (potentially earlier100 // if libc++abi is dlclose()'d). Any thread_locals initialized after this101 // point will not be destroyed.102 run_dtors(nullptr);103 }104 };105} // namespace106 107#endif // HAVE___CXA_THREAD_ATEXIT_IMPL108 109extern "C" {110 111 _LIBCXXABI_FUNC_VIS int __cxa_thread_atexit(Dtor dtor, void* obj, void* dso_symbol) throw() {112#ifdef HAVE___CXA_THREAD_ATEXIT_IMPL113 return __cxa_thread_atexit_impl(dtor, obj, dso_symbol);114#else115 if (__cxa_thread_atexit_impl) {116 return __cxa_thread_atexit_impl(dtor, obj, dso_symbol);117 } else {118 // Initialize the dtors std::__libcpp_tls_key (uses __cxa_guard_*() for119 // one-time initialization and __cxa_atexit() for destruction)120 static DtorsManager manager;121 122 if (!dtors_alive) {123 if (std::__libcpp_tls_set(dtors_key, &dtors_key) != 0) {124 return -1;125 }126 dtors_alive = true;127 }128 129 auto head = static_cast<DtorList*>(::malloc(sizeof(DtorList)));130 if (!head) {131 return -1;132 }133 134 head->dtor = dtor;135 head->obj = obj;136 head->next = dtors;137 dtors = head;138 139 return 0;140 }141#endif // HAVE___CXA_THREAD_ATEXIT_IMPL142 }143 144} // extern "C"145} // namespace __cxxabiv1146