proxygen
ThreadExecutor Class Reference

Simple executor that does work in another thread. More...

Inheritance diagram for ThreadExecutor:
folly::Executor

Public Member Functions

 ThreadExecutor (size_t n=1024)
 
 ~ThreadExecutor () override
 
void add (Func fn) override
 
void waitForStartup ()
 
- Public Member Functions inherited from folly::Executor
virtual ~Executor ()
 
virtual void add (Func)=0
 
virtual void addWithPriority (Func, int8_t priority)
 
virtual uint8_t getNumPriorities () const
 

Private Member Functions

void work ()
 

Private Attributes

folly::MPMCQueue< Func > funcs
 
std::atomic< bool > done {false}
 
std::thread worker
 
folly::Baton baton
 

Additional Inherited Members

- Static Public Member Functions inherited from folly::Executor
template<typename ExecutorT >
static KeepAlive< ExecutorT > getKeepAliveToken (ExecutorT *executor)
 
template<typename ExecutorT >
static KeepAlive< ExecutorT > getKeepAliveToken (ExecutorT &executor)
 
- Static Public Attributes inherited from folly::Executor
static const int8_t LO_PRI = SCHAR_MIN
 
static const int8_t MID_PRI = 0
 
static const int8_t HI_PRI = SCHAR_MAX
 
- Protected Member Functions inherited from folly::Executor
virtual bool keepAliveAcquire ()
 
virtual void keepAliveRelease ()
 
- Static Protected Member Functions inherited from folly::Executor
template<typename ExecutorT >
static bool isKeepAliveDummy (const KeepAlive< ExecutorT > &keepAlive)
 
template<typename ExecutorT >
static KeepAlive< ExecutorT > makeKeepAlive (ExecutorT *executor)
 

Detailed Description

Simple executor that does work in another thread.

Definition at line 314 of file ViaTest.cpp.

Constructor & Destructor Documentation

ThreadExecutor::ThreadExecutor ( size_t  n = 1024)
inlineexplicit

Definition at line 332 of file ViaTest.cpp.

References folly::netops::bind(), and work().

332  : funcs(n) {
333  worker = std::thread(std::bind(&ThreadExecutor::work, this));
334  }
folly::MPMCQueue< Func > funcs
Definition: ViaTest.cpp:315
std::thread worker
Definition: ViaTest.cpp:317
int bind(NetworkSocket s, const sockaddr *name, socklen_t namelen)
Definition: NetOps.cpp:76
ThreadExecutor::~ThreadExecutor ( )
inlineoverride

Definition at line 336 of file ViaTest.cpp.

336  {
337  done = true;
338  funcs.write([] {});
339  worker.join();
340  }
folly::MPMCQueue< Func > funcs
Definition: ViaTest.cpp:315
std::thread worker
Definition: ViaTest.cpp:317
std::atomic< bool > done
Definition: ViaTest.cpp:316

Member Function Documentation

void ThreadExecutor::add ( Func  fn)
inlineoverride

Definition at line 342 of file ViaTest.cpp.

References folly::gen::move.

Referenced by TEST().

342  {
343  funcs.blockingWrite(std::move(fn));
344  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::MPMCQueue< Func > funcs
Definition: ViaTest.cpp:315
void ThreadExecutor::waitForStartup ( )
inline

Definition at line 346 of file ViaTest.cpp.

References folly::Baton< MayBlock, Atom >::wait().

Referenced by TEST().

346  {
347  baton.wait();
348  }
FOLLY_ALWAYS_INLINE void wait(const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:170
folly::Baton baton
Definition: ViaTest.cpp:318
void ThreadExecutor::work ( )
inlineprivate

Definition at line 320 of file ViaTest.cpp.

References folly::Baton< MayBlock, Atom >::post().

Referenced by ThreadExecutor().

320  {
321  baton.post();
322  Func fn;
323  while (!done) {
324  while (!funcs.isEmpty()) {
325  funcs.blockingRead(fn);
326  fn();
327  }
328  }
329  }
Function< void()> Func
Definition: Executor.h:27
folly::MPMCQueue< Func > funcs
Definition: ViaTest.cpp:315
void post() noexcept
Definition: Baton.h:123
folly::Baton baton
Definition: ViaTest.cpp:318
std::atomic< bool > done
Definition: ViaTest.cpp:316

Member Data Documentation

folly::Baton ThreadExecutor::baton
private

Definition at line 318 of file ViaTest.cpp.

std::atomic<bool> ThreadExecutor::done {false}
private

Definition at line 316 of file ViaTest.cpp.

folly::MPMCQueue<Func> ThreadExecutor::funcs
private

Definition at line 315 of file ViaTest.cpp.

std::thread ThreadExecutor::worker
private

Definition at line 317 of file ViaTest.cpp.


The documentation for this class was generated from the following file: