2019-10-22 11:24:23 +00:00
|
|
|
#ifndef UDPC_THREADSAFE_LINKEDLIST_QUEUE_HPP
|
|
|
|
#define UDPC_THREADSAFE_LINKEDLIST_QUEUE_HPP
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2019-10-24 08:49:28 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
2019-10-22 11:24:23 +00:00
|
|
|
#include <optional>
|
2019-10-29 11:33:16 +00:00
|
|
|
#include <cassert>
|
2019-10-22 11:24:23 +00:00
|
|
|
#include <list>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class TSLQueue {
|
|
|
|
public:
|
|
|
|
TSLQueue();
|
|
|
|
~TSLQueue();
|
|
|
|
|
|
|
|
// disable copy
|
|
|
|
TSLQueue(const TSLQueue &other) = delete;
|
|
|
|
TSLQueue &operator=(const TSLQueue &other) = delete;
|
|
|
|
// enable move
|
|
|
|
TSLQueue(TSLQueue &&other);
|
|
|
|
TSLQueue &operator=(TSLQueue &&other);
|
|
|
|
|
2019-10-29 11:33:16 +00:00
|
|
|
void push(const T &data);
|
2019-10-24 08:49:28 +00:00
|
|
|
bool push_nb(const T &data);
|
2019-12-17 10:12:54 +00:00
|
|
|
std::unique_ptr<T> top();
|
|
|
|
std::unique_ptr<T> top_nb();
|
2019-10-22 11:24:23 +00:00
|
|
|
bool pop();
|
2019-12-17 10:12:54 +00:00
|
|
|
std::unique_ptr<T> top_and_pop();
|
|
|
|
std::unique_ptr<T> top_and_pop_and_empty(bool *isEmpty);
|
|
|
|
std::unique_ptr<T> top_and_pop_and_rsize(unsigned long *rsize);
|
2019-10-22 11:24:23 +00:00
|
|
|
void clear();
|
|
|
|
|
|
|
|
bool empty();
|
2019-11-06 05:35:16 +00:00
|
|
|
unsigned long size();
|
2019-10-22 11:24:23 +00:00
|
|
|
|
2019-10-29 11:33:16 +00:00
|
|
|
private:
|
|
|
|
struct TSLQNode {
|
2019-11-03 09:46:25 +00:00
|
|
|
TSLQNode();
|
2019-10-29 11:33:16 +00:00
|
|
|
// disable copy
|
|
|
|
TSLQNode(TSLQNode& other) = delete;
|
|
|
|
TSLQNode& operator=(TSLQNode& other) = delete;
|
|
|
|
// enable move
|
|
|
|
TSLQNode(TSLQNode&& other) = default;
|
|
|
|
TSLQNode& operator=(TSLQNode&& other) = default;
|
|
|
|
|
|
|
|
std::shared_ptr<TSLQNode> next;
|
|
|
|
std::weak_ptr<TSLQNode> prev;
|
|
|
|
std::unique_ptr<T> data;
|
2019-11-03 09:46:25 +00:00
|
|
|
|
|
|
|
enum TSLQN_Type {
|
|
|
|
TSLQN_NORMAL,
|
|
|
|
TSLQN_HEAD,
|
|
|
|
TSLQN_TAIL
|
|
|
|
};
|
|
|
|
|
|
|
|
TSLQN_Type type;
|
|
|
|
bool isNormal() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TSLQIter {
|
|
|
|
public:
|
2019-12-11 11:00:48 +00:00
|
|
|
TSLQIter(std::mutex *mutex,
|
2019-11-06 04:32:39 +00:00
|
|
|
std::weak_ptr<TSLQNode> currentNode,
|
2019-11-06 05:35:16 +00:00
|
|
|
unsigned long *msize);
|
2019-11-03 09:46:25 +00:00
|
|
|
~TSLQIter();
|
|
|
|
|
2019-12-17 10:12:54 +00:00
|
|
|
std::unique_ptr<T> current();
|
2019-11-03 09:46:25 +00:00
|
|
|
bool next();
|
|
|
|
bool prev();
|
|
|
|
bool remove();
|
|
|
|
|
|
|
|
private:
|
2019-12-11 11:00:48 +00:00
|
|
|
std::mutex *mutex;
|
2019-11-03 09:46:25 +00:00
|
|
|
std::weak_ptr<TSLQNode> currentNode;
|
2019-11-06 05:35:16 +00:00
|
|
|
unsigned long *const msize;
|
2019-11-06 04:32:39 +00:00
|
|
|
|
2019-10-22 11:24:23 +00:00
|
|
|
};
|
|
|
|
|
2019-11-03 09:46:25 +00:00
|
|
|
public:
|
|
|
|
TSLQIter begin();
|
|
|
|
|
|
|
|
private:
|
2019-10-24 08:49:28 +00:00
|
|
|
std::mutex mutex;
|
2019-10-29 11:33:16 +00:00
|
|
|
std::shared_ptr<TSLQNode> head;
|
|
|
|
std::shared_ptr<TSLQNode> tail;
|
2019-11-06 05:35:16 +00:00
|
|
|
unsigned long msize;
|
2019-10-22 11:24:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2019-10-24 08:49:28 +00:00
|
|
|
TSLQueue<T>::TSLQueue() :
|
2020-03-06 03:05:33 +00:00
|
|
|
head(std::shared_ptr<TSLQNode>(new TSLQNode())),
|
|
|
|
tail(std::shared_ptr<TSLQNode>(new TSLQNode())),
|
2019-10-29 11:33:16 +00:00
|
|
|
msize(0)
|
2019-10-24 08:49:28 +00:00
|
|
|
{
|
2019-10-29 11:33:16 +00:00
|
|
|
head->next = tail;
|
|
|
|
tail->prev = head;
|
2019-11-03 09:46:25 +00:00
|
|
|
head->type = TSLQNode::TSLQN_Type::TSLQN_HEAD;
|
|
|
|
tail->type = TSLQNode::TSLQN_Type::TSLQN_TAIL;
|
2019-10-22 11:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
TSLQueue<T>::~TSLQueue() {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-11-03 09:46:25 +00:00
|
|
|
TSLQueue<T>::TSLQueue(TSLQueue &&other)
|
2019-10-24 08:49:28 +00:00
|
|
|
{
|
2019-12-11 11:00:48 +00:00
|
|
|
std::lock_guard<std::mutex> lock(other.mutex);
|
2019-10-29 11:33:16 +00:00
|
|
|
head = std::move(other.head);
|
|
|
|
tail = std::move(other.tail);
|
|
|
|
msize = std::move(other.msize);
|
2019-10-22 11:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
TSLQueue<T> & TSLQueue<T>::operator=(TSLQueue &&other) {
|
2019-12-11 11:00:48 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
std::lock_guard<std::mutex> otherLock(other.mutex);
|
2019-10-29 11:33:16 +00:00
|
|
|
head = std::move(other.head);
|
|
|
|
tail = std::move(other.tail);
|
|
|
|
msize = std::move(other.msize);
|
2019-10-24 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-10-29 11:33:16 +00:00
|
|
|
void TSLQueue<T>::push(const T &data) {
|
2019-12-11 11:00:48 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2020-03-06 03:05:33 +00:00
|
|
|
auto newNode = std::shared_ptr<TSLQNode>(new TSLQNode());
|
|
|
|
newNode->data = std::unique_ptr<T>(new T(data));
|
2019-10-29 11:33:16 +00:00
|
|
|
|
|
|
|
auto last = tail->prev.lock();
|
|
|
|
assert(last);
|
|
|
|
|
|
|
|
newNode->prev = last;
|
|
|
|
newNode->next = tail;
|
|
|
|
last->next = newNode;
|
|
|
|
tail->prev = newNode;
|
|
|
|
++msize;
|
2019-10-24 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool TSLQueue<T>::push_nb(const T &data) {
|
2019-11-03 09:46:25 +00:00
|
|
|
if(mutex.try_lock()) {
|
2020-03-06 03:05:33 +00:00
|
|
|
auto newNode = std::shared_ptr<TSLQNode>(new TSLQNode());
|
|
|
|
newNode->data = std::unique_ptr<T>(new T(data));
|
2019-10-29 11:33:16 +00:00
|
|
|
|
|
|
|
auto last = tail->prev.lock();
|
|
|
|
assert(last);
|
|
|
|
|
|
|
|
newNode->prev = last;
|
|
|
|
newNode->next = tail;
|
|
|
|
last->next = newNode;
|
|
|
|
tail->prev = newNode;
|
|
|
|
++msize;
|
|
|
|
|
2019-10-24 08:49:28 +00:00
|
|
|
mutex.unlock();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-12-17 10:12:54 +00:00
|
|
|
std::unique_ptr<T> TSLQueue<T>::top() {
|
2019-12-11 11:00:48 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2019-12-17 10:12:54 +00:00
|
|
|
std::unique_ptr<T> result;
|
2019-10-29 11:33:16 +00:00
|
|
|
if(head->next != tail) {
|
|
|
|
assert(head->next->data);
|
2019-12-17 10:12:54 +00:00
|
|
|
result = std::unique_ptr<T>(new T);
|
|
|
|
*result = *head->next->data;
|
2019-10-24 08:49:28 +00:00
|
|
|
}
|
2019-12-17 10:12:54 +00:00
|
|
|
return result;
|
2019-10-24 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-12-17 10:12:54 +00:00
|
|
|
std::unique_ptr<T> TSLQueue<T>::top_nb() {
|
|
|
|
std::unique_ptr<T> result;
|
2019-11-03 09:46:25 +00:00
|
|
|
if(mutex.try_lock()) {
|
2019-10-29 11:33:16 +00:00
|
|
|
if(head->next != tail) {
|
|
|
|
assert(head->next->data);
|
2019-12-17 10:12:54 +00:00
|
|
|
result = std::unique_ptr<T>(new T);
|
|
|
|
*result = *head->next->data;
|
2019-10-29 11:33:16 +00:00
|
|
|
}
|
2019-10-24 08:49:28 +00:00
|
|
|
mutex.unlock();
|
|
|
|
}
|
2019-12-17 10:12:54 +00:00
|
|
|
return result;
|
2019-10-24 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool TSLQueue<T>::pop() {
|
2019-12-11 11:00:48 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2019-10-29 11:33:16 +00:00
|
|
|
if(head->next == tail) {
|
2019-10-24 08:49:28 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2019-10-29 11:33:16 +00:00
|
|
|
auto& newNext = head->next->next;
|
|
|
|
newNext->prev = head;
|
|
|
|
head->next = newNext;
|
|
|
|
assert(msize > 0);
|
|
|
|
--msize;
|
|
|
|
|
2019-10-24 08:49:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-12-17 10:12:54 +00:00
|
|
|
std::unique_ptr<T> TSLQueue<T>::top_and_pop() {
|
|
|
|
std::unique_ptr<T> result;
|
2019-12-11 11:00:48 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2019-10-29 11:33:16 +00:00
|
|
|
if(head->next != tail) {
|
|
|
|
assert(head->next->data);
|
2019-12-17 10:12:54 +00:00
|
|
|
result = std::unique_ptr<T>(new T);
|
|
|
|
*result = *head->next->data;
|
2019-10-29 11:33:16 +00:00
|
|
|
|
|
|
|
auto& newNext = head->next->next;
|
|
|
|
newNext->prev = head;
|
|
|
|
head->next = newNext;
|
|
|
|
assert(msize > 0);
|
|
|
|
--msize;
|
2019-10-24 08:49:28 +00:00
|
|
|
}
|
2019-12-17 10:12:54 +00:00
|
|
|
return result;
|
2019-10-24 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-12-17 10:12:54 +00:00
|
|
|
std::unique_ptr<T> TSLQueue<T>::top_and_pop_and_empty(bool *isEmpty) {
|
|
|
|
std::unique_ptr<T> result;
|
2019-12-11 11:00:48 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2019-10-29 11:33:16 +00:00
|
|
|
if(head->next == tail) {
|
2019-10-24 08:49:28 +00:00
|
|
|
if(isEmpty) {
|
|
|
|
*isEmpty = true;
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-29 11:33:16 +00:00
|
|
|
assert(head->next->data);
|
2019-12-17 10:12:54 +00:00
|
|
|
result = std::unique_ptr<T>(new T);
|
|
|
|
*result = *head->next->data;
|
2019-10-29 11:33:16 +00:00
|
|
|
|
|
|
|
auto& newNext = head->next->next;
|
|
|
|
newNext->prev = head;
|
|
|
|
head->next = newNext;
|
|
|
|
assert(msize > 0);
|
|
|
|
--msize;
|
|
|
|
|
2019-10-24 08:49:28 +00:00
|
|
|
if(isEmpty) {
|
2019-10-29 11:33:16 +00:00
|
|
|
*isEmpty = head->next == tail;
|
2019-10-24 08:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-17 10:12:54 +00:00
|
|
|
return result;
|
2019-10-24 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-06 05:35:16 +00:00
|
|
|
template <typename T>
|
2019-12-17 10:12:54 +00:00
|
|
|
std::unique_ptr<T> TSLQueue<T>::top_and_pop_and_rsize(unsigned long *rsize) {
|
|
|
|
std::unique_ptr<T> result;
|
2019-12-11 11:00:48 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2019-11-06 05:35:16 +00:00
|
|
|
if(head->next == tail) {
|
|
|
|
if(rsize) {
|
|
|
|
*rsize = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(head->next->data);
|
2019-12-17 10:12:54 +00:00
|
|
|
result = std::unique_ptr<T>(new T);
|
|
|
|
*result = *head->next->data;
|
2019-11-06 05:35:16 +00:00
|
|
|
|
|
|
|
auto& newNext = head->next->next;
|
|
|
|
newNext->prev = head;
|
|
|
|
head->next = newNext;
|
|
|
|
assert(msize > 0);
|
|
|
|
--msize;
|
|
|
|
|
|
|
|
if(rsize) {
|
|
|
|
*rsize = msize;
|
|
|
|
}
|
|
|
|
}
|
2019-12-17 10:12:54 +00:00
|
|
|
return result;
|
2019-11-06 05:35:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 08:49:28 +00:00
|
|
|
template <typename T>
|
|
|
|
void TSLQueue<T>::clear() {
|
2019-12-11 11:00:48 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2019-10-29 11:33:16 +00:00
|
|
|
|
|
|
|
head->next = tail;
|
|
|
|
tail->prev = head;
|
|
|
|
msize = 0;
|
2019-10-22 11:24:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 08:51:40 +00:00
|
|
|
template <typename T>
|
|
|
|
bool TSLQueue<T>::empty() {
|
2019-12-11 11:00:48 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2019-10-29 11:33:16 +00:00
|
|
|
return head->next == tail;
|
2019-10-24 08:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-11-06 05:35:16 +00:00
|
|
|
unsigned long TSLQueue<T>::size() {
|
2019-12-11 11:00:48 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2019-10-29 11:33:16 +00:00
|
|
|
return msize;
|
2019-10-24 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 09:46:25 +00:00
|
|
|
template <typename T>
|
|
|
|
TSLQueue<T>::TSLQNode::TSLQNode() :
|
|
|
|
type(TSLQN_Type::TSLQN_NORMAL)
|
|
|
|
{}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool TSLQueue<T>::TSLQNode::isNormal() const {
|
|
|
|
return type == TSLQN_Type::TSLQN_NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-12-11 11:00:48 +00:00
|
|
|
TSLQueue<T>::TSLQIter::TSLQIter(std::mutex *mutex,
|
2019-11-06 04:32:39 +00:00
|
|
|
std::weak_ptr<TSLQNode> currentNode,
|
2019-11-06 05:35:16 +00:00
|
|
|
unsigned long *msize) :
|
2019-12-11 11:00:48 +00:00
|
|
|
mutex(mutex),
|
2019-11-06 04:32:39 +00:00
|
|
|
currentNode(currentNode),
|
|
|
|
msize(msize)
|
2019-11-03 09:46:25 +00:00
|
|
|
{
|
2019-12-11 11:00:48 +00:00
|
|
|
mutex->lock();
|
2019-11-03 09:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
TSLQueue<T>::TSLQIter::~TSLQIter() {
|
2019-12-11 11:00:48 +00:00
|
|
|
mutex->unlock();
|
2019-11-03 09:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-12-17 10:12:54 +00:00
|
|
|
std::unique_ptr<T> TSLQueue<T>::TSLQIter::current() {
|
|
|
|
std::unique_ptr<T> result;
|
2019-11-03 09:46:25 +00:00
|
|
|
std::shared_ptr<TSLQNode> currentNode = this->currentNode.lock();
|
|
|
|
assert(currentNode);
|
|
|
|
if(currentNode->isNormal()) {
|
2019-12-17 10:12:54 +00:00
|
|
|
result = std::unique_ptr<T>(new T);
|
|
|
|
*result = *currentNode->data;
|
2019-11-03 09:46:25 +00:00
|
|
|
}
|
2019-12-17 10:12:54 +00:00
|
|
|
return result;
|
2019-11-03 09:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool TSLQueue<T>::TSLQIter::next() {
|
|
|
|
std::shared_ptr<TSLQNode> currentNode = this->currentNode.lock();
|
|
|
|
assert(currentNode);
|
|
|
|
if(currentNode->type == TSLQNode::TSLQN_Type::TSLQN_TAIL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->currentNode = currentNode->next;
|
|
|
|
return currentNode->next->type != TSLQNode::TSLQN_Type::TSLQN_TAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool TSLQueue<T>::TSLQIter::prev() {
|
|
|
|
std::shared_ptr<TSLQNode> currentNode = this->currentNode.lock();
|
|
|
|
assert(currentNode);
|
|
|
|
if(currentNode->type == TSLQNode::TSLQN_Type::TSLQN_HEAD) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto parent = currentNode->prev.lock();
|
|
|
|
assert(parent);
|
|
|
|
this->currentNode = currentNode->prev;
|
|
|
|
return parent->type != TSLQNode::TSLQN_Type::TSLQN_HEAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool TSLQueue<T>::TSLQIter::remove() {
|
|
|
|
std::shared_ptr<TSLQNode> currentNode = this->currentNode.lock();
|
|
|
|
assert(currentNode);
|
|
|
|
if(!currentNode->isNormal()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->currentNode = currentNode->next;
|
|
|
|
auto parent = currentNode->prev.lock();
|
|
|
|
assert(parent);
|
|
|
|
|
|
|
|
currentNode->next->prev = parent;
|
|
|
|
parent->next = currentNode->next;
|
|
|
|
|
2019-11-06 04:32:39 +00:00
|
|
|
assert(*msize > 0);
|
|
|
|
--(*msize);
|
|
|
|
|
2019-11-03 09:46:25 +00:00
|
|
|
return parent->next->isNormal();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename TSLQueue<T>::TSLQIter TSLQueue<T>::begin() {
|
2019-12-11 11:00:48 +00:00
|
|
|
return TSLQIter(&mutex, head->next, &msize);
|
2019-11-03 09:46:25 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 11:24:23 +00:00
|
|
|
#endif
|