Commit 48d3f5e1 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

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
parent b71a1b76
...@@ -68,6 +68,9 @@ namespace folly { ...@@ -68,6 +68,9 @@ namespace folly {
/// pre block option is applicable only if MayBlock is true. /// pre block option is applicable only if MayBlock is true.
/// ///
/// Functions: /// 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(); /// void reset();
/// Clears the flag. /// Clears the flag.
/// void post(); /// void post();
...@@ -149,6 +152,11 @@ class SaturatingSemaphore { ...@@ -149,6 +152,11 @@ class SaturatingSemaphore {
/** destructor */ /** destructor */
~SaturatingSemaphore() {} ~SaturatingSemaphore() {}
/** ready */
FOLLY_ALWAYS_INLINE bool ready() const noexcept {
return state_.load(std::memory_order_acquire) == READY;
}
/** reset */ /** reset */
void reset() noexcept { void reset() noexcept {
state_.store(NOTREADY, std::memory_order_relaxed); state_.store(NOTREADY, std::memory_order_relaxed);
...@@ -170,8 +178,8 @@ class SaturatingSemaphore { ...@@ -170,8 +178,8 @@ class SaturatingSemaphore {
} }
/** try_wait */ /** try_wait */
FOLLY_ALWAYS_INLINE bool try_wait() const noexcept { FOLLY_ALWAYS_INLINE bool try_wait() noexcept {
return state_.load(std::memory_order_acquire) == READY; return ready();
} }
/** try_wait_until */ /** try_wait_until */
......
...@@ -26,6 +26,7 @@ using DSched = folly::test::DeterministicSchedule; ...@@ -26,6 +26,7 @@ using DSched = folly::test::DeterministicSchedule;
template <bool MayBlock, template <typename> class Atom = std::atomic> template <bool MayBlock, template <typename> class Atom = std::atomic>
void run_basic_test() { void run_basic_test() {
SaturatingSemaphore<MayBlock, Atom> f; SaturatingSemaphore<MayBlock, Atom> f;
ASSERT_FALSE(f.ready());
ASSERT_FALSE(f.try_wait()); ASSERT_FALSE(f.try_wait());
ASSERT_FALSE(f.try_wait_until( ASSERT_FALSE(f.try_wait_until(
std::chrono::steady_clock::now() + std::chrono::microseconds(1))); std::chrono::steady_clock::now() + std::chrono::microseconds(1)));
...@@ -36,6 +37,7 @@ void run_basic_test() { ...@@ -36,6 +37,7 @@ void run_basic_test() {
f.post(); f.post();
f.wait(); f.wait();
f.wait(f.wait_options().pre_block(std::chrono::nanoseconds(100))); 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());
ASSERT_TRUE(f.try_wait_until( ASSERT_TRUE(f.try_wait_until(
std::chrono::steady_clock::now() + std::chrono::microseconds(1))); std::chrono::steady_clock::now() + std::chrono::microseconds(1)));
...@@ -89,6 +91,7 @@ void run_multi_poster_multi_waiter_test(int np, int nw) { ...@@ -89,6 +91,7 @@ void run_multi_poster_multi_waiter_test(int np, int nw) {
for (int i = 0; i < nw; ++i) { for (int i = 0; i < nw; ++i) {
cons[i] = DSched::thread([&] { cons[i] = DSched::thread([&] {
ASSERT_FALSE(f.ready());
ASSERT_FALSE(f.try_wait()); ASSERT_FALSE(f.try_wait());
ASSERT_FALSE(f.try_wait_for(std::chrono::microseconds(1))); ASSERT_FALSE(f.try_wait_for(std::chrono::microseconds(1)));
ASSERT_FALSE(f.try_wait_until( ASSERT_FALSE(f.try_wait_until(
...@@ -100,6 +103,7 @@ void run_multi_poster_multi_waiter_test(int np, int nw) { ...@@ -100,6 +103,7 @@ void run_multi_poster_multi_waiter_test(int np, int nw) {
while (!go_wait.load()) { while (!go_wait.load()) {
/* spin */; /* spin */;
} }
ASSERT_TRUE(f.ready());
ASSERT_TRUE(f.try_wait()); ASSERT_TRUE(f.try_wait());
ASSERT_TRUE(f.try_wait_for(std::chrono::microseconds(1))); ASSERT_TRUE(f.try_wait_for(std::chrono::microseconds(1)));
ASSERT_TRUE(f.try_wait_until( ASSERT_TRUE(f.try_wait_until(
......
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