Skip to content

Commit aac3a2a

Browse files
authored
[libc] Fix callback type in exit_handlers.cpp not matching (#97642)
Summary: This file is an object library, but uses the `LIBC_COPT_PUBLIC_PACKAING` option. This will always be undefined which leads to a type mismatch when uses actually try to link against it. This patch simply removes this and turns it into a header only library. This means that the implementations of the callback lists and the mutexes need to live in their respective files. The result is that `atexit` needs to be defined for `at_quick_exit` to be valid.
1 parent 6777c34 commit aac3a2a

File tree

5 files changed

+36
-55
lines changed

5 files changed

+36
-55
lines changed

libc/src/stdlib/CMakeLists.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ add_entrypoint_object(
5151
DEPENDS
5252
libc.src.__support.OSUtil.osutil
5353
.exit_handler
54+
.at_quick_exit
55+
.atexit
5456
)
5557

5658
add_entrypoint_object(
@@ -462,15 +464,11 @@ add_entrypoint_object(
462464

463465
# TODO: Move all exit functions to linux specific
464466

465-
if (TARGET libc.src.__support.threads.mutex)
466-
add_object_library(
467+
if(TARGET libc.src.__support.threads.mutex)
468+
add_header_library(
467469
exit_handler
468-
SRCS
469-
exit_handler.cpp
470470
HDRS
471471
exit_handler.h
472-
CXX_STANDARD
473-
20 # For constinit
474472
DEPENDS
475473
libc.src.__support.CPP.mutex
476474
libc.src.__support.CPP.new
@@ -487,6 +485,8 @@ add_entrypoint_object(
487485
atexit.cpp
488486
HDRS
489487
atexit.h
488+
CXX_STANDARD
489+
20 # For constinit
490490
DEPENDS
491491
.exit_handler
492492
)
@@ -497,8 +497,11 @@ add_entrypoint_object(
497497
at_quick_exit.cpp
498498
HDRS
499499
at_quick_exit.h
500+
CXX_STANDARD
501+
20 # For constinit
500502
DEPENDS
501503
.exit_handler
504+
.atexit
502505
)
503506

504507
list(APPEND exit_deps

libc/src/stdlib/at_quick_exit.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

17+
constinit ExitCallbackList at_quick_exit_callbacks;
18+
1719
LLVM_LIBC_FUNCTION(int, at_quick_exit, (__atexithandler_t callback)) {
1820
return add_atexit_unit(
1921
at_quick_exit_callbacks,

libc/src/stdlib/atexit.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

17+
constinit ExitCallbackList atexit_callbacks;
18+
Mutex handler_list_mtx(false, false, false, false);
19+
1720
extern "C" {
1821

1922
int __cxa_atexit(AtExitCallback *callback, void *payload, void *) {

libc/src/stdlib/exit_handler.cpp

Lines changed: 0 additions & 43 deletions
This file was deleted.

libc/src/stdlib/exit_handler.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,32 @@ using ExitCallbackList = ReverseOrderBlockStore<AtExitUnit, 32>;
3838
using ExitCallbackList = FixedVector<AtExitUnit, CALLBACK_LIST_SIZE_FOR_TESTS>;
3939
#endif
4040

41-
extern ExitCallbackList atexit_callbacks;
42-
extern ExitCallbackList at_quick_exit_callbacks;
43-
41+
// This is handled by the 'atexit' implementation and shared by 'at_quick_exit'.
4442
extern Mutex handler_list_mtx;
4543

46-
void stdc_at_exit_func(void *payload);
44+
LIBC_INLINE void stdc_at_exit_func(void *payload) {
45+
reinterpret_cast<StdCAtExitCallback *>(payload)();
46+
}
4747

48-
void call_exit_callbacks(ExitCallbackList &callbacks);
48+
LIBC_INLINE void call_exit_callbacks(ExitCallbackList &callbacks) {
49+
handler_list_mtx.lock();
50+
while (!callbacks.empty()) {
51+
AtExitUnit &unit = callbacks.back();
52+
callbacks.pop_back();
53+
handler_list_mtx.unlock();
54+
unit.callback(unit.payload);
55+
handler_list_mtx.lock();
56+
}
57+
ExitCallbackList::destroy(&callbacks);
58+
}
4959

50-
int add_atexit_unit(ExitCallbackList &callbacks, const AtExitUnit &unit);
60+
LIBC_INLINE int add_atexit_unit(ExitCallbackList &callbacks,
61+
const AtExitUnit &unit) {
62+
cpp::lock_guard lock(handler_list_mtx);
63+
if (callbacks.push_back(unit))
64+
return 0;
65+
return -1;
66+
}
5167

5268
} // namespace LIBC_NAMESPACE_DECL
5369

0 commit comments

Comments
 (0)