From: Yedidya Feldblum Date: Thu, 30 Nov 2017 22:56:07 +0000 (-0800) Subject: Let SaturatingSemaphore::try_wait be non-const and add ready X-Git-Tag: v2017.12.04.00~9 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=48d3f5e1691033d00ecc2ef0c3ced83ce148705a Let SaturatingSemaphore::try_wait be non-const and add ready 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 --- diff --git a/folly/synchronization/SaturatingSemaphore.h b/folly/synchronization/SaturatingSemaphore.h index 169bb8d8..d222dc4a 100644 --- a/folly/synchronization/SaturatingSemaphore.h +++ b/folly/synchronization/SaturatingSemaphore.h @@ -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 */ diff --git a/folly/synchronization/test/SaturatingSemaphoreTest.cpp b/folly/synchronization/test/SaturatingSemaphoreTest.cpp index 2be96155..2db42266 100644 --- a/folly/synchronization/test/SaturatingSemaphoreTest.cpp +++ b/folly/synchronization/test/SaturatingSemaphoreTest.cpp @@ -26,6 +26,7 @@ using DSched = folly::test::DeterministicSchedule; template class Atom = std::atomic> void run_basic_test() { SaturatingSemaphore 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(