2015-05-24 04:32:32 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-05-24 04:32:32 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2010-07-29 10:21:48 +00:00
|
|
|
|
|
|
|
// a simple lockless thread-safe,
|
2017-08-23 23:45:42 +00:00
|
|
|
// single producer, single consumer queue
|
2010-07-29 10:21:48 +00:00
|
|
|
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <algorithm>
|
2015-05-25 22:52:04 +00:00
|
|
|
#include <atomic>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
2010-08-10 04:12:32 +00:00
|
|
|
|
2010-07-29 10:21:48 +00:00
|
|
|
namespace Common
|
|
|
|
{
|
2013-08-19 19:16:01 +00:00
|
|
|
template <typename T, bool NeedSize = true>
|
2017-08-23 23:45:42 +00:00
|
|
|
class SPSCQueue
|
2010-07-29 10:21:48 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-08-23 23:45:42 +00:00
|
|
|
SPSCQueue() : m_size(0) { m_write_ptr = m_read_ptr = new ElementPtr(); }
|
|
|
|
~SPSCQueue()
|
2010-07-29 10:21:48 +00:00
|
|
|
{
|
|
|
|
// this will empty out the whole queue
|
|
|
|
delete m_read_ptr;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-08-10 04:12:32 +00:00
|
|
|
u32 Size() const
|
2010-07-29 10:21:48 +00:00
|
|
|
{
|
2017-08-23 23:45:42 +00:00
|
|
|
static_assert(NeedSize, "using Size() on SPSCQueue without NeedSize");
|
2015-05-25 22:52:04 +00:00
|
|
|
return m_size.load();
|
2010-08-10 04:12:32 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-25 22:52:04 +00:00
|
|
|
bool Empty() const { return !m_read_ptr->next.load(); }
|
2013-08-19 19:16:01 +00:00
|
|
|
T& Front() const { return m_read_ptr->current; }
|
2013-04-05 00:34:50 +00:00
|
|
|
template <typename Arg>
|
|
|
|
void Push(Arg&& t)
|
2010-07-29 10:21:48 +00:00
|
|
|
{
|
|
|
|
// create the element, add it to the queue
|
2013-09-23 00:38:03 +00:00
|
|
|
m_write_ptr->current = std::forward<Arg>(t);
|
2010-07-29 10:21:48 +00:00
|
|
|
// set the next pointer to a new element ptr
|
|
|
|
// then advance the write pointer
|
2013-08-29 23:19:44 +00:00
|
|
|
ElementPtr* new_ptr = new ElementPtr();
|
2015-05-25 22:52:04 +00:00
|
|
|
m_write_ptr->next.store(new_ptr, std::memory_order_release);
|
2013-08-29 23:19:44 +00:00
|
|
|
m_write_ptr = new_ptr;
|
2013-08-19 19:16:01 +00:00
|
|
|
if (NeedSize)
|
2015-05-25 22:52:04 +00:00
|
|
|
m_size++;
|
2010-07-29 10:21:48 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-08-10 04:12:32 +00:00
|
|
|
void Pop()
|
2010-07-29 10:21:48 +00:00
|
|
|
{
|
2013-08-19 19:16:01 +00:00
|
|
|
if (NeedSize)
|
2015-05-25 22:52:04 +00:00
|
|
|
m_size--;
|
2016-01-21 20:16:51 +00:00
|
|
|
ElementPtr* tmpptr = m_read_ptr;
|
2010-07-29 10:21:48 +00:00
|
|
|
// advance the read pointer
|
2015-05-25 22:52:04 +00:00
|
|
|
m_read_ptr = tmpptr->next.load();
|
2014-03-09 20:14:26 +00:00
|
|
|
// set the next element to nullptr to stop the recursive deletion
|
2015-05-25 22:52:04 +00:00
|
|
|
tmpptr->next.store(nullptr);
|
2010-07-29 10:21:48 +00:00
|
|
|
delete tmpptr; // this also deletes the element
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-08-10 04:12:32 +00:00
|
|
|
bool Pop(T& t)
|
2010-07-29 10:21:48 +00:00
|
|
|
{
|
2010-08-10 04:12:32 +00:00
|
|
|
if (Empty())
|
2010-07-29 10:21:48 +00:00
|
|
|
return false;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-08-29 23:19:44 +00:00
|
|
|
if (NeedSize)
|
2015-05-25 22:52:04 +00:00
|
|
|
m_size--;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-21 20:16:51 +00:00
|
|
|
ElementPtr* tmpptr = m_read_ptr;
|
2015-05-25 22:52:04 +00:00
|
|
|
m_read_ptr = tmpptr->next.load(std::memory_order_acquire);
|
2013-08-29 23:19:44 +00:00
|
|
|
t = std::move(tmpptr->current);
|
2015-05-25 22:52:04 +00:00
|
|
|
tmpptr->next.store(nullptr);
|
2013-08-29 23:19:44 +00:00
|
|
|
delete tmpptr;
|
2010-07-29 10:21:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-08-10 04:12:32 +00:00
|
|
|
// not thread-safe
|
|
|
|
void Clear()
|
|
|
|
{
|
2015-05-25 22:52:04 +00:00
|
|
|
m_size.store(0);
|
2010-08-10 04:12:32 +00:00
|
|
|
delete m_read_ptr;
|
|
|
|
m_write_ptr = m_read_ptr = new ElementPtr();
|
|
|
|
}
|
|
|
|
|
2010-07-29 10:21:48 +00:00
|
|
|
private:
|
2010-08-10 04:12:32 +00:00
|
|
|
// stores a pointer to element
|
2010-07-29 10:21:48 +00:00
|
|
|
// and a pointer to the next ElementPtr
|
|
|
|
class ElementPtr
|
|
|
|
{
|
|
|
|
public:
|
2014-03-09 20:14:26 +00:00
|
|
|
ElementPtr() : next(nullptr) {}
|
2010-07-29 10:21:48 +00:00
|
|
|
~ElementPtr()
|
|
|
|
{
|
2015-05-25 22:52:04 +00:00
|
|
|
ElementPtr* next_ptr = next.load();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-25 22:52:04 +00:00
|
|
|
if (next_ptr)
|
|
|
|
delete next_ptr;
|
2010-07-29 10:21:48 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-08-19 19:16:01 +00:00
|
|
|
T current;
|
2015-05-25 22:52:04 +00:00
|
|
|
std::atomic<ElementPtr*> next;
|
2010-07-29 10:21:48 +00:00
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-21 20:16:51 +00:00
|
|
|
ElementPtr* m_write_ptr;
|
|
|
|
ElementPtr* m_read_ptr;
|
2015-05-25 22:52:04 +00:00
|
|
|
std::atomic<u32> m_size;
|
2010-07-29 10:21:48 +00:00
|
|
|
};
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace Common
|