[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 5846da0 | 2009-03-06 20:35:45 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 5 | #include "base/threading/worker_pool_posix.h" |
[email protected] | 5846da0 | 2009-03-06 20:35:45 | [diff] [blame] | 6 | |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 19d8a90 | 2011-10-03 17:51:25 | [diff] [blame] | 8 | #include "base/debug/trace_event.h" |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 9 | #include "base/lazy_instance.h" |
[email protected] | 56d01f6 | 2009-03-12 22:41:54 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 12 | #include "base/stringprintf.h" |
[email protected] | 5846da0 | 2009-03-06 20:35:45 | [diff] [blame] | 13 | #include "base/task.h" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 14 | #include "base/threading/platform_thread.h" |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 15 | #include "base/threading/worker_pool.h" |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 16 | #include "base/tracked_objects.h" |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 17 | |
| 18 | namespace base { |
[email protected] | 5846da0 | 2009-03-06 20:35:45 | [diff] [blame] | 19 | |
| 20 | namespace { |
| 21 | |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 22 | const int kIdleSecondsBeforeExit = 10 * 60; |
[email protected] | 82fbbe6 | 2009-05-28 08:45:19 | [diff] [blame] | 23 | // A stack size of 64 KB is too small for the CERT_PKIXVerifyCert |
| 24 | // function of NSS because of NSS bug 439169. |
| 25 | const int kWorkerThreadStackSize = 128 * 1024; |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 26 | |
| 27 | class WorkerPoolImpl { |
| 28 | public: |
| 29 | WorkerPoolImpl(); |
| 30 | ~WorkerPoolImpl(); |
| 31 | |
| 32 | void PostTask(const tracked_objects::Location& from_here, Task* task, |
| 33 | bool task_is_slow); |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 34 | void PostTask(const tracked_objects::Location& from_here, |
| 35 | const base::Closure& task, bool task_is_slow); |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 36 | |
| 37 | private: |
[email protected] | 9f2ca2b | 2010-12-18 19:53:18 | [diff] [blame] | 38 | scoped_refptr<base::PosixDynamicThreadPool> pool_; |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | WorkerPoolImpl::WorkerPoolImpl() |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 42 | : pool_(new base::PosixDynamicThreadPool("WorkerPool", |
| 43 | kIdleSecondsBeforeExit)) { |
| 44 | } |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 45 | |
| 46 | WorkerPoolImpl::~WorkerPoolImpl() { |
| 47 | pool_->Terminate(); |
| 48 | } |
| 49 | |
| 50 | void WorkerPoolImpl::PostTask(const tracked_objects::Location& from_here, |
| 51 | Task* task, bool task_is_slow) { |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 52 | pool_->PostTask(from_here, task); |
| 53 | } |
| 54 | |
| 55 | void WorkerPoolImpl::PostTask(const tracked_objects::Location& from_here, |
| 56 | const base::Closure& task, bool task_is_slow) { |
| 57 | pool_->PostTask(from_here, task); |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | base::LazyInstance<WorkerPoolImpl> g_lazy_worker_pool(base::LINKER_INITIALIZED); |
| 61 | |
| 62 | class WorkerThread : public PlatformThread::Delegate { |
| 63 | public: |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 64 | WorkerThread(const std::string& name_prefix, |
[email protected] | 9f2ca2b | 2010-12-18 19:53:18 | [diff] [blame] | 65 | base::PosixDynamicThreadPool* pool) |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 66 | : name_prefix_(name_prefix), |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 67 | pool_(pool) {} |
| 68 | |
| 69 | virtual void ThreadMain(); |
| 70 | |
| 71 | private: |
| 72 | const std::string name_prefix_; |
[email protected] | 9f2ca2b | 2010-12-18 19:53:18 | [diff] [blame] | 73 | scoped_refptr<base::PosixDynamicThreadPool> pool_; |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 74 | |
| 75 | DISALLOW_COPY_AND_ASSIGN(WorkerThread); |
| 76 | }; |
| 77 | |
| 78 | void WorkerThread::ThreadMain() { |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 79 | const std::string name = base::StringPrintf( |
| 80 | "%s/%d", name_prefix_.c_str(), PlatformThread::CurrentId()); |
[email protected] | ab39dc8 | 2011-11-08 10:27:17 | [diff] [blame^] | 81 | // Note |name.c_str()| must remain valid for for the whole life of the thread. |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 82 | PlatformThread::SetName(name.c_str()); |
| 83 | |
| 84 | for (;;) { |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 85 | PosixDynamicThreadPool::PendingTask pending_task = pool_->WaitForTask(); |
| 86 | if (pending_task.task.is_null()) |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 87 | break; |
[email protected] | 19d8a90 | 2011-10-03 17:51:25 | [diff] [blame] | 88 | UNSHIPPED_TRACE_EVENT2("task", "WorkerThread::ThreadMain::Run", |
| 89 | "src_file", pending_task.posted_from.file_name(), |
| 90 | "src_func", pending_task.posted_from.function_name()); |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 91 | |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 92 | tracked_objects::TrackedTime start_time = |
| 93 | tracked_objects::ThreadData::Now(); |
| 94 | |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 95 | pending_task.task.Run(); |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 96 | |
| 97 | tracked_objects::ThreadData::TallyRunOnWorkerThreadIfTracking( |
| 98 | pending_task.birth_tally, pending_task.time_posted, |
| 99 | start_time, tracked_objects::ThreadData::Now()); |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // The WorkerThread is non-joinable, so it deletes itself. |
| 103 | delete this; |
[email protected] | 5846da0 | 2009-03-06 20:35:45 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace |
| 107 | |
| 108 | bool WorkerPool::PostTask(const tracked_objects::Location& from_here, |
| 109 | Task* task, bool task_is_slow) { |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 110 | g_lazy_worker_pool.Pointer()->PostTask(from_here, task, task_is_slow); |
[email protected] | 5846da0 | 2009-03-06 20:35:45 | [diff] [blame] | 111 | return true; |
| 112 | } |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 113 | |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 114 | bool WorkerPool::PostTask(const tracked_objects::Location& from_here, |
| 115 | const base::Closure& task, bool task_is_slow) { |
| 116 | g_lazy_worker_pool.Pointer()->PostTask(from_here, task, task_is_slow); |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | PosixDynamicThreadPool::PendingTask::PendingTask( |
| 121 | const tracked_objects::Location& posted_from, |
| 122 | const base::Closure& task) |
[email protected] | 19d8a90 | 2011-10-03 17:51:25 | [diff] [blame] | 123 | : posted_from(posted_from), |
| 124 | task(task) { |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 125 | birth_tally = tracked_objects::ThreadData::TallyABirthIfActive(posted_from); |
[email protected] | 84b5795 | 2011-10-15 23:52:45 | [diff] [blame] | 126 | time_posted = tracked_objects::ThreadData::Now(); |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | PosixDynamicThreadPool::PendingTask::~PendingTask() { |
| 130 | } |
| 131 | |
[email protected] | 9f2ca2b | 2010-12-18 19:53:18 | [diff] [blame] | 132 | PosixDynamicThreadPool::PosixDynamicThreadPool( |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 133 | const std::string& name_prefix, |
| 134 | int idle_seconds_before_exit) |
| 135 | : name_prefix_(name_prefix), |
| 136 | idle_seconds_before_exit_(idle_seconds_before_exit), |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 137 | pending_tasks_available_cv_(&lock_), |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 138 | num_idle_threads_(0), |
| 139 | terminated_(false), |
| 140 | num_idle_threads_cv_(NULL) {} |
| 141 | |
[email protected] | 9f2ca2b | 2010-12-18 19:53:18 | [diff] [blame] | 142 | PosixDynamicThreadPool::~PosixDynamicThreadPool() { |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 143 | while (!pending_tasks_.empty()) { |
| 144 | PendingTask pending_task = pending_tasks_.front(); |
| 145 | pending_tasks_.pop(); |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
[email protected] | 9f2ca2b | 2010-12-18 19:53:18 | [diff] [blame] | 149 | void PosixDynamicThreadPool::Terminate() { |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 150 | { |
| 151 | AutoLock locked(lock_); |
| 152 | DCHECK(!terminated_) << "Thread pool is already terminated."; |
| 153 | terminated_ = true; |
| 154 | } |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 155 | pending_tasks_available_cv_.Broadcast(); |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 158 | void PosixDynamicThreadPool::PostTask( |
| 159 | const tracked_objects::Location& from_here, |
| 160 | Task* task) { |
| 161 | PendingTask pending_task(from_here, |
| 162 | base::Bind(&subtle::TaskClosureAdapter::Run, |
| 163 | new subtle::TaskClosureAdapter(task))); |
| 164 | // |pending_task| and AddTask() work in conjunction here to ensure that after |
| 165 | // a successful AddTask(), the TaskClosureAdapter object is deleted on the |
| 166 | // worker thread. In AddTask(), the reference |pending_task.task| is handed |
| 167 | // off in a destructive manner to ensure that the local copy of |
| 168 | // |pending_task| doesn't keep a ref on the Closure causing the |
| 169 | // TaskClosureAdapter to be deleted on the wrong thread. |
| 170 | AddTask(&pending_task); |
| 171 | } |
| 172 | |
| 173 | void PosixDynamicThreadPool::PostTask( |
| 174 | const tracked_objects::Location& from_here, |
| 175 | const base::Closure& task) { |
| 176 | PendingTask pending_task(from_here, task); |
| 177 | AddTask(&pending_task); |
| 178 | } |
| 179 | |
| 180 | void PosixDynamicThreadPool::AddTask(PendingTask* pending_task) { |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 181 | AutoLock locked(lock_); |
| 182 | DCHECK(!terminated_) << |
| 183 | "This thread pool is already terminated. Do not post new tasks."; |
| 184 | |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 185 | pending_tasks_.push(*pending_task); |
| 186 | pending_task->task.Reset(); |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 187 | |
| 188 | // We have enough worker threads. |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 189 | if (static_cast<size_t>(num_idle_threads_) >= pending_tasks_.size()) { |
| 190 | pending_tasks_available_cv_.Signal(); |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 191 | } else { |
| 192 | // The new PlatformThread will take ownership of the WorkerThread object, |
| 193 | // which will delete itself on exit. |
| 194 | WorkerThread* worker = |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 195 | new WorkerThread(name_prefix_, this); |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 196 | PlatformThread::CreateNonJoinable(kWorkerThreadStackSize, worker); |
| 197 | } |
| 198 | } |
| 199 | |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 200 | PosixDynamicThreadPool::PendingTask PosixDynamicThreadPool::WaitForTask() { |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 201 | AutoLock locked(lock_); |
| 202 | |
| 203 | if (terminated_) |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 204 | return PendingTask(FROM_HERE, base::Closure()); |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 205 | |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 206 | if (pending_tasks_.empty()) { // No work available, wait for work. |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 207 | num_idle_threads_++; |
| 208 | if (num_idle_threads_cv_.get()) |
| 209 | num_idle_threads_cv_->Signal(); |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 210 | pending_tasks_available_cv_.TimedWait( |
| 211 | TimeDelta::FromSeconds(idle_seconds_before_exit_)); |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 212 | num_idle_threads_--; |
| 213 | if (num_idle_threads_cv_.get()) |
| 214 | num_idle_threads_cv_->Signal(); |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 215 | if (pending_tasks_.empty()) { |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 216 | // We waited for work, but there's still no work. Return NULL to signal |
| 217 | // the thread to terminate. |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 218 | return PendingTask(FROM_HERE, base::Closure()); |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 222 | PendingTask pending_task = pending_tasks_.front(); |
| 223 | pending_tasks_.pop(); |
| 224 | return pending_task; |
[email protected] | 250103f | 2009-03-24 23:41:53 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | } // namespace base |