Add capacity to semaphore so that initial size can be queried later.
authorLee Howes <lwh@fb.com>
Wed, 19 Oct 2016 15:34:20 +0000 (08:34 -0700)
committerFacebook Github Bot <facebook-github-bot-bot@fb.com>
Wed, 19 Oct 2016 15:38:31 +0000 (08:38 -0700)
Reviewed By: yfeldblum

Differential Revision: D4035412

fbshipit-source-id: 4b7a178088d2950f9f042e0c79b54b3982eb43f5

folly/fibers/Semaphore.cpp
folly/fibers/Semaphore.h

index 40efae3fa019f3cdbdd52d9ed4dc7eff43c55465..63c71e218da847f87fec0c0cb1881018f6f16974 100644 (file)
@@ -90,5 +90,9 @@ void Semaphore::wait() {
       std::memory_order_acquire));
 }
 
+size_t Semaphore::getCapacity() const {
+  return capacity_;
+}
+
 } // namespace fibers
 } // namespace folly
index 24e5707c67ea8222fe6cd405bb5ce4996c481c99..047abee8d888c076ecb261c43d43d427b0dab6e8 100644 (file)
@@ -27,7 +27,8 @@ namespace fibers {
  */
 class Semaphore {
  public:
-  explicit Semaphore(size_t tokenCount) : tokens_(tokenCount) {}
+  explicit Semaphore(size_t tokenCount)
+      : capacity_(tokenCount), tokens_(capacity_) {}
 
   Semaphore(const Semaphore&) = delete;
   Semaphore(Semaphore&&) = delete;
@@ -44,10 +45,13 @@ class Semaphore {
    */
   void wait();
 
+  size_t getCapacity() const;
+
  private:
   bool waitSlow();
   bool signalSlow();
 
+  size_t capacity_;
   // Atomic counter
   std::atomic<int64_t> tokens_;
   folly::Synchronized<std::queue<folly::fibers::Baton*>> waitList_;