Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 17 | #include "thread_pool.h" |
| 18 | |
Vladimir Marko | 0b6e283 | 2015-09-24 10:41:33 +0100 | [diff] [blame] | 19 | #include "base/bit_utils.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 20 | #include "base/casts.h" |
Vladimir Marko | 0b6e283 | 2015-09-24 10:41:33 +0100 | [diff] [blame] | 21 | #include "base/logging.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 22 | #include "base/stl_util.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 23 | #include "base/time_utils.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 24 | #include "runtime.h" |
Brian Carlstrom | a3d2718 | 2013-11-05 23:22:27 -0800 | [diff] [blame] | 25 | #include "thread-inl.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 26 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 27 | namespace art { |
| 28 | |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 29 | static constexpr bool kMeasureWaitTime = false; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 30 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 31 | ThreadPoolWorker::ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, |
| 32 | size_t stack_size) |
| 33 | : thread_pool_(thread_pool), |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 34 | name_(name) { |
Vladimir Marko | 0b6e283 | 2015-09-24 10:41:33 +0100 | [diff] [blame] | 35 | // Add an inaccessible page to catch stack overflow. |
| 36 | stack_size += kPageSize; |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 37 | std::string error_msg; |
| 38 | stack_.reset(MemMap::MapAnonymous(name.c_str(), nullptr, stack_size, PROT_READ | PROT_WRITE, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 39 | false, false, &error_msg)); |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 40 | CHECK(stack_.get() != nullptr) << error_msg; |
Vladimir Marko | 0b6e283 | 2015-09-24 10:41:33 +0100 | [diff] [blame] | 41 | CHECK_ALIGNED(stack_->Begin(), kPageSize); |
| 42 | int mprotect_result = mprotect(stack_->Begin(), kPageSize, PROT_NONE); |
| 43 | CHECK_EQ(mprotect_result, 0) << "Failed to mprotect() bottom page of thread pool worker stack."; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 44 | const char* reason = "new thread pool worker thread"; |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 45 | pthread_attr_t attr; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 46 | CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), reason); |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 47 | CHECK_PTHREAD_CALL(pthread_attr_setstack, (&attr, stack_->Begin(), stack_->Size()), reason); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 48 | CHECK_PTHREAD_CALL(pthread_create, (&pthread_, &attr, &Callback, this), reason); |
| 49 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), reason); |
| 50 | } |
| 51 | |
| 52 | ThreadPoolWorker::~ThreadPoolWorker() { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 53 | CHECK_PTHREAD_CALL(pthread_join, (pthread_, nullptr), "thread pool worker shutdown"); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void ThreadPoolWorker::Run() { |
| 57 | Thread* self = Thread::Current(); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 58 | Task* task = nullptr; |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 59 | thread_pool_->creation_barier_.Wait(self); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 60 | while ((task = thread_pool_->GetTask(self)) != nullptr) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 61 | task->Run(self); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 62 | task->Finalize(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | void* ThreadPoolWorker::Callback(void* arg) { |
| 67 | ThreadPoolWorker* worker = reinterpret_cast<ThreadPoolWorker*>(arg); |
| 68 | Runtime* runtime = Runtime::Current(); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 69 | CHECK(runtime->AttachCurrentThread(worker->name_.c_str(), true, nullptr, false)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 70 | // Do work until its time to shut down. |
| 71 | worker->Run(); |
| 72 | runtime->DetachCurrentThread(); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 73 | return nullptr; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 76 | void ThreadPool::AddTask(Thread* self, Task* task) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 77 | MutexLock mu(self, task_queue_lock_); |
| 78 | tasks_.push_back(task); |
| 79 | // If we have any waiters, signal one. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 80 | if (started_ && waiting_count_ != 0) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 81 | task_queue_condition_.Signal(self); |
| 82 | } |
| 83 | } |
| 84 | |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 85 | ThreadPool::ThreadPool(const char* name, size_t num_threads) |
| 86 | : name_(name), |
| 87 | task_queue_lock_("task queue lock"), |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 88 | task_queue_condition_("task queue condition", task_queue_lock_), |
| 89 | completion_condition_("task completion condition", task_queue_lock_), |
| 90 | started_(false), |
| 91 | shutting_down_(false), |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 92 | waiting_count_(0), |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 93 | start_time_(0), |
| 94 | total_wait_time_(0), |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 95 | // Add one since the caller of constructor waits on the barrier too. |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 96 | creation_barier_(num_threads + 1), |
| 97 | max_active_workers_(num_threads) { |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 98 | Thread* self = Thread::Current(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 99 | while (GetThreadCount() < num_threads) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 100 | const std::string worker_name = StringPrintf("%s worker thread %zu", name_.c_str(), |
| 101 | GetThreadCount()); |
Vladimir Marko | 0b6e283 | 2015-09-24 10:41:33 +0100 | [diff] [blame] | 102 | threads_.push_back( |
| 103 | new ThreadPoolWorker(this, worker_name, ThreadPoolWorker::kDefaultStackSize)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 104 | } |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 105 | // Wait for all of the threads to attach. |
| 106 | creation_barier_.Wait(self); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 109 | void ThreadPool::SetMaxActiveWorkers(size_t threads) { |
| 110 | MutexLock mu(Thread::Current(), task_queue_lock_); |
| 111 | CHECK_LE(threads, GetThreadCount()); |
| 112 | max_active_workers_ = threads; |
| 113 | } |
| 114 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 115 | ThreadPool::~ThreadPool() { |
Mathieu Chartier | e46cd75 | 2012-10-31 16:56:18 -0700 | [diff] [blame] | 116 | { |
| 117 | Thread* self = Thread::Current(); |
| 118 | MutexLock mu(self, task_queue_lock_); |
| 119 | // Tell any remaining workers to shut down. |
| 120 | shutting_down_ = true; |
Mathieu Chartier | e46cd75 | 2012-10-31 16:56:18 -0700 | [diff] [blame] | 121 | // Broadcast to everyone waiting. |
| 122 | task_queue_condition_.Broadcast(self); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 123 | completion_condition_.Broadcast(self); |
Mathieu Chartier | e46cd75 | 2012-10-31 16:56:18 -0700 | [diff] [blame] | 124 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 125 | // Wait for the threads to finish. |
| 126 | STLDeleteElements(&threads_); |
| 127 | } |
| 128 | |
| 129 | void ThreadPool::StartWorkers(Thread* self) { |
| 130 | MutexLock mu(self, task_queue_lock_); |
| 131 | started_ = true; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 132 | task_queue_condition_.Broadcast(self); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 133 | start_time_ = NanoTime(); |
| 134 | total_wait_time_ = 0; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void ThreadPool::StopWorkers(Thread* self) { |
| 138 | MutexLock mu(self, task_queue_lock_); |
| 139 | started_ = false; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 142 | Task* ThreadPool::GetTask(Thread* self) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 143 | MutexLock mu(self, task_queue_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 144 | while (!IsShuttingDown()) { |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 145 | const size_t thread_count = GetThreadCount(); |
| 146 | // Ensure that we don't use more threads than the maximum active workers. |
| 147 | const size_t active_threads = thread_count - waiting_count_; |
| 148 | // <= since self is considered an active worker. |
| 149 | if (active_threads <= max_active_workers_) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 150 | Task* task = TryGetTaskLocked(); |
| 151 | if (task != nullptr) { |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 152 | return task; |
| 153 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 156 | ++waiting_count_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 157 | if (waiting_count_ == GetThreadCount() && tasks_.empty()) { |
| 158 | // We may be done, lets broadcast to the completion condition. |
| 159 | completion_condition_.Broadcast(self); |
| 160 | } |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 161 | const uint64_t wait_start = kMeasureWaitTime ? NanoTime() : 0; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 162 | task_queue_condition_.Wait(self); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 163 | if (kMeasureWaitTime) { |
| 164 | const uint64_t wait_end = NanoTime(); |
| 165 | total_wait_time_ += wait_end - std::max(wait_start, start_time_); |
| 166 | } |
| 167 | --waiting_count_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 170 | // 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] | 171 | return nullptr; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 174 | Task* ThreadPool::TryGetTask(Thread* self) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 175 | MutexLock mu(self, task_queue_lock_); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 176 | return TryGetTaskLocked(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 179 | Task* ThreadPool::TryGetTaskLocked() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 180 | if (started_ && !tasks_.empty()) { |
| 181 | Task* task = tasks_.front(); |
| 182 | tasks_.pop_front(); |
| 183 | return task; |
| 184 | } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 185 | return nullptr; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 188 | void ThreadPool::Wait(Thread* self, bool do_work, bool may_hold_locks) { |
| 189 | if (do_work) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 190 | Task* task = nullptr; |
| 191 | while ((task = TryGetTask(self)) != nullptr) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 192 | task->Run(self); |
| 193 | task->Finalize(); |
| 194 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 195 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 196 | // Wait until each thread is waiting and the task list is empty. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 197 | MutexLock mu(self, task_queue_lock_); |
| 198 | while (!shutting_down_ && (waiting_count_ != GetThreadCount() || !tasks_.empty())) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 199 | if (!may_hold_locks) { |
| 200 | completion_condition_.Wait(self); |
| 201 | } else { |
| 202 | completion_condition_.WaitHoldingLocks(self); |
| 203 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 207 | size_t ThreadPool::GetTaskCount(Thread* self) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 208 | MutexLock mu(self, task_queue_lock_); |
| 209 | return tasks_.size(); |
| 210 | } |
| 211 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 212 | } // namespace art |