diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt --- a/libc/src/stdlib/CMakeLists.txt +++ b/libc/src/stdlib/CMakeLists.txt @@ -276,11 +276,11 @@ atexit.cpp HDRS atexit.h + COMPILE_OPTIONS + -O3 DEPENDS libc.src.__support.CPP.vector - libc.src.threads.mtx_init - libc.src.threads.mtx_lock - libc.src.threads.mtx_unlock + libc.src.__support.threads.thread ) add_entrypoint_object( diff --git a/libc/src/stdlib/atexit.cpp b/libc/src/stdlib/atexit.cpp --- a/libc/src/stdlib/atexit.cpp +++ b/libc/src/stdlib/atexit.cpp @@ -9,20 +9,13 @@ #include "src/stdlib/atexit.h" #include "src/__support/CPP/vector.h" #include "src/__support/common.h" -#include "src/threads/mtx_init.h" -#include "src/threads/mtx_lock.h" -#include "src/threads/mtx_unlock.h" +#include "src/__support/threads/mutex.h" namespace __llvm_libc { namespace { -mtx_t lock; -// TODO need an easier way to use mtx_t internally, or use pthread_mutex_t -// with PTHREAD_MUTEX_INITIALIZER when it lands. -struct Init { - Init() { __llvm_libc::mtx_init(&lock, mtx_plain); } -} init; +Mutex handler_list_mtx(false, false, false); // TOOD should we make cpp::vector like llvm::SmallVector where it will // allocate at least N before needing dynamic allocation? @@ -33,21 +26,21 @@ namespace internal { void call_exit_handlers() { - __llvm_libc::mtx_lock(&lock); + handler_list_mtx.lock(); // TODO: implement rbegin() + rend() for cpp::vector for (int i = handlers.size() - 1; i >= 0; i--) { - __llvm_libc::mtx_unlock(&lock); + handler_list_mtx.unlock(); handlers[i](); - __llvm_libc::mtx_lock(&lock); + handler_list_mtx.lock(); } } } // namespace internal LLVM_LIBC_FUNCTION(int, atexit, (void (*function)())) { - __llvm_libc::mtx_lock(&lock); + handler_list_mtx.lock(); handlers.push_back(function); - __llvm_libc::mtx_unlock(&lock); + handler_list_mtx.unlock(); return 0; }