From 709b457e176263673dd54a67f74a60f23930667b Mon Sep 17 00:00:00 2001 From: Andrej Babic Date: Wed, 29 Apr 2020 11:10:43 +0200 Subject: [PATCH] Fix FastQueue bugs --- core-buffer/include/FastQueue.hpp | 8 +++++--- core-buffer/src/FastQueue.cpp | 11 +++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/core-buffer/include/FastQueue.hpp b/core-buffer/include/FastQueue.hpp index 84a9139..4103c55 100644 --- a/core-buffer/include/FastQueue.hpp +++ b/core-buffer/include/FastQueue.hpp @@ -17,9 +17,11 @@ class FastQueue { public: - int SLOT_EMPTY=0; - int SLOT_RESERVED=1; - int SLOT_READY=1; + enum SLOT_STATUS { + EMPTY=0, + RESERVED=1, + READY=2 + }; FastQueue(const size_t slot_data_n_bytes, const uint16_t n_slots); virtual ~FastQueue(); diff --git a/core-buffer/src/FastQueue.cpp b/core-buffer/src/FastQueue.cpp index 4ce18b6..3e9765f 100644 --- a/core-buffer/src/FastQueue.cpp +++ b/core-buffer/src/FastQueue.cpp @@ -45,10 +45,11 @@ char* FastQueue::get_data_buffer(const int slot_id) template int FastQueue::reserve() { + int expected = SLOT_STATUS::EMPTY; // If (buffer_status==SLOT_EMPTY) buffer_status=SLOT_RESERVED. bool slot_reserved = buffer_status_[write_slot_id_].compare_exchange_strong( - SLOT_EMPTY, SLOT_RESERVED); + expected, SLOT_STATUS::RESERVED); if (!slot_reserved) { return -1; @@ -60,10 +61,11 @@ int FastQueue::reserve() template void FastQueue::commit() { + int expected = SLOT_STATUS::RESERVED; // If (buffer_status==SLOT_RESERVED) buffer_status=SLOT_READY. bool slot_ready = buffer_status_[write_slot_id_].compare_exchange_strong( - SLOT_RESERVED, SLOT_READY); + expected, SLOT_STATUS::READY); if (!slot_ready) { throw runtime_error("Slot should be reserved first."); @@ -76,7 +78,7 @@ void FastQueue::commit() template int FastQueue::read() { - if (buffer_status_[read_slot_id_] != SLOT_READY) { + if (buffer_status_[read_slot_id_] != SLOT_STATUS::READY) { return -1; } @@ -86,10 +88,11 @@ int FastQueue::read() template void FastQueue::release() { + int expected = SLOT_STATUS::READY; // If (buffer_status==SLOT_RESERVED) buffer_status=SLOT_READY. bool slot_empty = buffer_status_[read_slot_id_].compare_exchange_strong( - SLOT_READY, SLOT_EMPTY); + expected, SLOT_STATUS::EMPTY); if (!slot_empty) { throw runtime_error("Slot should be ready first.");