1 //===--- Definitions of common thread items ---------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
12 #include "src/__support/CPP/array.h"
13 #include "src/__support/CPP/optional.h"
14 #include "src/__support/fixedvector.h"
15 #include "src/__support/macros/attributes.h"
17 namespace __llvm_libc
{
19 LIBC_THREAD_LOCAL Thread self
;
23 using AtExitCallback
= void(void *);
26 AtExitCallback
*callback
= nullptr;
28 constexpr AtExitUnit() = default;
29 constexpr AtExitUnit(AtExitCallback
*cb
, void *o
) : callback(cb
), obj(o
) {}
32 constexpr size_t TSS_KEY_COUNT
= 1024;
35 // Indicates whether is unit is active. Presence of a non-null dtor
36 // is not sufficient to indicate the same information as a TSS key can
37 // have a null destructor.
40 TSSDtor
*dtor
= nullptr;
42 constexpr TSSKeyUnit() = default;
43 constexpr TSSKeyUnit(TSSDtor
*d
) : active(true), dtor(d
) {}
53 cpp::array
<TSSKeyUnit
, TSS_KEY_COUNT
> units
;
56 constexpr TSSKeyMgr() : mtx(false, false, false) {}
58 cpp::optional
<unsigned int> new_key(TSSDtor
*dtor
) {
60 for (size_t i
= 0; i
< TSS_KEY_COUNT
; ++i
) {
61 TSSKeyUnit
&u
= units
[i
];
67 return cpp::optional
<unsigned int>();
70 TSSDtor
*get_dtor(unsigned int key
) {
71 if (key
>= TSS_KEY_COUNT
)
74 return units
[key
].dtor
;
77 bool remove_key(unsigned int key
) {
78 if (key
>= TSS_KEY_COUNT
)
85 bool is_valid_key(unsigned int key
) {
87 return units
[key
].active
;
91 TSSKeyMgr tss_key_mgr
;
95 void *payload
= nullptr;
96 TSSDtor
*dtor
= nullptr;
98 constexpr TSSValueUnit() = default;
99 constexpr TSSValueUnit(void *p
, TSSDtor
*d
)
100 : active(true), payload(p
), dtor(d
) {}
103 static LIBC_THREAD_LOCAL
cpp::array
<TSSValueUnit
, TSS_KEY_COUNT
> tss_values
;
105 } // anonymous namespace
107 class ThreadAtExitCallbackMgr
{
109 // TODO: Use a BlockStore when compiled for production.
110 FixedVector
<AtExitUnit
, 1024> callback_list
;
113 constexpr ThreadAtExitCallbackMgr() : mtx(false, false, false) {}
115 int add_callback(AtExitCallback
*callback
, void *obj
) {
116 MutexLock
lock(&mtx
);
117 return callback_list
.push_back({callback
, obj
});
122 while (!callback_list
.empty()) {
123 auto atexit_unit
= callback_list
.back();
124 callback_list
.pop_back();
126 atexit_unit
.callback(atexit_unit
.obj
);
132 static LIBC_THREAD_LOCAL ThreadAtExitCallbackMgr atexit_callback_mgr
;
134 // The function __cxa_thread_atexit is provided by C++ runtimes like libcxxabi.
135 // It is used by thread local object runtime to register destructor calls. To
136 // actually register destructor call with the threading library, it calls
137 // __cxa_thread_atexit_impl, which is to be provided by the threading library.
138 // The semantics are very similar to the __cxa_atexit function except for the
139 // fact that the registered callback is thread specific.
140 extern "C" int __cxa_thread_atexit_impl(AtExitCallback
*callback
, void *obj
,
142 return atexit_callback_mgr
.add_callback(callback
, obj
);
147 ThreadAtExitCallbackMgr
*get_thread_atexit_callback_mgr() {
148 return &atexit_callback_mgr
;
151 void call_atexit_callbacks(ThreadAttributes
*attrib
) {
152 attrib
->atexit_callback_mgr
->call();
153 for (size_t i
= 0; i
< TSS_KEY_COUNT
; ++i
) {
154 TSSValueUnit
&unit
= tss_values
[i
];
155 // Both dtor and value need to nonnull to call dtor
156 if (unit
.dtor
!= nullptr && unit
.payload
!= nullptr)
157 unit
.dtor(unit
.payload
);
161 } // namespace internal
163 cpp::optional
<unsigned int> new_tss_key(TSSDtor
*dtor
) {
164 return tss_key_mgr
.new_key(dtor
);
167 bool tss_key_delete(unsigned int key
) { return tss_key_mgr
.remove_key(key
); }
169 bool set_tss_value(unsigned int key
, void *val
) {
170 if (!tss_key_mgr
.is_valid_key(key
))
172 tss_values
[key
] = {val
, tss_key_mgr
.get_dtor(key
)};
176 void *get_tss_value(unsigned int key
) {
177 if (key
>= TSS_KEY_COUNT
)
180 auto &u
= tss_values
[key
];
186 } // namespace __llvm_libc