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

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
parent b3dc093e
...@@ -250,19 +250,59 @@ struct Baton { ...@@ -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 /// Similar to wait, but with a timeout. The thread is unblocked if the
/// timeout expires. /// timeout expires.
/// Note: Only a single call to timed_wait/wait is allowed during a baton's /// Note: Only a single call to wait/try_wait_for/try_wait_until is allowed
/// life-cycle (from construction/reset to destruction/reset). In other /// during a baton's life-cycle (from ctor/reset to dtor/reset). In other
/// words, after timed_wait the caller can't invoke wait/timed_wait/try_wait /// 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. /// 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, /// @return true if the baton was posted to before timeout,
/// false otherwise /// false otherwise
template <typename Clock, typename Duration = typename Clock::duration> template <typename Rep, typename Period>
bool timed_wait(const std::chrono::time_point<Clock,Duration>& deadline) { bool try_wait_for(const std::chrono::duration<Rep, Period>& timeout) {
static_assert(Blocking, "Non-blocking Baton does not support timed wait."); 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 <typename Clock, typename Duration>
bool try_wait_until(
const std::chrono::time_point<Clock, Duration>& deadline) {
static_assert(
Blocking, "Non-blocking Baton does not support try_wait_until.");
if (spinWaitForEarlyDelivery()) { if (spinWaitForEarlyDelivery()) {
assert(state_.load(std::memory_order_acquire) == EARLY_DELIVERY); assert(state_.load(std::memory_order_acquire) == EARLY_DELIVERY);
...@@ -292,28 +332,16 @@ struct Baton { ...@@ -292,28 +332,16 @@ struct Baton {
} }
} }
/// Similar to timed_wait, but with a duration. /// Alias to try_wait_for. Deprecated.
template <typename Clock = std::chrono::steady_clock, typename Duration> template <typename Rep, typename Period>
bool timed_wait(const Duration& duration) { bool timed_wait(const std::chrono::duration<Rep, Period>& timeout) {
auto deadline = Clock::now() + duration; return try_wait_for(timeout);
return timed_wait(deadline);
} }
/// Similar to wait, but doesn't block the thread if it hasn't been posted. /// Alias to try_wait_until. Deprecated.
/// template <typename Clock, typename Duration>
/// try_wait has the following semantics: bool timed_wait(const std::chrono::time_point<Clock, Duration>& deadline) {
/// - It is ok to call try_wait any number times on the same baton until return try_wait_until(deadline);
/// 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;
} }
private: private:
......
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