Mathieu Chartier | 93c21ba | 2018-12-10 13:08:30 -0800 | [diff] [blame] | 1 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 18 | #include "thread_pool.h" |
| 19 | |
Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 20 | #include <sys/mman.h> |
Andreas Gampe | 9e927f5 | 2016-02-29 20:49:38 -0800 | [diff] [blame] | 21 | #include <sys/resource.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 22 | #include <sys/time.h> |
| 23 | |
| 24 | #include <pthread.h> |
Andreas Gampe | 9e927f5 | 2016-02-29 20:49:38 -0800 | [diff] [blame] | 25 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 26 | #include <android-base/logging.h> |
| 27 | #include <android-base/stringprintf.h> |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 28 | |
Vladimir Marko | 0b6e283 | 2015-09-24 10:41:33 +0100 | [diff] [blame] | 29 | #include "base/bit_utils.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 30 | #include "base/casts.h" |
| 31 | #include "base/stl_util.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 32 | #include "base/time_utils.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 33 | #include "base/utils.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 34 | #include "runtime.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 35 | #include "thread-current-inl.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 36 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 37 | namespace art { |
| 38 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 39 | using android::base::StringPrintf; |
| 40 | |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 41 | static constexpr bool kMeasureWaitTime = false; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 42 | |
Daniel Colascione | 034ac51 | 2020-03-18 19:06:11 -0700 | [diff] [blame] | 43 | #if defined(__BIONIC__) |
| 44 | static constexpr bool kUseCustomThreadPoolStack = false; |
| 45 | #else |
| 46 | static constexpr bool kUseCustomThreadPoolStack = true; |
| 47 | #endif |
| 48 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 49 | ThreadPoolWorker::ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, |
| 50 | size_t stack_size) |
| 51 | : thread_pool_(thread_pool), |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 52 | name_(name) { |
| 53 | std::string error_msg; |
Daniel Colascione | 034ac51 | 2020-03-18 19:06:11 -0700 | [diff] [blame] | 54 | // On Bionic, we know pthreads will give us a big-enough stack with |
| 55 | // a guard page, so don't do anything special on Bionic libc. |
| 56 | if (kUseCustomThreadPoolStack) { |
| 57 | // Add an inaccessible page to catch stack overflow. |
| 58 | stack_size += kPageSize; |
| 59 | stack_ = MemMap::MapAnonymous(name.c_str(), |
| 60 | stack_size, |
| 61 | PROT_READ | PROT_WRITE, |
| 62 | /*low_4gb=*/ false, |
| 63 | &error_msg); |
| 64 | CHECK(stack_.IsValid()) << error_msg; |
| 65 | CHECK_ALIGNED(stack_.Begin(), kPageSize); |
| 66 | CheckedCall(mprotect, |
| 67 | "mprotect bottom page of thread pool worker stack", |
| 68 | stack_.Begin(), |
| 69 | kPageSize, |
| 70 | PROT_NONE); |
| 71 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 72 | const char* reason = "new thread pool worker thread"; |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 73 | pthread_attr_t attr; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 74 | CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), reason); |
Daniel Colascione | 034ac51 | 2020-03-18 19:06:11 -0700 | [diff] [blame] | 75 | if (kUseCustomThreadPoolStack) { |
| 76 | CHECK_PTHREAD_CALL(pthread_attr_setstack, (&attr, stack_.Begin(), stack_.Size()), reason); |
| 77 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 78 | CHECK_PTHREAD_CALL(pthread_create, (&pthread_, &attr, &Callback, this), reason); |
| 79 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), reason); |
| 80 | } |
| 81 | |
| 82 | ThreadPoolWorker::~ThreadPoolWorker() { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 83 | CHECK_PTHREAD_CALL(pthread_join, (pthread_, nullptr), "thread pool worker shutdown"); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Andreas Gampe | 9e927f5 | 2016-02-29 20:49:38 -0800 | [diff] [blame] | 86 | void ThreadPoolWorker::SetPthreadPriority(int priority) { |
| 87 | CHECK_GE(priority, PRIO_MIN); |
| 88 | CHECK_LE(priority, PRIO_MAX); |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 89 | #if defined(ART_TARGET_ANDROID) |
Andreas Gampe | 9e927f5 | 2016-02-29 20:49:38 -0800 | [diff] [blame] | 90 | int result = setpriority(PRIO_PROCESS, pthread_gettid_np(pthread_), priority); |
| 91 | if (result != 0) { |
| 92 | PLOG(ERROR) << "Failed to setpriority to :" << priority; |
| 93 | } |
| 94 | #else |
| 95 | UNUSED(priority); |
| 96 | #endif |
| 97 | } |
| 98 | |
Nicolas Geoffray | f9dbb97 | 2020-08-27 15:21:11 +0100 | [diff] [blame] | 99 | int ThreadPoolWorker::GetPthreadPriority() { |
| 100 | #if defined(ART_TARGET_ANDROID) |
| 101 | return getpriority(PRIO_PROCESS, pthread_gettid_np(pthread_)); |
| 102 | #else |
| 103 | return 0; |
| 104 | #endif |
| 105 | } |
| 106 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 107 | void ThreadPoolWorker::Run() { |
| 108 | Thread* self = Thread::Current(); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 109 | Task* task = nullptr; |
Mathieu Chartier | 93c21ba | 2018-12-10 13:08:30 -0800 | [diff] [blame] | 110 | thread_pool_->creation_barier_.Pass(self); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 111 | while ((task = thread_pool_->GetTask(self)) != nullptr) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 112 | task->Run(self); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 113 | task->Finalize(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | void* ThreadPoolWorker::Callback(void* arg) { |
| 118 | ThreadPoolWorker* worker = reinterpret_cast<ThreadPoolWorker*>(arg); |
| 119 | Runtime* runtime = Runtime::Current(); |
Alex Light | 80777ed | 2019-12-17 17:07:33 +0000 | [diff] [blame] | 120 | CHECK(runtime->AttachCurrentThread( |
| 121 | worker->name_.c_str(), |
| 122 | true, |
| 123 | // Thread-groups are only tracked by the peer j.l.Thread objects. If we aren't creating peers |
| 124 | // we don't need to specify the thread group. We want to place these threads in the System |
| 125 | // thread group because that thread group is where important threads that debuggers and |
| 126 | // similar tools should not mess with are placed. As this is an internal-thread-pool we might |
| 127 | // rely on being able to (for example) wait for all threads to finish some task. If debuggers |
| 128 | // are suspending these threads that might not be possible. |
| 129 | worker->thread_pool_->create_peers_ ? runtime->GetSystemThreadGroup() : nullptr, |
| 130 | worker->thread_pool_->create_peers_)); |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 131 | worker->thread_ = Thread::Current(); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 132 | // Mark thread pool workers as runtime-threads. |
| 133 | worker->thread_->SetIsRuntimeThread(true); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 134 | // Do work until its time to shut down. |
| 135 | worker->Run(); |
| 136 | runtime->DetachCurrentThread(); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 137 | return nullptr; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 140 | void ThreadPool::AddTask(Thread* self, Task* task) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 141 | MutexLock mu(self, task_queue_lock_); |
| 142 | tasks_.push_back(task); |
| 143 | // If we have any waiters, signal one. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 144 | if (started_ && waiting_count_ != 0) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 145 | task_queue_condition_.Signal(self); |
| 146 | } |
| 147 | } |
| 148 | |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 149 | void ThreadPool::RemoveAllTasks(Thread* self) { |
Nicolas Geoffray | 714fad6 | 2019-06-27 15:32:00 +0100 | [diff] [blame] | 150 | // The ThreadPool is responsible for calling Finalize (which usually delete |
| 151 | // the task memory) on all the tasks. |
| 152 | Task* task = nullptr; |
| 153 | while ((task = TryGetTask(self)) != nullptr) { |
| 154 | task->Finalize(); |
| 155 | } |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 156 | MutexLock mu(self, task_queue_lock_); |
| 157 | tasks_.clear(); |
| 158 | } |
| 159 | |
Mathieu Chartier | eac4d6a | 2018-12-05 12:33:46 -0800 | [diff] [blame] | 160 | ThreadPool::ThreadPool(const char* name, |
| 161 | size_t num_threads, |
| 162 | bool create_peers, |
| 163 | size_t worker_stack_size) |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 164 | : name_(name), |
| 165 | task_queue_lock_("task queue lock"), |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 166 | task_queue_condition_("task queue condition", task_queue_lock_), |
| 167 | completion_condition_("task completion condition", task_queue_lock_), |
| 168 | started_(false), |
| 169 | shutting_down_(false), |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 170 | waiting_count_(0), |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 171 | start_time_(0), |
| 172 | total_wait_time_(0), |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 173 | creation_barier_(0), |
Andreas Gampe | b15de0c | 2017-01-24 13:12:19 -0800 | [diff] [blame] | 174 | max_active_workers_(num_threads), |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 175 | create_peers_(create_peers), |
| 176 | worker_stack_size_(worker_stack_size) { |
| 177 | CreateThreads(); |
| 178 | } |
| 179 | |
| 180 | void ThreadPool::CreateThreads() { |
| 181 | CHECK(threads_.empty()); |
| 182 | Thread* self = Thread::Current(); |
| 183 | { |
| 184 | MutexLock mu(self, task_queue_lock_); |
| 185 | shutting_down_ = false; |
| 186 | // Add one since the caller of constructor waits on the barrier too. |
Mathieu Chartier | 93c21ba | 2018-12-10 13:08:30 -0800 | [diff] [blame] | 187 | creation_barier_.Init(self, max_active_workers_); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 188 | while (GetThreadCount() < max_active_workers_) { |
| 189 | const std::string worker_name = StringPrintf("%s worker thread %zu", name_.c_str(), |
| 190 | GetThreadCount()); |
| 191 | threads_.push_back( |
| 192 | new ThreadPoolWorker(this, worker_name, worker_stack_size_)); |
| 193 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 194 | } |
Mathieu Chartier | 93c21ba | 2018-12-10 13:08:30 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void ThreadPool::WaitForWorkersToBeCreated() { |
| 198 | creation_barier_.Increment(Thread::Current(), 0); |
| 199 | } |
| 200 | |
| 201 | const std::vector<ThreadPoolWorker*>& ThreadPool::GetWorkers() { |
| 202 | // Wait for all the workers to be created before returning them. |
| 203 | WaitForWorkersToBeCreated(); |
| 204 | return threads_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 207 | void ThreadPool::DeleteThreads() { |
Mathieu Chartier | e46cd75 | 2012-10-31 16:56:18 -0700 | [diff] [blame] | 208 | { |
| 209 | Thread* self = Thread::Current(); |
| 210 | MutexLock mu(self, task_queue_lock_); |
| 211 | // Tell any remaining workers to shut down. |
| 212 | shutting_down_ = true; |
Mathieu Chartier | e46cd75 | 2012-10-31 16:56:18 -0700 | [diff] [blame] | 213 | // Broadcast to everyone waiting. |
| 214 | task_queue_condition_.Broadcast(self); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 215 | completion_condition_.Broadcast(self); |
Mathieu Chartier | e46cd75 | 2012-10-31 16:56:18 -0700 | [diff] [blame] | 216 | } |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 217 | // Wait for the threads to finish. We expect the user of the pool |
| 218 | // not to run multi-threaded calls to `CreateThreads` and `DeleteThreads`, |
| 219 | // so we don't guard the field here. |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 220 | STLDeleteElements(&threads_); |
| 221 | } |
| 222 | |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 223 | void ThreadPool::SetMaxActiveWorkers(size_t max_workers) { |
| 224 | MutexLock mu(Thread::Current(), task_queue_lock_); |
| 225 | CHECK_LE(max_workers, GetThreadCount()); |
| 226 | max_active_workers_ = max_workers; |
| 227 | } |
| 228 | |
| 229 | ThreadPool::~ThreadPool() { |
| 230 | DeleteThreads(); |
Nicolas Geoffray | 714fad6 | 2019-06-27 15:32:00 +0100 | [diff] [blame] | 231 | RemoveAllTasks(Thread::Current()); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 234 | void ThreadPool::StartWorkers(Thread* self) { |
| 235 | MutexLock mu(self, task_queue_lock_); |
| 236 | started_ = true; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 237 | task_queue_condition_.Broadcast(self); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 238 | start_time_ = NanoTime(); |
| 239 | total_wait_time_ = 0; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void ThreadPool::StopWorkers(Thread* self) { |
| 243 | MutexLock mu(self, task_queue_lock_); |
| 244 | started_ = false; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 247 | Task* ThreadPool::GetTask(Thread* self) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 248 | MutexLock mu(self, task_queue_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 249 | while (!IsShuttingDown()) { |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 250 | const size_t thread_count = GetThreadCount(); |
| 251 | // Ensure that we don't use more threads than the maximum active workers. |
| 252 | const size_t active_threads = thread_count - waiting_count_; |
| 253 | // <= since self is considered an active worker. |
| 254 | if (active_threads <= max_active_workers_) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 255 | Task* task = TryGetTaskLocked(); |
| 256 | if (task != nullptr) { |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 257 | return task; |
| 258 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 261 | ++waiting_count_; |
Andreas Gampe | 6f3a70f | 2016-11-16 13:58:05 -0800 | [diff] [blame] | 262 | if (waiting_count_ == GetThreadCount() && !HasOutstandingTasks()) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 263 | // We may be done, lets broadcast to the completion condition. |
| 264 | completion_condition_.Broadcast(self); |
| 265 | } |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 266 | const uint64_t wait_start = kMeasureWaitTime ? NanoTime() : 0; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 267 | task_queue_condition_.Wait(self); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 268 | if (kMeasureWaitTime) { |
| 269 | const uint64_t wait_end = NanoTime(); |
| 270 | total_wait_time_ += wait_end - std::max(wait_start, start_time_); |
| 271 | } |
| 272 | --waiting_count_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 275 | // We are shutting down, return null to tell the worker thread to stop looping. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 276 | return nullptr; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 279 | Task* ThreadPool::TryGetTask(Thread* self) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 280 | MutexLock mu(self, task_queue_lock_); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 281 | return TryGetTaskLocked(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 284 | Task* ThreadPool::TryGetTaskLocked() { |
Andreas Gampe | 6f3a70f | 2016-11-16 13:58:05 -0800 | [diff] [blame] | 285 | if (HasOutstandingTasks()) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 286 | Task* task = tasks_.front(); |
| 287 | tasks_.pop_front(); |
| 288 | return task; |
| 289 | } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 290 | return nullptr; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 293 | void ThreadPool::Wait(Thread* self, bool do_work, bool may_hold_locks) { |
| 294 | if (do_work) { |
Andreas Gampe | b15de0c | 2017-01-24 13:12:19 -0800 | [diff] [blame] | 295 | CHECK(!create_peers_); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 296 | Task* task = nullptr; |
| 297 | while ((task = TryGetTask(self)) != nullptr) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 298 | task->Run(self); |
| 299 | task->Finalize(); |
| 300 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 301 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 302 | // Wait until each thread is waiting and the task list is empty. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 303 | MutexLock mu(self, task_queue_lock_); |
Andreas Gampe | 6f3a70f | 2016-11-16 13:58:05 -0800 | [diff] [blame] | 304 | while (!shutting_down_ && (waiting_count_ != GetThreadCount() || HasOutstandingTasks())) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 305 | if (!may_hold_locks) { |
| 306 | completion_condition_.Wait(self); |
| 307 | } else { |
| 308 | completion_condition_.WaitHoldingLocks(self); |
| 309 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 313 | size_t ThreadPool::GetTaskCount(Thread* self) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 314 | MutexLock mu(self, task_queue_lock_); |
| 315 | return tasks_.size(); |
| 316 | } |
| 317 | |
Andreas Gampe | 9e927f5 | 2016-02-29 20:49:38 -0800 | [diff] [blame] | 318 | void ThreadPool::SetPthreadPriority(int priority) { |
| 319 | for (ThreadPoolWorker* worker : threads_) { |
| 320 | worker->SetPthreadPriority(priority); |
| 321 | } |
| 322 | } |
| 323 | |
Nicolas Geoffray | f9dbb97 | 2020-08-27 15:21:11 +0100 | [diff] [blame] | 324 | void ThreadPool::CheckPthreadPriority(int priority) { |
| 325 | #if defined(ART_TARGET_ANDROID) |
| 326 | for (ThreadPoolWorker* worker : threads_) { |
| 327 | CHECK_EQ(worker->GetPthreadPriority(), priority); |
| 328 | } |
| 329 | #else |
| 330 | UNUSED(priority); |
| 331 | #endif |
| 332 | } |
| 333 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 334 | } // namespace art |