/// 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();
/** 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);
}
/** 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 */
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)));
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)));
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(
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(