Summary:
Oops.
Also: documented the slightly confusing behavior w.r.t. 'size'.
Test Plan: Added a unit test.
Reviewers: tjackson, jdelong
Reviewed By: jdelong
CC: folly@lists, lr, bagashe
Differential Revision: https://phabricator.fb.com/
D486832
struct ProducerConsumerQueue : private boost::noncopyable {
typedef T value_type;
- // size must be >= 2
+ // size must be >= 2.
+ //
+ // Also, note that the number of usable slots in the queue at any
+ // given time is actually (size-1), so if you start with an empty queue,
+ // isFull() will return true after size-1 insertions.
explicit ProducerConsumerQueue(uint32_t size)
: size_(size)
, records_(static_cast<T*>(std::malloc(sizeof(T) * size)))
}
bool isEmpty() const {
- return readIndex_.load(std::memory_order_consume) !=
+ return readIndex_.load(std::memory_order_consume) ==
writeIndex_.load(std::memory_order_consume);
}
}
EXPECT_EQ(DtorChecker::numInstances, 0);
}
+
+TEST(PCQ, EmptyFull) {
+ folly::ProducerConsumerQueue<int> queue(3);
+ EXPECT_TRUE(queue.isEmpty());
+ EXPECT_FALSE(queue.isFull());
+
+ EXPECT_TRUE(queue.write(1));
+ EXPECT_FALSE(queue.isEmpty());
+ EXPECT_FALSE(queue.isFull());
+
+ EXPECT_TRUE(queue.write(2));
+ EXPECT_FALSE(queue.isEmpty());
+ EXPECT_TRUE(queue.isFull()); // Tricky: full after 2 writes, not 3.
+
+ EXPECT_FALSE(queue.write(3));
+}