From 7fc3f9174951941df7a7a32acf320185716c26a6 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Mon, 18 Dec 2017 17:13:12 -0800 Subject: [PATCH] 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 --- folly/fibers/Baton.cpp | 3 +-- folly/fibers/Baton.h | 5 +++++ folly/synchronization/Baton.h | 10 +++++++--- folly/synchronization/test/BatonTestHelpers.h | 2 ++ 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/folly/fibers/Baton.cpp b/folly/fibers/Baton.cpp index baaa15b0..5780202d 100644 --- a/folly/fibers/Baton.cpp +++ b/folly/fibers/Baton.cpp @@ -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() { diff --git a/folly/fibers/Baton.h b/folly/fibers/Baton.h index d2b5885f..286ded79 100644 --- a/folly/fibers/Baton.h +++ b/folly/fibers/Baton.h @@ -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. */ diff --git a/folly/synchronization/Baton.h b/folly/synchronization/Baton.h index ff6b2978..69135d00 100644 --- a/folly/synchronization/Baton.h +++ b/folly/synchronization/Baton.h @@ -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 diff --git a/folly/synchronization/test/BatonTestHelpers.h b/folly/synchronization/test/BatonTestHelpers.h index f1f46703..a0bba6e0 100644 --- a/folly/synchronization/test/BatonTestHelpers.h +++ b/folly/synchronization/test/BatonTestHelpers.h @@ -107,8 +107,10 @@ void run_timed_wait_regular_test() { template