proxygen
|
#include <Baton.h>
Public Member Functions | |
constexpr | Baton () noexcept |
Baton (Baton const &)=delete | |
Baton & | operator= (Baton const &)=delete |
~Baton () noexcept | |
FOLLY_ALWAYS_INLINE bool | ready () const noexcept |
void | reset () noexcept |
void | post () noexcept |
FOLLY_ALWAYS_INLINE void | wait (const WaitOptions &opt=wait_options()) noexcept |
FOLLY_ALWAYS_INLINE bool | try_wait () const noexcept |
template<typename Rep , typename Period > | |
FOLLY_ALWAYS_INLINE bool | try_wait_for (const std::chrono::duration< Rep, Period > &timeout, const WaitOptions &opt=wait_options()) noexcept |
template<typename Clock , typename Duration > | |
FOLLY_ALWAYS_INLINE bool | try_wait_until (const std::chrono::time_point< Clock, Duration > &deadline, const WaitOptions &opt=wait_options()) noexcept |
template<typename Rep , typename Period > | |
FOLLY_ALWAYS_INLINE bool | timed_wait (const std::chrono::duration< Rep, Period > &timeout) noexcept |
Alias to try_wait_for. Deprecated. More... | |
template<typename Clock , typename Duration > | |
FOLLY_ALWAYS_INLINE bool | timed_wait (const std::chrono::time_point< Clock, Duration > &deadline) noexcept |
Alias to try_wait_until. Deprecated. More... | |
Static Public Member Functions | |
static FOLLY_ALWAYS_INLINE WaitOptions | wait_options () |
Private Types | |
enum | State : uint32_t { INIT = 0, EARLY_DELIVERY = 1, WAITING = 2, LATE_DELIVERY = 3, TIMED_OUT = 4 } |
Private Member Functions | |
template<typename Clock , typename Duration > | |
FOLLY_NOINLINE bool | tryWaitSlow (const std::chrono::time_point< Clock, Duration > &deadline, const WaitOptions &opt) noexcept |
Private Attributes | |
detail::Futex< Atom > | state_ |
A Baton allows a thread to block once and be awoken. Captures a single handoff, and during its lifecycle (from construction/reset to destruction/reset) a baton must either be post()ed and wait()ed exactly once each, or not at all.
Baton includes no internal padding, and is only 4 bytes in size. Any alignment or padding to avoid false sharing is up to the user.
This is basically a stripped-down semaphore that supports only a single call to sem_post and a single call to sem_wait.
The non-blocking version (MayBlock == false) provides more speed by using only load acquire and store release operations in the critical path, at the cost of disallowing blocking.
The current posix semaphore sem_t isn't too bad, but this provides more a bit more speed, inlining, smaller size, a guarantee that the implementation won't change, and compatibility with DeterministicSchedule. By having a much more restrictive lifecycle we can also add a bunch of assertions that can help to catch race conditions ahead of time.
|
private |
Enumerator | |
---|---|
INIT | |
EARLY_DELIVERY | |
WAITING | |
LATE_DELIVERY | |
TIMED_OUT |
Definition at line 254 of file Baton.h.
|
inlinenoexcept |
Definition at line 62 of file Baton.h.
References folly::Baton< MayBlock, Atom >::operator=().
|
delete |
|
inlinenoexcept |
It is an error to destroy a Baton on which a thread is currently wait()ing. In practice this means that the waiter usually takes responsibility for destroying the Baton.
Definition at line 70 of file Baton.h.
References folly::Baton< MayBlock, Atom >::state_, and folly::Baton< MayBlock, Atom >::WAITING.
|
delete |
Referenced by folly::Baton< MayBlock, Atom >::Baton().
|
inlinenoexcept |
Causes wait() to wake up. For each lifetime of a Baton (where a lifetime starts at construction or reset() and ends at destruction or reset()) there can be at most one call to post(), in the single poster version. Any thread may call post().
Spin-only version
May-block versions
Definition at line 123 of file Baton.h.
References folly::Baton< MayBlock, Atom >::EARLY_DELIVERY, FOLLY_ALWAYS_INLINE, folly::detail::futexWake(), folly::Baton< MayBlock, Atom >::INIT, folly::Baton< MayBlock, Atom >::LATE_DELIVERY, folly::Baton< MayBlock, Atom >::state_, folly::Baton< MayBlock, Atom >::TIMED_OUT, uint32_t, and folly::Baton< MayBlock, Atom >::WAITING.
Referenced by folly::basicTest(), BENCHMARK(), BENCHMARK_RELATIVE(), folly::SingletonVault::doEagerInitVia(), futureExecutor(), folly::DefaultKeepAliveExecutor::keepAliveRelease(), poolStats(), RecursiveAddTest(), folly::test::run_basic_test(), folly::test::run_basic_timed_wait_tests(), folly::test::run_timed_wait_regular_test(), folly::test::run_try_wait_tests(), folly::EventBase::runInEventBaseThreadAndWait(), SimpleTest(), SlowpokeNeedySingleton::SlowpokeNeedySingleton(), TEST(), TEST_F(), ThreadExecutor::work(), and folly::ScopedEventBaseThread::~ScopedEventBaseThread().
|
inlinenoexcept |
Definition at line 87 of file Baton.h.
References folly::Baton< MayBlock, Atom >::EARLY_DELIVERY, folly::Baton< MayBlock, Atom >::INIT, LIKELY, s, and folly::Baton< MayBlock, Atom >::state_.
Referenced by folly::test::run_try_wait_tests(), folly::Baton< MayBlock, Atom >::try_wait(), and folly::Baton< MayBlock, Atom >::tryWaitSlow().
|
inlinenoexcept |
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.
Definition at line 96 of file Baton.h.
References folly::Baton< MayBlock, Atom >::INIT, folly::Baton< MayBlock, Atom >::state_, and folly::Baton< MayBlock, Atom >::WAITING.
Referenced by TEST(), and TEST_F().
|
inlinenoexcept |
Alias to try_wait_for. Deprecated.
Definition at line 241 of file Baton.h.
References folly::Baton< MayBlock, Atom >::try_wait_for().
Referenced by TEST().
|
inlinenoexcept |
Alias to try_wait_until. Deprecated.
Definition at line 248 of file Baton.h.
References folly::Baton< MayBlock, Atom >::try_wait_until().
|
inlinenoexcept |
Similar to wait, but doesn't block the thread if it hasn't been posted.
try_wait has the following semantics:
Definition at line 190 of file Baton.h.
References folly::Baton< MayBlock, Atom >::ready().
Referenced by folly::test::run_try_wait_tests(), folly::Baton< MayBlock, Atom >::try_wait_for(), folly::Baton< MayBlock, Atom >::try_wait_until(), and folly::Baton< MayBlock, Atom >::wait().
|
inlinenoexcept |
Similar to wait, but with a timeout. The thread is unblocked if the timeout 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_for the caller can't invoke wait/try_wait/try_wait_for/try_wait_until again on the same baton without resetting it.
timeout | Time until which the thread can block |
Definition at line 206 of file Baton.h.
References now(), folly::Baton< MayBlock, Atom >::try_wait(), and folly::Baton< MayBlock, Atom >::tryWaitSlow().
Referenced by TEST(), TEST_F(), and folly::Baton< MayBlock, Atom >::timed_wait().
|
inlinenoexcept |
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.
deadline | Time until which the thread can block |
Definition at line 229 of file Baton.h.
References folly::Baton< MayBlock, Atom >::try_wait(), and folly::Baton< MayBlock, Atom >::tryWaitSlow().
Referenced by folly::test::run_basic_timed_wait_tests(), folly::test::run_timed_wait_regular_test(), folly::test::run_timed_wait_tmo_tests(), and folly::Baton< MayBlock, Atom >::timed_wait().
|
inlineprivatenoexcept |
Definition at line 263 of file Baton.h.
References folly::detail::advance, folly::Baton< MayBlock, Atom >::EARLY_DELIVERY, folly::detail::MemoryIdler::futexWaitUntil(), folly::Baton< MayBlock, Atom >::INIT, folly::Baton< MayBlock, Atom >::LATE_DELIVERY, max, folly::Baton< MayBlock, Atom >::ready(), s, folly::detail::spin_pause_until(), folly::detail::spin_yield_until(), folly::Baton< MayBlock, Atom >::state_, folly::detail::success, folly::Baton< MayBlock, Atom >::TIMED_OUT, folly::detail::TIMEDOUT, folly::detail::timeout, uint32_t, and folly::Baton< MayBlock, Atom >::WAITING.
Referenced by folly::Baton< MayBlock, Atom >::try_wait_for(), folly::Baton< MayBlock, Atom >::try_wait_until(), and folly::Baton< MayBlock, Atom >::wait().
|
inlinenoexcept |
Waits until post() has been called in the current Baton lifetime. May be called at most once during a Baton lifetime (construction |reset until destruction|reset). If post is called before wait in the current lifetime then this method returns immediately.
The restriction that there can be at most one wait() per lifetime could be relaxed somewhat without any perf or size regressions, but by making this condition very restrictive we can provide better checking in debug builds.
Definition at line 170 of file Baton.h.
References max, folly::Baton< MayBlock, Atom >::try_wait(), and folly::Baton< MayBlock, Atom >::tryWaitSlow().
Referenced by folly::basicTest(), BENCHMARK(), BENCHMARK_RELATIVE(), futureExecutor(), folly::DefaultKeepAliveExecutor::joinKeepAlive(), poolStats(), RecursiveAddTest(), folly::run(), folly::test::run_basic_test(), folly::EventBase::runInEventBaseThreadAndWait(), SimpleTest(), TEST(), TEST_F(), and ThreadExecutor::waitForStartup().
|
inlinestatic |
|
private |
Definition at line 338 of file Baton.h.
Referenced by folly::Baton< MayBlock, Atom >::post(), folly::Baton< MayBlock, Atom >::ready(), folly::Baton< MayBlock, Atom >::reset(), folly::Baton< MayBlock, Atom >::tryWaitSlow(), and folly::Baton< MayBlock, Atom >::~Baton().