From cc9ce0e1e7d84db3ccb553cc8c75e179459ef42f Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Wed, 6 Dec 2017 11:36:23 -0800 Subject: [PATCH] Rename Baton::timed_wait to try_wait_for and try_wait_until Summary: [Folly] Rename `Baton::timed_wait` to `try_wait_for` and `try_wait_until`. For consistency with `std::timed_mutex`'s suite of `lock` member functions, as well as `SaturatingSemaphore` and others. Reviewed By: davidtgoldblatt Differential Revision: D6493714 fbshipit-source-id: 566c6d5dc0f6db4a0308cbd1972d53794f898383 --- folly/synchronization/Baton.h | 82 +++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 27 deletions(-) diff --git a/folly/synchronization/Baton.h b/folly/synchronization/Baton.h index 83323915..8a2c0c2c 100644 --- a/folly/synchronization/Baton.h +++ b/folly/synchronization/Baton.h @@ -250,19 +250,59 @@ struct Baton { } } + /// Similar to wait, but doesn't block the thread if it hasn't been posted. + /// + /// try_wait has the following semantics: + /// - It is ok to call try_wait any number times on the same baton until + /// try_wait reports that the baton has been posted. + /// - It is ok to call timed_wait or wait on the same baton if try_wait + /// reports that baton hasn't been posted. + /// - If try_wait indicates that the baton has been posted, it is invalid to + /// call wait, try_wait or timed_wait on the same baton without resetting + /// + /// @return true if baton has been posted, false othewise + bool try_wait() const { + auto s = state_.load(std::memory_order_acquire); + assert(s == INIT || s == EARLY_DELIVERY); + return s == EARLY_DELIVERY; + } + /// Similar to wait, but with a timeout. The thread is unblocked if the /// timeout expires. - /// Note: Only a single call to timed_wait/wait is allowed during a baton's - /// life-cycle (from construction/reset to destruction/reset). In other - /// words, after timed_wait the caller can't invoke wait/timed_wait/try_wait + /// Note: Only a single call to wait/try_wait_for/try_wait_until is allowed + /// during a baton's life-cycle (from ctor/reset to dtor/reset). In other + /// words, after try_wait_for the caller can't invoke + /// wait/try_wait/try_wait_for/try_wait_until /// again on the same baton without resetting it. /// - /// @param deadline Time until which the thread can block + /// @param timeout Time until which the thread can block /// @return true if the baton was posted to before timeout, /// false otherwise - template - bool timed_wait(const std::chrono::time_point& deadline) { - static_assert(Blocking, "Non-blocking Baton does not support timed wait."); + template + bool try_wait_for(const std::chrono::duration& timeout) { + static_assert( + Blocking, "Non-blocking Baton does not support try_wait_for."); + + auto deadline = std::chrono::steady_clock::now() + timeout; + return try_wait_until(deadline); + } + + /// Similar to wait, but with a deadline. The thread is unblocked if the + /// deadline expires. + /// Note: Only a single call to wait/try_wait_for/try_wait_until is allowed + /// during a baton's life-cycle (from ctor/reset to dtor/reset). In other + /// words, after try_wait_until the caller can't invoke + /// wait/try_wait/try_wait_for/try_wait_until + /// again on the same baton without resetting it. + /// + /// @param deadline Time until which the thread can block + /// @return true if the baton was posted to before deadline, + /// false otherwise + template + bool try_wait_until( + const std::chrono::time_point& deadline) { + static_assert( + Blocking, "Non-blocking Baton does not support try_wait_until."); if (spinWaitForEarlyDelivery()) { assert(state_.load(std::memory_order_acquire) == EARLY_DELIVERY); @@ -292,28 +332,16 @@ struct Baton { } } - /// Similar to timed_wait, but with a duration. - template - bool timed_wait(const Duration& duration) { - auto deadline = Clock::now() + duration; - return timed_wait(deadline); + /// Alias to try_wait_for. Deprecated. + template + bool timed_wait(const std::chrono::duration& timeout) { + return try_wait_for(timeout); } - /// Similar to wait, but doesn't block the thread if it hasn't been posted. - /// - /// try_wait has the following semantics: - /// - It is ok to call try_wait any number times on the same baton until - /// try_wait reports that the baton has been posted. - /// - It is ok to call timed_wait or wait on the same baton if try_wait - /// reports that baton hasn't been posted. - /// - If try_wait indicates that the baton has been posted, it is invalid to - /// call wait, try_wait or timed_wait on the same baton without resetting - /// - /// @return true if baton has been posted, false othewise - bool try_wait() const { - auto s = state_.load(std::memory_order_acquire); - assert(s == INIT || s == EARLY_DELIVERY); - return s == EARLY_DELIVERY; + /// Alias to try_wait_until. Deprecated. + template + bool timed_wait(const std::chrono::time_point& deadline) { + return try_wait_until(deadline); } private: -- 2.34.1