Commit 7fc3f917 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Baton::ready, a const variant of try_wait

Summary: [Folly] `Baton::ready`, a `const` variant of `try_wait`.

Reviewed By: djwatson

Differential Revision: D6508064

fbshipit-source-id: ba458577574ba58165408a93238da7eb09adf1e6
parent cfc602df
......@@ -153,8 +153,7 @@ void Baton::postHelper(intptr_t new_value) {
}
bool Baton::try_wait() {
auto state = waitingFiber_.load();
return state == POSTED;
return ready();
}
void Baton::postThread() {
......
......@@ -40,6 +40,11 @@ class Baton {
~Baton() {}
bool ready() const {
auto state = waitingFiber_.load();
return state == POSTED;
}
/**
* Puts active fiber to sleep. Returns when post is called.
*/
......
......@@ -79,6 +79,12 @@ struct Baton {
assert(state_.load(std::memory_order_relaxed) != WAITING);
}
FOLLY_ALWAYS_INLINE bool ready() const noexcept {
auto s = state_.load(std::memory_order_acquire);
assert(s == INIT || s == EARLY_DELIVERY);
return LIKELY(s == EARLY_DELIVERY);
}
/// Equivalent to destroying the Baton and creating a new one. It is
/// a bug to call this while there is a waiting thread, so in practice
/// the waiter will be the one that resets the baton.
......@@ -176,9 +182,7 @@ struct Baton {
///
/// @return true if baton has been posted, false othewise
FOLLY_ALWAYS_INLINE bool try_wait() const noexcept {
auto s = state_.load(std::memory_order_acquire);
assert(s == INIT || s == EARLY_DELIVERY);
return LIKELY(s == EARLY_DELIVERY);
return ready();
}
/// Similar to wait, but with a timeout. The thread is unblocked if the
......
......@@ -107,8 +107,10 @@ void run_timed_wait_regular_test() {
template <template <typename> class Atom, bool Blocking>
void run_try_wait_tests() {
Baton<Atom, Blocking> b;
EXPECT_FALSE(b.ready());
EXPECT_FALSE(b.try_wait());
b.post();
EXPECT_TRUE(b.ready());
EXPECT_TRUE(b.try_wait());
}
......
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