Commit 61b06488 authored by Lee Howes's avatar Lee Howes Committed by Facebook Github Bot

Add capacity to semaphore so that initial size can be queried later.

Reviewed By: yfeldblum

Differential Revision: D4035412

fbshipit-source-id: 4b7a178088d2950f9f042e0c79b54b3982eb43f5
parent 3aea6e3b
...@@ -90,5 +90,9 @@ void Semaphore::wait() { ...@@ -90,5 +90,9 @@ void Semaphore::wait() {
std::memory_order_acquire)); std::memory_order_acquire));
} }
size_t Semaphore::getCapacity() const {
return capacity_;
}
} // namespace fibers } // namespace fibers
} // namespace folly } // namespace folly
...@@ -27,7 +27,8 @@ namespace fibers { ...@@ -27,7 +27,8 @@ namespace fibers {
*/ */
class Semaphore { class Semaphore {
public: public:
explicit Semaphore(size_t tokenCount) : tokens_(tokenCount) {} explicit Semaphore(size_t tokenCount)
: capacity_(tokenCount), tokens_(capacity_) {}
Semaphore(const Semaphore&) = delete; Semaphore(const Semaphore&) = delete;
Semaphore(Semaphore&&) = delete; Semaphore(Semaphore&&) = delete;
...@@ -44,10 +45,13 @@ class Semaphore { ...@@ -44,10 +45,13 @@ class Semaphore {
*/ */
void wait(); void wait();
size_t getCapacity() const;
private: private:
bool waitSlow(); bool waitSlow();
bool signalSlow(); bool signalSlow();
size_t capacity_;
// Atomic counter // Atomic counter
std::atomic<int64_t> tokens_; std::atomic<int64_t> tokens_;
folly::Synchronized<std::queue<folly::fibers::Baton*>> waitList_; folly::Synchronized<std::queue<folly::fibers::Baton*>> waitList_;
......
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