Add ProducerConsumerQueue::capacity()
authorPeter Alexander <pja@fb.com>
Thu, 19 Oct 2017 09:46:56 +0000 (02:46 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 19 Oct 2017 09:52:54 +0000 (02:52 -0700)
Summary: Simple addition. Easy to track externally, but might as well provide it in the class if it is readily available.

Reviewed By: yfeldblum

Differential Revision: D6093826

fbshipit-source-id: 9d8c02891b2cea9ce0d3f6ea78e1e0055b536eb8

folly/ProducerConsumerQueue.h
folly/test/ProducerConsumerQueueTest.cpp

index fd9c94a..8d2e47f 100644 (file)
@@ -167,6 +167,11 @@ struct ProducerConsumerQueue {
     return ret;
   }
 
+  // maximum number of items in the queue.
+  size_t capacity() const {
+    return size_ - 1;
+  }
+
  private:
   char pad0_[CacheLocality::kFalseSharingRange];
   const uint32_t size_;
index a30b6e2..b6dc84c 100644 (file)
@@ -288,3 +288,8 @@ TEST(PCQ, EmptyFull) {
   EXPECT_FALSE(queue.write(3));
   EXPECT_EQ(queue.sizeGuess(), 2);
 }
+
+TEST(PCQ, Capacity) {
+  folly::ProducerConsumerQueue<int> queue(3);
+  EXPECT_EQ(queue.capacity(), 2); // PCQ max size is buffer size - 1.
+}