FifoQueue: Use atomics instead of volatile members

This commit is contained in:
Lioncash 2015-05-25 18:52:04 -04:00
parent 1fc3c8aa02
commit de5038849e

View File

@ -8,9 +8,9 @@
// single reader, single writer queue // single reader, single writer queue
#include <algorithm> #include <algorithm>
#include <atomic>
#include <cstddef> #include <cstddef>
#include "Common/Atomic.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
namespace Common namespace Common
@ -34,17 +34,16 @@ public:
u32 Size() const u32 Size() const
{ {
static_assert(NeedSize, "using Size() on FifoQueue without NeedSize"); static_assert(NeedSize, "using Size() on FifoQueue without NeedSize");
return m_size; return m_size.load();
} }
bool Empty() const bool Empty() const
{ {
return !AtomicLoad(m_read_ptr->next); return !m_read_ptr->next.load();
} }
T& Front() const T& Front() const
{ {
AtomicLoadAcquire(m_read_ptr->next);
return m_read_ptr->current; return m_read_ptr->current;
} }
@ -56,21 +55,21 @@ public:
// set the next pointer to a new element ptr // set the next pointer to a new element ptr
// then advance the write pointer // then advance the write pointer
ElementPtr* new_ptr = new ElementPtr(); 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; m_write_ptr = new_ptr;
if (NeedSize) if (NeedSize)
Common::AtomicIncrement(m_size); m_size++;
} }
void Pop() void Pop()
{ {
if (NeedSize) if (NeedSize)
Common::AtomicDecrement(m_size); m_size--;
ElementPtr *tmpptr = m_read_ptr; ElementPtr *tmpptr = m_read_ptr;
// advance the read pointer // 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 // 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 delete tmpptr; // this also deletes the element
} }
@ -80,12 +79,12 @@ public:
return false; return false;
if (NeedSize) if (NeedSize)
Common::AtomicDecrement(m_size); m_size--;
ElementPtr *tmpptr = m_read_ptr; 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); t = std::move(tmpptr->current);
tmpptr->next = nullptr; tmpptr->next.store(nullptr);
delete tmpptr; delete tmpptr;
return true; return true;
} }
@ -93,7 +92,7 @@ public:
// not thread-safe // not thread-safe
void Clear() void Clear()
{ {
m_size = 0; m_size.store(0);
delete m_read_ptr; delete m_read_ptr;
m_write_ptr = m_read_ptr = new ElementPtr(); m_write_ptr = m_read_ptr = new ElementPtr();
} }
@ -108,17 +107,19 @@ private:
~ElementPtr() ~ElementPtr()
{ {
if (next) ElementPtr* next_ptr = next.load();
delete next;
if (next_ptr)
delete next_ptr;
} }
T current; T current;
ElementPtr *volatile next; std::atomic<ElementPtr*> next;
}; };
ElementPtr *m_write_ptr; ElementPtr *m_write_ptr;
ElementPtr *m_read_ptr; ElementPtr *m_read_ptr;
volatile u32 m_size; std::atomic<u32> m_size;
}; };
} }