Skip to content

[libc] Fix callback type in exit_handlers.cpp not matching #97642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading