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