[PATCH] [libcxxabi] Add __cxa_thread_atexit for TLS support on Linux.

Dan Albert danalbert at google.com
Wed Dec 17 10:37:33 PST 2014


Hi mclow.lists, EricWF, jroelofs,

Fixes PR21738.

The implementation for this is handled by __cxa_thread_atexit_impl,
which is supplied by libc.

More information:
https://sourceware.org/glibc/wiki/Destructor%20support%20for%20thread_local%20variables

http://reviews.llvm.org/D6708

Files:
  include/cxxabi.h
  src/CMakeLists.txt
  src/cxa_thread_atexit.cpp
  test/cxa_thread_atexit_test.cpp

Index: include/cxxabi.h
===================================================================
--- include/cxxabi.h
+++ include/cxxabi.h
@@ -178,6 +178,12 @@
 // Apple addition to support std::uncaught_exception()
 extern bool __cxa_uncaught_exception() throw();
 
+#ifdef __linux__
+// Linux TLS support. Not yet an official part of the Itanium ABI.
+// https://sourceware.org/glibc/wiki/Destructor%20support%20for%20thread_local%20variables
+extern int __cxa_thread_atexit(void (*)(void *), void *, void *) throw();
+#endif
+
   } // extern "C"
 } // namespace __cxxabiv1
 
Index: src/CMakeLists.txt
===================================================================
--- src/CMakeLists.txt
+++ src/CMakeLists.txt
@@ -9,6 +9,7 @@
   cxa_guard.cpp
   cxa_handlers.cpp
   cxa_new_delete.cpp
+  cxa_thread_atexit.cpp
   cxa_personality.cpp
   cxa_unexpected.cpp
   cxa_vector.cpp
Index: src/cxa_thread_atexit.cpp
===================================================================
--- /dev/null
+++ src/cxa_thread_atexit.cpp
@@ -0,0 +1,24 @@
+//===----------------------- cxa_thread_atexit.cpp ------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "cxxabi.h"
+
+namespace __cxxabiv1 {
+
+extern "C" {
+
+int __cxa_thread_atexit(void (*dtor)(void *), void *obj,
+                        void *dso_symbol) throw() {
+  extern int __cxa_thread_atexit_impl(void (*)(void *), void *, void *);
+  return __cxa_thread_atexit_impl(dtor, obj, dso_symbol);
+}
+
+} // extern "C"
+
+} // namespace __cxxabiv1
Index: test/cxa_thread_atexit_test.cpp
===================================================================
--- /dev/null
+++ test/cxa_thread_atexit_test.cpp
@@ -0,0 +1,31 @@
+//===--------------------- cxa_thread_atexit_test.cpp ---------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <assert.h>
+#include <cxxabi.h>
+
+static bool AtexitImplCalled = false;
+
+extern "C" int __cxa_thread_atexit_impl(void (*dtor)(void *), void *obj,
+                                        void *dso_symbol) {
+  assert(dtor == reinterpret_cast<void (*)(void *)>(1));
+  assert(obj == reinterpret_cast<void *>(2));
+  assert(dso_symbol == reinterpret_cast<void *>(3));
+  AtexitImplCalled = true;
+  return 4;
+}
+
+int main() {
+  int RV = __cxxabiv1::__cxa_thread_atexit(
+      reinterpret_cast<void (*)(void *)>(1), reinterpret_cast<void *>(2),
+      reinterpret_cast<void *>(3));
+  assert(RV = 4);
+  assert(AtexitImplCalled);
+  return 0;
+}

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D6708.17405.patch
Type: text/x-patch
Size: 2964 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20141217/9f85cd1a/attachment.bin>


More information about the cfe-commits mailing list