[libcxx-commits] [libcxxabi] 8271aa5 - [libcxxabi] Always use thread_local for cxa_exception_storage

Shoaib Meenai via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 22 12:30:05 PST 2022


Author: Shoaib Meenai
Date: 2022-11-22T12:22:06-08:00
New Revision: 8271aa5335668a1dc62168a4e90b4554bd3a0ca7

URL: https://github.com/llvm/llvm-project/commit/8271aa5335668a1dc62168a4e90b4554bd3a0ca7
DIFF: https://github.com/llvm/llvm-project/commit/8271aa5335668a1dc62168a4e90b4554bd3a0ca7.diff

LOG: [libcxxabi] Always use thread_local for cxa_exception_storage

This was previously guarded by HAS_THREAD_LOCAL, which was never set by
CMake and had to be specified manually. Android has been setting this to
solve https://github.com/android/ndk/issues/1200 [1], but every compiler
and platform libc++abi supports should have thread_local by now, so we
can just get rid of the fallback implementation and simplify things
significantly (including removing the now unused fallback calloc).

[1] https://android-review.googlesource.com/c/toolchain/llvm-project/+/1285596

Reviewed By: #libc_abi, MaskRay, ldionne

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

Added: 
    

Modified: 
    libcxxabi/src/cxa_exception_storage.cpp
    libcxxabi/src/fallback_malloc.cpp
    libcxxabi/src/fallback_malloc.h

Removed: 
    


################################################################################
diff  --git a/libcxxabi/src/cxa_exception_storage.cpp b/libcxxabi/src/cxa_exception_storage.cpp
index 3a3233a1b9272..1abf6143e59f7 100644
--- a/libcxxabi/src/cxa_exception_storage.cpp
+++ b/libcxxabi/src/cxa_exception_storage.cpp
@@ -12,21 +12,18 @@
 
 #include "cxa_exception.h"
 
-#include <__threading_support>
+namespace __cxxabiv1 {
 
 #if defined(_LIBCXXABI_HAS_NO_THREADS)
 
-namespace __cxxabiv1 {
 extern "C" {
     static __cxa_eh_globals eh_globals;
     __cxa_eh_globals *__cxa_get_globals() { return &eh_globals; }
     __cxa_eh_globals *__cxa_get_globals_fast() { return &eh_globals; }
 } // extern "C"
-} // namespace __cxxabiv1
 
-#elif defined(HAS_THREAD_LOCAL)
+#else
 
-namespace __cxxabiv1 {
 namespace {
     __cxa_eh_globals *__globals() {
         static thread_local __cxa_eh_globals eh_globals;
@@ -38,66 +35,7 @@ extern "C" {
     __cxa_eh_globals *__cxa_get_globals() { return __globals(); }
     __cxa_eh_globals *__cxa_get_globals_fast() { return __globals(); }
 } // extern "C"
-} // namespace __cxxabiv1
 
-#else
-
-#include "abort_message.h"
-#include "fallback_malloc.h"
-
-#if defined(__ELF__) && defined(_LIBCXXABI_LINK_PTHREAD_LIB)
-#pragma comment(lib, "pthread")
 #endif
 
-//  In general, we treat all threading errors as fatal.
-//  We cannot call std::terminate() because that will in turn
-//  call __cxa_get_globals() and cause infinite recursion.
-
-namespace __cxxabiv1 {
-namespace {
-    std::__libcpp_tls_key key_;
-    std::__libcpp_exec_once_flag flag_ = _LIBCPP_EXEC_ONCE_INITIALIZER;
-
-    void _LIBCPP_TLS_DESTRUCTOR_CC destruct_(void *p) {
-        __free_with_fallback(p);
-        if (0 != std::__libcpp_tls_set(key_, NULL))
-            abort_message("cannot zero out thread value for __cxa_get_globals()");
-    }
-
-    void construct_() {
-        if (0 != std::__libcpp_tls_create(&key_, destruct_))
-            abort_message("cannot create thread specific key for __cxa_get_globals()");
-    }
-} // namespace
-
-extern "C" {
-    __cxa_eh_globals *__cxa_get_globals() {
-        // Try to get the globals for this thread
-        __cxa_eh_globals *retVal = __cxa_get_globals_fast();
-
-        // If this is the first time we've been asked for these globals, create them
-        if (NULL == retVal) {
-            retVal = static_cast<__cxa_eh_globals*>(
-                __calloc_with_fallback(1, sizeof(__cxa_eh_globals)));
-            if (NULL == retVal)
-                abort_message("cannot allocate __cxa_eh_globals");
-            if (0 != std::__libcpp_tls_set(key_, retVal))
-               abort_message("std::__libcpp_tls_set failure in __cxa_get_globals()");
-        }
-        return retVal;
-    }
-
-    // Note that this implementation will reliably return NULL if not
-    // preceded by a call to __cxa_get_globals().  This is an extension
-    // to the Itanium ABI and is taken advantage of in several places in
-    // libc++abi.
-    __cxa_eh_globals *__cxa_get_globals_fast() {
-        // First time through, create the key.
-        if (0 != std::__libcpp_execute_once(&flag_, construct_))
-            abort_message("execute once failure in __cxa_get_globals_fast()");
-        return static_cast<__cxa_eh_globals*>(std::__libcpp_tls_get(key_));
-    }
-} // extern "C"
 } // namespace __cxxabiv1
-
-#endif

diff  --git a/libcxxabi/src/fallback_malloc.cpp b/libcxxabi/src/fallback_malloc.cpp
index 591efbefc8a52..759bb58f7c308 100644
--- a/libcxxabi/src/fallback_malloc.cpp
+++ b/libcxxabi/src/fallback_malloc.cpp
@@ -270,17 +270,6 @@ void* __aligned_malloc_with_fallback(size_t size) {
   return fallback_malloc(size);
 }
 
-void* __calloc_with_fallback(size_t count, size_t size) {
-  void* ptr = ::calloc(count, size);
-  if (NULL != ptr)
-    return ptr;
-  // if calloc fails, fall back to emergency stash
-  ptr = fallback_malloc(size * count);
-  if (NULL != ptr)
-    ::memset(ptr, 0, size * count);
-  return ptr;
-}
-
 void __aligned_free_with_fallback(void* ptr) {
   if (is_fallback_ptr(ptr))
     fallback_free(ptr);

diff  --git a/libcxxabi/src/fallback_malloc.h b/libcxxabi/src/fallback_malloc.h
index 816e691ed2313..528014b3456d8 100644
--- a/libcxxabi/src/fallback_malloc.h
+++ b/libcxxabi/src/fallback_malloc.h
@@ -17,9 +17,6 @@ namespace __cxxabiv1 {
 // Allocate some memory from _somewhere_
 _LIBCXXABI_HIDDEN void * __aligned_malloc_with_fallback(size_t size);
 
-// Allocate and zero-initialize memory from _somewhere_
-_LIBCXXABI_HIDDEN void * __calloc_with_fallback(size_t count, size_t size);
-
 _LIBCXXABI_HIDDEN void __aligned_free_with_fallback(void *ptr);
 _LIBCXXABI_HIDDEN void __free_with_fallback(void *ptr);
 


        


More information about the libcxx-commits mailing list