~Baton() {}
+ bool ready() const {
+ auto state = waitingFiber_.load();
+ return state == POSTED;
+ }
+
/**
* Puts active fiber to sleep. Returns when post is called.
*/
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.
///
/// @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
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());
}