From 29009c2b8c2da6f52e9e9ba98159e89231daa59e Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 18 Aug 2019 18:10:11 +0900 Subject: [PATCH] Replace spinlock with mutex/lock_guard in TSQueue --- cpp_impl/src/TSQueue.hpp | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/cpp_impl/src/TSQueue.hpp b/cpp_impl/src/TSQueue.hpp index 7d83dee..bef1fa5 100644 --- a/cpp_impl/src/TSQueue.hpp +++ b/cpp_impl/src/TSQueue.hpp @@ -3,9 +3,9 @@ #define UDPC_TSQUEUE_DEFAULT_CAPACITY 32 -#include #include #include +#include #include @@ -31,13 +31,13 @@ class TSQueue { bool empty(); private: - std::atomic_bool spinLock; + std::mutex mutex; RB::RingBuffer rb; }; template TSQueue::TSQueue(unsigned int capacity) : -spinLock(false), +mutex(), rb(capacity) { rb.setResizePolicy(false); @@ -49,55 +49,47 @@ TSQueue::~TSQueue() template bool TSQueue::push(const T &data) { - while(spinLock.exchange(true)) {} + std::lock_guard lock(mutex); if(rb.getSize() == rb.getCapacity()) { - spinLock.store(false); return false; } rb.push(data); - spinLock.store(false); return true; } template T TSQueue::top() { - while(spinLock.exchange(true)) {} + std::lock_guard lock(mutex); T value = rb.top(); - spinLock.store(false); return value; } template bool TSQueue::pop() { - while(spinLock.exchange(true)) {} + std::lock_guard lock(mutex); if(rb.empty()) { - spinLock.store(false); return false; } rb.pop(); - spinLock.store(false); return true; } template void TSQueue::clear() { - while(spinLock.exchange(true)) {} + std::lock_guard lock(mutex); rb.resize(0); - spinLock.store(false); } template void TSQueue::changeCapacity(unsigned int newCapacity) { - while(spinLock.exchange(true)) {} + std::lock_guard lock(mutex); rb.changeCapacity(newCapacity); - spinLock.store(false); } template unsigned int TSQueue::size() { - while(spinLock.exchange(true)) {} + std::lock_guard lock(mutex); unsigned int size = rb.getSize(); - spinLock.store(false); return size; }