proxygen
folly::TestExecutor Class Reference

#include <TestExecutor.h>

Inheritance diagram for folly::TestExecutor:
folly::Executor

Public Member Functions

 TestExecutor (size_t numThreads)
 
 ~TestExecutor () override
 
void add (Func f) override
 
size_t numThreads () const
 
- Public Member Functions inherited from folly::Executor
virtual ~Executor ()
 
virtual void addWithPriority (Func, int8_t priority)
 
virtual uint8_t getNumPriorities () const
 

Private Member Functions

void addImpl (Func f)
 

Private Attributes

std::mutex m_
 
std::queue< FuncworkItems_
 
std::condition_variable cv_
 
std::vector< std::thread > workers_
 

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

A simple multithreaded executor for use in tests etc

Definition at line 30 of file TestExecutor.h.

Constructor & Destructor Documentation

folly::TestExecutor::TestExecutor ( size_t  numThreads)
explicit

Definition at line 23 of file TestExecutor.cpp.

References cv_, max, and folly::gen::move.

23  {
24  const auto kWorkers = std::max(size_t(1), numThreads);
25  for (auto idx = 0u; idx < kWorkers; ++idx) {
26  workers_.emplace_back([this] {
27  while (true) {
28  Func work;
29  {
30  unique_lock<mutex> lk(m_);
31  cv_.wait(lk, [this] { return !workItems_.empty(); });
32  work = std::move(workItems_.front());
33  workItems_.pop();
34  }
35  if (!work) {
36  break;
37  }
38  work();
39  }
40  });
41  }
42 }
std::condition_variable cv_
Definition: TestExecutor.h:45
LogLevel max
Definition: LogLevel.cpp:31
std::queue< Func > workItems_
Definition: TestExecutor.h:44
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
size_t numThreads() const
Function< void()> Func
Definition: Executor.h:27
std::vector< std::thread > workers_
Definition: TestExecutor.h:47
folly::TestExecutor::~TestExecutor ( )
override

Definition at line 44 of file TestExecutor.cpp.

44  {
45  for (auto& worker : workers_) {
46  (void)worker;
47  addImpl({});
48  }
49 
50  for (auto& worker : workers_) {
51  worker.join();
52  }
53 }
void addImpl(Func f)
std::vector< std::thread > workers_
Definition: TestExecutor.h:47

Member Function Documentation

void folly::TestExecutor::add ( Func  )
overridevirtual

Enqueue a function to executed by this executor. This and all variants must be threadsafe.

Implements folly::Executor.

Definition at line 55 of file TestExecutor.cpp.

References folly::gen::move.

55  {
56  if (f) {
58  }
59 }
auto f
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void addImpl(Func f)
void folly::TestExecutor::addImpl ( Func  f)
private

Definition at line 65 of file TestExecutor.cpp.

References cv_, g(), and folly::gen::move.

65  {
66  {
67  lock_guard<mutex> g(m_);
68  workItems_.push(std::move(f));
69  }
70  cv_.notify_one();
71 }
std::condition_variable cv_
Definition: TestExecutor.h:45
auto f
std::queue< Func > workItems_
Definition: TestExecutor.h:44
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
g_t g(f_t)
size_t folly::TestExecutor::numThreads ( ) const

Definition at line 61 of file TestExecutor.cpp.

61  {
62  return workers_.size();
63 }
std::vector< std::thread > workers_
Definition: TestExecutor.h:47

Member Data Documentation

std::condition_variable folly::TestExecutor::cv_
private

Definition at line 45 of file TestExecutor.h.

std::mutex folly::TestExecutor::m_
private

Definition at line 43 of file TestExecutor.h.

std::vector<std::thread> folly::TestExecutor::workers_
private

Definition at line 47 of file TestExecutor.h.

std::queue<Func> folly::TestExecutor::workItems_
private

Definition at line 44 of file TestExecutor.h.


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