proxygen
folly::ThreadedExecutor Class Reference

#include <ThreadedExecutor.h>

Inheritance diagram for folly::ThreadedExecutor:
folly::Executor

Public Member Functions

 ThreadedExecutor (std::shared_ptr< ThreadFactory > threadFactory=newDefaultThreadFactory())
 
 ~ThreadedExecutor () override
 
 ThreadedExecutor (ThreadedExecutor const &)=delete
 
 ThreadedExecutor (ThreadedExecutor &&)=delete
 
ThreadedExecutoroperator= (ThreadedExecutor const &)=delete
 
ThreadedExecutoroperator= (ThreadedExecutor &&)=delete
 
void add (Func func) override
 
- 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 notify ()
 
void control ()
 
void controlWait ()
 
bool controlPerformAll ()
 
void controlJoinFinishedThreads ()
 
void controlLaunchEnqueuedTasks ()
 
void work (Func &func)
 

Static Private Member Functions

static std::shared_ptr< ThreadFactorynewDefaultThreadFactory ()
 

Private Attributes

std::shared_ptr< ThreadFactorythreadFactory_
 
std::atomic< bool > stopping_ {false}
 
std::mutex controlm_
 
std::condition_variable controlc_
 
bool controls_ = false
 
std::thread controlt_
 
std::mutex enqueuedm_
 
std::deque< Funcenqueued_
 
std::map< std::thread::id, std::thread > running_
 
std::mutex finishedm_
 
std::deque< std::thread::id > finished_
 

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

Definition at line 55 of file ThreadedExecutor.h.

Constructor & Destructor Documentation

folly::ThreadedExecutor::ThreadedExecutor ( std::shared_ptr< ThreadFactory threadFactory = newDefaultThreadFactory())
explicit

Definition at line 34 of file ThreadedExecutor.cpp.

References control(), and controlt_.

35  : threadFactory_(std::move(threadFactory)) {
36  controlt_ = std::thread([this] { control(); });
37 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::shared_ptr< ThreadFactory > threadFactory_
folly::ThreadedExecutor::~ThreadedExecutor ( )
override

Definition at line 39 of file ThreadedExecutor.cpp.

References controlt_, finished_, notify(), running_, and stopping_.

39  {
40  stopping_.store(true, std::memory_order_release);
41  notify();
42  controlt_.join();
43  CHECK(running_.empty());
44  CHECK(finished_.empty());
45 }
std::deque< std::thread::id > finished_
std::atomic< bool > stopping_
std::map< std::thread::id, std::thread > running_
folly::ThreadedExecutor::ThreadedExecutor ( ThreadedExecutor const &  )
delete
folly::ThreadedExecutor::ThreadedExecutor ( ThreadedExecutor &&  )
delete

Member Function Documentation

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

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

Implements folly::Executor.

Definition at line 47 of file ThreadedExecutor.cpp.

References enqueued_, enqueuedm_, folly::gen::move, notify(), stopping_, and folly::with_unique_lock().

47  {
48  CHECK(!stopping_.load(std::memory_order_acquire));
49  with_unique_lock(enqueuedm_, [&] { enqueued_.push_back(std::move(func)); });
50  notify();
51 }
std::deque< Func > enqueued_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static auto with_unique_lock(std::mutex &m, F &&f) -> decltype(f())
std::atomic< bool > stopping_
void folly::ThreadedExecutor::control ( )
private

Definition at line 62 of file ThreadedExecutor.cpp.

References controlPerformAll(), controlWait(), and folly::setThreadName().

Referenced by ThreadedExecutor().

62  {
63  folly::setThreadName("ThreadedCtrl");
64  auto looping = true;
65  while (looping) {
66  controlWait();
67  looping = controlPerformAll();
68  }
69 }
bool setThreadName(std::thread::id tid, StringPiece name)
Definition: ThreadName.cpp:109
void folly::ThreadedExecutor::controlJoinFinishedThreads ( )
private

Definition at line 85 of file ThreadedExecutor.cpp.

References finished_, finishedm_, running_, folly::f14::swap(), and folly::with_unique_lock().

Referenced by controlPerformAll().

85  {
86  std::deque<std::thread::id> finishedt;
87  with_unique_lock(finishedm_, [&] { std::swap(finishedt, finished_); });
88  for (auto id : finishedt) {
89  running_[id].join();
90  running_.erase(id);
91  }
92 }
static auto with_unique_lock(std::mutex &m, F &&f) -> decltype(f())
std::deque< std::thread::id > finished_
std::map< std::thread::id, std::thread > running_
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
void folly::ThreadedExecutor::controlLaunchEnqueuedTasks ( )
private

Definition at line 94 of file ThreadedExecutor.cpp.

References enqueued_, enqueuedm_, f, folly::gen::move, running_, folly::f14::swap(), threadFactory_, folly::with_unique_lock(), and work().

Referenced by controlPerformAll().

94  {
95  std::deque<Func> enqueuedt;
96  with_unique_lock(enqueuedm_, [&] { std::swap(enqueuedt, enqueued_); });
97  for (auto& f : enqueuedt) {
98  auto th = threadFactory_->newThread(
99  [this, f = std::move(f)]() mutable { work(f); });
100  auto id = th.get_id();
101  running_[id] = std::move(th);
102  }
103 }
std::deque< Func > enqueued_
auto f
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static auto with_unique_lock(std::mutex &m, F &&f) -> decltype(f())
std::shared_ptr< ThreadFactory > threadFactory_
std::map< std::thread::id, std::thread > running_
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
bool folly::ThreadedExecutor::controlPerformAll ( )
private

Definition at line 105 of file ThreadedExecutor.cpp.

References controlJoinFinishedThreads(), controlLaunchEnqueuedTasks(), running_, and stopping_.

Referenced by control().

105  {
106  auto stopping = stopping_.load(std::memory_order_acquire);
109  return !stopping || !running_.empty();
110 }
std::atomic< bool > stopping_
std::map< std::thread::id, std::thread > running_
void folly::ThreadedExecutor::controlWait ( )
private

Definition at line 71 of file ThreadedExecutor.cpp.

References controlc_, controlm_, controls_, and folly::lock().

Referenced by control().

71  {
72  constexpr auto kMaxWait = std::chrono::seconds(10);
73  std::unique_lock<std::mutex> lock(controlm_);
74  controlc_.wait_for(lock, kMaxWait, [&] { return controls_; });
75  controls_ = false;
76 }
std::condition_variable controlc_
auto lock(Synchronized< D, M > &synchronized, Args &&...args)
std::shared_ptr< ThreadFactory > folly::ThreadedExecutor::newDefaultThreadFactory ( )
staticprivate

Definition at line 53 of file ThreadedExecutor.cpp.

53  {
54  return std::make_shared<NamedThreadFactory>("Threaded");
55 }
void folly::ThreadedExecutor::notify ( )
private

Definition at line 57 of file ThreadedExecutor.cpp.

References controlc_, controlm_, controls_, and folly::with_unique_lock().

Referenced by add(), work(), and ~ThreadedExecutor().

57  {
58  with_unique_lock(controlm_, [&] { controls_ = true; });
59  controlc_.notify_one();
60 }
std::condition_variable controlc_
static auto with_unique_lock(std::mutex &m, F &&f) -> decltype(f())
ThreadedExecutor& folly::ThreadedExecutor::operator= ( ThreadedExecutor const &  )
delete
ThreadedExecutor& folly::ThreadedExecutor::operator= ( ThreadedExecutor &&  )
delete
void folly::ThreadedExecutor::work ( Func func)
private

Definition at line 78 of file ThreadedExecutor.cpp.

References finished_, finishedm_, notify(), and folly::with_unique_lock().

Referenced by controlLaunchEnqueuedTasks().

78  {
79  func();
80  auto id = std::this_thread::get_id();
81  with_unique_lock(finishedm_, [&] { finished_.push_back(id); });
82  notify();
83 }
static auto with_unique_lock(std::mutex &m, F &&f) -> decltype(f())
std::deque< std::thread::id > finished_

Member Data Documentation

std::condition_variable folly::ThreadedExecutor::controlc_
private

Definition at line 86 of file ThreadedExecutor.h.

Referenced by controlWait(), and notify().

std::mutex folly::ThreadedExecutor::controlm_
private

Definition at line 85 of file ThreadedExecutor.h.

Referenced by controlWait(), and notify().

bool folly::ThreadedExecutor::controls_ = false
private

Definition at line 87 of file ThreadedExecutor.h.

Referenced by controlWait(), and notify().

std::thread folly::ThreadedExecutor::controlt_
private

Definition at line 88 of file ThreadedExecutor.h.

Referenced by ThreadedExecutor(), and ~ThreadedExecutor().

std::deque<Func> folly::ThreadedExecutor::enqueued_
private

Definition at line 91 of file ThreadedExecutor.h.

Referenced by add(), and controlLaunchEnqueuedTasks().

std::mutex folly::ThreadedExecutor::enqueuedm_
private

Definition at line 90 of file ThreadedExecutor.h.

Referenced by add(), and controlLaunchEnqueuedTasks().

std::deque<std::thread::id> folly::ThreadedExecutor::finished_
private

Definition at line 97 of file ThreadedExecutor.h.

Referenced by controlJoinFinishedThreads(), work(), and ~ThreadedExecutor().

std::mutex folly::ThreadedExecutor::finishedm_
private

Definition at line 96 of file ThreadedExecutor.h.

Referenced by controlJoinFinishedThreads(), and work().

std::map<std::thread::id, std::thread> folly::ThreadedExecutor::running_
private
std::atomic<bool> folly::ThreadedExecutor::stopping_ {false}
private

Definition at line 83 of file ThreadedExecutor.h.

Referenced by add(), controlPerformAll(), and ~ThreadedExecutor().

std::shared_ptr<ThreadFactory> folly::ThreadedExecutor::threadFactory_
private

Definition at line 81 of file ThreadedExecutor.h.

Referenced by controlLaunchEnqueuedTasks().


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