proxygen
|
#include <Baton.h>
Classes | |
class | FiberWaiter |
class | TimeoutHandler |
class | Waiter |
Public Member Functions | |
Baton () noexcept | |
~Baton () noexcept=default | |
bool | ready () const |
void | setWaiter (Waiter &waiter) |
void | wait () |
void | wait (TimeoutHandler &timeoutHandler) |
template<typename F > | |
void | wait (F &&mainContextFunc) |
bool | try_wait () |
template<typename Rep , typename Period > | |
bool | try_wait_for (const std::chrono::duration< Rep, Period > &timeout) |
template<typename Rep , typename Period , typename F > | |
bool | try_wait_for (const std::chrono::duration< Rep, Period > &timeout, F &&mainContextFunc) |
template<typename Clock , typename Duration > | |
bool | try_wait_until (const std::chrono::time_point< Clock, Duration > &deadline) |
template<typename Clock , typename Duration , typename F > | |
bool | try_wait_until (const std::chrono::time_point< Clock, Duration > &deadline, F &&mainContextFunc) |
template<typename Clock , typename Duration , typename F > | |
bool | try_wait_for (const std::chrono::time_point< Clock, Duration > &deadline, F &&mainContextFunc) |
template<typename Rep , typename Period > | |
bool | timed_wait (const std::chrono::duration< Rep, Period > &timeout) |
Alias to try_wait_for. Deprecated. More... | |
template<typename Rep , typename Period , typename F > | |
bool | timed_wait (const std::chrono::duration< Rep, Period > &timeout, F &&mainContextFunc) |
Alias to try_wait_for. Deprecated. More... | |
template<typename Clock , typename Duration > | |
bool | timed_wait (const std::chrono::time_point< Clock, Duration > &deadline) |
Alias to try_wait_until. Deprecated. More... | |
template<typename Clock , typename Duration , typename F > | |
bool | timed_wait (const std::chrono::time_point< Clock, Duration > &deadline, F &&mainContextFunc) |
Alias to try_wait_until. Deprecated. More... | |
void | post () |
void | reset () |
Private Types | |
enum | { PreBlockAttempts = 300 } |
Private Member Functions | |
Baton (intptr_t state) | |
void | postHelper (intptr_t new_value) |
void | postThread () |
void | waitThread () |
template<typename F > | |
void | waitFiber (FiberManager &fm, F &&mainContextFunc) |
bool | spinWaitForEarlyPost () |
bool | timedWaitThread (TimeoutController::Duration timeout) |
Private Attributes | |
union { | |
std::atomic< intptr_t > waiter_ | |
struct { | |
folly::detail::Futex futex {} | |
int32_t _unused_packing | |
} futex_ | |
}; | |
Static Private Attributes | |
static constexpr intptr_t | NO_WAITER = 0 |
static constexpr intptr_t | POSTED = -1 |
static constexpr intptr_t | TIMEOUT = -2 |
static constexpr intptr_t | THREAD_WAITING = -3 |
Primitive which allows one to put current Fiber to sleep and wake it from another Fiber/thread.
|
private |
Enumerator | |
---|---|
PreBlockAttempts |
Must be positive. If multiple threads are actively using a higher-level data structure that uses batons internally, it is likely that the post() and wait() calls happen almost at the same time. In this state, we lose big 50% of the time if the wait goes to sleep immediately. On circa-2013 devbox hardware it costs about 7 usec to FUTEX_WAIT and then be awoken (half the t/iter as the posix_sem_pingpong test in BatonTests). We can improve our chances of early post by spinning for a bit, although we have to balance this against the loss if we end up sleeping any way. Spins on this hw take about 7 nanos (all but 0.5 nanos is the pause instruction). We give ourself 300 spins, which is about 2 usec of waiting. As a partial consolation, since we are using the pause instruction we are giving a speed boost to the colocated hyperthread. |
Definition at line 236 of file Baton.h.
|
inlinenoexcept |
Definition at line 37 of file Baton-inl.h.
References futex_, POSTED, THREAD_WAITING, TIMEOUT, and waiter_.
Referenced by folly::fibers::Baton::Waiter::~Waiter().
|
defaultnoexcept |
Referenced by folly::fibers::Baton::Waiter::~Waiter().
|
inlineexplicitprivate |
Definition at line 255 of file Baton.h.
References postHelper(), postThread(), spinWaitForEarlyPost(), timedWaitThread(), waitFiber(), and waitThread().
void folly::fibers::Baton::post | ( | ) |
Wakes up Fiber which was waiting on this Baton (or if no Fiber is waiting, next wait() call will return immediately).
Definition at line 150 of file Baton.cpp.
References POSTED, and postHelper().
Referenced by folly::coro::detail::BlockingWaitPromiseBase::FinalAwaiter::await_suspend(), postHelper(), TEST(), and folly::fibers::TimedMutex::unlock().
|
private |
|
private |
Definition at line 178 of file Baton.cpp.
References futex, futex_, folly::detail::futexWake(), POSTED, THREAD_WAITING, and waiter_.
Referenced by Baton(), and postHelper().
|
inline |
Definition at line 55 of file Baton.h.
References POSTED, setWaiter(), try_wait(), wait(), and waiter_.
Referenced by try_wait().
void folly::fibers::Baton::reset | ( | ) |
Reset's the baton (equivalent to destroying the object and constructing another one in place). Caller is responsible for making sure no one is waiting on/posting the baton when reset() is called.
Definition at line 188 of file Baton.cpp.
References NO_WAITER, and waiter_.
Referenced by timed_wait().
void folly::fibers::Baton::setWaiter | ( | Waiter & | waiter | ) |
Registers a waiter for the baton. The waiter will be notified when the baton is posted.
Definition at line 30 of file Baton.cpp.
References LIKELY, NO_WAITER, folly::fibers::Baton::Waiter::post(), POSTED, TIMEOUT, and waiter_.
Referenced by ready(), and waitFiber().
|
private |
Spin for "some time" (see discussion on PreBlockAttempts) waiting for a post.
Definition at line 94 of file Baton.cpp.
References folly::asm_volatile_pause(), i, PreBlockAttempts, and try_wait().
Referenced by Baton(), timedWaitThread(), and waitThread().
|
inline |
Alias to try_wait_for. Deprecated.
Definition at line 170 of file Baton.h.
References try_wait_for().
|
inline |
Alias to try_wait_for. Deprecated.
Definition at line 176 of file Baton.h.
References try_wait_for().
|
inline |
Alias to try_wait_until. Deprecated.
Definition at line 184 of file Baton.h.
References try_wait_until().
|
inline |
Alias to try_wait_until. Deprecated.
Definition at line 190 of file Baton.h.
References folly::fibers::Baton::Waiter::post(), reset(), and try_wait_until().
|
private |
Definition at line 113 of file Baton.cpp.
References futex, futex_, futexWaitUntil(), LIKELY, NO_WAITER, now(), POSTED, spinWaitForEarlyPost(), THREAD_WAITING, folly::detail::TIMEDOUT, TIMEOUT, uint32_t, and waiter_.
Referenced by Baton(), and try_wait_for().
bool folly::fibers::Baton::try_wait | ( | ) |
|
inline |
Puts active fiber to sleep. Returns when post is called or the timeout expires.
timeout | Baton will be automatically awaken if timeout expires |
Definition at line 105 of file Baton.h.
References folly::detail::timeout.
Referenced by TEST(), folly::fibers::TimedMutex::timed_lock(), timed_wait(), and try_wait_until().
bool folly::fibers::Baton::try_wait_for | ( | const std::chrono::duration< Rep, Period > & | timeout, |
F && | mainContextFunc | ||
) |
Puts active fiber to sleep. Returns when post is called or the timeout expires.
timeout | Baton will be automatically awaken if timeout expires |
mainContextFunc | this function is immediately executed on the main context. |
Definition at line 75 of file Baton-inl.h.
References folly::fibers::FiberManager::getFiberManagerUnsafe(), POSTED, timedWaitThread(), TIMEOUT, waiter_, and waitFiber().
bool folly::fibers::Baton::try_wait_for | ( | const std::chrono::time_point< Clock, Duration > & | deadline, |
F && | mainContextFunc | ||
) |
Puts active fiber to sleep. Returns when post is called or the deadline expires.
timeout | Baton will be automatically awaken if deadline expires |
mainContextFunc | this function is immediately executed on the main context. |
|
inline |
Puts active fiber to sleep. Returns when post is called or the deadline expires.
timeout | Baton will be automatically awaken if deadline expires |
Definition at line 133 of file Baton.h.
References try_wait_for().
Referenced by timed_wait().
bool folly::fibers::Baton::try_wait_until | ( | const std::chrono::time_point< Clock, Duration > & | deadline, |
F && | mainContextFunc | ||
) |
Puts active fiber to sleep. Returns when post is called or the deadline expires.
timeout | Baton will be automatically awaken if deadline expires |
mainContextFunc | this function is immediately executed on the main context. |
Definition at line 107 of file Baton-inl.h.
References LIKELY, now(), and try_wait_for().
void folly::fibers::Baton::wait | ( | ) |
Puts active fiber to sleep. Returns when post is called.
Definition at line 45 of file Baton.cpp.
Referenced by folly::fibers::forEach(), folly::fibers::TimedMutex::lock(), ready(), TEST(), folly::coro::detail::BlockingWaitPromiseBase::wait(), wait(), and folly::fibers::Semaphore::waitSlow().
void folly::fibers::Baton::wait | ( | TimeoutHandler & | timeoutHandler | ) |
Put active fiber to sleep indefinitely. However, timeoutHandler may be used elsewhere on the same thread in order to schedule a wakeup for the active fiber. Users of timeoutHandler must be on the same thread as the active fiber and may only schedule one timeout, which must occur after the active fiber calls wait.
Definition at line 49 of file Baton.cpp.
References folly::fibers::Baton::TimeoutHandler::cancelTimeout(), folly::fibers::Baton::TimeoutHandler::fiberManager_, folly::fibers::FiberManager::getFiberManagerUnsafe(), postHelper(), TIMEOUT, folly::fibers::Baton::TimeoutHandler::timeoutFunc_, folly::fibers::Baton::TimeoutHandler::timeoutPtr_, try_wait(), and wait().
void folly::fibers::Baton::wait | ( | F && | mainContextFunc | ) |
Puts active fiber to sleep. Returns when post is called.
mainContextFunc | this function is immediately executed on the main context. |
Definition at line 50 of file Baton-inl.h.
References folly::fibers::FiberManager::getFiberManagerUnsafe(), waitFiber(), and waitThread().
|
inlineprivate |
Definition at line 61 of file Baton-inl.h.
References folly::fibers::FiberManager::activeFiber_, folly::fibers::FiberManager::awaitFunc_, folly::fibers::Fiber::AWAITING, f, folly::fibers::Fiber::preempt(), folly::fibers::Baton::FiberWaiter::setFiber(), and setWaiter().
Referenced by Baton(), try_wait_for(), and wait().
|
private |
Definition at line 62 of file Baton.cpp.
References futex_, folly::detail::MemoryIdler::futexWait(), LIKELY, NO_WAITER, POSTED, spinWaitForEarlyPost(), THREAD_WAITING, TIMEOUT, uint32_t, and waiter_.
Referenced by Baton(), and wait().
union { ... } |
folly::detail::Futex folly::fibers::Baton::futex {} |
Definition at line 281 of file Baton.h.
Referenced by postThread(), and timedWaitThread().
struct { ... } folly::fibers::Baton::futex_ |
Referenced by Baton(), postThread(), timedWaitThread(), and waitThread().
|
staticprivate |
Definition at line 273 of file Baton.h.
Referenced by postHelper(), reset(), setWaiter(), timedWaitThread(), and waitThread().
|
staticprivate |
Definition at line 274 of file Baton.h.
Referenced by Baton(), post(), postHelper(), postThread(), ready(), setWaiter(), timedWaitThread(), try_wait_for(), and waitThread().
|
staticprivate |
Definition at line 276 of file Baton.h.
Referenced by Baton(), postHelper(), postThread(), timedWaitThread(), and waitThread().
|
staticprivate |
Definition at line 275 of file Baton.h.
Referenced by Baton(), postHelper(), setWaiter(), timedWaitThread(), try_wait_for(), wait(), and waitThread().
std::atomic<intptr_t> folly::fibers::Baton::waiter_ |
Definition at line 279 of file Baton.h.
Referenced by Baton(), postHelper(), postThread(), ready(), reset(), setWaiter(), timedWaitThread(), try_wait_for(), and waitThread().