|
|
@ -9,6 +9,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <mutex>
|
|
|
|
#include <mutex>
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
@ -41,7 +42,7 @@ public:
|
|
|
|
template <typename Arg>
|
|
|
|
template <typename Arg>
|
|
|
|
void Push(Arg&& t) {
|
|
|
|
void Push(Arg&& t) {
|
|
|
|
// create the element, add it to the queue
|
|
|
|
// create the element, add it to the queue
|
|
|
|
write_ptr->current = std::forward<Arg>(t);
|
|
|
|
write_ptr->current = std::move(t);
|
|
|
|
// set the next pointer to a new element ptr
|
|
|
|
// set the next pointer to a new element ptr
|
|
|
|
// then advance the write pointer
|
|
|
|
// then advance the write pointer
|
|
|
|
ElementPtr* new_ptr = new ElementPtr();
|
|
|
|
ElementPtr* new_ptr = new ElementPtr();
|
|
|
@ -49,6 +50,7 @@ public:
|
|
|
|
write_ptr = new_ptr;
|
|
|
|
write_ptr = new_ptr;
|
|
|
|
if (NeedSize)
|
|
|
|
if (NeedSize)
|
|
|
|
size++;
|
|
|
|
size++;
|
|
|
|
|
|
|
|
cv.notify_one();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Pop() {
|
|
|
|
void Pop() {
|
|
|
@ -66,10 +68,11 @@ public:
|
|
|
|
if (Empty())
|
|
|
|
if (Empty())
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ElementPtr* tmpptr = read_ptr;
|
|
|
|
|
|
|
|
|
|
|
|
if (NeedSize)
|
|
|
|
if (NeedSize)
|
|
|
|
size--;
|
|
|
|
size--;
|
|
|
|
|
|
|
|
|
|
|
|
ElementPtr* tmpptr = read_ptr;
|
|
|
|
|
|
|
|
read_ptr = tmpptr->next.load(std::memory_order_acquire);
|
|
|
|
read_ptr = tmpptr->next.load(std::memory_order_acquire);
|
|
|
|
t = std::move(tmpptr->current);
|
|
|
|
t = std::move(tmpptr->current);
|
|
|
|
tmpptr->next.store(nullptr);
|
|
|
|
tmpptr->next.store(nullptr);
|
|
|
@ -77,6 +80,14 @@ public:
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool PopWait(T& t) {
|
|
|
|
|
|
|
|
if (Empty()) {
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(cv_mutex);
|
|
|
|
|
|
|
|
cv.wait(lock, [this]() { return !Empty(); });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return Pop(t);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// not thread-safe
|
|
|
|
// not thread-safe
|
|
|
|
void Clear() {
|
|
|
|
void Clear() {
|
|
|
|
size.store(0);
|
|
|
|
size.store(0);
|
|
|
@ -104,6 +115,8 @@ private:
|
|
|
|
ElementPtr* write_ptr;
|
|
|
|
ElementPtr* write_ptr;
|
|
|
|
ElementPtr* read_ptr;
|
|
|
|
ElementPtr* read_ptr;
|
|
|
|
std::atomic<u32> size;
|
|
|
|
std::atomic<u32> size;
|
|
|
|
|
|
|
|
std::mutex cv_mutex;
|
|
|
|
|
|
|
|
std::condition_variable cv;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// a simple thread-safe,
|
|
|
|
// a simple thread-safe,
|
|
|
@ -138,6 +151,10 @@ public:
|
|
|
|
return spsc_queue.Pop(t);
|
|
|
|
return spsc_queue.Pop(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool PopWait(T& t) {
|
|
|
|
|
|
|
|
return spsc_queue.PopWait(t);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// not thread-safe
|
|
|
|
// not thread-safe
|
|
|
|
void Clear() {
|
|
|
|
void Clear() {
|
|
|
|
spsc_queue.Clear();
|
|
|
|
spsc_queue.Clear();
|
|
|
|