Commit de733c9a authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Fix PriorityUnboundedBlockingQueue priority translation

Summary: [Folly] Fix `PriorityUnboundedBlockingQueue` priority translation from `Executor` priorities to offsets as used by `PriorityUnboundedBlockingQueueSet`.

Reviewed By: andriigrynenko

Differential Revision: D15619435

fbshipit-source-id: 1c5d4d68dfde7f9db07d64f40e73717b48a05ed1
parent 07d59092
......@@ -76,11 +76,11 @@ class PriorityUnboundedBlockingQueue : public BlockingQueue<T> {
private:
size_t translatePriority(int8_t const priority) {
int8_t const priorities = queue_.priorities();
int8_t const value = (priorities - 1) / 2 + priority;
int8_t const lo = 0;
int8_t const hi = priorities - 1;
return hi - constexpr_clamp(value, lo, hi);
size_t const priorities = queue_.priorities();
assert(priorities <= 255);
int8_t const hi = (priorities + 1) / 2 - 1;
int8_t const lo = hi - (priorities - 1);
return hi - constexpr_clamp(priority, lo, hi);
}
T dequeue() {
......
......@@ -79,7 +79,7 @@ TEST_F(PriorityUnboundedBlockingQueueTest, priority_order) {
EXPECT_EQ(0, q.size());
}
// Since PrioritizedBlockingQueueSetTest implements folly::BlockingQueue<T>,
// Since PriorityUnboundedBlockingQueue implements folly::BlockingQueue<T>,
// addWithPriority method has to accept priority as int_8. This means invalid
// values for priority (such as negative or very large numbers) might get
// passed. Verify this behavior.
......@@ -91,3 +91,12 @@ TEST_F(PriorityUnboundedBlockingQueueTest, invalid_priorities) {
EXPECT_EQ(q.take(), 2);
EXPECT_EQ(q.take(), 1);
}
TEST_F(PriorityUnboundedBlockingQueueTest, invalid_priorities_edge) {
PriorityUnboundedBlockingQueue<int> q(2);
q.addWithPriority(1, Executor::LO_PRI);
q.addWithPriority(2, Executor::HI_PRI);
EXPECT_EQ(q.take(), 2);
EXPECT_EQ(q.take(), 1);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment