proxygen
|
#include <WorkerThread.h>
Public Member Functions | |
WorkerThread (folly::EventBaseManager *ebm, const std::string &evbName=std::string()) | |
virtual | ~WorkerThread () |
void | start () |
void | stopWhenIdle () |
void | forceStop () |
void | wait () |
folly::EventBase * | getEventBase () |
std::thread::native_handle_type | getThreadNativeHandle () noexcept |
std::thread::id | getThreadId () const noexcept |
Static Public Member Functions | |
static WorkerThread * | getCurrentWorkerThread () |
Protected Member Functions | |
virtual void | setup () |
virtual void | cleanup () |
Private Types | |
enum | State : uint8_t { State::IDLE, State::STARTING, State::RUNNING, State::STOP_WHEN_IDLE, State::FORCE_STOP } |
Private Member Functions | |
WorkerThread (WorkerThread const &)=delete | |
WorkerThread & | operator= (WorkerThread const &)=delete |
void | runLoop () |
Private Attributes | |
State | state_ {State::IDLE} |
std::thread | thread_ |
std::mutex | joinLock_ |
folly::EventBase | eventBase_ |
folly::EventBaseManager * | eventBaseManager_ {nullptr} |
Static Private Attributes | |
static FOLLY_TLS WorkerThread * | currentWorker_ = nullptr |
A WorkerThread represents an independent event loop that runs in its own thread.
Definition at line 29 of file WorkerThread.h.
|
strongprivate |
Enumerator | |
---|---|
IDLE | |
STARTING | |
RUNNING | |
STOP_WHEN_IDLE | |
FORCE_STOP |
Definition at line 120 of file WorkerThread.h.
|
explicit |
Definition at line 22 of file WorkerThread.cpp.
References eventBase_, and folly::EventBase::setName().
|
virtual |
Definition at line 33 of file WorkerThread.cpp.
|
privatedelete |
|
protectedvirtual |
Reimplemented in proxygen::RequestWorker.
Definition at line 127 of file WorkerThread.cpp.
References folly::EventBaseManager::clearEventBase(), currentWorker_, and eventBaseManager_.
Referenced by proxygen::RequestWorker::cleanup(), and start().
void proxygen::WorkerThread::forceStop | ( | ) |
Request that the worker stop executing as soon as possible.
This will terminate the worker thread's event loop, and cause the thread to return. If there are any services still running in the worker thread, their events will no longer be processed.
This function is asynchronous: it signals the worker thread to stop, and returns without waiting for the thread to actually terminate. The wait() method must be called to wait for the thread to terminate.
Definition at line 73 of file WorkerThread.cpp.
References eventBase_, FORCE_STOP, IDLE, folly::EventBase::runInEventBaseThread(), RUNNING, state_, STOP_WHEN_IDLE, and folly::EventBase::terminateLoopSoon().
|
inlinestatic |
Get the current WorkerThread running this thread.
Returns nullptr if called from a thread that is not running inside WorkerThread.
Definition at line 111 of file WorkerThread.h.
References folly::ssl::cleanup().
Referenced by proxygen::RequestWorker::getRequestWorker().
|
inline |
Get the EventBase used to drive the events in this worker thread.
Definition at line 85 of file WorkerThread.h.
Referenced by proxygen::RequestWorker::flushStats().
|
inlinenoexcept |
Get ID of the underlying thread objects (valid only when the thread is running).
Definition at line 101 of file WorkerThread.h.
|
inlinenoexcept |
Get native handle of the underlying thread object (valid only when the thread is running).
Definition at line 93 of file WorkerThread.h.
|
privatedelete |
|
private |
Definition at line 134 of file WorkerThread.cpp.
References eventBase_, FORCE_STOP, IDLE, folly::EventBase::loop(), folly::EventBase::loopForever(), RUNNING, STARTING, state_, and STOP_WHEN_IDLE.
Referenced by start().
|
protectedvirtual |
Reimplemented in proxygen::RequestWorker.
Definition at line 98 of file WorkerThread.cpp.
References currentWorker_, eventBase_, eventBaseManager_, and folly::EventBaseManager::setEventBase().
Referenced by proxygen::RequestWorker::setup(), and start().
void proxygen::WorkerThread::start | ( | ) |
Begin execution of the worker.
This starts the worker thread, and returns immediately.
Definition at line 37 of file WorkerThread.cpp.
References cleanup(), eventBase_, folly::gen::guard(), IDLE, joinLock_, runLoop(), setup(), STARTING, state_, thread_, and folly::EventBase::waitUntilRunning().
void proxygen::WorkerThread::stopWhenIdle | ( | ) |
Request that the worker thread stop when there are no more events to process.
Normally each worker thread runs forever, even if it is idle with no events to process. This function requests that the worker thread return when it becomes idle.
This is used for graceful shutdown: Once the services have been asked to shutdown, stopWhenIdle() can be called on the WorkerThread so that it will return as soon as the services in this thread no longer have any events to process.
Typically you will still want to call forceStop() after a timeout, in case some of the services take too long to shut down gracefully.
Definition at line 55 of file WorkerThread.cpp.
References eventBase_, IDLE, folly::EventBase::runInEventBaseThread(), RUNNING, state_, STOP_WHEN_IDLE, and folly::EventBase::terminateLoopSoon().
void proxygen::WorkerThread::wait | ( | ) |
Synchronously wait for termination of the worker thread.
Note that the worker thread will only terminate after stopWhenIdle() or forceStop() has been called, so you typically should only call wait() after first using one of these functions.
Definition at line 91 of file WorkerThread.cpp.
References folly::gen::guard(), joinLock_, and thread_.
|
staticprivate |
Definition at line 141 of file WorkerThread.h.
|
private |
Definition at line 137 of file WorkerThread.h.
Referenced by forceStop(), runLoop(), setup(), start(), stopWhenIdle(), and WorkerThread().
|
private |
Definition at line 138 of file WorkerThread.h.
|
private |
Definition at line 136 of file WorkerThread.h.
|
private |
Definition at line 134 of file WorkerThread.h.
Referenced by forceStop(), runLoop(), start(), stopWhenIdle(), and ~WorkerThread().
|
private |
Definition at line 135 of file WorkerThread.h.