Skip to content

[libc][stdlib] initial support for __cxa_finalize #85865

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
Mar 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
31 changes: 19 additions & 12 deletions libc/src/stdlib/atexit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,10 @@ void stdc_at_exit_func(void *payload) {
reinterpret_cast<StdCAtExitCallback *>(payload)();
}

} // namespace

namespace internal {

void call_exit_callbacks() {
handler_list_mtx.lock();
while (!exit_callbacks.empty()) {
auto unit = exit_callbacks.back();
AtExitUnit &unit = exit_callbacks.back();
exit_callbacks.pop_back();
handler_list_mtx.unlock();
unit.callback(unit.payload);
Expand All @@ -71,20 +67,31 @@ void call_exit_callbacks() {
ExitCallbackList::destroy(&exit_callbacks);
}

} // namespace internal

static int add_atexit_unit(const AtExitUnit &unit) {
int add_atexit_unit(const AtExitUnit &unit) {
MutexLock lock(&handler_list_mtx);
if (!exit_callbacks.push_back(unit))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I slightly prefer the original version where we only execute the if body in the case of an error and continue otherwise.

return -1;
return 0;
if (exit_callbacks.push_back(unit))
return 0;
return -1;
}

} // namespace

extern "C" {

// TODO: Handle the last dso handle argument.
extern "C" int __cxa_atexit(AtExitCallback *callback, void *payload, void *) {
int __cxa_atexit(AtExitCallback *callback, void *payload, void *) {
return add_atexit_unit({callback, payload});
}

// TODO: Handle the dso handle argument. call_exit_callbacks should only invoke
// the callbacks from this DSO. Requires adding support for __dso_handle.
void __cxa_finalize(void *dso) {
if (!dso)
call_exit_callbacks();
}

} // extern "C"

LLVM_LIBC_FUNCTION(int, atexit, (StdCAtExitCallback * callback)) {
return add_atexit_unit(
{&stdc_at_exit_func, reinterpret_cast<void *>(callback)});
Expand Down
8 changes: 3 additions & 5 deletions libc/src/stdlib/exit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
#include "src/__support/OSUtil/quick_exit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {
extern "C" void __cxa_finalize(void *);

namespace internal {
void call_exit_callbacks();
}
namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(void, exit, (int status)) {
internal::call_exit_callbacks();
__cxa_finalize(nullptr);
quick_exit(status);
__builtin_unreachable();
}
Expand Down