[PATCH] D31884: Don't delete lsan thread-local data until it's no longer required
Francis Ricci via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 10 11:07:46 PDT 2017
fjricci created this revision.
The routines for thread destruction in the thread registry require
the lsan thread index, which is stored in pthread tls on OS X.
This means that we need to make sure that the lsan tls isn't destroyed
until after the thread registry tls. This change ensures that we
don't delete the lsan tls until we've finished destroying the thread
in the registry, ensuring that the destructor for the lsan tls runs
after the destructor for the thread registry tls.
This patch also adds a check to ensure that the thread ID is valid before
returning it in GetThreadID(), to ensure that the above behavior
is working correctly.
https://reviews.llvm.org/D31884
Files:
lib/lsan/lsan_common_mac.cc
lib/lsan/lsan_thread.cc
Index: lib/lsan/lsan_thread.cc
===================================================================
--- lib/lsan/lsan_thread.cc
+++ lib/lsan/lsan_thread.cc
@@ -92,6 +92,7 @@
void ThreadFinish() {
thread_registry->FinishThread(GetCurrentThread());
+ SetCurrentThread(kInvalidTid);
}
ThreadContext *CurrentThreadContext() {
Index: lib/lsan/lsan_common_mac.cc
===================================================================
--- lib/lsan/lsan_common_mac.cc
+++ lib/lsan/lsan_common_mac.cc
@@ -33,7 +33,22 @@
static pthread_key_t key;
static pthread_once_t key_once = PTHREAD_ONCE_INIT;
-static void make_tls_key() { CHECK_EQ(pthread_key_create(&key, NULL), 0); }
+// The main thread destructor requires the current thread id,
+// so we can't destroy it until it's been used and reset to invalid tid
+void keep_tid_alive(void *ptr) {
+ if (!ptr) {
+ return;
+ }
+
+ thread_local_data_t *data = (thread_local_data_t *)ptr;
+ if (data->current_thread_id != kInvalidTid) {
+ pthread_setspecific(key, data);
+ }
+}
+
+static void make_tls_key() {
+ CHECK_EQ(pthread_key_create(&key, keep_tid_alive), 0);
+}
static thread_local_data_t *get_tls_val(bool alloc) {
pthread_once(&key_once, make_tls_key);
@@ -65,9 +80,15 @@
--*disable_counter;
}
-u32 GetCurrentThread() { return get_tls_val(true)->current_thread_id; }
+u32 GetCurrentThread() {
+ thread_local_data_t *data = get_tls_val(false);
+ CHECK(data);
+ return data->current_thread_id;
+}
-void SetCurrentThread(u32 tid) { get_tls_val(true)->current_thread_id = tid; }
+void SetCurrentThread(u32 tid) {
+ get_tls_val(true)->current_thread_id = tid;
+}
AllocatorCache *GetAllocatorCache() { return &get_tls_val(true)->cache; }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31884.94669.patch
Type: text/x-patch
Size: 1723 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170410/7b61d769/attachment.bin>
More information about the llvm-commits
mailing list