xref: /aosp_15_r20/art/runtime/thread_pool.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker 
2*795d594fSAndroid Build Coastguard Worker /*
3*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2012 The Android Open Source Project
4*795d594fSAndroid Build Coastguard Worker  *
5*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
6*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
7*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
8*795d594fSAndroid Build Coastguard Worker  *
9*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
10*795d594fSAndroid Build Coastguard Worker  *
11*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
12*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
13*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
15*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
16*795d594fSAndroid Build Coastguard Worker  */
17*795d594fSAndroid Build Coastguard Worker 
18*795d594fSAndroid Build Coastguard Worker #include "thread_pool.h"
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <sys/mman.h>
21*795d594fSAndroid Build Coastguard Worker #include <sys/resource.h>
22*795d594fSAndroid Build Coastguard Worker #include <sys/time.h>
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include <pthread.h>
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
27*795d594fSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils.h"
30*795d594fSAndroid Build Coastguard Worker #include "base/casts.h"
31*795d594fSAndroid Build Coastguard Worker #include "base/stl_util.h"
32*795d594fSAndroid Build Coastguard Worker #include "base/time_utils.h"
33*795d594fSAndroid Build Coastguard Worker #include "base/utils.h"
34*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
35*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker using android::base::StringPrintf;
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker static constexpr bool kMeasureWaitTime = false;
42*795d594fSAndroid Build Coastguard Worker 
43*795d594fSAndroid Build Coastguard Worker #if defined(__BIONIC__)
44*795d594fSAndroid Build Coastguard Worker static constexpr bool kUseCustomThreadPoolStack = false;
45*795d594fSAndroid Build Coastguard Worker #else
46*795d594fSAndroid Build Coastguard Worker static constexpr bool kUseCustomThreadPoolStack = true;
47*795d594fSAndroid Build Coastguard Worker #endif
48*795d594fSAndroid Build Coastguard Worker 
ThreadPoolWorker(AbstractThreadPool * thread_pool,const std::string & name,size_t stack_size)49*795d594fSAndroid Build Coastguard Worker ThreadPoolWorker::ThreadPoolWorker(AbstractThreadPool* thread_pool,
50*795d594fSAndroid Build Coastguard Worker                                    const std::string& name,
51*795d594fSAndroid Build Coastguard Worker                                    size_t stack_size)
52*795d594fSAndroid Build Coastguard Worker     : thread_pool_(thread_pool),
53*795d594fSAndroid Build Coastguard Worker       name_(name) {
54*795d594fSAndroid Build Coastguard Worker   std::string error_msg;
55*795d594fSAndroid Build Coastguard Worker   // On Bionic, we know pthreads will give us a big-enough stack with
56*795d594fSAndroid Build Coastguard Worker   // a guard page, so don't do anything special on Bionic libc.
57*795d594fSAndroid Build Coastguard Worker   if (kUseCustomThreadPoolStack) {
58*795d594fSAndroid Build Coastguard Worker     // Add an inaccessible page to catch stack overflow.
59*795d594fSAndroid Build Coastguard Worker     stack_size += gPageSize;
60*795d594fSAndroid Build Coastguard Worker     stack_ = MemMap::MapAnonymous(name.c_str(),
61*795d594fSAndroid Build Coastguard Worker                                   stack_size,
62*795d594fSAndroid Build Coastguard Worker                                   PROT_READ | PROT_WRITE,
63*795d594fSAndroid Build Coastguard Worker                                   /*low_4gb=*/ false,
64*795d594fSAndroid Build Coastguard Worker                                   &error_msg);
65*795d594fSAndroid Build Coastguard Worker     CHECK(stack_.IsValid()) << error_msg;
66*795d594fSAndroid Build Coastguard Worker     CHECK_ALIGNED_PARAM(stack_.Begin(), gPageSize);
67*795d594fSAndroid Build Coastguard Worker     CheckedCall(mprotect,
68*795d594fSAndroid Build Coastguard Worker                 "mprotect bottom page of thread pool worker stack",
69*795d594fSAndroid Build Coastguard Worker                 stack_.Begin(),
70*795d594fSAndroid Build Coastguard Worker                 gPageSize,
71*795d594fSAndroid Build Coastguard Worker                 PROT_NONE);
72*795d594fSAndroid Build Coastguard Worker   }
73*795d594fSAndroid Build Coastguard Worker   const char* reason = "new thread pool worker thread";
74*795d594fSAndroid Build Coastguard Worker   pthread_attr_t attr;
75*795d594fSAndroid Build Coastguard Worker   CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), reason);
76*795d594fSAndroid Build Coastguard Worker   if (kUseCustomThreadPoolStack) {
77*795d594fSAndroid Build Coastguard Worker     CHECK_PTHREAD_CALL(pthread_attr_setstack, (&attr, stack_.Begin(), stack_.Size()), reason);
78*795d594fSAndroid Build Coastguard Worker   } else {
79*795d594fSAndroid Build Coastguard Worker     CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), reason);
80*795d594fSAndroid Build Coastguard Worker   }
81*795d594fSAndroid Build Coastguard Worker   CHECK_PTHREAD_CALL(pthread_create, (&pthread_, &attr, &Callback, this), reason);
82*795d594fSAndroid Build Coastguard Worker   CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), reason);
83*795d594fSAndroid Build Coastguard Worker }
84*795d594fSAndroid Build Coastguard Worker 
~ThreadPoolWorker()85*795d594fSAndroid Build Coastguard Worker ThreadPoolWorker::~ThreadPoolWorker() {
86*795d594fSAndroid Build Coastguard Worker   CHECK_PTHREAD_CALL(pthread_join, (pthread_, nullptr), "thread pool worker shutdown");
87*795d594fSAndroid Build Coastguard Worker }
88*795d594fSAndroid Build Coastguard Worker 
89*795d594fSAndroid Build Coastguard Worker // Set the "nice" priority for tid (0 means self).
SetPriorityForTid(pid_t tid,int priority)90*795d594fSAndroid Build Coastguard Worker static void SetPriorityForTid(pid_t tid, int priority) {
91*795d594fSAndroid Build Coastguard Worker   CHECK_GE(priority, PRIO_MIN);
92*795d594fSAndroid Build Coastguard Worker   CHECK_LE(priority, PRIO_MAX);
93*795d594fSAndroid Build Coastguard Worker   int result = setpriority(PRIO_PROCESS, tid, priority);
94*795d594fSAndroid Build Coastguard Worker   if (result != 0) {
95*795d594fSAndroid Build Coastguard Worker #if defined(ART_TARGET_ANDROID)
96*795d594fSAndroid Build Coastguard Worker     PLOG(WARNING) << "Failed to setpriority to :" << priority;
97*795d594fSAndroid Build Coastguard Worker #endif
98*795d594fSAndroid Build Coastguard Worker     // Setpriority may fail on host due to ulimit issues.
99*795d594fSAndroid Build Coastguard Worker   }
100*795d594fSAndroid Build Coastguard Worker }
101*795d594fSAndroid Build Coastguard Worker 
SetPthreadPriority(int priority)102*795d594fSAndroid Build Coastguard Worker void ThreadPoolWorker::SetPthreadPriority(int priority) {
103*795d594fSAndroid Build Coastguard Worker #if defined(ART_TARGET_ANDROID)
104*795d594fSAndroid Build Coastguard Worker   SetPriorityForTid(pthread_gettid_np(pthread_), priority);
105*795d594fSAndroid Build Coastguard Worker #else
106*795d594fSAndroid Build Coastguard Worker   UNUSED(priority);
107*795d594fSAndroid Build Coastguard Worker #endif
108*795d594fSAndroid Build Coastguard Worker }
109*795d594fSAndroid Build Coastguard Worker 
GetPthreadPriority()110*795d594fSAndroid Build Coastguard Worker int ThreadPoolWorker::GetPthreadPriority() {
111*795d594fSAndroid Build Coastguard Worker #if defined(ART_TARGET_ANDROID)
112*795d594fSAndroid Build Coastguard Worker   return getpriority(PRIO_PROCESS, pthread_gettid_np(pthread_));
113*795d594fSAndroid Build Coastguard Worker #else
114*795d594fSAndroid Build Coastguard Worker   return 0;
115*795d594fSAndroid Build Coastguard Worker #endif
116*795d594fSAndroid Build Coastguard Worker }
117*795d594fSAndroid Build Coastguard Worker 
Run()118*795d594fSAndroid Build Coastguard Worker void ThreadPoolWorker::Run() {
119*795d594fSAndroid Build Coastguard Worker   Thread* self = Thread::Current();
120*795d594fSAndroid Build Coastguard Worker   Task* task = nullptr;
121*795d594fSAndroid Build Coastguard Worker   thread_pool_->creation_barier_.Pass(self);
122*795d594fSAndroid Build Coastguard Worker   while ((task = thread_pool_->GetTask(self)) != nullptr) {
123*795d594fSAndroid Build Coastguard Worker     task->Run(self);
124*795d594fSAndroid Build Coastguard Worker     task->Finalize();
125*795d594fSAndroid Build Coastguard Worker   }
126*795d594fSAndroid Build Coastguard Worker }
127*795d594fSAndroid Build Coastguard Worker 
Callback(void * arg)128*795d594fSAndroid Build Coastguard Worker void* ThreadPoolWorker::Callback(void* arg) {
129*795d594fSAndroid Build Coastguard Worker   ThreadPoolWorker* worker = reinterpret_cast<ThreadPoolWorker*>(arg);
130*795d594fSAndroid Build Coastguard Worker   Runtime* runtime = Runtime::Current();
131*795d594fSAndroid Build Coastguard Worker   // Don't run callbacks for ThreadPoolWorkers. These are created for JITThreadPool and
132*795d594fSAndroid Build Coastguard Worker   // HeapThreadPool and are purely internal threads of the runtime and we don't need to run
133*795d594fSAndroid Build Coastguard Worker   // callbacks for the thread attach / detach listeners.
134*795d594fSAndroid Build Coastguard Worker   // (b/251163712) Calling callbacks for heap thread pool workers causes deadlocks in some libjdwp
135*795d594fSAndroid Build Coastguard Worker   // tests. Deadlocks happen when a GC thread is attached while libjdwp holds the event handler
136*795d594fSAndroid Build Coastguard Worker   // lock for an event that triggers an entrypoint update from deopt manager.
137*795d594fSAndroid Build Coastguard Worker   CHECK(runtime->AttachCurrentThread(
138*795d594fSAndroid Build Coastguard Worker       worker->name_.c_str(),
139*795d594fSAndroid Build Coastguard Worker       true,
140*795d594fSAndroid Build Coastguard Worker       // Thread-groups are only tracked by the peer j.l.Thread objects. If we aren't creating peers
141*795d594fSAndroid Build Coastguard Worker       // we don't need to specify the thread group. We want to place these threads in the System
142*795d594fSAndroid Build Coastguard Worker       // thread group because that thread group is where important threads that debuggers and
143*795d594fSAndroid Build Coastguard Worker       // similar tools should not mess with are placed. As this is an internal-thread-pool we might
144*795d594fSAndroid Build Coastguard Worker       // rely on being able to (for example) wait for all threads to finish some task. If debuggers
145*795d594fSAndroid Build Coastguard Worker       // are suspending these threads that might not be possible.
146*795d594fSAndroid Build Coastguard Worker       worker->thread_pool_->create_peers_ ? runtime->GetSystemThreadGroup() : nullptr,
147*795d594fSAndroid Build Coastguard Worker       worker->thread_pool_->create_peers_,
148*795d594fSAndroid Build Coastguard Worker       /* should_run_callbacks= */ false));
149*795d594fSAndroid Build Coastguard Worker   worker->thread_ = Thread::Current();
150*795d594fSAndroid Build Coastguard Worker   // Mark thread pool workers as runtime-threads.
151*795d594fSAndroid Build Coastguard Worker   worker->thread_->SetIsRuntimeThread(true);
152*795d594fSAndroid Build Coastguard Worker   // Do work until its time to shut down.
153*795d594fSAndroid Build Coastguard Worker   worker->Run();
154*795d594fSAndroid Build Coastguard Worker   runtime->DetachCurrentThread(/* should_run_callbacks= */ false);
155*795d594fSAndroid Build Coastguard Worker   // On zygote fork, we wait for this thread to exit completely. Set to highest Java priority
156*795d594fSAndroid Build Coastguard Worker   // to speed that up.
157*795d594fSAndroid Build Coastguard Worker   constexpr int kJavaMaxPrioNiceness = -8;
158*795d594fSAndroid Build Coastguard Worker   SetPriorityForTid(0 /* this thread */, kJavaMaxPrioNiceness);
159*795d594fSAndroid Build Coastguard Worker   return nullptr;
160*795d594fSAndroid Build Coastguard Worker }
161*795d594fSAndroid Build Coastguard Worker 
AddTask(Thread * self,Task * task)162*795d594fSAndroid Build Coastguard Worker void ThreadPool::AddTask(Thread* self, Task* task) {
163*795d594fSAndroid Build Coastguard Worker   MutexLock mu(self, task_queue_lock_);
164*795d594fSAndroid Build Coastguard Worker   tasks_.push_back(task);
165*795d594fSAndroid Build Coastguard Worker   // If we have any waiters, signal one.
166*795d594fSAndroid Build Coastguard Worker   if (started_ && waiting_count_ != 0) {
167*795d594fSAndroid Build Coastguard Worker     task_queue_condition_.Signal(self);
168*795d594fSAndroid Build Coastguard Worker   }
169*795d594fSAndroid Build Coastguard Worker }
170*795d594fSAndroid Build Coastguard Worker 
RemoveAllTasks(Thread * self)171*795d594fSAndroid Build Coastguard Worker void ThreadPool::RemoveAllTasks(Thread* self) {
172*795d594fSAndroid Build Coastguard Worker   // The ThreadPool is responsible for calling Finalize (which usually delete
173*795d594fSAndroid Build Coastguard Worker   // the task memory) on all the tasks.
174*795d594fSAndroid Build Coastguard Worker   Task* task = nullptr;
175*795d594fSAndroid Build Coastguard Worker   do {
176*795d594fSAndroid Build Coastguard Worker     {
177*795d594fSAndroid Build Coastguard Worker       MutexLock mu(self, task_queue_lock_);
178*795d594fSAndroid Build Coastguard Worker       if (tasks_.empty()) {
179*795d594fSAndroid Build Coastguard Worker         return;
180*795d594fSAndroid Build Coastguard Worker       }
181*795d594fSAndroid Build Coastguard Worker       task = tasks_.front();
182*795d594fSAndroid Build Coastguard Worker       tasks_.pop_front();
183*795d594fSAndroid Build Coastguard Worker     }
184*795d594fSAndroid Build Coastguard Worker     task->Finalize();
185*795d594fSAndroid Build Coastguard Worker   } while (true);
186*795d594fSAndroid Build Coastguard Worker }
187*795d594fSAndroid Build Coastguard Worker 
~ThreadPool()188*795d594fSAndroid Build Coastguard Worker ThreadPool::~ThreadPool() {
189*795d594fSAndroid Build Coastguard Worker   DeleteThreads();
190*795d594fSAndroid Build Coastguard Worker   RemoveAllTasks(Thread::Current());
191*795d594fSAndroid Build Coastguard Worker }
192*795d594fSAndroid Build Coastguard Worker 
AbstractThreadPool(const char * name,size_t num_threads,bool create_peers,size_t worker_stack_size)193*795d594fSAndroid Build Coastguard Worker AbstractThreadPool::AbstractThreadPool(const char* name,
194*795d594fSAndroid Build Coastguard Worker                                        size_t num_threads,
195*795d594fSAndroid Build Coastguard Worker                                        bool create_peers,
196*795d594fSAndroid Build Coastguard Worker                                        size_t worker_stack_size)
197*795d594fSAndroid Build Coastguard Worker   : name_(name),
198*795d594fSAndroid Build Coastguard Worker     task_queue_lock_("task queue lock", kGenericBottomLock),
199*795d594fSAndroid Build Coastguard Worker     task_queue_condition_("task queue condition", task_queue_lock_),
200*795d594fSAndroid Build Coastguard Worker     completion_condition_("task completion condition", task_queue_lock_),
201*795d594fSAndroid Build Coastguard Worker     started_(false),
202*795d594fSAndroid Build Coastguard Worker     shutting_down_(false),
203*795d594fSAndroid Build Coastguard Worker     waiting_count_(0),
204*795d594fSAndroid Build Coastguard Worker     start_time_(0),
205*795d594fSAndroid Build Coastguard Worker     total_wait_time_(0),
206*795d594fSAndroid Build Coastguard Worker     creation_barier_(0),
207*795d594fSAndroid Build Coastguard Worker     max_active_workers_(num_threads),
208*795d594fSAndroid Build Coastguard Worker     create_peers_(create_peers),
209*795d594fSAndroid Build Coastguard Worker     worker_stack_size_(worker_stack_size) {}
210*795d594fSAndroid Build Coastguard Worker 
CreateThreads()211*795d594fSAndroid Build Coastguard Worker void AbstractThreadPool::CreateThreads() {
212*795d594fSAndroid Build Coastguard Worker   CHECK(threads_.empty());
213*795d594fSAndroid Build Coastguard Worker   Thread* self = Thread::Current();
214*795d594fSAndroid Build Coastguard Worker   {
215*795d594fSAndroid Build Coastguard Worker     MutexLock mu(self, task_queue_lock_);
216*795d594fSAndroid Build Coastguard Worker     shutting_down_ = false;
217*795d594fSAndroid Build Coastguard Worker     // Add one since the caller of constructor waits on the barrier too.
218*795d594fSAndroid Build Coastguard Worker     creation_barier_.Init(self, max_active_workers_);
219*795d594fSAndroid Build Coastguard Worker     while (GetThreadCount() < max_active_workers_) {
220*795d594fSAndroid Build Coastguard Worker       const std::string worker_name = StringPrintf("%s worker thread %zu", name_.c_str(),
221*795d594fSAndroid Build Coastguard Worker                                                    GetThreadCount());
222*795d594fSAndroid Build Coastguard Worker       threads_.push_back(
223*795d594fSAndroid Build Coastguard Worker           new ThreadPoolWorker(this, worker_name, worker_stack_size_));
224*795d594fSAndroid Build Coastguard Worker     }
225*795d594fSAndroid Build Coastguard Worker   }
226*795d594fSAndroid Build Coastguard Worker }
227*795d594fSAndroid Build Coastguard Worker 
WaitForWorkersToBeCreated()228*795d594fSAndroid Build Coastguard Worker void AbstractThreadPool::WaitForWorkersToBeCreated() {
229*795d594fSAndroid Build Coastguard Worker   creation_barier_.Increment(Thread::Current(), 0);
230*795d594fSAndroid Build Coastguard Worker }
231*795d594fSAndroid Build Coastguard Worker 
GetWorkers()232*795d594fSAndroid Build Coastguard Worker const std::vector<ThreadPoolWorker*>& AbstractThreadPool::GetWorkers() {
233*795d594fSAndroid Build Coastguard Worker   // Wait for all the workers to be created before returning them.
234*795d594fSAndroid Build Coastguard Worker   WaitForWorkersToBeCreated();
235*795d594fSAndroid Build Coastguard Worker   return threads_;
236*795d594fSAndroid Build Coastguard Worker }
237*795d594fSAndroid Build Coastguard Worker 
DeleteThreads()238*795d594fSAndroid Build Coastguard Worker void AbstractThreadPool::DeleteThreads() {
239*795d594fSAndroid Build Coastguard Worker   {
240*795d594fSAndroid Build Coastguard Worker     Thread* self = Thread::Current();
241*795d594fSAndroid Build Coastguard Worker     MutexLock mu(self, task_queue_lock_);
242*795d594fSAndroid Build Coastguard Worker     // Tell any remaining workers to shut down.
243*795d594fSAndroid Build Coastguard Worker     shutting_down_ = true;
244*795d594fSAndroid Build Coastguard Worker     // Broadcast to everyone waiting.
245*795d594fSAndroid Build Coastguard Worker     task_queue_condition_.Broadcast(self);
246*795d594fSAndroid Build Coastguard Worker     completion_condition_.Broadcast(self);
247*795d594fSAndroid Build Coastguard Worker   }
248*795d594fSAndroid Build Coastguard Worker   // Wait for the threads to finish. We expect the user of the pool
249*795d594fSAndroid Build Coastguard Worker   // not to run multi-threaded calls to `CreateThreads` and `DeleteThreads`,
250*795d594fSAndroid Build Coastguard Worker   // so we don't guard the field here.
251*795d594fSAndroid Build Coastguard Worker   STLDeleteElements(&threads_);
252*795d594fSAndroid Build Coastguard Worker }
253*795d594fSAndroid Build Coastguard Worker 
SetMaxActiveWorkers(size_t max_workers)254*795d594fSAndroid Build Coastguard Worker void AbstractThreadPool::SetMaxActiveWorkers(size_t max_workers) {
255*795d594fSAndroid Build Coastguard Worker   MutexLock mu(Thread::Current(), task_queue_lock_);
256*795d594fSAndroid Build Coastguard Worker   CHECK_LE(max_workers, GetThreadCount());
257*795d594fSAndroid Build Coastguard Worker   max_active_workers_ = max_workers;
258*795d594fSAndroid Build Coastguard Worker }
259*795d594fSAndroid Build Coastguard Worker 
StartWorkers(Thread * self)260*795d594fSAndroid Build Coastguard Worker void AbstractThreadPool::StartWorkers(Thread* self) {
261*795d594fSAndroid Build Coastguard Worker   MutexLock mu(self, task_queue_lock_);
262*795d594fSAndroid Build Coastguard Worker   started_ = true;
263*795d594fSAndroid Build Coastguard Worker   task_queue_condition_.Broadcast(self);
264*795d594fSAndroid Build Coastguard Worker   start_time_ = NanoTime();
265*795d594fSAndroid Build Coastguard Worker   total_wait_time_ = 0;
266*795d594fSAndroid Build Coastguard Worker }
267*795d594fSAndroid Build Coastguard Worker 
StopWorkers(Thread * self)268*795d594fSAndroid Build Coastguard Worker void AbstractThreadPool::StopWorkers(Thread* self) {
269*795d594fSAndroid Build Coastguard Worker   MutexLock mu(self, task_queue_lock_);
270*795d594fSAndroid Build Coastguard Worker   started_ = false;
271*795d594fSAndroid Build Coastguard Worker }
272*795d594fSAndroid Build Coastguard Worker 
HasStarted(Thread * self)273*795d594fSAndroid Build Coastguard Worker bool AbstractThreadPool::HasStarted(Thread* self) {
274*795d594fSAndroid Build Coastguard Worker   MutexLock mu(self, task_queue_lock_);
275*795d594fSAndroid Build Coastguard Worker   return started_;
276*795d594fSAndroid Build Coastguard Worker }
277*795d594fSAndroid Build Coastguard Worker 
GetTask(Thread * self)278*795d594fSAndroid Build Coastguard Worker Task* AbstractThreadPool::GetTask(Thread* self) {
279*795d594fSAndroid Build Coastguard Worker   MutexLock mu(self, task_queue_lock_);
280*795d594fSAndroid Build Coastguard Worker   while (!IsShuttingDown()) {
281*795d594fSAndroid Build Coastguard Worker     const size_t thread_count = GetThreadCount();
282*795d594fSAndroid Build Coastguard Worker     // Ensure that we don't use more threads than the maximum active workers.
283*795d594fSAndroid Build Coastguard Worker     const size_t active_threads = thread_count - waiting_count_;
284*795d594fSAndroid Build Coastguard Worker     // <= since self is considered an active worker.
285*795d594fSAndroid Build Coastguard Worker     if (active_threads <= max_active_workers_) {
286*795d594fSAndroid Build Coastguard Worker       Task* task = TryGetTaskLocked();
287*795d594fSAndroid Build Coastguard Worker       if (task != nullptr) {
288*795d594fSAndroid Build Coastguard Worker         return task;
289*795d594fSAndroid Build Coastguard Worker       }
290*795d594fSAndroid Build Coastguard Worker     }
291*795d594fSAndroid Build Coastguard Worker 
292*795d594fSAndroid Build Coastguard Worker     ++waiting_count_;
293*795d594fSAndroid Build Coastguard Worker     if (waiting_count_ == GetThreadCount() && !HasOutstandingTasks()) {
294*795d594fSAndroid Build Coastguard Worker       // We may be done, lets broadcast to the completion condition.
295*795d594fSAndroid Build Coastguard Worker       completion_condition_.Broadcast(self);
296*795d594fSAndroid Build Coastguard Worker     }
297*795d594fSAndroid Build Coastguard Worker     const uint64_t wait_start = kMeasureWaitTime ? NanoTime() : 0;
298*795d594fSAndroid Build Coastguard Worker     task_queue_condition_.Wait(self);
299*795d594fSAndroid Build Coastguard Worker     if (kMeasureWaitTime) {
300*795d594fSAndroid Build Coastguard Worker       const uint64_t wait_end = NanoTime();
301*795d594fSAndroid Build Coastguard Worker       total_wait_time_ += wait_end - std::max(wait_start, start_time_);
302*795d594fSAndroid Build Coastguard Worker     }
303*795d594fSAndroid Build Coastguard Worker     --waiting_count_;
304*795d594fSAndroid Build Coastguard Worker   }
305*795d594fSAndroid Build Coastguard Worker 
306*795d594fSAndroid Build Coastguard Worker   // We are shutting down, return null to tell the worker thread to stop looping.
307*795d594fSAndroid Build Coastguard Worker   return nullptr;
308*795d594fSAndroid Build Coastguard Worker }
309*795d594fSAndroid Build Coastguard Worker 
TryGetTask(Thread * self)310*795d594fSAndroid Build Coastguard Worker Task* AbstractThreadPool::TryGetTask(Thread* self) {
311*795d594fSAndroid Build Coastguard Worker   MutexLock mu(self, task_queue_lock_);
312*795d594fSAndroid Build Coastguard Worker   return TryGetTaskLocked();
313*795d594fSAndroid Build Coastguard Worker }
314*795d594fSAndroid Build Coastguard Worker 
TryGetTaskLocked()315*795d594fSAndroid Build Coastguard Worker Task* ThreadPool::TryGetTaskLocked() {
316*795d594fSAndroid Build Coastguard Worker   if (HasOutstandingTasks()) {
317*795d594fSAndroid Build Coastguard Worker     Task* task = tasks_.front();
318*795d594fSAndroid Build Coastguard Worker     tasks_.pop_front();
319*795d594fSAndroid Build Coastguard Worker     return task;
320*795d594fSAndroid Build Coastguard Worker   }
321*795d594fSAndroid Build Coastguard Worker   return nullptr;
322*795d594fSAndroid Build Coastguard Worker }
323*795d594fSAndroid Build Coastguard Worker 
Wait(Thread * self,bool do_work,bool may_hold_locks)324*795d594fSAndroid Build Coastguard Worker void AbstractThreadPool::Wait(Thread* self, bool do_work, bool may_hold_locks) {
325*795d594fSAndroid Build Coastguard Worker   if (do_work) {
326*795d594fSAndroid Build Coastguard Worker     CHECK(!create_peers_);
327*795d594fSAndroid Build Coastguard Worker     Task* task = nullptr;
328*795d594fSAndroid Build Coastguard Worker     while ((task = TryGetTask(self)) != nullptr) {
329*795d594fSAndroid Build Coastguard Worker       task->Run(self);
330*795d594fSAndroid Build Coastguard Worker       task->Finalize();
331*795d594fSAndroid Build Coastguard Worker     }
332*795d594fSAndroid Build Coastguard Worker   }
333*795d594fSAndroid Build Coastguard Worker   // Wait until each thread is waiting and the task list is empty.
334*795d594fSAndroid Build Coastguard Worker   MutexLock mu(self, task_queue_lock_);
335*795d594fSAndroid Build Coastguard Worker   while (!shutting_down_ && (waiting_count_ != GetThreadCount() || HasOutstandingTasks())) {
336*795d594fSAndroid Build Coastguard Worker     if (!may_hold_locks) {
337*795d594fSAndroid Build Coastguard Worker       completion_condition_.Wait(self);
338*795d594fSAndroid Build Coastguard Worker     } else {
339*795d594fSAndroid Build Coastguard Worker       completion_condition_.WaitHoldingLocks(self);
340*795d594fSAndroid Build Coastguard Worker     }
341*795d594fSAndroid Build Coastguard Worker   }
342*795d594fSAndroid Build Coastguard Worker }
343*795d594fSAndroid Build Coastguard Worker 
GetTaskCount(Thread * self)344*795d594fSAndroid Build Coastguard Worker size_t ThreadPool::GetTaskCount(Thread* self) {
345*795d594fSAndroid Build Coastguard Worker   MutexLock mu(self, task_queue_lock_);
346*795d594fSAndroid Build Coastguard Worker   return tasks_.size();
347*795d594fSAndroid Build Coastguard Worker }
348*795d594fSAndroid Build Coastguard Worker 
SetPthreadPriority(int priority)349*795d594fSAndroid Build Coastguard Worker void AbstractThreadPool::SetPthreadPriority(int priority) {
350*795d594fSAndroid Build Coastguard Worker   for (ThreadPoolWorker* worker : threads_) {
351*795d594fSAndroid Build Coastguard Worker     worker->SetPthreadPriority(priority);
352*795d594fSAndroid Build Coastguard Worker   }
353*795d594fSAndroid Build Coastguard Worker }
354*795d594fSAndroid Build Coastguard Worker 
CheckPthreadPriority(int priority)355*795d594fSAndroid Build Coastguard Worker void AbstractThreadPool::CheckPthreadPriority(int priority) {
356*795d594fSAndroid Build Coastguard Worker #if defined(ART_TARGET_ANDROID)
357*795d594fSAndroid Build Coastguard Worker   for (ThreadPoolWorker* worker : threads_) {
358*795d594fSAndroid Build Coastguard Worker     CHECK_EQ(worker->GetPthreadPriority(), priority);
359*795d594fSAndroid Build Coastguard Worker   }
360*795d594fSAndroid Build Coastguard Worker #else
361*795d594fSAndroid Build Coastguard Worker   UNUSED(priority);
362*795d594fSAndroid Build Coastguard Worker #endif
363*795d594fSAndroid Build Coastguard Worker }
364*795d594fSAndroid Build Coastguard Worker 
365*795d594fSAndroid Build Coastguard Worker }  // namespace art
366