2017-11-25 14:56:57 +01:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// a simple lockless thread-safe,
|
|
|
|
// single reader, single writer queue
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
2018-09-09 13:08:57 +02:00
|
|
|
#include <condition_variable>
|
2017-11-25 14:56:57 +01:00
|
|
|
#include <cstddef>
|
|
|
|
#include <mutex>
|
|
|
|
#include "common/common_types.h"
|
|
|
|
|
|
|
|
namespace Common {
|
2019-02-13 04:03:48 +01:00
|
|
|
template <typename T>
|
2017-11-25 14:56:57 +01:00
|
|
|
class SPSCQueue {
|
|
|
|
public:
|
2019-02-13 04:03:48 +01:00
|
|
|
SPSCQueue() {
|
2017-11-25 14:56:57 +01:00
|
|
|
write_ptr = read_ptr = new ElementPtr();
|
|
|
|
}
|
|
|
|
~SPSCQueue() {
|
|
|
|
// this will empty out the whole queue
|
|
|
|
delete read_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 Size() const {
|
|
|
|
return size.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Empty() const {
|
2019-02-13 04:03:48 +01:00
|
|
|
return Size() == 0;
|
2017-11-25 14:56:57 +01:00
|
|
|
}
|
2018-07-21 16:16:21 +02:00
|
|
|
|
2017-11-25 14:56:57 +01:00
|
|
|
T& Front() const {
|
|
|
|
return read_ptr->current;
|
|
|
|
}
|
2018-07-21 16:16:21 +02:00
|
|
|
|
2017-11-25 14:56:57 +01:00
|
|
|
template <typename Arg>
|
|
|
|
void Push(Arg&& t) {
|
|
|
|
// create the element, add it to the queue
|
2018-10-08 23:28:54 +02:00
|
|
|
write_ptr->current = std::forward<Arg>(t);
|
2017-11-25 14:56:57 +01:00
|
|
|
// set the next pointer to a new element ptr
|
|
|
|
// then advance the write pointer
|
|
|
|
ElementPtr* new_ptr = new ElementPtr();
|
|
|
|
write_ptr->next.store(new_ptr, std::memory_order_release);
|
|
|
|
write_ptr = new_ptr;
|
2018-09-09 13:08:57 +02:00
|
|
|
cv.notify_one();
|
2019-02-13 04:03:48 +01:00
|
|
|
|
|
|
|
++size;
|
2017-11-25 14:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Pop() {
|
2019-02-13 04:03:48 +01:00
|
|
|
--size;
|
|
|
|
|
2017-11-25 14:56:57 +01:00
|
|
|
ElementPtr* tmpptr = read_ptr;
|
|
|
|
// advance the read pointer
|
|
|
|
read_ptr = tmpptr->next.load();
|
|
|
|
// set the next element to nullptr to stop the recursive deletion
|
|
|
|
tmpptr->next.store(nullptr);
|
|
|
|
delete tmpptr; // this also deletes the element
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Pop(T& t) {
|
|
|
|
if (Empty())
|
|
|
|
return false;
|
|
|
|
|
2019-02-13 04:03:48 +01:00
|
|
|
--size;
|
2017-11-25 14:56:57 +01:00
|
|
|
|
2018-10-08 23:28:54 +02:00
|
|
|
ElementPtr* tmpptr = read_ptr;
|
2017-11-25 14:56:57 +01:00
|
|
|
read_ptr = tmpptr->next.load(std::memory_order_acquire);
|
|
|
|
t = std::move(tmpptr->current);
|
|
|
|
tmpptr->next.store(nullptr);
|
|
|
|
delete tmpptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-08 23:28:54 +02:00
|
|
|
T PopWait() {
|
2018-09-09 13:08:57 +02:00
|
|
|
if (Empty()) {
|
|
|
|
std::unique_lock<std::mutex> lock(cv_mutex);
|
|
|
|
cv.wait(lock, [this]() { return !Empty(); });
|
|
|
|
}
|
2018-10-08 23:28:54 +02:00
|
|
|
T t;
|
|
|
|
Pop(t);
|
|
|
|
return t;
|
2018-09-09 13:08:57 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 14:56:57 +01:00
|
|
|
// not thread-safe
|
|
|
|
void Clear() {
|
|
|
|
size.store(0);
|
|
|
|
delete read_ptr;
|
|
|
|
write_ptr = read_ptr = new ElementPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// stores a pointer to element
|
|
|
|
// and a pointer to the next ElementPtr
|
|
|
|
class ElementPtr {
|
|
|
|
public:
|
|
|
|
ElementPtr() : next(nullptr) {}
|
|
|
|
~ElementPtr() {
|
|
|
|
ElementPtr* next_ptr = next.load();
|
|
|
|
|
|
|
|
if (next_ptr)
|
|
|
|
delete next_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
T current;
|
|
|
|
std::atomic<ElementPtr*> next;
|
|
|
|
};
|
|
|
|
|
|
|
|
ElementPtr* write_ptr;
|
|
|
|
ElementPtr* read_ptr;
|
|
|
|
std::atomic<u32> size;
|
2018-09-09 13:08:57 +02:00
|
|
|
std::mutex cv_mutex;
|
|
|
|
std::condition_variable cv;
|
2017-11-25 14:56:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// a simple thread-safe,
|
|
|
|
// single reader, multiple writer queue
|
|
|
|
|
2019-02-13 04:03:48 +01:00
|
|
|
template <typename T>
|
2018-07-21 16:16:21 +02:00
|
|
|
class MPSCQueue {
|
2017-11-25 14:56:57 +01:00
|
|
|
public:
|
2018-07-21 16:16:21 +02:00
|
|
|
u32 Size() const {
|
|
|
|
return spsc_queue.Size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Empty() const {
|
|
|
|
return spsc_queue.Empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
T& Front() const {
|
|
|
|
return spsc_queue.Front();
|
|
|
|
}
|
|
|
|
|
2017-11-25 14:56:57 +01:00
|
|
|
template <typename Arg>
|
|
|
|
void Push(Arg&& t) {
|
|
|
|
std::lock_guard<std::mutex> lock(write_lock);
|
2018-07-21 16:16:21 +02:00
|
|
|
spsc_queue.Push(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pop() {
|
|
|
|
return spsc_queue.Pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Pop(T& t) {
|
|
|
|
return spsc_queue.Pop(t);
|
|
|
|
}
|
|
|
|
|
2018-10-08 23:28:54 +02:00
|
|
|
T PopWait() {
|
|
|
|
return spsc_queue.PopWait();
|
2018-09-09 13:08:57 +02:00
|
|
|
}
|
|
|
|
|
2018-07-21 16:16:21 +02:00
|
|
|
// not thread-safe
|
|
|
|
void Clear() {
|
|
|
|
spsc_queue.Clear();
|
2017-11-25 14:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-02-13 04:03:48 +01:00
|
|
|
SPSCQueue<T> spsc_queue;
|
2017-11-25 14:56:57 +01:00
|
|
|
std::mutex write_lock;
|
|
|
|
};
|
|
|
|
} // namespace Common
|