From dc7a03e2c512bfde86daf471f095135c9f7c7014 Mon Sep 17 00:00:00 2001 From: vsadov Date: Fri, 31 Jul 2020 12:07:12 -0700 Subject: [PATCH] SatoriQueue --- src/coreclr/gc/CMakeLists.txt | 1 + src/coreclr/src/gc/satori/SatoriQueue.h | 144 ++++++++++++++++++ src/coreclr/src/gc/satori/SatoriRegion.h | 2 +- .../src/gc/satori/SatoriRegionQueue.cpp | 107 ------------- src/coreclr/src/gc/satori/SatoriRegionQueue.h | 32 +--- src/coreclr/vm/CMakeLists.txt | 1 + 6 files changed, 149 insertions(+), 138 deletions(-) create mode 100644 src/coreclr/src/gc/satori/SatoriQueue.h diff --git a/src/coreclr/gc/CMakeLists.txt b/src/coreclr/gc/CMakeLists.txt index b96281f1ea7..89dbc0c5e42 100644 --- a/src/coreclr/gc/CMakeLists.txt +++ b/src/coreclr/gc/CMakeLists.txt @@ -110,6 +110,7 @@ if (CLR_CMAKE_TARGET_WIN32) satori/SatoriRegion.inl satori/SatoriObject.h satori/SatoriObject.inl + satori/SatoriQueue.h satori/SatoriRegionQueue.h satori/SatoriAllocator.h satori/SatoriRecycler.h diff --git a/src/coreclr/src/gc/satori/SatoriQueue.h b/src/coreclr/src/gc/satori/SatoriQueue.h new file mode 100644 index 00000000000..a960e9e9244 --- /dev/null +++ b/src/coreclr/src/gc/satori/SatoriQueue.h @@ -0,0 +1,144 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. +// +// TQueue.h +// + +#ifndef __SATORI_QUEUE_H__ +#define __SATORI_QUEUE_H__ + +#include "common.h" +#include "../gc.h" +#include "SatoriLock.h" + +template +class SatoriQueue +{ +public: + + SatoriQueue() : + m_lock(), m_head(), m_tail() + { + m_lock.Initialize(); + }; + + void Push(T* item) + { + SatoriLockHolder holder(&m_lock); + item->m_containingQueue = this; + if (m_head == nullptr) + { + _ASSERTE(m_tail == nullptr); + m_head = m_tail = item; + } + else + { + item->m_next = m_head; + m_head->m_prev = item; + m_head = item; + } + } + + T* TryPop() + { + T* result; + { + SatoriLockHolder holder(&m_lock); + result = m_head; + if (result == nullptr) + { + return nullptr; + } + + result->m_containingQueue = nullptr; + m_head = result->m_next; + if (m_head == nullptr) + { + m_tail = nullptr; + } + else + { + m_head->m_prev = nullptr; + } + } + + _ASSERTE(result->m_prev == nullptr); + result->m_next = nullptr; + + return result; + } + + void Enqueue(T* item) + { + SatoriLockHolder holder(&m_lock); + item->m_containingQueue = this; + if (m_tail == nullptr) + { + _ASSERTE(m_head == nullptr); + m_head = m_tail = item; + } + else + { + item->m_prev = m_tail; + m_tail->m_next = item; + m_tail = item; + } + } + + bool TryRemove(T* item) + { + { + SatoriLockHolder holder(&m_lock); + if (!Contains(item)) + { + return false; + } + + item->m_containingQueue = nullptr; + if (item->m_prev == nullptr) + { + m_head = item->m_next; + } + else + { + item->m_prev->m_next = item->m_next; + } + + if (item->m_next == nullptr) + { + m_tail = item->m_prev; + } + else + { + item->m_next->m_prev = item->m_prev; + } + } + + item->m_next = nullptr; + item->m_prev = nullptr; + return true; + } + + bool Contains(T* item) + { + return item->m_containingQueue == this; + } + + bool CanPop() + { + return m_head != nullptr; + } + + bool CanDequeue() + { + return m_tail != nullptr; + } + +protected: + SatoriLock m_lock; + T* m_head; + T* m_tail; +}; + +#endif diff --git a/src/coreclr/src/gc/satori/SatoriRegion.h b/src/coreclr/src/gc/satori/SatoriRegion.h index ac2946a3927..1877879590e 100644 --- a/src/coreclr/src/gc/satori/SatoriRegion.h +++ b/src/coreclr/src/gc/satori/SatoriRegion.h @@ -90,7 +90,7 @@ private: SatoriRegion* m_prev; SatoriRegion* m_next; - SatoriRegionQueue* m_containingQueue; + SatoriQueue* m_containingQueue; // active allocation may happen in the following range. // the range may not be parseable as sequence of objects diff --git a/src/coreclr/src/gc/satori/SatoriRegionQueue.cpp b/src/coreclr/src/gc/satori/SatoriRegionQueue.cpp index 8de34da2a1b..884b6d2d068 100644 --- a/src/coreclr/src/gc/satori/SatoriRegionQueue.cpp +++ b/src/coreclr/src/gc/satori/SatoriRegionQueue.cpp @@ -15,74 +15,6 @@ #include "SatoriRegion.h" #include "SatoriRegion.inl" -void SatoriRegionQueue::Push(SatoriRegion* region) -{ - _ASSERTE(region->ValidateBlank()); - - SatoriLockHolder holder(&m_lock); - region->m_containingQueue = this; - if (m_head == nullptr) - { - _ASSERTE(m_tail == nullptr); - m_head = m_tail = region; - } - else - { - region->m_next = m_head; - m_head->m_prev = region; - m_head = region; - } -} - -void SatoriRegionQueue::Enqueue(SatoriRegion* region) -{ - _ASSERTE(region->ValidateBlank()); - - SatoriLockHolder holder(&m_lock); - region->m_containingQueue = this; - if (m_tail == nullptr) - { - _ASSERTE(m_head == nullptr); - m_head = m_tail = region; - } - else - { - region->m_prev = m_tail; - m_tail->m_next = region; - m_tail = region; - } -} - -SatoriRegion* SatoriRegionQueue::TryPop() -{ - SatoriRegion* result; - { - SatoriLockHolder holder(&m_lock); - result = m_head; - if (result == nullptr) - { - return nullptr; - } - - result->m_containingQueue = nullptr; - m_head = result->m_next; - if (m_head == nullptr) - { - m_tail = nullptr; - } - else - { - m_head->m_prev = nullptr; - } - } - - _ASSERTE(result->m_prev == nullptr); - result->m_next = nullptr; - - _ASSERTE(result->ValidateBlank()); - return result; -} - SatoriRegion* SatoriRegionQueue::TryPop(size_t regionSize, SatoriRegion*& putBack) { m_lock.Enter(); @@ -218,42 +150,3 @@ SatoriRegion* SatoriRegionQueue::TryRemove(size_t regionSize, SatoriRegion*& put return result; } - -bool SatoriRegionQueue::Contains(SatoriRegion* region) -{ - return region->m_containingQueue == this; -} - -bool SatoriRegionQueue::TryRemove(SatoriRegion* region) -{ - { - SatoriLockHolder holder(&m_lock); - if (!Contains(region)) - { - return false; - } - - region->m_containingQueue = nullptr; - if (region->m_prev == nullptr) - { - m_head = region->m_next; - } - else - { - region->m_prev->m_next = region->m_next; - } - - if (region->m_next == nullptr) - { - m_tail = region->m_prev; - } - else - { - region->m_next->m_prev = region->m_prev; - } - } - - region->m_next = nullptr; - region->m_prev = nullptr; - return true; -} diff --git a/src/coreclr/src/gc/satori/SatoriRegionQueue.h b/src/coreclr/src/gc/satori/SatoriRegionQueue.h index 19c4bf8cfc0..105db1bb68e 100644 --- a/src/coreclr/src/gc/satori/SatoriRegionQueue.h +++ b/src/coreclr/src/gc/satori/SatoriRegionQueue.h @@ -10,43 +10,15 @@ #include "common.h" #include "../gc.h" -#include "SatoriLock.h" +#include "SatoriQueue.h" class SatoriRegion; -class SatoriRegionQueue +class SatoriRegionQueue : public SatoriQueue { public: - SatoriRegion* TryPop(); SatoriRegion* TryPop(size_t regionSize, SatoriRegion* &putBack); - - bool TryRemove(SatoriRegion* region); SatoriRegion* TryRemove(size_t regionSize, SatoriRegion*& putBack); - - void Push(SatoriRegion* region); - void Enqueue(SatoriRegion* region); - bool Contains(SatoriRegion* region); - - SatoriRegionQueue() : - m_lock(), m_head(), m_tail() - { - m_lock.Initialize(); - }; - - bool CanPop() - { - return m_head != nullptr; - } - - bool CanDequeue() - { - return m_tail != nullptr; - } - -private: - SatoriLock m_lock; - SatoriRegion* m_head; - SatoriRegion* m_tail; }; #endif diff --git a/src/coreclr/vm/CMakeLists.txt b/src/coreclr/vm/CMakeLists.txt index 8d1cfe65e4e..49649371ba0 100644 --- a/src/coreclr/vm/CMakeLists.txt +++ b/src/coreclr/vm/CMakeLists.txt @@ -547,6 +547,7 @@ set(GC_HEADERS_WKS ../gc/satori/SatoriRegion.inl ../gc/satori/SatoriObject.h ../gc/satori/SatoriObject.inl + ../gc/satori/SatoriQueue.h ../gc/satori/SatoriRegionQueue.h ../gc/satori/SatoriAllocator.h ../gc/satori/SatoriRecycler.h