[libc-commits] [libc] 83b8878 - [libc] Use the constexpr constructor to initialize exit handlers mutex.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Wed Mar 9 18:52:42 PST 2022


Author: Siva Chandra Reddy
Date: 2022-03-10T02:52:35Z
New Revision: 83b8878fbb0ede596bd85400e921422e45376016

URL: https://github.com/llvm/llvm-project/commit/83b8878fbb0ede596bd85400e921422e45376016
DIFF: https://github.com/llvm/llvm-project/commit/83b8878fbb0ede596bd85400e921422e45376016.diff

LOG: [libc] Use the constexpr constructor to initialize exit handlers mutex.

Reviewed By: abrachet

Differential Revision: https://reviews.llvm.org/D121334

Added: 
    

Modified: 
    libc/src/stdlib/CMakeLists.txt
    libc/src/stdlib/atexit.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 2472466a086dc..692134727164a 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -278,9 +278,7 @@ add_entrypoint_object(
     atexit.h
   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
index 9d18cd9c3cece..31943e09b61da 100644
--- 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<T, N> where it will
 // allocate at least N before needing dynamic allocation?
@@ -33,21 +26,21 @@ static cpp::vector<void (*)(void)> handlers;
 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;
 }
 


        


More information about the libc-commits mailing list