Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Member Functions | Protected Member Functions | List of all members
dart::ThreadPool Class Reference

#include <thread_pool.h>

Inheritance diagram for dart::ThreadPool:
dart::MutatorThreadPool

Classes

class  Task
 

Public Member Functions

 ThreadPool (uintptr_t max_pool_size=0)
 
virtual ~ThreadPool ()
 
template<typename T , typename... Args>
bool Run (Args &&... args)
 
bool CurrentThreadIsWorker ()
 
void MarkCurrentWorkerAsBlocked ()
 
void MarkCurrentWorkerAsUnBlocked ()
 
void Shutdown ()
 
uint64_t workers_started () const
 
uint64_t workers_stopped () const
 

Protected Member Functions

virtual void OnEnterIdleLocked (MonitorLocker *ml)
 
bool ShuttingDownLocked ()
 
bool TasksWaitingToRunLocked ()
 

Detailed Description

Definition at line 20 of file thread_pool.h.

Constructor & Destructor Documentation

◆ ThreadPool()

dart::ThreadPool::ThreadPool ( uintptr_t  max_pool_size = 0)
explicit

Definition at line 37 of file thread_pool.cc.

38 : all_workers_dead_(false), max_pool_size_(max_pool_size) {}

◆ ~ThreadPool()

dart::ThreadPool::~ThreadPool ( )
virtual

Definition at line 40 of file thread_pool.cc.

40 {
41 Shutdown();
42}

Member Function Documentation

◆ CurrentThreadIsWorker()

bool dart::ThreadPool::CurrentThreadIsWorker ( )

Definition at line 99 of file thread_pool.cc.

99 {
100 auto worker =
101 static_cast<Worker*>(OSThread::Current()->owning_thread_pool_worker_);
102 return worker != nullptr && worker->pool_ == this;
103}
static OSThread * Current()
Definition os_thread.h:175

◆ MarkCurrentWorkerAsBlocked()

void dart::ThreadPool::MarkCurrentWorkerAsBlocked ( )

Definition at line 105 of file thread_pool.cc.

105 {
106 auto worker =
107 static_cast<Worker*>(OSThread::Current()->owning_thread_pool_worker_);
108 Worker* new_worker = nullptr;
109 if (worker != nullptr) {
110 MonitorLocker ml(&pool_monitor_);
111 ASSERT(!worker->is_blocked_);
112 worker->is_blocked_ = true;
113 if (max_pool_size_ > 0) {
114 ++max_pool_size_;
115 // This thread is blocked and therefore no longer usable as a worker.
116 // If we have pending tasks and there are no idle workers, we will spawn a
117 // new thread (temporarily allow exceeding the maximum pool size) to
118 // handle the pending tasks.
119 if (idle_workers_.IsEmpty() && pending_tasks_ > 0) {
120 new_worker = new Worker(this);
121 idle_workers_.Append(new_worker);
122 count_idle_++;
123 }
124 }
125 }
126 if (new_worker != nullptr) {
127 new_worker->StartThread();
128 }
129}
#define ASSERT(E)

◆ MarkCurrentWorkerAsUnBlocked()

void dart::ThreadPool::MarkCurrentWorkerAsUnBlocked ( )

Definition at line 131 of file thread_pool.cc.

131 {
132 auto worker =
133 static_cast<Worker*>(OSThread::Current()->owning_thread_pool_worker_);
134 if (worker != nullptr) {
135 MonitorLocker ml(&pool_monitor_);
136 if (worker->is_blocked_) {
137 worker->is_blocked_ = false;
138 if (max_pool_size_ > 0) {
139 --max_pool_size_;
140 ASSERT(max_pool_size_ > 0);
141 }
142 }
143 }
144}

◆ OnEnterIdleLocked()

virtual void dart::ThreadPool::OnEnterIdleLocked ( MonitorLocker ml)
inlineprotectedvirtual

Reimplemented in dart::MutatorThreadPool.

Definition at line 99 of file thread_pool.h.

99{}

◆ Run()

template<typename T , typename... Args>
bool dart::ThreadPool::Run ( Args &&...  args)
inline

Definition at line 45 of file thread_pool.h.

45 {
46 return RunImpl(std::unique_ptr<Task>(new T(std::forward<Args>(args)...)));
47 }
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
#define T

◆ Shutdown()

void dart::ThreadPool::Shutdown ( )

Definition at line 44 of file thread_pool.cc.

44 {
45 {
46 MonitorLocker ml(&pool_monitor_);
47
48 // Prevent scheduling of new tasks.
49 shutting_down_ = true;
50
51 if (running_workers_.IsEmpty() && idle_workers_.IsEmpty()) {
52 // All workers have already died.
53 all_workers_dead_ = true;
54 } else {
55 // Tell workers to drain remaining work and then shut down.
56 ml.NotifyAll();
57 }
58 }
59
60 // Wait until all workers are dead. Any new death will notify the exit
61 // monitor.
62 {
63 MonitorLocker eml(&exit_monitor_);
64 while (!all_workers_dead_) {
65 eml.Wait();
66 }
67 }
68 ASSERT(count_idle_ == 0);
69 ASSERT(count_running_ == 0);
70 ASSERT(idle_workers_.IsEmpty());
71 ASSERT(running_workers_.IsEmpty());
72
73 WorkerList dead_workers_to_join;
74 {
75 MonitorLocker ml(&pool_monitor_);
76 ObtainDeadWorkersLocked(&dead_workers_to_join);
77 }
78 JoinDeadWorkersLocked(&dead_workers_to_join);
79
80 ASSERT(count_dead_ == 0);
81 ASSERT(dead_workers_.IsEmpty());
82}

◆ ShuttingDownLocked()

bool dart::ThreadPool::ShuttingDownLocked ( )
inlineprotected

Definition at line 102 of file thread_pool.h.

102{ return shutting_down_; }

◆ TasksWaitingToRunLocked()

bool dart::ThreadPool::TasksWaitingToRunLocked ( )
inlineprotected

Definition at line 105 of file thread_pool.h.

105{ return !tasks_.IsEmpty(); }

◆ workers_started()

uint64_t dart::ThreadPool::workers_started ( ) const
inline

Definition at line 65 of file thread_pool.h.

65{ return count_idle_ + count_running_; }

◆ workers_stopped()

uint64_t dart::ThreadPool::workers_stopped ( ) const
inline

Definition at line 67 of file thread_pool.h.

67{ return count_dead_; }

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