Skip to content

Commit

Permalink
Let SaturatingSemaphore::try_wait be non-const and add ready
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Nov 30, 2017
1 parent b71a1b7 commit 48d3f5e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 10 additions & 2 deletions folly/synchronization/SaturatingSemaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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 */
Expand Down
4 changes: 4 additions & 0 deletions folly/synchronization/test/SaturatingSemaphoreTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -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)));
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 48d3f5e

Please sign in to comment.