proxygen
|
#include <SaturatingSemaphore.h>
Public Member Functions | |
constexpr | SaturatingSemaphore () noexcept |
~SaturatingSemaphore () | |
FOLLY_ALWAYS_INLINE bool | ready () const noexcept |
void | reset () noexcept |
FOLLY_ALWAYS_INLINE void | post () noexcept |
FOLLY_ALWAYS_INLINE void | wait (const WaitOptions &opt=wait_options()) noexcept |
FOLLY_ALWAYS_INLINE bool | try_wait () 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<class Rep , class Period > | |
FOLLY_ALWAYS_INLINE bool | try_wait_for (const std::chrono::duration< Rep, Period > &duration, const WaitOptions &opt=wait_options()) noexcept |
template<typename Clock , typename Duration > | |
FOLLY_NOINLINE bool | tryWaitSlow (const std::chrono::time_point< Clock, Duration > &deadline, const WaitOptions &opt) noexcept |
Static Public Member Functions | |
static FOLLY_ALWAYS_INLINE WaitOptions | wait_options () |
Private Types | |
enum | State : uint32_t { NOTREADY = 0, READY = 1, BLOCKED = 2 } |
Private Member Functions | |
FOLLY_ALWAYS_INLINE void | postFastWaiterMayBlock () noexcept |
void | postSlowWaiterMayBlock (uint32_t before) noexcept |
template<typename Clock , typename Duration > | |
bool | tryWaitSlow (const std::chrono::time_point< Clock, Duration > &deadline, const WaitOptions &opt) noexcept |
Private Attributes | |
detail::Futex< Atom > | state_ |
SaturatingSemaphore is a flag that allows concurrent posting by multiple posters and concurrent non-destructive waiting by multiple waiters.
A SaturatingSemaphore allows one or more waiter threads to check, spin, or block, indefinitely or with timeout, for a flag to be set by one or more poster threads. By setting the flag, posters announce to waiters (that may be already waiting or will check the flag in the future) that some condition is true. Posts to an already set flag are idempotent.
SaturatingSemaphore is called so because it behaves like a hybrid binary/counted semaphore with values zero and infinity, and post() and wait() functions. It is called saturating because one post() is enough to set it to infinity and to satisfy any number of wait()-s. Once set (to infinity) it remains unchanged by subsequent post()-s and wait()-s, until it is reset() back to zero.
The implementation of SaturatingSemaphore is based on that of Baton. It includes no internal padding, and is only 4 bytes in size. Any alignment or padding to avoid false sharing is up to the user. SaturatingSemaphore differs from Baton as follows:
Template parameter:
Wait options: WaitOptions contains optional per call setting for spin-max duration: Calls to wait(), try_wait_until(), and try_wait_for() block only after the passage of the spin-max period. The default spin-max duration is 10 usec. The spin-max option is applicable only if MayBlock is true.
Functions: bool ready(): Returns true if the flag is set by a call to post, otherwise false. Equivalent to try_wait, but available on const receivers. void reset(); Clears the flag. void post(); Sets the flag and wakes all current waiters, i.e., causes all concurrent calls to wait, try_wait_for, and try_wait_until to return. void wait( WaitOptions opt = wait_options()); Waits for the flag to be set by a call to post. bool try_wait(); Returns true if the flag is set by a call to post, otherwise false. bool try_wait_until( time_point& deadline, WaitOptions& = wait_options()); Returns true if the flag is set by a call to post before the deadline, otherwise false. bool try_wait_for( duration&, WaitOptions& = wait_options()); Returns true if the flag is set by a call to post before the expiration of the specified duration, otherwise false.
Usage:
Definition at line 120 of file SaturatingSemaphore.h.
|
private |
|
inlinenoexcept |
|
inline |
|
inlinenoexcept |
post
Definition at line 151 of file SaturatingSemaphore.h.
Referenced by folly::UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atom >::Entry::putItem(), run_basic_test(), run_multi_poster_multi_waiter_test(), folly::FlatCombining< T, Mutex, Atom, Req >::Rec::setDisconnected(), folly::FlatCombining< T, Mutex, Atom, Req >::Rec::setDone(), folly::FlatCombining< FcSimpleExample< Mutex, Atom >, Mutex, Atom >::setPending(), folly::FlatCombining< T, Mutex, Atom, Req >::Rec::setValid(), and TEST_F().
|
inlineprivatenoexcept |
Definition at line 194 of file SaturatingSemaphore.h.
Referenced by folly::SaturatingSemaphore< true, Atom >::post().
|
privatenoexcept |
Member function definitioonspostSlowWaiterMayBlock
Definition at line 220 of file SaturatingSemaphore.h.
References folly::SaturatingSemaphore< MayBlock, Atom >::BLOCKED, folly::detail::futexWake(), folly::SaturatingSemaphore< MayBlock, Atom >::NOTREADY, and folly::SaturatingSemaphore< MayBlock, Atom >::READY.
Referenced by folly::SaturatingSemaphore< true, Atom >::postFastWaiterMayBlock().
|
inlinenoexcept |
ready
Definition at line 141 of file SaturatingSemaphore.h.
Referenced by folly::FlatCombining< T, Mutex, Atom, Req >::Rec::isDisconnected(), folly::FlatCombining< T, Mutex, Atom, Req >::Rec::isDone(), folly::FlatCombining< FcSimpleExample< Mutex, Atom >, Mutex, Atom >::isPending(), folly::FlatCombining< T, Mutex, Atom, Req >::Rec::isValid(), run_basic_test(), run_multi_poster_multi_waiter_test(), folly::SaturatingSemaphore< true, Atom >::try_wait(), and folly::SaturatingSemaphore< MayBlock, Atom >::tryWaitSlow().
|
inlinenoexcept |
reset
Definition at line 146 of file SaturatingSemaphore.h.
Referenced by folly::FlatCombining< T, Mutex, Atom, Req >::Rec::clearDisconnected(), folly::FlatCombining< T, Mutex, Atom, Req >::Rec::clearDone(), folly::FlatCombining< FcSimpleExample< Mutex, Atom >, Mutex, Atom >::clearPending(), folly::FlatCombining< T, Mutex, Atom, Req >::Rec::clearValid(), run_basic_test(), and TEST_F().
|
inlinenoexcept |
try_wait
Definition at line 166 of file SaturatingSemaphore.h.
Referenced by run_basic_test(), run_multi_poster_multi_waiter_test(), folly::SaturatingSemaphore< true, Atom >::try_wait_for(), and folly::SaturatingSemaphore< true, Atom >::try_wait_until().
|
inlinenoexcept |
try_wait_for
Definition at line 183 of file SaturatingSemaphore.h.
Referenced by run_multi_poster_multi_waiter_test().
|
inlinenoexcept |
try_wait_until
Definition at line 172 of file SaturatingSemaphore.h.
Referenced by run_basic_test(), run_multi_poster_multi_waiter_test(), TEST_F(), folly::UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atom >::Entry::tryWaitUntil(), and folly::SaturatingSemaphore< true, Atom >::wait().
|
privatenoexcept |
|
noexcept |
tryWaitSlow
Definition at line 280 of file SaturatingSemaphore.h.
References folly::detail::advance, folly::SaturatingSemaphore< MayBlock, Atom >::BLOCKED, folly::detail::MemoryIdler::futexWaitUntil(), max, folly::SaturatingSemaphore< MayBlock, Atom >::NOTREADY, folly::SaturatingSemaphore< MayBlock, Atom >::READY, folly::SaturatingSemaphore< MayBlock, Atom >::ready(), folly::detail::spin_pause_until(), folly::detail::spin_yield_until(), folly::detail::success, folly::detail::TIMEDOUT, and folly::detail::timeout.
|
inlinenoexcept |
wait
Definition at line 161 of file SaturatingSemaphore.h.
Referenced by folly::FlatCombining< T, Mutex, Atom, Req >::Rec::awaitDone(), folly::FlatCombining< FcSimpleExample< Mutex, Atom >, Mutex, Atom >::awaitPending(), folly::UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atom >::Entry::peekItem(), run_basic_test(), run_multi_poster_multi_waiter_test(), and folly::UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atom >::Entry::takeItem().
|
inlinestatic |
Definition at line 130 of file SaturatingSemaphore.h.
Referenced by run_basic_test(), run_multi_poster_multi_waiter_test(), and folly::UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atom >::Entry::tryWaitUntil().
|
private |
Definition at line 121 of file SaturatingSemaphore.h.