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 fd9c94a0dc3fd49edda24695cb16323be4580a77..8d2e47f084fdea9288ece427f9c343f973d80d42 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 a30b6e2f134436f222f8b30186b00a2fbf8c1c52..b6dc84c167c3a259549f390d517c87c3975501b7 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.
+}