From f9ec62cb3acd62a089f71eab4185ba1310296ea1 Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Fri, 7 Sep 2018 00:16:55 +0000 Subject: [PATCH] [hwasan] change the thread list so that main_thread can also be removed llvm-svn: 341610 --- compiler-rt/lib/hwasan/hwasan_thread.cc | 16 ++++++++++------ compiler-rt/lib/hwasan/hwasan_thread.h | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/compiler-rt/lib/hwasan/hwasan_thread.cc b/compiler-rt/lib/hwasan/hwasan_thread.cc index aa60d199af0a..1d008982d4d7 100644 --- a/compiler-rt/lib/hwasan/hwasan_thread.cc +++ b/compiler-rt/lib/hwasan/hwasan_thread.cc @@ -24,7 +24,7 @@ static u32 RandomSeed() { return seed; } -Thread *Thread::main_thread; +Thread *Thread::thread_list_head; SpinMutex Thread::thread_list_mutex; Thread::ThreadStats Thread::thread_stats; @@ -33,22 +33,26 @@ void Thread::InsertIntoThreadList(Thread *t) { SpinMutexLock l(&thread_list_mutex); thread_stats.n_live_threads++; thread_stats.total_stack_size += t->stack_size(); - if (!main_thread) { - main_thread = t; + if (!thread_list_head) { + thread_list_head = t; return; } - Thread *last = main_thread; + Thread *last = thread_list_head; while (last->next_) last = last->next_; last->next_ = t; } void Thread::RemoveFromThreadList(Thread *t) { - if (t == main_thread) return; // Do nothing (happens due to pthread_exit). SpinMutexLock l(&thread_list_mutex); thread_stats.n_live_threads--; thread_stats.total_stack_size -= t->stack_size(); - Thread *prev = main_thread; + if (t == thread_list_head) { + thread_list_head = t->next_; + t->next_ = nullptr; + return; + } + Thread *prev = thread_list_head; Thread *cur = prev->next_; CHECK(cur); while (cur) { diff --git a/compiler-rt/lib/hwasan/hwasan_thread.h b/compiler-rt/lib/hwasan/hwasan_thread.h index 6b41f51d8b53..6a27ba6f2166 100644 --- a/compiler-rt/lib/hwasan/hwasan_thread.h +++ b/compiler-rt/lib/hwasan/hwasan_thread.h @@ -62,7 +62,7 @@ class Thread { template static void VisitAllLiveThreads(CB cb) { SpinMutexLock l(&thread_list_mutex); - Thread *t = main_thread; + Thread *t = thread_list_head; while (t) { cb(t); t = t->next_; @@ -113,7 +113,7 @@ class Thread { static void RemoveFromThreadList(Thread *t); Thread *next_; // All live threads form a linked list. static SpinMutex thread_list_mutex; - static Thread *main_thread; + static Thread *thread_list_head; static ThreadStats thread_stats; u64 unique_id_; // counting from zero.