threadsafe_queue: Use std::size_t for representing size

Makes it consistent with the regular standard containers in terms of
size representation. This also gets rid of dependence on our own
type aliases, removing the need for an include.
This commit is contained in:
Lioncash 2019-02-12 22:12:23 -05:00 committed by fearlessTobi
parent 3bc78e577f
commit 0556cac37d

View File

@ -7,12 +7,11 @@
// a simple lockless thread-safe, // a simple lockless thread-safe,
// single reader, single writer queue // single reader, single writer queue
#include <algorithm>
#include <atomic> #include <atomic>
#include <condition_variable> #include <condition_variable>
#include <cstddef> #include <cstddef>
#include <mutex> #include <mutex>
#include "common/common_types.h" #include <utility>
namespace Common { namespace Common {
template <typename T> template <typename T>
@ -26,7 +25,7 @@ public:
delete read_ptr; delete read_ptr;
} }
u32 Size() const { std::size_t Size() const {
return size.load(); return size.load();
} }
@ -47,9 +46,9 @@ public:
ElementPtr* new_ptr = new ElementPtr(); ElementPtr* new_ptr = new ElementPtr();
write_ptr->next.store(new_ptr, std::memory_order_release); write_ptr->next.store(new_ptr, std::memory_order_release);
write_ptr = new_ptr; write_ptr = new_ptr;
cv.notify_one();
++size; ++size;
cv.notify_one();
} }
void Pop() { void Pop() {
@ -99,7 +98,7 @@ private:
// and a pointer to the next ElementPtr // and a pointer to the next ElementPtr
class ElementPtr { class ElementPtr {
public: public:
ElementPtr() : next(nullptr) {} ElementPtr() = default;
~ElementPtr() { ~ElementPtr() {
ElementPtr* next_ptr = next.load(); ElementPtr* next_ptr = next.load();
@ -108,12 +107,12 @@ private:
} }
T current; T current;
std::atomic<ElementPtr*> next; std::atomic<ElementPtr*> next{nullptr};
}; };
ElementPtr* write_ptr; ElementPtr* write_ptr;
ElementPtr* read_ptr; ElementPtr* read_ptr;
std::atomic<u32> size; std::atomic_size_t size{0};
std::mutex cv_mutex; std::mutex cv_mutex;
std::condition_variable cv; std::condition_variable cv;
}; };
@ -124,7 +123,7 @@ private:
template <typename T> template <typename T>
class MPSCQueue { class MPSCQueue {
public: public:
u32 Size() const { std::size_t Size() const {
return spsc_queue.Size(); return spsc_queue.Size();
} }