Skip to content

Commit

Permalink
[libc] Fix callback type in exit_handlers.cpp not matching (llvm#97642
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
jhuber6 authored Jul 22, 2024
1 parent 6777c34 commit aac3a2a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 55 deletions.
15 changes: 9 additions & 6 deletions libc/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ add_entrypoint_object(
DEPENDS
libc.src.__support.OSUtil.osutil
.exit_handler
.at_quick_exit
.atexit
)

add_entrypoint_object(
Expand Down Expand Up @@ -462,15 +464,11 @@ add_entrypoint_object(

# TODO: Move all exit functions to linux specific

if (TARGET libc.src.__support.threads.mutex)
add_object_library(
if(TARGET libc.src.__support.threads.mutex)
add_header_library(
exit_handler
SRCS
exit_handler.cpp
HDRS
exit_handler.h
CXX_STANDARD
20 # For constinit
DEPENDS
libc.src.__support.CPP.mutex
libc.src.__support.CPP.new
Expand All @@ -487,6 +485,8 @@ add_entrypoint_object(
atexit.cpp
HDRS
atexit.h
CXX_STANDARD
20 # For constinit
DEPENDS
.exit_handler
)
Expand All @@ -497,8 +497,11 @@ add_entrypoint_object(
at_quick_exit.cpp
HDRS
at_quick_exit.h
CXX_STANDARD
20 # For constinit
DEPENDS
.exit_handler
.atexit
)

list(APPEND exit_deps
Expand Down
2 changes: 2 additions & 0 deletions libc/src/stdlib/at_quick_exit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace LIBC_NAMESPACE_DECL {

constinit ExitCallbackList at_quick_exit_callbacks;

LLVM_LIBC_FUNCTION(int, at_quick_exit, (__atexithandler_t callback)) {
return add_atexit_unit(
at_quick_exit_callbacks,
Expand Down
3 changes: 3 additions & 0 deletions libc/src/stdlib/atexit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

namespace LIBC_NAMESPACE_DECL {

constinit ExitCallbackList atexit_callbacks;
Mutex handler_list_mtx(false, false, false, false);

extern "C" {

int __cxa_atexit(AtExitCallback *callback, void *payload, void *) {
Expand Down
43 changes: 0 additions & 43 deletions libc/src/stdlib/exit_handler.cpp

This file was deleted.

28 changes: 22 additions & 6 deletions libc/src/stdlib/exit_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,32 @@ using ExitCallbackList = ReverseOrderBlockStore<AtExitUnit, 32>;
using ExitCallbackList = FixedVector<AtExitUnit, CALLBACK_LIST_SIZE_FOR_TESTS>;
#endif

extern ExitCallbackList atexit_callbacks;
extern ExitCallbackList at_quick_exit_callbacks;

// This is handled by the 'atexit' implementation and shared by 'at_quick_exit'.
extern Mutex handler_list_mtx;

void stdc_at_exit_func(void *payload);
LIBC_INLINE void stdc_at_exit_func(void *payload) {
reinterpret_cast<StdCAtExitCallback *>(payload)();
}

void call_exit_callbacks(ExitCallbackList &callbacks);
LIBC_INLINE void call_exit_callbacks(ExitCallbackList &callbacks) {
handler_list_mtx.lock();
while (!callbacks.empty()) {
AtExitUnit &unit = callbacks.back();
callbacks.pop_back();
handler_list_mtx.unlock();
unit.callback(unit.payload);
handler_list_mtx.lock();
}
ExitCallbackList::destroy(&callbacks);
}

int add_atexit_unit(ExitCallbackList &callbacks, const AtExitUnit &unit);
LIBC_INLINE int add_atexit_unit(ExitCallbackList &callbacks,
const AtExitUnit &unit) {
cpp::lock_guard lock(handler_list_mtx);
if (callbacks.push_back(unit))
return 0;
return -1;
}

} // namespace LIBC_NAMESPACE_DECL

Expand Down

0 comments on commit aac3a2a

Please sign in to comment.