Revert "Simplify lf_queue<>"

This reverts commit 050e5b4aec533da248478bd5bf7abd0784e5d805.
This commit is contained in:
Nekotekina 2019-10-23 21:53:55 +03:00
parent d0ea71289e
commit 6867fc5f06

View File

@ -311,16 +311,22 @@ public:
} }
}; };
class lf_queue_base
{
protected:
atomic_t<std::uintptr_t> m_head = 0;
};
// Linked list-based multi-producer queue (the consumer drains the whole queue at once) // Linked list-based multi-producer queue (the consumer drains the whole queue at once)
template <typename T> template <typename T>
class lf_queue final class lf_queue : public lf_queue_base
{ {
atomic_t<lf_queue_item<T>*> m_head; using lf_queue_base::m_head;
// Extract all elements and reverse element order (FILO to FIFO) // Extract all elements and reverse element order (FILO to FIFO)
lf_queue_item<T>* reverse() noexcept lf_queue_item<T>* reverse() noexcept
{ {
if (auto* head = m_head.load() ? m_head.exchange(nullptr) : nullptr) if (auto* head = m_head.load() ? reinterpret_cast<lf_queue_item<T>*>(m_head.exchange(0)) : nullptr)
{ {
if (auto* prev = head->m_link) if (auto* prev = head->m_link)
{ {
@ -346,26 +352,26 @@ public:
~lf_queue() ~lf_queue()
{ {
delete m_head.load(); delete reinterpret_cast<lf_queue_item<T>*>(m_head.load());
} }
void wait() noexcept void wait() noexcept
{ {
if (m_head == nullptr) if (m_head == 0)
{ {
m_head.wait(nullptr); m_head.wait(0);
} }
} }
template <typename... Args> template <typename... Args>
void push(Args&&... args) void push(Args&&... args)
{ {
auto _old = m_head.load(); auto _old = m_head.load();
auto item = new lf_queue_item<T>(_old, std::forward<Args>(args)...); auto* item = new lf_queue_item<T>(reinterpret_cast<lf_queue_item<T>*>(_old), std::forward<Args>(args)...);
while (!m_head.compare_exchange(_old, item)) while (!m_head.compare_exchange(_old, reinterpret_cast<std::uint64_t>(item)))
{ {
item->m_link = _old; item->m_link = reinterpret_cast<lf_queue_item<T>*>(_old);
} }
if (!_old) if (!_old)