| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 namespace base { | 24 namespace base { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 base::LazyInstance<ThreadLocalBoolean>::Leaky | 28 base::LazyInstance<ThreadLocalBoolean>::Leaky |
| 29 g_worker_pool_running_on_this_thread = LAZY_INSTANCE_INITIALIZER; | 29 g_worker_pool_running_on_this_thread = LAZY_INSTANCE_INITIALIZER; |
| 30 | 30 |
| 31 const int kIdleSecondsBeforeExit = 10 * 60; | 31 const int kIdleSecondsBeforeExit = 10 * 60; |
| 32 | 32 |
| 33 #if defined(OS_MACOSX) |
| 34 // On Mac OS X a background thread's default stack size is 512Kb. We need at |
| 35 // least 1MB for compilation tasks in V8, so increase this default. |
| 36 const int kStackSize = 1 * 1024 * 1024; |
| 37 #else |
| 38 const int kStackSize = 0; |
| 39 #endif |
| 40 |
| 33 class WorkerPoolImpl { | 41 class WorkerPoolImpl { |
| 34 public: | 42 public: |
| 35 WorkerPoolImpl(); | 43 WorkerPoolImpl(); |
| 36 | 44 |
| 37 // WorkerPoolImpl is only instantiated as a leaky LazyInstance, so the | 45 // WorkerPoolImpl is only instantiated as a leaky LazyInstance, so the |
| 38 // destructor is never called. | 46 // destructor is never called. |
| 39 ~WorkerPoolImpl() = delete; | 47 ~WorkerPoolImpl() = delete; |
| 40 | 48 |
| 41 void PostTask(const tracked_objects::Location& from_here, | 49 void PostTask(const tracked_objects::Location& from_here, |
| 42 const base::Closure& task, | 50 const base::Closure& task, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 | 147 |
| 140 pending_tasks_.push(std::move(*pending_task)); | 148 pending_tasks_.push(std::move(*pending_task)); |
| 141 | 149 |
| 142 // We have enough worker threads. | 150 // We have enough worker threads. |
| 143 if (static_cast<size_t>(num_idle_threads_) >= pending_tasks_.size()) { | 151 if (static_cast<size_t>(num_idle_threads_) >= pending_tasks_.size()) { |
| 144 pending_tasks_available_cv_.Signal(); | 152 pending_tasks_available_cv_.Signal(); |
| 145 } else { | 153 } else { |
| 146 // The new PlatformThread will take ownership of the WorkerThread object, | 154 // The new PlatformThread will take ownership of the WorkerThread object, |
| 147 // which will delete itself on exit. | 155 // which will delete itself on exit. |
| 148 WorkerThread* worker = new WorkerThread(name_prefix_, this); | 156 WorkerThread* worker = new WorkerThread(name_prefix_, this); |
| 149 PlatformThread::CreateNonJoinable(0, worker); | 157 PlatformThread::CreateNonJoinable(kStackSize, worker); |
| 150 } | 158 } |
| 151 } | 159 } |
| 152 | 160 |
| 153 PendingTask PosixDynamicThreadPool::WaitForTask() { | 161 PendingTask PosixDynamicThreadPool::WaitForTask() { |
| 154 AutoLock locked(lock_); | 162 AutoLock locked(lock_); |
| 155 | 163 |
| 156 if (pending_tasks_.empty()) { // No work available, wait for work. | 164 if (pending_tasks_.empty()) { // No work available, wait for work. |
| 157 num_idle_threads_++; | 165 num_idle_threads_++; |
| 158 if (num_idle_threads_cv_.get()) | 166 if (num_idle_threads_cv_.get()) |
| 159 num_idle_threads_cv_->Signal(); | 167 num_idle_threads_cv_->Signal(); |
| 160 pending_tasks_available_cv_.TimedWait( | 168 pending_tasks_available_cv_.TimedWait( |
| 161 TimeDelta::FromSeconds(idle_seconds_before_exit_)); | 169 TimeDelta::FromSeconds(idle_seconds_before_exit_)); |
| 162 num_idle_threads_--; | 170 num_idle_threads_--; |
| 163 if (num_idle_threads_cv_.get()) | 171 if (num_idle_threads_cv_.get()) |
| 164 num_idle_threads_cv_->Signal(); | 172 num_idle_threads_cv_->Signal(); |
| 165 if (pending_tasks_.empty()) { | 173 if (pending_tasks_.empty()) { |
| 166 // We waited for work, but there's still no work. Return NULL to signal | 174 // We waited for work, but there's still no work. Return NULL to signal |
| 167 // the thread to terminate. | 175 // the thread to terminate. |
| 168 return PendingTask(FROM_HERE, base::Closure()); | 176 return PendingTask(FROM_HERE, base::Closure()); |
| 169 } | 177 } |
| 170 } | 178 } |
| 171 | 179 |
| 172 PendingTask pending_task = std::move(pending_tasks_.front()); | 180 PendingTask pending_task = std::move(pending_tasks_.front()); |
| 173 pending_tasks_.pop(); | 181 pending_tasks_.pop(); |
| 174 return pending_task; | 182 return pending_task; |
| 175 } | 183 } |
| 176 | 184 |
| 177 } // namespace base | 185 } // namespace base |
| OLD | NEW |