proxygen
|
#include <EventBase.h>
Classes | |
class | FunctionLoopCallback |
class | FunctionRunner |
class | LoopCallback |
class | SmoothLoopTime |
class | StackFunctionLoopCallback |
Public Types | |
using | Func = folly::Function< void()> |
![]() | |
enum | InternalEnum { InternalEnum::INTERNAL, InternalEnum::NORMAL } |
typedef std::chrono::milliseconds | timeout_type |
using | Func = folly::Function< void()> |
![]() | |
typedef std::chrono::microseconds | Duration |
typedef std::chrono::steady_clock::time_point | TimePoint |
Public Member Functions | |
EventBase () | |
EventBase (bool enableTimeMeasurement) | |
EventBase (event_base *evb, bool enableTimeMeasurement=true) | |
~EventBase () override | |
bool | loop () |
bool | loopIgnoreKeepAlive () |
bool | loopOnce (int flags=0) |
void | loopForever () |
void | terminateLoopSoon () |
void | runInLoop (LoopCallback *callback, bool thisIteration=false) |
void | runInLoop (Func c, bool thisIteration=false) |
void | runOnDestruction (LoopCallback *callback) |
void | runBeforeLoop (LoopCallback *callback) |
template<typename T > | |
bool | runInEventBaseThread (void(*fn)(T *), T *arg) |
bool | runInEventBaseThread (Func fn) |
template<typename T > | |
bool | runInEventBaseThreadAndWait (void(*fn)(T *), T *arg) |
bool | runInEventBaseThreadAndWait (Func fn) |
template<typename T > | |
bool | runImmediatelyOrRunInEventBaseThreadAndWait (void(*fn)(T *), T *arg) |
bool | runImmediatelyOrRunInEventBaseThreadAndWait (Func fn) |
void | setMaxLatency (std::chrono::microseconds maxLatency, Func maxLatencyCob) |
void | setLoadAvgMsec (std::chrono::milliseconds ms) |
void | resetLoadAvg (double value=0.0) |
double | getAvgLoopTime () const |
bool | isRunning () const |
void | waitUntilRunning () |
size_t | getNotificationQueueSize () const |
void | setMaxReadAtOnce (uint32_t maxAtOnce) |
bool | isInEventBaseThread () const |
bool | inRunningEventBaseThread () const |
void | checkIsInEventBaseThread () const |
void | dcheckIsInEventBaseThread () const |
HHWheelTimer & | timer () |
event_base * | getLibeventBase () const |
void | bumpHandlingTime () final |
void | setObserver (const std::shared_ptr< EventBaseObserver > &observer) |
const std::shared_ptr< EventBaseObserver > & | getObserver () |
void | setExecutionObserver (ExecutionObserver *observer) |
ExecutionObserver * | getExecutionObserver () |
void | setName (const std::string &name) |
const std::string & | getName () |
void | add (Cob fn) override |
Implements the Executor interface. More... | |
void | drive () override |
Implements the DrivableExecutor interface. More... | |
void | scheduleAt (Func &&fn, TimePoint const &timeout) override |
void | attachTimeoutManager (AsyncTimeout *obj, TimeoutManager::InternalEnum internal) final |
void | detachTimeoutManager (AsyncTimeout *obj) final |
bool | scheduleTimeout (AsyncTimeout *obj, TimeoutManager::timeout_type timeout) final |
void | cancelTimeout (AsyncTimeout *obj) final |
bool | isInTimeoutManagerThread () final |
folly::VirtualEventBase & | getVirtualEventBase () |
EventBase * | getEventBase () override |
Implements the IOExecutor interface. More... | |
![]() | |
TimeoutManager () | |
virtual | ~TimeoutManager () |
void | runAfterDelay (Func cob, uint32_t milliseconds, InternalEnum internal=InternalEnum::NORMAL) |
bool | tryRunAfterDelay (Func cob, uint32_t milliseconds, InternalEnum internal=InternalEnum::NORMAL) |
![]() | |
~DrivableExecutor () override=default | |
![]() | |
virtual | ~Executor () |
virtual void | addWithPriority (Func, int8_t priority) |
virtual uint8_t | getNumPriorities () const |
![]() | |
~IOExecutor () override=default | |
![]() | |
~ScheduledExecutor () override=default | |
void | schedule (Func &&a) |
Alias for add() (for Rx consistency) More... | |
void | schedule (Func &&a, Duration const &dur) |
virtual TimePoint | now () |
Get this executor's notion of time. Must be threadsafe. More... | |
Static Public Member Functions | |
static const char * | getLibeventVersion () |
static const char * | getLibeventMethod () |
![]() | |
template<typename ExecutorT > | |
static KeepAlive< ExecutorT > | getKeepAliveToken (ExecutorT *executor) |
template<typename ExecutorT > | |
static KeepAlive< ExecutorT > | getKeepAliveToken (ExecutorT &executor) |
Protected Member Functions | |
bool | keepAliveAcquire () override |
void | keepAliveRelease () override |
![]() | |
void | clearCobTimeouts () |
Private Types | |
typedef LoopCallback::List | LoopCallbackList |
Private Member Functions | |
void | applyLoopKeepAlive () |
ssize_t | loopKeepAliveCount () |
bool | nothingHandledYet () const noexcept |
bool | loopBody (int flags=0, bool ignoreKeepAlive=false) |
bool | runLoopCallbacks () |
void | initNotificationQueue () |
Friends | |
class | detail::EventBaseLocalBase |
template<typename T > | |
class | EventBaseLocal |
Additional Inherited Members | |
![]() | |
static const int8_t | LO_PRI = SCHAR_MIN |
static const int8_t | MID_PRI = 0 |
static const int8_t | HI_PRI = SCHAR_MAX |
![]() | |
template<typename ExecutorT > | |
static bool | isKeepAliveDummy (const KeepAlive< ExecutorT > &keepAlive) |
template<typename ExecutorT > | |
static KeepAlive< ExecutorT > | makeKeepAlive (ExecutorT *executor) |
This class is a wrapper for all asynchronous I/O processing functionality
EventBase provides a main loop that notifies EventHandler callback objects when I/O is ready on a file descriptor, and notifies AsyncTimeout objects when a specified timeout has expired. More complex, higher-level callback mechanisms can then be built on top of EventHandler and AsyncTimeout.
A EventBase object can only drive an event loop for a single thread. To take advantage of multiple CPU cores, most asynchronous I/O servers have one thread per CPU, and use a separate EventBase for each thread.
In general, most EventBase methods may only be called from the thread running the EventBase's loop. There are a few exceptions to this rule, for methods that are explicitly intended to allow communication with a EventBase from other threads. When it is safe to call a method from another thread it is explicitly listed in the method comments.
Definition at line 128 of file EventBase.h.
using folly::EventBase::Func = folly::Function<void()> |
Definition at line 135 of file EventBase.h.
|
private |
Definition at line 698 of file EventBase.h.
|
inline |
|
explicit |
Create a new EventBase object.
enableTimeMeasurement | Informs whether this event base should measure time. Disabling it would likely improve performance, but will disable some features that relies on time-measurement, including: observer, max latency and avg loop time. |
Definition at line 76 of file EventBase.cpp.
References evb_, initNotificationQueue(), folly::lock(), folly::throwSystemError(), and UNLIKELY.
|
explicit |
Create a new EventBase object that will use the specified libevent event_base object to drive the event loop.
The EventBase will take ownership of this event_base, and will call event_base_free(evb) when the EventBase is destroyed.
enableTimeMeasurement | Informs whether this event base should measure time. Disabling it would likely improve performance, but will disable some features that relies on time-measurement, including: observer, max latency and avg loop time. |
Definition at line 122 of file EventBase.cpp.
References evb_, initNotificationQueue(), and UNLIKELY.
|
override |
Definition at line 148 of file EventBase.cpp.
References applyLoopKeepAlive(), folly::TimeoutManager::clearCobTimeouts(), evb_, fnRunner_, localStorageToDtor_, folly::lock(), loopKeepAliveCount(), loopOnce(), onDestructionCallbacks_, runBeforeLoopCallbacks_, folly::EventBase::LoopCallback::runLoopCallback(), runLoopCallbacks(), and virtualEventBase_.
Referenced by folly::run().
|
inlineoverridevirtual |
Implements the Executor interface.
Implements folly::ScheduledExecutor.
Definition at line 624 of file EventBase.h.
References folly::gen::move.
Referenced by folly::ScopedEventBaseThread::add().
|
private |
Definition at line 433 of file EventBase.cpp.
References fnRunner_, loopKeepAliveActive_, loopKeepAliveCount(), queue_, and virtualEventBase_.
Referenced by loopBody(), loopForever(), and ~EventBase().
|
finalvirtual |
Attaches/detaches TimeoutManager to AsyncTimeout
Implements folly::TimeoutManager.
Definition at line 694 of file EventBase.cpp.
References folly::event_ref_flags(), folly::AsyncTimeout::getEvent(), getLibeventBase(), and folly::TimeoutManager::INTERNAL.
|
finalvirtual |
only EventHandler/AsyncTimeout subclasses and ourselves should ever call this.
This is used to mark the beginning of a new loop cycle by the first handler fired within that cycle.
Implements folly::TimeoutManager.
Definition at line 476 of file EventBase.cpp.
References enableTimeMeasurement_, latestLoopCnt_, nextLoopCnt_, nothingHandledYet(), now(), and startWork_.
Referenced by folly::EventHandler::libeventCallback(), and runLoopCallbacks().
|
finalvirtual |
Cancels the AsyncTimeout, if scheduled
Implements folly::TimeoutManager.
Definition at line 732 of file EventBase.cpp.
References dcheckIsInEventBaseThread(), folly::AsyncTimeout::getEvent(), and folly::EventUtil::isEventRegistered().
Referenced by detachTimeoutManager().
void folly::EventBase::checkIsInEventBaseThread | ( | ) | const |
Equivalent to CHECK(isInEventBaseThread()) (and assert/DCHECK for dcheckIsInEventBaseThread), but it prints more information on failure.
Definition at line 205 of file EventBase.cpp.
References folly::getThreadName(), loopThread_, and folly::Optional< Value >::value_or().
|
inline |
Definition at line 520 of file EventBase.h.
References folly::kIsDebug.
Referenced by folly::AsyncServerSocket::addAcceptCallback(), folly::AsyncUDPSocket::AsyncUDPSocket(), folly::EventHandler::attachEventBase(), folly::AsyncServerSocket::attachEventBase(), folly::AsyncSocket::attachEventBase(), folly::AsyncServerSocket::backoffTimeoutExpired(), folly::AsyncServerSocket::bind(), cancelTimeout(), folly::AsyncUDPSocket::close(), folly::AsyncSocket::close(), folly::AsyncSocket::closeNow(), folly::AsyncSocket::connect(), folly::AsyncServerSocket::detachEventBase(), folly::AsyncSocket::detachEventBase(), wangle::EvbHandshakeHelper::dropConnection(), folly::detail::EventBaseLocalBase::erase(), getName(), folly::AsyncServerSocket::getNumPendingMessagesInQueue(), folly::detail::EventBaseLocalBase::getVoid(), folly::NotificationQueue< MessageT >::Consumer::init(), folly::AsyncSocket::init(), folly::AsyncSocket::ioReady(), folly::AsyncSocket::isDetachable(), folly::AsyncSocket::isZeroCopyWriteInProgress(), folly::AsyncServerSocket::listen(), folly::detail::EventBaseLocalBase::onEventBaseDestruction(), folly::AsyncServerSocket::pauseAccepting(), folly::AsyncSocket::processZeroCopyWriteInProgress(), folly::AsyncServerSocket::removeAcceptCallback(), runBeforeLoop(), runInLoop(), scheduleTimeout(), folly::AsyncSocket::setErrMessageCB(), setName(), folly::AsyncSocket::setReadCB(), folly::AsyncSocket::setSendTimeout(), folly::detail::EventBaseLocalBase::setVoid(), folly::AsyncSocket::shutdownWrite(), folly::AsyncSocket::shutdownWriteNow(), folly::AsyncSSLSocket::sslAccept(), folly::AsyncSSLSocket::sslConn(), folly::AsyncServerSocket::startAccepting(), folly::AsyncServerSocket::stopAccepting(), folly::AsyncSocket::timeoutExpired(), folly::AsyncSocket::updateEventRegistration(), folly::AsyncServerSocket::useExistingSockets(), and folly::AsyncSocket::writeImpl().
|
finalvirtual |
Implements folly::TimeoutManager.
Definition at line 705 of file EventBase.cpp.
References cancelTimeout(), and folly::AsyncTimeout::getEvent().
|
inlineoverridevirtual |
Implements the DrivableExecutor interface.
Implements folly::DrivableExecutor.
Definition at line 631 of file EventBase.h.
References SCOPE_EXIT, and folly::detail::timeout.
Referenced by TEST().
|
inline |
Get the average loop time in microseconds (an exponentially-smoothed ave)
Definition at line 479 of file EventBase.h.
Referenced by TEST().
|
overridevirtual |
Implements the IOExecutor interface.
Implements folly::IOExecutor.
Definition at line 776 of file EventBase.cpp.
References folly::EventBase::SmoothLoopTime::buffer_interval_.
Referenced by AcceptRoutingHandlerTest::getEventBase(), proxygen::PassThroughTransportFilter::getEventBase(), and TEST().
|
inline |
Gets the execution observer associated with this EventBase.
Definition at line 609 of file EventBase.h.
Referenced by folly::EventHandler::libeventCallback().
|
inline |
Definition at line 537 of file EventBase.h.
References evb_.
Referenced by attachTimeoutManager(), folly::AsyncSignalHandler::registerSignalHandler(), and folly::EventHandler::setEventBase().
|
static |
Definition at line 764 of file EventBase.cpp.
|
static |
Definition at line 761 of file EventBase.cpp.
const std::string & folly::EventBase::getName | ( | ) |
Returns the name of the thread that runs this event base.
Definition at line 749 of file EventBase.cpp.
References dcheckIsInEventBaseThread(), and name_.
size_t folly::EventBase::getNotificationQueueSize | ( | ) | const |
Definition at line 197 of file EventBase.cpp.
References queue_.
Referenced by loopBody().
|
inline |
Definition at line 592 of file EventBase.h.
VirtualEventBase & folly::EventBase::getVirtualEventBase | ( | ) |
Definition at line 768 of file EventBase.cpp.
References folly::call_once(), virtualEventBase_, and virtualEventBaseInitFlag_.
Referenced by folly::fibers::EventBaseLoopController::attachEventBase(), and TEST().
|
private |
Definition at line 640 of file EventBase.cpp.
References fnRunner_, and queue_.
Referenced by EventBase().
|
inline |
Definition at line 509 of file EventBase.h.
Referenced by runInEventBaseThread(), and runInEventBaseThreadAndWait().
|
inline |
Verify that current thread is the EventBase thread, if the EventBase is running.
Definition at line 504 of file EventBase.h.
Referenced by folly::AsyncUDPSocket::attachEventBase(), folly::AsyncUDPSocket::detachEventBase(), wangle::Acceptor::dropAllConnections(), folly::AttachDetachClient::readDataAvailable(), runImmediatelyOrRunInEventBaseThreadAndWait(), UDPServer::shutdown(), UDPClient::shutdown(), UDPServer::start(), and UDPClient::start().
|
inlinefinalvirtual |
Helper method to know whether we are running in the timeout manager thread
Implements folly::TimeoutManager.
Definition at line 654 of file EventBase.h.
References folly::getEventBase().
|
inline |
check if the event base loop is running.
Definition at line 487 of file EventBase.h.
References uint32_t.
Referenced by setName(), and waitUntilRunning().
|
inlineoverrideprotectedvirtual |
Reimplemented from folly::Executor.
Definition at line 671 of file EventBase.h.
|
inlineoverrideprotectedvirtual |
Reimplemented from folly::Executor.
Definition at line 680 of file EventBase.h.
References add, and folly::pushmi::__adl::noexcept().
bool folly::EventBase::loop | ( | ) |
Runs the event loop.
loop() will loop waiting for I/O or timeouts and invoking EventHandler and AsyncTimeout callbacks as their events become ready. loop() will only return when there are no more events remaining to process, or after terminateLoopSoon() has been called.
loop() may be called again to restart event processing after a previous call to loop() or loopForever() has returned.
Returns true if the loop completed normally (if it processed all outstanding requests, or if terminateLoopSoon() was called). If an error occurs waiting for events, false will be returned.
Definition at line 256 of file EventBase.cpp.
References loopBody().
Referenced by BENCHMARK(), BENCHMARK_RELATIVE(), QueueTest::destroyCallback(), fizz::test::HandshakeTest::doClientHandshake(), fizz::test::HandshakeTest::doHandshake(), fizz::test::HandshakeTest::doServerHandshake(), fizz::tool::fizzClientCommand(), fizz::tool::fizzServerCommand(), folly::TestSSLServer::init(), loopForever(), main(), QueueTest::maxReadAtOnce(), QueueTest::multiConsumer(), BlockingSocket::open(), QueueTest::putMessages(), ClientRunner::run(), proxygen::WorkerThread::runLoop(), QueueTest::sendOne(), serverSocketSanityTest(), serverTest(), wangle::TEST(), fizz::server::test::TEST(), folly::TEST(), TEST(), TEST_F(), fizz::client::test::TEST_F(), TEST_P(), testConnectOptWrite(), and BlockingSocket::write().
|
private |
Definition at line 275 of file EventBase.cpp.
References folly::EventBase::SmoothLoopTime::addSample(), applyLoopKeepAlive(), avgLoopTime_, folly::EventBase::SmoothLoopTime::dampen(), enableTimeMeasurement_, evb_, fnRunner_, folly::EventBase::SmoothLoopTime::get(), getNotificationQueueSize(), folly::getTimeDelta(), invokingLoop_, loopCallbacks_, loopThread_, maxLatency_, maxLatencyCob_, maxLatencyLoopTime_, name_, nextLoopCnt_, nothingHandledYet(), now(), folly::ScheduledExecutor::now(), observer_, observerSampleCount_, runBeforeLoopCallbacks_, runLoopCallbacks(), SCOPE_EXIT, folly::setThreadName(), startWork_, and stop_.
Referenced by loop(), loopIgnoreKeepAlive(), and loopOnce().
void folly::EventBase::loopForever | ( | ) |
Runs the event loop.
loopForever() behaves like loop(), except that it keeps running even if when there are no more user-supplied EventHandlers or AsyncTimeouts registered. It will only return after terminateLoopSoon() has been called.
This is useful for callers that want to wait for other threads to call runInEventBaseThread(), even when there are no other scheduled events.
loopForever() may be called again to restart event processing after a previous call to loop() or loopForever() has returned.
Throws a std::system_error if an error occurs.
Definition at line 455 of file EventBase.cpp.
References applyLoopKeepAlive(), loop(), loopKeepAliveCount_, SCOPE_EXIT, and folly::throwSystemError().
Referenced by folly::run(), folly::ZeroCopyTest::run(), proxygen::WorkerThread::runLoop(), runTest(), proxygen::HTTPServer::start(), wangle::TEST(), TEST(), folly::TEST(), TEST_F(), and folly::ThreadWheelTimekeeper::ThreadWheelTimekeeper().
bool folly::EventBase::loopIgnoreKeepAlive | ( | ) |
Same as loop(), but doesn't wait for all keep-alive tokens to be released.
Definition at line 260 of file EventBase.cpp.
References fnRunner_, loopBody(), loopKeepAliveActive_, and queue_.
|
private |
Definition at line 423 of file EventBase.cpp.
References loopKeepAliveCount_, and loopKeepAliveCountAtomic_.
Referenced by applyLoopKeepAlive(), and ~EventBase().
bool folly::EventBase::loopOnce | ( | int | flags = 0 | ) |
Wait for some events to become active, run them, then return.
When EVLOOP_NONBLOCK is set in flags, the loop won't block if there are not any events to process.
This is useful for callers that want to run the loop manually.
Returns the same result as loop().
Definition at line 271 of file EventBase.cpp.
References loopBody().
Referenced by BlockingSocket::readHelper(), folly::TEST(), TEST(), TEST_F(), and ~EventBase().
|
privatenoexcept |
Definition at line 689 of file EventBase.cpp.
References latestLoopCnt_, and nextLoopCnt_.
Referenced by bumpHandlingTime(), and loopBody().
void folly::EventBase::resetLoadAvg | ( | double | value = 0.0 | ) |
reset the load average to a desired value
Definition at line 235 of file EventBase.cpp.
References avgLoopTime_, enableTimeMeasurement_, maxLatencyLoopTime_, and folly::EventBase::SmoothLoopTime::reset().
Referenced by TEST().
void folly::EventBase::runBeforeLoop | ( | LoopCallback * | callback | ) |
Adds a callback that will run immediately before the event loop. This is very similar to runInLoop(), but will not cause the loop to break: For example, this callback could be used to get loop times.
Definition at line 548 of file EventBase.cpp.
References folly::EventBase::LoopCallback::cancelLoopCallback(), dcheckIsInEventBaseThread(), and runBeforeLoopCallbacks_.
Referenced by folly::MemoryIdlerTimeout::runLoopCallback(), and TEST().
bool folly::EventBase::runImmediatelyOrRunInEventBaseThreadAndWait | ( | void(*)(T *) | fn, |
T * | arg | ||
) |
Definition at line 804 of file EventBase.h.
Referenced by wangle::AsyncServerSocketFactory::ThreadSafeDestructor::operator()(), wangle::AsyncUDPServerSocketFactory::ThreadSafeDestructor::operator()(), and TEST().
bool folly::EventBase::runImmediatelyOrRunInEventBaseThreadAndWait | ( | Func | fn | ) |
Definition at line 603 of file EventBase.cpp.
References isInEventBaseThread(), folly::gen::move, and runInEventBaseThreadAndWait().
Run the specified function in the EventBase's thread.
This method is thread-safe, and may be called from another thread.
If runInEventBaseThread() is called when the EventBase loop is not running, the function call will be delayed until the next time the loop is started.
If runInEventBaseThread() returns true the function has successfully been scheduled to run in the loop thread. However, if the loop is terminated (and never later restarted) before it has a chance to run the requested function, the function will be run upon the EventBase's destruction.
If two calls to runInEventBaseThread() are made from the same thread, the functions will always be run in the order that they were scheduled. Ordering between functions scheduled from separate threads is not guaranteed.
fn | The function to run. The function must not throw any exceptions. |
arg | An argument to pass to the function. |
Definition at line 794 of file EventBase.h.
Referenced by folly::ThreadWheelTimekeeper::after(), AcceptRoutingHandlerTest::clientConnectAndCleanClose(), AcceptRoutingHandlerTest::clientConnectAndWrite(), folly::AttachDetachClient::connect(), wangle::EvbHandshakeHelper::connectionError(), wangle::EvbHandshakeHelper::connectionReady(), folly::AttachDetachClient::connectSuccess(), wangle::EvbHandshakeHelper::dropConnection(), wangle::Acceptor::dropConnections(), proxygen::WorkerThread::forceStop(), wangle::Acceptor::forceStop(), AcceptRoutingHandlerTest::justClientConnect(), QueueTest::multiConsumer(), StaticService::StaticHandler::readFile(), folly::ZeroCopyTest::run(), runInEventBaseThreadAndWait(), runTest(), AcceptRoutingHandlerTest::sendClientException(), QueueTest::sendOne(), folly::AsyncServerSocket::RemoteAcceptor::start(), folly::AsyncServerSocket::RemoteAcceptor::stop(), proxygen::WorkerThread::stopWhenIdle(), TEST(), folly::TEST(), TEST_F(), proxygen::HandlerCallbacks::threadStopped(), folly::ThreadWheelTimekeeper::ThreadWheelTimekeeper(), proxygen::HTTPServer::updateTicketSeeds(), proxygen::HTTPServer::updateTLSCredentials(), and folly::TestSSLServer::~TestSSLServer().
bool folly::EventBase::runInEventBaseThread | ( | Func | fn | ) |
Run the specified function in the EventBase's thread
This version of runInEventBaseThread() takes a folly::Function object. Note that this may be less efficient than the version that takes a plain function pointer and void* argument, if moving the function is expensive (e.g., if it wraps a lambda which captures some values with expensive move constructors).
If the loop is terminated (and never later restarted) before it has a chance to run the requested function, the function will be run upon the EventBase's destruction.
The function must not throw any exceptions.
Definition at line 554 of file EventBase.cpp.
References inRunningEventBaseThread(), folly::gen::move, queue_, and runInLoop().
bool folly::EventBase::runInEventBaseThreadAndWait | ( | void(*)(T *) | fn, |
T * | arg | ||
) |
Definition at line 799 of file EventBase.h.
Referenced by runImmediatelyOrRunInEventBaseThreadAndWait(), TEST(), TEST_F(), and folly::ThreadWheelTimekeeper::~ThreadWheelTimekeeper().
bool folly::EventBase::runInEventBaseThreadAndWait | ( | Func | fn | ) |
Definition at line 582 of file EventBase.cpp.
References folly::copy(), inRunningEventBaseThread(), folly::gen::move, folly::Baton< MayBlock, Atom >::post(), runInEventBaseThread(), SCOPE_EXIT, and folly::Baton< MayBlock, Atom >::wait().
void folly::EventBase::runInLoop | ( | LoopCallback * | callback, |
bool | thisIteration = false |
||
) |
Adds the given callback to a queue of things run after the current pass through the event loop completes. Note that if this callback calls runInLoop() the new callback won't be called until the main event loop has gone through a cycle.
This method may only be called from the EventBase's thread. This essentially allows an event handler to schedule an additional callback to be invoked after it returns.
Use runInEventBaseThread() to schedule functions from another thread.
The thisIteration parameter makes this callback run in this loop iteration, instead of the next one, even if called from a runInLoop callback (normal io callbacks that call runInLoop will always run in this iteration). This was originally added to support detachEventBase, as a user callback may have called terminateLoopSoon(), but we want to make sure we detach. Also, detachEventBase almost always must be called from the base event loop to ensure the stack is unwound, since most users of EventBase are not thread safe.
Ideally we would not need thisIteration, and instead just use runInLoop with loop() (instead of terminateLoopSoon).
Definition at line 520 of file EventBase.cpp.
References folly::EventBase::LoopCallback::cancelLoopCallback(), folly::EventBase::LoopCallback::context_, dcheckIsInEventBaseThread(), loopCallbacks_, runOnceCallbacks_, and folly::RequestContext::saveContext().
Referenced by BENCHMARK(), BENCHMARK_RELATIVE(), TestAsyncTransport::fireNextReadEvent(), fizz::tool::TerminalInputHandler::hitEOF(), QueueTest::maxReadAtOnce(), folly::ZeroCopyTestAsyncSocket::onDataReady(), runInEventBaseThread(), folly::fibers::EventBaseLoopController::schedule(), folly::AsyncSocket::scheduleImmediateRead(), folly::AsyncSocket::scheduleInitialReadWrite(), proxygen::HTTPServer::start(), TEST(), and tmpDisableReads().
void folly::EventBase::runInLoop | ( | Func | c, |
bool | thisIteration = false |
||
) |
Convenience function to call runInLoop() with a folly::Function.
This creates a LoopCallback object to wrap the folly::Function, and invoke the folly::Function when the loop callback fires. This is slightly more expensive than defining your own LoopCallback, but more convenient in areas that aren't too performance sensitive.
This method may only be called from the EventBase's thread. This essentially allows an event handler to schedule an additional callback to be invoked after it returns.
Use runInEventBaseThread() to schedule functions from another thread.
Definition at line 531 of file EventBase.cpp.
References dcheckIsInEventBaseThread(), loopCallbacks_, folly::gen::move, runOnceCallbacks_, and folly::RequestContext::saveContext().
|
private |
Definition at line 612 of file EventBase.cpp.
References bumpHandlingTime(), folly::EventBase::LoopCallback::context_, loopCallbacks_, folly::gen::move, folly::EventBase::LoopCallback::runLoopCallback(), and runOnceCallbacks_.
Referenced by loopBody(), and ~EventBase().
void folly::EventBase::runOnDestruction | ( | LoopCallback * | callback | ) |
Adds the given callback to a queue of things run before destruction of current EventBase.
This allows users of EventBase that run in it, but don't control it, to be notified before EventBase gets destructed.
Note: will be called from the thread that invoked EventBase destructor, before the final run of loop callbacks.
Definition at line 542 of file EventBase.cpp.
References folly::EventBase::LoopCallback::cancelLoopCallback(), onDestructionCallbacks_, and onDestructionCallbacksMutex_.
Referenced by folly::run(), and TEST_F().
Schedule a Func to be executed at time t, or as soon afterward as possible. Expect millisecond resolution at best. Must be threadsafe.
Reimplemented from folly::ScheduledExecutor.
Definition at line 754 of file EventBase.cpp.
References folly::gen::move, folly::ScheduledExecutor::now(), folly::HHWheelTimer::scheduleTimeoutFn(), and timer().
Referenced by fizz::server::test::TEST(), and TEST().
|
finalvirtual |
Schedules AsyncTimeout to fire after timeout
milliseconds
Implements folly::TimeoutManager.
Definition at line 711 of file EventBase.cpp.
References dcheckIsInEventBaseThread(), folly::errnoStr(), and folly::AsyncTimeout::getEvent().
|
inline |
Setup execution observation/instrumentation for every EventHandler executed in this EventBase.
executionObserver | EventHandle's execution observer. |
Definition at line 602 of file EventBase.h.
void folly::EventBase::setLoadAvgMsec | ( | std::chrono::milliseconds | ms | ) |
Set smoothing coefficient for loop load average; # of milliseconds for exp(-1) (1/2.71828...) decay.
Definition at line 224 of file EventBase.cpp.
References avgLoopTime_, enableTimeMeasurement_, maxLatencyLoopTime_, folly::EventBase::SmoothLoopTime::setTimeInterval(), and us.
Referenced by TEST().
|
inline |
Set the maximum desired latency in us and provide a callback which will be called when that latency is exceeded. OBS: This functionality depends on time-measurement.
Definition at line 459 of file EventBase.h.
References folly::gen::move, and folly::value().
Referenced by TEST().
void folly::EventBase::setMaxReadAtOnce | ( | uint32_t | maxAtOnce | ) |
void folly::EventBase::setName | ( | const std::string & | name | ) |
Set the name of the thread that runs this event base.
Definition at line 740 of file EventBase.cpp.
References dcheckIsInEventBaseThread(), isRunning(), loopThread_, name, name_, and folly::setThreadName().
Referenced by TEST(), folly::ThreadWheelTimekeeper::ThreadWheelTimekeeper(), and proxygen::WorkerThread::WorkerThread().
|
inline |
Definition at line 587 of file EventBase.h.
void folly::EventBase::terminateLoopSoon | ( | ) |
Causes the event loop to exit soon.
This will cause an existing call to loop() or loopForever() to stop event processing and return, even if there are still events remaining to be processed.
It is safe to call terminateLoopSoon() from another thread to cause loop() to wake up and return in the EventBase loop thread. terminateLoopSoon() may also be called from the loop thread itself (for example, a EventHandler or AsyncTimeout callback may call terminateLoopSoon() to cause the loop to exit after the callback returns.) If the loop is not running, this will cause the next call to loop to terminate soon after starting. If a loop runs out of work (and so terminates on its own) concurrently with a call to terminateLoopSoon(), this may cause a race condition.
Note that the caller is responsible for ensuring that cleanup of all event callbacks occurs properly. Since terminateLoopSoon() causes the loop to exit even when there are pending events present, there may be remaining callbacks present waiting to be invoked. If the loop is later restarted pending events will continue to be processed normally, however if the EventBase is destroyed after calling terminateLoopSoon() it is the caller's responsibility to ensure that cleanup happens properly even if some outstanding events are never processed.
Definition at line 493 of file EventBase.cpp.
References evb_, queue_, and stop_.
Referenced by proxygen::WorkerThread::forceStop(), QueueTest::maxReadAtOnce(), folly::ZeroCopyTestAsyncSocket::onDataFinish(), folly::ZeroCopyTestAsyncSocket::onDataReady(), TestAcceptor::onNewConnection(), runInThreadTestFunc(), runTest(), UDPClient::shutdown(), proxygen::HTTPServer::stop(), proxygen::WorkerThread::stopWhenIdle(), fizz::server::test::TEST(), wangle::TEST(), TEST(), folly::TEST(), TEST_F(), folly::ScopedEventBaseThread::~ScopedEventBaseThread(), and folly::ThreadWheelTimekeeper::~ThreadWheelTimekeeper().
|
inline |
Definition at line 526 of file EventBase.h.
References folly::HHWheelTimer::newTimer().
Referenced by scheduleAt(), TEST_F(), and proxygen::WheelTimerInstance::WheelTimerInstance().
void folly::EventBase::waitUntilRunning | ( | ) |
wait until the event loop starts (after starting the event loop thread).
Definition at line 249 of file EventBase.cpp.
References isRunning(), and folly::fibers::yield().
Referenced by folly::ScopedEventBaseThread::ScopedEventBaseThread(), proxygen::WorkerThread::start(), TEST(), and folly::ThreadWheelTimekeeper::ThreadWheelTimekeeper().
|
friend |
Definition at line 783 of file EventBase.h.
|
friend |
Definition at line 785 of file EventBase.h.
|
private |
Definition at line 744 of file EventBase.h.
Referenced by loopBody(), resetLoadAvg(), and setLoadAvgMsec().
|
private |
Definition at line 757 of file EventBase.h.
Referenced by bumpHandlingTime(), loopBody(), resetLoadAvg(), and setLoadAvgMsec().
|
private |
Definition at line 730 of file EventBase.h.
Referenced by EventBase(), loopBody(), folly::EventBase::FunctionRunner::messageAvailable(), terminateLoopSoon(), and ~EventBase().
|
private |
Definition at line 774 of file EventBase.h.
|
private |
Definition at line 735 of file EventBase.h.
Referenced by applyLoopKeepAlive(), initNotificationQueue(), loopBody(), loopIgnoreKeepAlive(), setMaxReadAtOnce(), and ~EventBase().
|
private |
Definition at line 767 of file EventBase.h.
Referenced by loopBody().
|
private |
Definition at line 761 of file EventBase.h.
Referenced by bumpHandlingTime(), and nothingHandledYet().
|
private |
Definition at line 786 of file EventBase.h.
Referenced by folly::detail::EventBaseLocalBase::erase(), folly::detail::EventBaseLocalBase::getVoid(), and folly::detail::EventBaseLocalBase::setVoid().
|
private |
Definition at line 787 of file EventBase.h.
Referenced by folly::detail::EventBaseLocalBase::erase(), folly::detail::EventBaseLocalBase::setVoid(), and ~EventBase().
|
private |
Definition at line 711 of file EventBase.h.
Referenced by loopBody(), runInLoop(), and runLoopCallbacks().
|
private |
Definition at line 738 of file EventBase.h.
Referenced by applyLoopKeepAlive(), and loopIgnoreKeepAlive().
|
private |
Definition at line 736 of file EventBase.h.
Referenced by loopForever(), and loopKeepAliveCount().
|
private |
Definition at line 737 of file EventBase.h.
Referenced by loopKeepAliveCount().
|
private |
Definition at line 727 of file EventBase.h.
Referenced by checkIsInEventBaseThread(), loopBody(), and setName().
|
private |
Definition at line 741 of file EventBase.h.
Referenced by loopBody().
|
private |
Definition at line 752 of file EventBase.h.
Referenced by loopBody().
|
private |
Definition at line 749 of file EventBase.h.
Referenced by loopBody(), resetLoadAvg(), and setLoadAvgMsec().
|
private |
Definition at line 777 of file EventBase.h.
Referenced by getName(), loopBody(), and setName().
|
private |
Definition at line 760 of file EventBase.h.
Referenced by bumpHandlingTime(), loopBody(), and nothingHandledYet().
|
private |
Definition at line 770 of file EventBase.h.
Referenced by loopBody().
|
private |
Definition at line 771 of file EventBase.h.
Referenced by loopBody().
|
private |
Definition at line 713 of file EventBase.h.
Referenced by runOnDestruction(), and ~EventBase().
|
private |
Definition at line 780 of file EventBase.h.
Referenced by runOnDestruction().
|
private |
Definition at line 734 of file EventBase.h.
Referenced by applyLoopKeepAlive(), getNotificationQueueSize(), initNotificationQueue(), loopIgnoreKeepAlive(), runInEventBaseThread(), and terminateLoopSoon().
|
private |
Definition at line 712 of file EventBase.h.
Referenced by loopBody(), runBeforeLoop(), and ~EventBase().
|
private |
Definition at line 719 of file EventBase.h.
Referenced by runInLoop(), and runLoopCallbacks().
|
private |
Definition at line 762 of file EventBase.h.
Referenced by bumpHandlingTime(), and loopBody().
|
private |
Definition at line 723 of file EventBase.h.
Referenced by loopBody(), and terminateLoopSoon().
|
private |
Definition at line 790 of file EventBase.h.
Referenced by applyLoopKeepAlive(), getVirtualEventBase(), and ~EventBase().
|
private |
Definition at line 789 of file EventBase.h.
Referenced by getVirtualEventBase().
|
private |
Definition at line 709 of file EventBase.h.