Let SaturatingSemaphore::try_wait be non-const and add ready
authorYedidya Feldblum <yfeldblum@fb.com>
Thu, 30 Nov 2017 22:56:07 +0000 (14:56 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 30 Nov 2017 23:05:43 +0000 (15:05 -0800)
Summary:
[Folly] Let `SaturatingSemaphore::try_wait` be non-`const` and add `ready`.

For internal API consistency.

Reviewed By: magedm

Differential Revision: D6450089

fbshipit-source-id: 65b9b723672521710a69719b192eb2922a27b778

folly/synchronization/SaturatingSemaphore.h
folly/synchronization/test/SaturatingSemaphoreTest.cpp

index 169bb8d82e6abe632fc0a65361bdcade45fc174a..d222dc4aa4ee10577492c4efdc094726dea92c7c 100644 (file)
@@ -68,6 +68,9 @@ namespace folly {
 ///   pre block option is applicable only if MayBlock is true.
 ///
 /// Functions:
+///   bool ready():
+///     Returns true if the flag is set by a call to post, otherwise false.
+///     Equivalent to try_wait, but available on const receivers.
 ///   void reset();
 ///     Clears the flag.
 ///   void post();
@@ -149,6 +152,11 @@ class SaturatingSemaphore {
   /** destructor */
   ~SaturatingSemaphore() {}
 
+  /** ready */
+  FOLLY_ALWAYS_INLINE bool ready() const noexcept {
+    return state_.load(std::memory_order_acquire) == READY;
+  }
+
   /** reset */
   void reset() noexcept {
     state_.store(NOTREADY, std::memory_order_relaxed);
@@ -170,8 +178,8 @@ class SaturatingSemaphore {
   }
 
   /** try_wait */
-  FOLLY_ALWAYS_INLINE bool try_wait() const noexcept {
-    return state_.load(std::memory_order_acquire) == READY;
+  FOLLY_ALWAYS_INLINE bool try_wait() noexcept {
+    return ready();
   }
 
   /** try_wait_until */
index 2be96155129163caf9b27a3e9bff772dc76951c3..2db422667576cb8d29d66aa8811ad58487148fc9 100644 (file)
@@ -26,6 +26,7 @@ using DSched = folly::test::DeterministicSchedule;
 template <bool MayBlock, template <typename> class Atom = std::atomic>
 void run_basic_test() {
   SaturatingSemaphore<MayBlock, Atom> f;
+  ASSERT_FALSE(f.ready());
   ASSERT_FALSE(f.try_wait());
   ASSERT_FALSE(f.try_wait_until(
       std::chrono::steady_clock::now() + std::chrono::microseconds(1)));
@@ -36,6 +37,7 @@ void run_basic_test() {
   f.post();
   f.wait();
   f.wait(f.wait_options().pre_block(std::chrono::nanoseconds(100)));
+  ASSERT_TRUE(f.ready());
   ASSERT_TRUE(f.try_wait());
   ASSERT_TRUE(f.try_wait_until(
       std::chrono::steady_clock::now() + std::chrono::microseconds(1)));
@@ -89,6 +91,7 @@ void run_multi_poster_multi_waiter_test(int np, int nw) {
 
   for (int i = 0; i < nw; ++i) {
     cons[i] = DSched::thread([&] {
+      ASSERT_FALSE(f.ready());
       ASSERT_FALSE(f.try_wait());
       ASSERT_FALSE(f.try_wait_for(std::chrono::microseconds(1)));
       ASSERT_FALSE(f.try_wait_until(
@@ -100,6 +103,7 @@ void run_multi_poster_multi_waiter_test(int np, int nw) {
       while (!go_wait.load()) {
         /* spin */;
       }
+      ASSERT_TRUE(f.ready());
       ASSERT_TRUE(f.try_wait());
       ASSERT_TRUE(f.try_wait_for(std::chrono::microseconds(1)));
       ASSERT_TRUE(f.try_wait_until(