diff --git a/src/core/common/message.cpp b/src/core/common/message.cpp index f8ade9ad8..40cd1c861 100644 --- a/src/core/common/message.cpp +++ b/src/core/common/message.cpp @@ -955,7 +955,7 @@ Message *PriorityQueue::GetHead(void) const { Message *tail; - tail = FindFirstNonNullTail(Message::kNumPriorities - 1); + tail = FindFirstNonNullTail(0); return (tail == NULL) ? NULL : tail->Next(MessageInfo::kListInterface); } @@ -983,7 +983,7 @@ Message *PriorityQueue::GetHeadForPriority(uint8_t aPriority) const Message *PriorityQueue::GetTail(void) const { - return FindFirstNonNullTail(Message::kNumPriorities - 1); + return FindFirstNonNullTail(0); } void PriorityQueue::AddToList(uint8_t aList, Message &aMessage) diff --git a/src/core/common/message.hpp b/src/core/common/message.hpp index cd124213c..b6e20a4b9 100644 --- a/src/core/common/message.hpp +++ b/src/core/common/message.hpp @@ -229,10 +229,10 @@ public: enum { - kPriorityHigh = 0, ///< High priority level. - kPriorityMedium = 1, ///< Medium priority level. - kPriorityLow = 2, ///< Low priority level. - kPriorityVeryLow = 3, ///< Very low priority level. + kPriorityVeryLow = 0, ///< Very low priority level. + kPriorityLow = 1, ///< Low priority level. + kPriorityMedium = 2, ///< Medium priority level. + kPriorityHigh = 3, ///< High priority level. kNumPriorities = 4, ///< Number of priority levels. }; @@ -1103,21 +1103,21 @@ private: void RemoveFromList(uint8_t aListId, Message &aMessage); /** - * This method decreases (moves back) the given priority while ensuring to wrap from - * priority value 0 back to `kNumPriorities` -1. + * This method increases (moves forward) the given priority while ensuring to wrap from + * priority value `kNumPriorities` -1 back to 0. * * @param[in] aPriority A given priority level * - * @returns Decreased/Moved back priority level + * @returns Increased/Moved forward priority level */ uint8_t PrevPriority(uint8_t aPriority) const { - return (aPriority == 0) ? (Message::kNumPriorities - 1) : (aPriority - 1); + return (aPriority == Message::kNumPriorities - 1) ? 0 : (aPriority + 1); } /** - * This private method finds the first non-NULL tail starting from the given priority level and moving back. - * It wraps from priority value 0 back to `kNumPriorities` -1. + * This private method finds the first non-NULL tail starting from the given priority level and moving forward. + * It wraps from priority value `kNumPriorities` -1 back to 0. * * aStartPriorityLevel Starting priority level. * diff --git a/src/core/thread/mesh_forwarder_ftd.cpp b/src/core/thread/mesh_forwarder_ftd.cpp index c871afad1..970902ba9 100644 --- a/src/core/thread/mesh_forwarder_ftd.cpp +++ b/src/core/thread/mesh_forwarder_ftd.cpp @@ -242,14 +242,14 @@ otError MeshForwarder::EvictMessage(uint8_t aPriority) VerifyOrExit((message = mSendQueue.GetTail()) != NULL); - if (message->GetPriority() > aPriority) + if (message->GetPriority() < aPriority) { RemoveMessage(*message); ExitNow(error = OT_ERROR_NONE); } else { - while (1) + while (aPriority <= Message::kPriorityHigh) { for (message = mSendQueue.GetHeadForPriority(aPriority); message && (message->GetPriority() == aPriority); message = message->GetNext()) @@ -261,9 +261,7 @@ otError MeshForwarder::EvictMessage(uint8_t aPriority) } } - VerifyOrExit(aPriority != Message::kPriorityHigh); - - aPriority--; + aPriority++; } } diff --git a/src/core/thread/mesh_forwarder_mtd.cpp b/src/core/thread/mesh_forwarder_mtd.cpp index 5ea2bc1ae..e38a7a485 100644 --- a/src/core/thread/mesh_forwarder_mtd.cpp +++ b/src/core/thread/mesh_forwarder_mtd.cpp @@ -61,7 +61,7 @@ otError MeshForwarder::EvictMessage(uint8_t aPriority) VerifyOrExit((message = mSendQueue.GetTail()) != NULL); - if (message->GetPriority() > aPriority) + if (message->GetPriority() < aPriority) { RemoveMessage(*message); ExitNow(error = OT_ERROR_NONE); diff --git a/tests/unit/test_priority_queue.cpp b/tests/unit/test_priority_queue.cpp index a3b8d0d01..62df79159 100644 --- a/tests/unit/test_priority_queue.cpp +++ b/tests/unit/test_priority_queue.cpp @@ -44,7 +44,7 @@ void VerifyPriorityQueueContent(ot::PriorityQueue &aPriorityQueue, int aExpected va_list args; ot::Message *message; ot::Message *msgArg; - uint8_t curPriority = 0xff; + int8_t curPriority = ot::Message::kNumPriorities; uint16_t msgCount, bufCount; // Check the `GetInfo` @@ -74,7 +74,7 @@ void VerifyPriorityQueueContent(ot::PriorityQueue &aPriorityQueue, int aExpected if (msgArg->GetPriority() != curPriority) { - for (curPriority++; curPriority != msgArg->GetPriority(); curPriority++) + for (curPriority--; curPriority != msgArg->GetPriority(); curPriority--) { // Check the `GetHeadForPriority` is NULL if there are no expected message for this priority level. VerifyOrQuit( @@ -96,7 +96,7 @@ void VerifyPriorityQueueContent(ot::PriorityQueue &aPriorityQueue, int aExpected VerifyOrQuit(aExpectedLength == 0, "PriorityQueue contains less entries than expected.\n"); // Check the `GetHeadForPriority` is NULL if there are no expected message for any remaining priority level. - for (curPriority++; curPriority < 4; curPriority++) + for (curPriority--; curPriority >= 0; curPriority--) { VerifyOrQuit(aPriorityQueue.GetHeadForPriority(curPriority) == NULL, "PriorityQueue::GetHeadForPriority is non-NULL when no expected msg for this priority.\n"); @@ -221,16 +221,16 @@ void TestPriorityQueue(void) { msgHigh[i] = messagePool->New(ot::Message::kTypeIp6, 0); VerifyOrQuit(msgHigh[i] != NULL, "Message::New failed\n"); - SuccessOrQuit(msgHigh[i]->SetPriority(0), "Message:SetPriority failed\n"); + SuccessOrQuit(msgHigh[i]->SetPriority(3), "Message:SetPriority failed\n"); msgMed[i] = messagePool->New(ot::Message::kTypeIp6, 0); VerifyOrQuit(msgMed[i] != NULL, "Message::New failed\n"); - SuccessOrQuit(msgMed[i]->SetPriority(1), "Message:SetPriority failed\n"); + SuccessOrQuit(msgMed[i]->SetPriority(2), "Message:SetPriority failed\n"); msgLow[i] = messagePool->New(ot::Message::kTypeIp6, 0); VerifyOrQuit(msgLow[i] != NULL, "Message::New failed\n"); - SuccessOrQuit(msgLow[i]->SetPriority(2), "Message:SetPriority failed\n"); + SuccessOrQuit(msgLow[i]->SetPriority(1), "Message:SetPriority failed\n"); msgVeryLow[i] = messagePool->New(ot::Message::kTypeIp6, 0); VerifyOrQuit(msgVeryLow[i] != NULL, "Message::New failed\n"); - SuccessOrQuit(msgVeryLow[i]->SetPriority(3), "Message:SetPriority failed\n"); + SuccessOrQuit(msgVeryLow[i]->SetPriority(0), "Message:SetPriority failed\n"); } // Check the failure case for `SetPriority` for invalid argument. @@ -240,10 +240,10 @@ void TestPriorityQueue(void) // Check the `GetPriority()` for (int i = 0; i < kNumTestMessages; i++) { - VerifyOrQuit(msgHigh[i]->GetPriority() == 0, "Message::GetPriority failed.\n"); - VerifyOrQuit(msgMed[i]->GetPriority() == 1, "Message::GetPriority failed.\n"); - VerifyOrQuit(msgLow[i]->GetPriority() == 2, "Message::GetPriority failed.\n"); - VerifyOrQuit(msgVeryLow[i]->GetPriority() == 3, "Message::GetPriority failed.\n"); + VerifyOrQuit(msgVeryLow[i]->GetPriority() == 0, "Message::GetPriority failed.\n"); + VerifyOrQuit(msgLow[i]->GetPriority() == 1, "Message::GetPriority failed.\n"); + VerifyOrQuit(msgMed[i]->GetPriority() == 2, "Message::GetPriority failed.\n"); + VerifyOrQuit(msgHigh[i]->GetPriority() == 3, "Message::GetPriority failed.\n"); } // Verify case of an empty queue. @@ -331,19 +331,19 @@ void TestPriorityQueue(void) VerifyPriorityQueueContent(queue, 3, msgMed[0], msgLow[0], msgVeryLow[0]); VerifyAllMessagesContent(messagePool, 3, msgMed[0], msgLow[0], msgVeryLow[0]); - SuccessOrQuit(msgLow[0]->SetPriority(0), "SetPriority failed for an already queued message.\n"); + SuccessOrQuit(msgLow[0]->SetPriority(3), "SetPriority failed for an already queued message.\n"); VerifyPriorityQueueContent(queue, 3, msgLow[0], msgMed[0], msgVeryLow[0]); - SuccessOrQuit(msgVeryLow[0]->SetPriority(3), "SetPriority failed for an already queued message.\n"); - VerifyPriorityQueueContent(queue, 3, msgLow[0], msgMed[0], msgVeryLow[0]); - SuccessOrQuit(msgVeryLow[0]->SetPriority(2), "SetPriority failed for an already queued message.\n"); + SuccessOrQuit(msgVeryLow[0]->SetPriority(0), "SetPriority failed for an already queued message.\n"); VerifyPriorityQueueContent(queue, 3, msgLow[0], msgMed[0], msgVeryLow[0]); SuccessOrQuit(msgVeryLow[0]->SetPriority(1), "SetPriority failed for an already queued message.\n"); VerifyPriorityQueueContent(queue, 3, msgLow[0], msgMed[0], msgVeryLow[0]); + SuccessOrQuit(msgVeryLow[0]->SetPriority(2), "SetPriority failed for an already queued message.\n"); + VerifyPriorityQueueContent(queue, 3, msgLow[0], msgMed[0], msgVeryLow[0]); VerifyAllMessagesContent(messagePool, 3, msgLow[0], msgMed[0], msgVeryLow[0]); - SuccessOrQuit(msgVeryLow[0]->SetPriority(0), "SetPriority failed for an already queued message.\n"); - VerifyPriorityQueueContent(queue, 3, msgLow[0], msgVeryLow[0], msgMed[0]); - SuccessOrQuit(msgLow[0]->SetPriority(2), "SetPriority failed for an already queued message.\n"); SuccessOrQuit(msgVeryLow[0]->SetPriority(3), "SetPriority failed for an already queued message.\n"); + VerifyPriorityQueueContent(queue, 3, msgLow[0], msgVeryLow[0], msgMed[0]); + SuccessOrQuit(msgLow[0]->SetPriority(1), "SetPriority failed for an already queued message.\n"); + SuccessOrQuit(msgVeryLow[0]->SetPriority(0), "SetPriority failed for an already queued message.\n"); VerifyPriorityQueueContent(queue, 3, msgMed[0], msgLow[0], msgVeryLow[0]); VerifyAllMessagesContent(messagePool, 3, msgMed[0], msgLow[0], msgVeryLow[0]); VerifyAllMessagesContentInReverse(messagePool, 3, msgVeryLow[0], msgLow[0], msgMed[0]); @@ -359,13 +359,13 @@ void TestPriorityQueue(void) VerifyMsgQueueContent(messageQueue, 3, msgLow[1], msgMed[1], msgHigh[1]); // Change priority of message and check that order changes in the AllMessage queue and not in messageQueue. - SuccessOrQuit(msgLow[1]->SetPriority(0), "SetPriority failed for an already queued message.\n"); + SuccessOrQuit(msgLow[1]->SetPriority(3), "SetPriority failed for an already queued message.\n"); VerifyAllMessagesContent(messagePool, 6, msgHigh[1], msgLow[1], msgMed[0], msgMed[1], msgLow[0], msgVeryLow[0]); VerifyAllMessagesContentInReverse(messagePool, 6, msgVeryLow[0], msgLow[0], msgMed[1], msgMed[0], msgLow[1], msgHigh[1]); VerifyMsgQueueContent(messageQueue, 3, msgLow[1], msgMed[1], msgHigh[1]); - SuccessOrQuit(msgVeryLow[0]->SetPriority(1), "SetPriority failed for an already queued message.\n"); + SuccessOrQuit(msgVeryLow[0]->SetPriority(2), "SetPriority failed for an already queued message.\n"); VerifyAllMessagesContent(messagePool, 6, msgHigh[1], msgLow[1], msgMed[0], msgMed[1], msgVeryLow[0], msgLow[0]); VerifyPriorityQueueContent(queue, 3, msgMed[0], msgVeryLow[0], msgLow[0]); VerifyMsgQueueContent(messageQueue, 3, msgLow[1], msgMed[1], msgHigh[1]);