Skip to content

Commit 01fd82e

Browse files
committed
[libc] Fix callback type in exit_handlers.cpp not matching
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 594bc52 commit 01fd82e

File tree

5 files changed

+32
-51
lines changed

5 files changed

+32
-51
lines changed

libc/src/stdlib/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -458,15 +458,11 @@ add_entrypoint_object(
458458

459459
# TODO: Move all exit functions to linux specific
460460

461-
if (TARGET libc.src.__support.threads.mutex)
462-
add_object_library(
461+
if(TARGET libc.src.__support.threads.mutex)
462+
add_header_library(
463463
exit_handler
464-
SRCS
465-
exit_handler.cpp
466464
HDRS
467465
exit_handler.h
468-
CXX_STANDARD
469-
20 # For constinit
470466
DEPENDS
471467
libc.src.__support.CPP.mutex
472468
libc.src.__support.CPP.new
@@ -483,6 +479,8 @@ add_entrypoint_object(
483479
atexit.cpp
484480
HDRS
485481
atexit.h
482+
CXX_STANDARD
483+
20 # For constinit
486484
DEPENDS
487485
.exit_handler
488486
)
@@ -493,6 +491,8 @@ add_entrypoint_object(
493491
at_quick_exit.cpp
494492
HDRS
495493
at_quick_exit.h
494+
CXX_STANDARD
495+
20 # For constinit
496496
DEPENDS
497497
.exit_handler
498498
)

libc/src/stdlib/at_quick_exit.cpp

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

1414
namespace LIBC_NAMESPACE {
1515

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

libc/src/stdlib/atexit.cpp

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

1414
namespace LIBC_NAMESPACE {
1515

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

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

libc/src/stdlib/exit_handler.cpp

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

libc/src/stdlib/exit_handler.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,29 @@ extern ExitCallbackList at_quick_exit_callbacks;
4242

4343
extern Mutex handler_list_mtx;
4444

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

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

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

5169
} // namespace LIBC_NAMESPACE
5270

0 commit comments

Comments
 (0)