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 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 19 | #include "base/casts.h" |
| 20 | #include "base/stl_util.h" |
| 21 | #include "runtime.h" |
Brian Carlstrom | a3d2718 | 2013-11-05 23:22:27 -0800 | [diff] [blame] | 22 | #include "thread-inl.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 23 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 24 | namespace art { |
| 25 | |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 26 | static constexpr bool kMeasureWaitTime = false; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 27 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 28 | ThreadPoolWorker::ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, |
| 29 | size_t stack_size) |
| 30 | : thread_pool_(thread_pool), |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 31 | name_(name) { |
| 32 | std::string error_msg; |
| 33 | 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] | 34 | false, false, &error_msg)); |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 35 | CHECK(stack_.get() != nullptr) << error_msg; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 36 | const char* reason = "new thread pool worker thread"; |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 37 | pthread_attr_t attr; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 38 | CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), reason); |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 39 | CHECK_PTHREAD_CALL(pthread_attr_setstack, (&attr, stack_->Begin(), stack_->Size()), reason); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 40 | CHECK_PTHREAD_CALL(pthread_create, (&pthread_, &attr, &Callback, this), reason); |
| 41 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), reason); |
| 42 | } |
| 43 | |
| 44 | ThreadPoolWorker::~ThreadPoolWorker() { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 45 | CHECK_PTHREAD_CALL(pthread_join, (pthread_, nullptr), "thread pool worker shutdown"); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void ThreadPoolWorker::Run() { |
| 49 | Thread* self = Thread::Current(); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 50 | Task* task = nullptr; |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 51 | thread_pool_->creation_barier_.Wait(self); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 52 | while ((task = thread_pool_->GetTask(self)) != nullptr) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 53 | task->Run(self); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 54 | task->Finalize(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | void* ThreadPoolWorker::Callback(void* arg) { |
| 59 | ThreadPoolWorker* worker = reinterpret_cast<ThreadPoolWorker*>(arg); |
| 60 | Runtime* runtime = Runtime::Current(); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 61 | CHECK(runtime->AttachCurrentThread(worker->name_.c_str(), true, nullptr, false)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 62 | // Do work until its time to shut down. |
| 63 | worker->Run(); |
| 64 | runtime->DetachCurrentThread(); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 65 | return nullptr; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 68 | void ThreadPool::AddTask(Thread* self, Task* task) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 69 | MutexLock mu(self, task_queue_lock_); |
| 70 | tasks_.push_back(task); |
| 71 | // If we have any waiters, signal one. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 72 | if (started_ && waiting_count_ != 0) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 73 | task_queue_condition_.Signal(self); |
| 74 | } |
| 75 | } |
| 76 | |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 77 | ThreadPool::ThreadPool(const char* name, size_t num_threads) |
| 78 | : name_(name), |
| 79 | task_queue_lock_("task queue lock"), |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 80 | task_queue_condition_("task queue condition", task_queue_lock_), |
| 81 | completion_condition_("task completion condition", task_queue_lock_), |
| 82 | started_(false), |
| 83 | shutting_down_(false), |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 84 | waiting_count_(0), |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 85 | start_time_(0), |
| 86 | total_wait_time_(0), |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 87 | // Add one since the caller of constructor waits on the barrier too. |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 88 | creation_barier_(num_threads + 1), |
| 89 | max_active_workers_(num_threads) { |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 90 | Thread* self = Thread::Current(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 91 | while (GetThreadCount() < num_threads) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 92 | const std::string worker_name = StringPrintf("%s worker thread %zu", name_.c_str(), |
| 93 | GetThreadCount()); |
| 94 | threads_.push_back(new ThreadPoolWorker(this, worker_name, ThreadPoolWorker::kDefaultStackSize)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 95 | } |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 96 | // Wait for all of the threads to attach. |
| 97 | creation_barier_.Wait(self); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 100 | void ThreadPool::SetMaxActiveWorkers(size_t threads) { |
| 101 | MutexLock mu(Thread::Current(), task_queue_lock_); |
| 102 | CHECK_LE(threads, GetThreadCount()); |
| 103 | max_active_workers_ = threads; |
| 104 | } |
| 105 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 106 | ThreadPool::~ThreadPool() { |
Mathieu Chartier | e46cd75 | 2012-10-31 16:56:18 -0700 | [diff] [blame] | 107 | { |
| 108 | Thread* self = Thread::Current(); |
| 109 | MutexLock mu(self, task_queue_lock_); |
| 110 | // Tell any remaining workers to shut down. |
| 111 | shutting_down_ = true; |
Mathieu Chartier | e46cd75 | 2012-10-31 16:56:18 -0700 | [diff] [blame] | 112 | // Broadcast to everyone waiting. |
| 113 | task_queue_condition_.Broadcast(self); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 114 | completion_condition_.Broadcast(self); |
Mathieu Chartier | e46cd75 | 2012-10-31 16:56:18 -0700 | [diff] [blame] | 115 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 116 | // Wait for the threads to finish. |
| 117 | STLDeleteElements(&threads_); |
| 118 | } |
| 119 | |
| 120 | void ThreadPool::StartWorkers(Thread* self) { |
| 121 | MutexLock mu(self, task_queue_lock_); |
| 122 | started_ = true; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 123 | task_queue_condition_.Broadcast(self); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 124 | start_time_ = NanoTime(); |
| 125 | total_wait_time_ = 0; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void ThreadPool::StopWorkers(Thread* self) { |
| 129 | MutexLock mu(self, task_queue_lock_); |
| 130 | started_ = false; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 133 | Task* ThreadPool::GetTask(Thread* self) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 134 | MutexLock mu(self, task_queue_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 135 | while (!IsShuttingDown()) { |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 136 | const size_t thread_count = GetThreadCount(); |
| 137 | // Ensure that we don't use more threads than the maximum active workers. |
| 138 | const size_t active_threads = thread_count - waiting_count_; |
| 139 | // <= since self is considered an active worker. |
| 140 | if (active_threads <= max_active_workers_) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 141 | Task* task = TryGetTaskLocked(); |
| 142 | if (task != nullptr) { |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 143 | return task; |
| 144 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 147 | ++waiting_count_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 148 | if (waiting_count_ == GetThreadCount() && tasks_.empty()) { |
| 149 | // We may be done, lets broadcast to the completion condition. |
| 150 | completion_condition_.Broadcast(self); |
| 151 | } |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 152 | const uint64_t wait_start = kMeasureWaitTime ? NanoTime() : 0; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 153 | task_queue_condition_.Wait(self); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 154 | if (kMeasureWaitTime) { |
| 155 | const uint64_t wait_end = NanoTime(); |
| 156 | total_wait_time_ += wait_end - std::max(wait_start, start_time_); |
| 157 | } |
| 158 | --waiting_count_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 161 | // 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] | 162 | return nullptr; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 165 | Task* ThreadPool::TryGetTask(Thread* self) { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 166 | MutexLock mu(self, task_queue_lock_); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 167 | return TryGetTaskLocked(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 170 | Task* ThreadPool::TryGetTaskLocked() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 171 | if (started_ && !tasks_.empty()) { |
| 172 | Task* task = tasks_.front(); |
| 173 | tasks_.pop_front(); |
| 174 | return task; |
| 175 | } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 176 | return nullptr; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 179 | void ThreadPool::Wait(Thread* self, bool do_work, bool may_hold_locks) { |
| 180 | if (do_work) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 181 | Task* task = nullptr; |
| 182 | while ((task = TryGetTask(self)) != nullptr) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 183 | task->Run(self); |
| 184 | task->Finalize(); |
| 185 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 186 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 187 | // Wait until each thread is waiting and the task list is empty. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 188 | MutexLock mu(self, task_queue_lock_); |
| 189 | while (!shutting_down_ && (waiting_count_ != GetThreadCount() || !tasks_.empty())) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 190 | if (!may_hold_locks) { |
| 191 | completion_condition_.Wait(self); |
| 192 | } else { |
| 193 | completion_condition_.WaitHoldingLocks(self); |
| 194 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 198 | size_t ThreadPool::GetTaskCount(Thread* self) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 199 | MutexLock mu(self, task_queue_lock_); |
| 200 | return tasks_.size(); |
| 201 | } |
| 202 | |
| 203 | WorkStealingWorker::WorkStealingWorker(ThreadPool* thread_pool, const std::string& name, |
| 204 | size_t stack_size) |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 205 | : ThreadPoolWorker(thread_pool, name, stack_size), task_(nullptr) {} |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 206 | |
| 207 | void WorkStealingWorker::Run() { |
| 208 | Thread* self = Thread::Current(); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 209 | Task* task = nullptr; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 210 | WorkStealingThreadPool* thread_pool = down_cast<WorkStealingThreadPool*>(thread_pool_); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 211 | while ((task = thread_pool_->GetTask(self)) != nullptr) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 212 | WorkStealingTask* stealing_task = down_cast<WorkStealingTask*>(task); |
| 213 | |
| 214 | { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 215 | CHECK(task_ == nullptr); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 216 | MutexLock mu(self, thread_pool->work_steal_lock_); |
| 217 | // Register that we are running the task |
| 218 | ++stealing_task->ref_count_; |
| 219 | task_ = stealing_task; |
| 220 | } |
| 221 | stealing_task->Run(self); |
| 222 | // Mark ourselves as not running a task so that nobody tries to steal from us. |
| 223 | // There is a race condition that someone starts stealing from us at this point. This is okay |
| 224 | // due to the reference counting. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 225 | task_ = nullptr; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 226 | |
| 227 | bool finalize; |
| 228 | |
| 229 | // Steal work from tasks until there is none left to steal. Note: There is a race, but |
| 230 | // all that happens when the race occurs is that we steal some work instead of processing a |
| 231 | // task from the queue. |
| 232 | while (thread_pool->GetTaskCount(self) == 0) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 233 | WorkStealingTask* steal_from_task = nullptr; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 234 | |
| 235 | { |
| 236 | MutexLock mu(self, thread_pool->work_steal_lock_); |
| 237 | // Try finding a task to steal from. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 238 | steal_from_task = thread_pool->FindTaskToStealFrom(); |
| 239 | if (steal_from_task != nullptr) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 240 | CHECK_NE(stealing_task, steal_from_task) |
| 241 | << "Attempting to steal from completed self task"; |
| 242 | steal_from_task->ref_count_++; |
| 243 | } else { |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 248 | if (steal_from_task != nullptr) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 249 | // Task which completed earlier is going to steal some work. |
| 250 | stealing_task->StealFrom(self, steal_from_task); |
| 251 | |
| 252 | { |
| 253 | // We are done stealing from the task, lets decrement its reference count. |
| 254 | MutexLock mu(self, thread_pool->work_steal_lock_); |
| 255 | finalize = !--steal_from_task->ref_count_; |
| 256 | } |
| 257 | |
| 258 | if (finalize) { |
| 259 | steal_from_task->Finalize(); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | { |
| 265 | MutexLock mu(self, thread_pool->work_steal_lock_); |
| 266 | // If nobody is still referencing task_ we can finalize it. |
| 267 | finalize = !--stealing_task->ref_count_; |
| 268 | } |
| 269 | |
| 270 | if (finalize) { |
| 271 | stealing_task->Finalize(); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 276 | WorkStealingWorker::~WorkStealingWorker() {} |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 277 | |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 278 | WorkStealingThreadPool::WorkStealingThreadPool(const char* name, size_t num_threads) |
| 279 | : ThreadPool(name, 0), |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 280 | work_steal_lock_("work stealing lock"), |
| 281 | steal_index_(0) { |
| 282 | while (GetThreadCount() < num_threads) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 283 | const std::string worker_name = StringPrintf("Work stealing worker %zu", GetThreadCount()); |
| 284 | threads_.push_back(new WorkStealingWorker(this, worker_name, |
| 285 | ThreadPoolWorker::kDefaultStackSize)); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 289 | WorkStealingTask* WorkStealingThreadPool::FindTaskToStealFrom() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 290 | const size_t thread_count = GetThreadCount(); |
| 291 | for (size_t i = 0; i < thread_count; ++i) { |
| 292 | // TODO: Use CAS instead of lock. |
| 293 | ++steal_index_; |
| 294 | if (steal_index_ >= thread_count) { |
| 295 | steal_index_-= thread_count; |
| 296 | } |
| 297 | |
| 298 | WorkStealingWorker* worker = down_cast<WorkStealingWorker*>(threads_[steal_index_]); |
| 299 | WorkStealingTask* task = worker->task_; |
| 300 | if (task) { |
| 301 | // Not null, we can probably steal from this worker. |
| 302 | return task; |
| 303 | } |
| 304 | } |
| 305 | // Couldn't find something to steal. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 306 | return nullptr; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 309 | WorkStealingThreadPool::~WorkStealingThreadPool() {} |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 310 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 311 | } // namespace art |