Simplify lf_queue<>

It doesn't need reinterpret_cast anymore.
This commit is contained in:
Nekotekina 2019-10-23 13:11:06 +03:00
parent 6e19881b82
commit 050e5b4aec
1 changed files with 10 additions and 16 deletions

View File

@ -311,22 +311,16 @@ 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)
template <typename T>
class lf_queue : public lf_queue_base
class lf_queue final
{
using lf_queue_base::m_head;
atomic_t<lf_queue_item<T>*> m_head;
// Extract all elements and reverse element order (FILO to FIFO)
lf_queue_item<T>* reverse() noexcept
{
if (auto* head = m_head.load() ? reinterpret_cast<lf_queue_item<T>*>(m_head.exchange(0)) : nullptr)
if (auto* head = m_head.load() ? m_head.exchange(nullptr) : nullptr)
{
if (auto* prev = head->m_link)
{
@ -352,26 +346,26 @@ public:
~lf_queue()
{
delete reinterpret_cast<lf_queue_item<T>*>(m_head.load());
delete m_head.load();
}
void wait() noexcept
{
if (m_head == 0)
if (m_head == nullptr)
{
m_head.wait(0);
m_head.wait(nullptr);
}
}
template <typename... Args>
void push(Args&&... args)
{
auto _old = m_head.load();
auto* item = new lf_queue_item<T>(reinterpret_cast<lf_queue_item<T>*>(_old), std::forward<Args>(args)...);
auto _old = m_head.load();
auto item = new lf_queue_item<T>(_old, std::forward<Args>(args)...);
while (!m_head.compare_exchange(_old, reinterpret_cast<std::uint64_t>(item)))
while (!m_head.compare_exchange(_old, item))
{
item->m_link = reinterpret_cast<lf_queue_item<T>*>(_old);
item->m_link = _old;
}
if (!_old)