diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2021-06-13 19:31:46 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2021-06-13 19:37:19 +0000 |
commit | e8d8bef961a50d4dc22501cde4fb9fb0be1b2532 (patch) | |
tree | 94f04805f47bb7c59ae29690d8952b6074fff602 /contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_thread_list.h | |
parent | bb130ff39747b94592cb26d71b7cb097b9a4ea6b (diff) | |
parent | b60736ec1405bb0a8dd40989f67ef4c93da068ab (diff) | |
download | src-e8d8bef961a50d4dc22501cde4fb9fb0be1b2532.tar.gz src-e8d8bef961a50d4dc22501cde4fb9fb0be1b2532.zip |
Diffstat (limited to 'contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_thread_list.h')
-rw-r--r-- | contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_thread_list.h | 63 |
1 files changed, 21 insertions, 42 deletions
diff --git a/contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_thread_list.h b/contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_thread_list.h index 914b632d9776..e596bde36662 100644 --- a/contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_thread_list.h +++ b/contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_thread_list.h @@ -66,40 +66,6 @@ static uptr RingBufferSize() { return 0; } -struct ThreadListHead { - Thread *list_; - - ThreadListHead() : list_(nullptr) {} - - void Push(Thread *t) { - t->next_ = list_; - list_ = t; - } - - Thread *Pop() { - Thread *t = list_; - if (t) - list_ = t->next_; - return t; - } - - void Remove(Thread *t) { - Thread **cur = &list_; - while (*cur != t) cur = &(*cur)->next_; - CHECK(*cur && "thread not found"); - *cur = (*cur)->next_; - } - - template <class CB> - void ForEach(CB cb) { - Thread *t = list_; - while (t) { - cb(t); - t = t->next_; - } - } -}; - struct ThreadStats { uptr n_live_threads; uptr total_stack_size; @@ -123,14 +89,15 @@ class HwasanThreadList { Thread *t; { SpinMutexLock l(&list_mutex_); - t = free_list_.Pop(); - if (t) { + if (!free_list_.empty()) { + t = free_list_.back(); + free_list_.pop_back(); uptr start = (uptr)t - ring_buffer_size_; internal_memset((void *)start, 0, ring_buffer_size_ + sizeof(Thread)); } else { t = AllocThread(); } - live_list_.Push(t); + live_list_.push_back(t); } t->Init((uptr)t - ring_buffer_size_, ring_buffer_size_); AddThreadStats(t); @@ -142,12 +109,24 @@ class HwasanThreadList { ReleaseMemoryPagesToOS(start, start + thread_alloc_size_); } + void RemoveThreadFromLiveList(Thread *t) { + for (Thread *&t2 : live_list_) + if (t2 == t) { + // To remove t2, copy the last element of the list in t2's position, and + // pop_back(). This works even if t2 is itself the last element. + t2 = live_list_.back(); + live_list_.pop_back(); + return; + } + CHECK(0 && "thread not found in live list"); + } + void ReleaseThread(Thread *t) { RemoveThreadStats(t); t->Destroy(); SpinMutexLock l(&list_mutex_); - live_list_.Remove(t); - free_list_.Push(t); + RemoveThreadFromLiveList(t); + free_list_.push_back(t); DontNeedThread(t); } @@ -166,7 +145,7 @@ class HwasanThreadList { template <class CB> void VisitAllLiveThreads(CB cb) { SpinMutexLock l(&list_mutex_); - live_list_.ForEach(cb); + for (Thread *t : live_list_) cb(t); } void AddThreadStats(Thread *t) { @@ -201,8 +180,8 @@ class HwasanThreadList { uptr ring_buffer_size_; uptr thread_alloc_size_; - ThreadListHead free_list_; - ThreadListHead live_list_; + InternalMmapVector<Thread *> free_list_; + InternalMmapVector<Thread *> live_list_; SpinMutex list_mutex_; ThreadStats stats_; |