1 //===----------------------------------------------------------------------===//
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-exception
7 //===----------------------------------------------------------------------===//
9 #include "abort_message.h"
11 #include <__thread/support.h>
12 #ifndef _LIBCXXABI_HAS_NO_THREADS
13 #if defined(__ELF__) && defined(_LIBCXXABI_LINK_PTHREAD_LIB)
14 #pragma comment(lib, "pthread")
20 namespace __cxxabiv1
{
22 using Dtor
= void(*)(void*);
25 #ifndef HAVE___CXA_THREAD_ATEXIT_IMPL
26 // A weak symbol is used to detect this function's presence in the C library
27 // at runtime, even if libc++ is built against an older libc
30 int __cxa_thread_atexit_impl(Dtor
, void*, void*);
32 #ifndef HAVE___CXA_THREAD_ATEXIT_IMPL
35 // This implementation is used if the C library does not provide
36 // __cxa_thread_atexit_impl() for us. It has a number of limitations that are
37 // difficult to impossible to address without ..._impl():
39 // - dso_symbol is ignored. This means that a shared library may be unloaded
40 // (via dlclose()) before its thread_local destructors have run.
42 // - thread_local destructors for the main thread are run by the destructor of
43 // a static object. This is later than expected; they should run before the
44 // destructors of any objects with static storage duration.
46 // - thread_local destructors on non-main threads run on the first iteration
47 // through the __libccpp_tls_key destructors.
48 // std::notify_all_at_thread_exit() and similar functions must be careful to
49 // wait until the second iteration to provide their intended ordering
52 // Another limitation, though one shared with ..._impl(), is that any
53 // thread_locals that are first initialized after non-thread_local global
54 // destructors begin to run will not be destroyed. [basic.start.term] states
55 // that all thread_local destructors are sequenced before the destruction of
56 // objects with static storage duration, resulting in a contradiction if a
57 // thread_local is constructed after that point. Thus we consider such
58 // programs ill-formed, and don't bother to run those destructors. (If the
59 // 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.)
69 // The linked list of thread-local destructors to run
70 __thread DtorList
* dtors
= nullptr;
71 // True if the destructors are currently scheduled to run on this thread
72 __thread
bool dtors_alive
= false;
73 // Used to trigger destructors on thread exit; value is ignored
74 std::__libcpp_tls_key dtors_key
;
76 void run_dtors(void*) {
77 while (auto head
= dtors
) {
79 head
->dtor(head
->obj
);
88 // There is intentionally no matching std::__libcpp_tls_delete call, as
89 // __cxa_thread_atexit() may be called arbitrarily late (for example, from
90 // 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()");
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 explicitly
99 // call the destructor here. This runs at exit time (potentially earlier
100 // if libc++abi is dlclose()'d). Any thread_locals initialized after this
101 // point will not be destroyed.
107 #endif // HAVE___CXA_THREAD_ATEXIT_IMPL
111 _LIBCXXABI_FUNC_VIS
int __cxa_thread_atexit(Dtor dtor
, void* obj
, void* dso_symbol
) throw() {
112 #ifdef HAVE___CXA_THREAD_ATEXIT_IMPL
113 return __cxa_thread_atexit_impl(dtor
, obj
, dso_symbol
);
115 if (__cxa_thread_atexit_impl
) {
116 return __cxa_thread_atexit_impl(dtor
, obj
, dso_symbol
);
118 // Initialize the dtors std::__libcpp_tls_key (uses __cxa_guard_*() for
119 // one-time initialization and __cxa_atexit() for destruction)
120 static DtorsManager manager
;
123 if (std::__libcpp_tls_set(dtors_key
, &dtors_key
) != 0) {
129 auto head
= static_cast<DtorList
*>(::malloc(sizeof(DtorList
)));
141 #endif // HAVE___CXA_THREAD_ATEXIT_IMPL
145 } // namespace __cxxabiv1