Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/c++abi/src/cxa_thread_atexit.cpp
12346 views
1
//===----------------------- cxa_thread_atexit.cpp ------------------------===//
2
//
3
// The LLVM Compiler Infrastructure
4
//
5
// This file is dual licensed under the MIT and the University of Illinois Open
6
// Source Licenses. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#include "abort_message.h"
11
#include "cxxabi.h"
12
#include <__threading_support>
13
#include <cstdlib>
14
15
namespace __cxxabiv1 {
16
17
using Dtor = void(*)(void*);
18
19
#ifndef HAVE___CXA_THREAD_ATEXIT_IMPL
20
21
namespace {
22
// This implementation is used if the C library does not provide
23
// __cxa_thread_atexit_impl() for us. It has a number of limitations that are
24
// difficult to impossible to address without ..._impl():
25
//
26
// - dso_symbol is ignored. This means that a shared library may be unloaded
27
// (via dlclose()) before its thread_local destructors have run.
28
//
29
// - thread_local destructors for the main thread are run by the destructor of
30
// a static object. This is later than expected; they should run before the
31
// destructors of any objects with static storage duration.
32
//
33
// - thread_local destructors on non-main threads run on the first iteration
34
// through the __libccpp_tls_key destructors.
35
// std::notify_all_at_thread_exit() and similar functions must be careful to
36
// wait until the second iteration to provide their intended ordering
37
// guarantees.
38
//
39
// Another limitation, though one shared with ..._impl(), is that any
40
// thread_locals that are first initialized after non-thread_local global
41
// destructors begin to run will not be destroyed. [basic.start.term] states
42
// that all thread_local destructors are sequenced before the destruction of
43
// objects with static storage duration, resulting in a contradiction if a
44
// thread_local is constructed after that point. Thus we consider such
45
// programs ill-formed, and don't bother to run those destructors. (If the
46
// program terminates abnormally after such a thread_local is constructed,
47
// the destructor is not expected to run and thus there is no contradiction.
48
// So construction still has to work.)
49
50
struct DtorList {
51
Dtor dtor;
52
void* obj;
53
DtorList* next;
54
};
55
56
// The linked list of thread-local destructors to run
57
__thread DtorList* dtors = nullptr;
58
// True if the destructors are currently scheduled to run on this thread
59
__thread bool dtors_alive = false;
60
// Used to trigger destructors on thread exit; value is ignored
61
std::__libcpp_tls_key dtors_key;
62
63
void _LIBCPP_TLS_DESTRUCTOR_CC run_dtors(void*) {
64
while (auto head = dtors) {
65
dtors = head->next;
66
head->dtor(head->obj);
67
std::free(head);
68
}
69
70
dtors_alive = false;
71
}
72
73
struct DtorsManager {
74
DtorsManager() {
75
// There is intentionally no matching std::__libcpp_tls_delete call, as
76
// __cxa_thread_atexit() may be called arbitrarily late (for example, from
77
// global destructors or atexit() handlers).
78
if (std::__libcpp_tls_create(&dtors_key, run_dtors) != 0) {
79
abort_message("std::__libcpp_tls_create() failed in __cxa_thread_atexit()");
80
}
81
}
82
83
~DtorsManager() {
84
// std::__libcpp_tls_key destructors do not run on threads that call exit()
85
// (including when the main thread returns from main()), so we explicitly
86
// call the destructor here. This runs at exit time (potentially earlier
87
// if libc++abi is dlclose()'d). Any thread_locals initialized after this
88
// point will not be destroyed.
89
run_dtors(nullptr);
90
}
91
};
92
} // namespace
93
94
#endif // HAVE___CXA_THREAD_ATEXIT_IMPL
95
96
extern "C" {
97
98
_LIBCXXABI_FUNC_VIS int __cxa_thread_atexit(Dtor dtor, void* obj, void* dso_symbol) throw() {
99
#ifdef HAVE___CXA_THREAD_ATEXIT_IMPL
100
return __cxa_thread_atexit_impl(dtor, obj, dso_symbol);
101
#else
102
// Initialize the dtors std::__libcpp_tls_key (uses __cxa_guard_*() for
103
// one-time initialization and __cxa_atexit() for destruction)
104
static DtorsManager manager;
105
106
if (!dtors_alive) {
107
if (std::__libcpp_tls_set(dtors_key, &dtors_key) != 0) {
108
return -1;
109
}
110
dtors_alive = true;
111
}
112
113
auto head = static_cast<DtorList*>(std::malloc(sizeof(DtorList)));
114
if (!head) {
115
return -1;
116
}
117
118
head->dtor = dtor;
119
head->obj = obj;
120
head->next = dtors;
121
dtors = head;
122
123
return 0;
124
#endif // HAVE___CXA_THREAD_ATEXIT_IMPL
125
}
126
127
} // extern "C"
128
} // namespace __cxxabiv1
129
130