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 {
/// 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 */
......
......@@ -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(
......
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