From de5038849e1bab2ed1c935311a16227663e35a4b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 25 May 2015 18:52:04 -0400 Subject: [PATCH] FifoQueue: Use atomics instead of volatile members --- Source/Core/Common/FifoQueue.h | 35 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/Source/Core/Common/FifoQueue.h b/Source/Core/Common/FifoQueue.h index 0d0df09f73..6f65253cc3 100644 --- a/Source/Core/Common/FifoQueue.h +++ b/Source/Core/Common/FifoQueue.h @@ -8,9 +8,9 @@ // single reader, single writer queue #include +#include #include -#include "Common/Atomic.h" #include "Common/CommonTypes.h" namespace Common @@ -34,17 +34,16 @@ public: u32 Size() const { static_assert(NeedSize, "using Size() on FifoQueue without NeedSize"); - return m_size; + return m_size.load(); } bool Empty() const { - return !AtomicLoad(m_read_ptr->next); + return !m_read_ptr->next.load(); } T& Front() const { - AtomicLoadAcquire(m_read_ptr->next); return m_read_ptr->current; } @@ -56,21 +55,21 @@ public: // set the next pointer to a new element ptr // then advance the write pointer ElementPtr* new_ptr = new ElementPtr(); - AtomicStoreRelease(m_write_ptr->next, new_ptr); + m_write_ptr->next.store(new_ptr, std::memory_order_release); m_write_ptr = new_ptr; if (NeedSize) - Common::AtomicIncrement(m_size); + m_size++; } void Pop() { if (NeedSize) - Common::AtomicDecrement(m_size); + m_size--; ElementPtr *tmpptr = m_read_ptr; // advance the read pointer - m_read_ptr = AtomicLoad(tmpptr->next); + m_read_ptr = tmpptr->next.load(); // set the next element to nullptr to stop the recursive deletion - tmpptr->next = nullptr; + tmpptr->next.store(nullptr); delete tmpptr; // this also deletes the element } @@ -80,12 +79,12 @@ public: return false; if (NeedSize) - Common::AtomicDecrement(m_size); + m_size--; ElementPtr *tmpptr = m_read_ptr; - m_read_ptr = AtomicLoadAcquire(tmpptr->next); + m_read_ptr = tmpptr->next.load(std::memory_order_acquire); t = std::move(tmpptr->current); - tmpptr->next = nullptr; + tmpptr->next.store(nullptr); delete tmpptr; return true; } @@ -93,7 +92,7 @@ public: // not thread-safe void Clear() { - m_size = 0; + m_size.store(0); delete m_read_ptr; m_write_ptr = m_read_ptr = new ElementPtr(); } @@ -108,17 +107,19 @@ private: ~ElementPtr() { - if (next) - delete next; + ElementPtr* next_ptr = next.load(); + + if (next_ptr) + delete next_ptr; } T current; - ElementPtr *volatile next; + std::atomic next; }; ElementPtr *m_write_ptr; ElementPtr *m_read_ptr; - volatile u32 m_size; + std::atomic m_size; }; }