| 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 18 matching lines...) Expand all Loading... |
| 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 class WorkerPoolImpl { | 33 class WorkerPoolImpl { |
| 34 public: | 34 public: |
| 35 WorkerPoolImpl(); | 35 WorkerPoolImpl(); |
| 36 ~WorkerPoolImpl(); | 36 ~WorkerPoolImpl(); |
| 37 | 37 |
| 38 void PostTask(const tracked_objects::Location& from_here, | 38 void PostTask(const tracked_objects::Location& from_here, |
| 39 const base::Closure& task, | 39 OnceClosure task, |
| 40 bool task_is_slow); | 40 bool task_is_slow); |
| 41 | 41 |
| 42 private: | 42 private: |
| 43 scoped_refptr<base::PosixDynamicThreadPool> pool_; | 43 scoped_refptr<base::PosixDynamicThreadPool> pool_; |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 WorkerPoolImpl::WorkerPoolImpl() | 46 WorkerPoolImpl::WorkerPoolImpl() |
| 47 : pool_(new base::PosixDynamicThreadPool("WorkerPool", | 47 : pool_(new base::PosixDynamicThreadPool("WorkerPool", |
| 48 kIdleSecondsBeforeExit)) {} | 48 kIdleSecondsBeforeExit)) {} |
| 49 | 49 |
| 50 WorkerPoolImpl::~WorkerPoolImpl() { | 50 WorkerPoolImpl::~WorkerPoolImpl() { |
| 51 pool_->Terminate(); | 51 pool_->Terminate(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void WorkerPoolImpl::PostTask(const tracked_objects::Location& from_here, | 54 void WorkerPoolImpl::PostTask(const tracked_objects::Location& from_here, |
| 55 const base::Closure& task, | 55 OnceClosure task, |
| 56 bool task_is_slow) { | 56 bool task_is_slow) { |
| 57 pool_->PostTask(from_here, task); | 57 pool_->PostTask(from_here, std::move(task)); |
| 58 } | 58 } |
| 59 | 59 |
| 60 base::LazyInstance<WorkerPoolImpl> g_lazy_worker_pool = | 60 base::LazyInstance<WorkerPoolImpl> g_lazy_worker_pool = |
| 61 LAZY_INSTANCE_INITIALIZER; | 61 LAZY_INSTANCE_INITIALIZER; |
| 62 | 62 |
| 63 class WorkerThread : public PlatformThread::Delegate { | 63 class WorkerThread : public PlatformThread::Delegate { |
| 64 public: | 64 public: |
| 65 WorkerThread(const std::string& name_prefix, | 65 WorkerThread(const std::string& name_prefix, |
| 66 base::PosixDynamicThreadPool* pool) | 66 base::PosixDynamicThreadPool* pool) |
| 67 : name_prefix_(name_prefix), pool_(pool) {} | 67 : name_prefix_(name_prefix), pool_(pool) {} |
| (...skipping 15 matching lines...) Expand all Loading... |
| 83 PlatformThread::SetName(name); | 83 PlatformThread::SetName(name); |
| 84 | 84 |
| 85 for (;;) { | 85 for (;;) { |
| 86 PendingTask pending_task = pool_->WaitForTask(); | 86 PendingTask pending_task = pool_->WaitForTask(); |
| 87 if (pending_task.task.is_null()) | 87 if (pending_task.task.is_null()) |
| 88 break; | 88 break; |
| 89 TRACE_TASK_EXECUTION("WorkerThread::ThreadMain::Run", pending_task); | 89 TRACE_TASK_EXECUTION("WorkerThread::ThreadMain::Run", pending_task); |
| 90 | 90 |
| 91 tracked_objects::TaskStopwatch stopwatch; | 91 tracked_objects::TaskStopwatch stopwatch; |
| 92 stopwatch.Start(); | 92 stopwatch.Start(); |
| 93 pending_task.task.Run(); | 93 std::move(pending_task.task).Run(); |
| 94 stopwatch.Stop(); | 94 stopwatch.Stop(); |
| 95 | 95 |
| 96 tracked_objects::ThreadData::TallyRunOnWorkerThreadIfTracking( | 96 tracked_objects::ThreadData::TallyRunOnWorkerThreadIfTracking( |
| 97 pending_task.birth_tally, pending_task.time_posted, stopwatch); | 97 pending_task.birth_tally, pending_task.time_posted, stopwatch); |
| 98 } | 98 } |
| 99 | 99 |
| 100 // The WorkerThread is non-joinable, so it deletes itself. | 100 // The WorkerThread is non-joinable, so it deletes itself. |
| 101 delete this; | 101 delete this; |
| 102 } | 102 } |
| 103 | 103 |
| 104 } // namespace | 104 } // namespace |
| 105 | 105 |
| 106 // static | 106 // static |
| 107 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, | 107 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, |
| 108 const base::Closure& task, | 108 OnceClosure task, |
| 109 bool task_is_slow) { | 109 bool task_is_slow) { |
| 110 g_lazy_worker_pool.Pointer()->PostTask(from_here, task, task_is_slow); | 110 g_lazy_worker_pool.Pointer()->PostTask(from_here, std::move(task), |
| 111 task_is_slow); |
| 111 return true; | 112 return true; |
| 112 } | 113 } |
| 113 | 114 |
| 114 // static | 115 // static |
| 115 bool WorkerPool::RunsTasksOnCurrentThread() { | 116 bool WorkerPool::RunsTasksOnCurrentThread() { |
| 116 return g_worker_pool_running_on_this_thread.Get().Get(); | 117 return g_worker_pool_running_on_this_thread.Get().Get(); |
| 117 } | 118 } |
| 118 | 119 |
| 119 PosixDynamicThreadPool::PosixDynamicThreadPool(const std::string& name_prefix, | 120 PosixDynamicThreadPool::PosixDynamicThreadPool(const std::string& name_prefix, |
| 120 int idle_seconds_before_exit) | 121 int idle_seconds_before_exit) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 133 { | 134 { |
| 134 AutoLock locked(lock_); | 135 AutoLock locked(lock_); |
| 135 DCHECK(!terminated_) << "Thread pool is already terminated."; | 136 DCHECK(!terminated_) << "Thread pool is already terminated."; |
| 136 terminated_ = true; | 137 terminated_ = true; |
| 137 } | 138 } |
| 138 pending_tasks_available_cv_.Broadcast(); | 139 pending_tasks_available_cv_.Broadcast(); |
| 139 } | 140 } |
| 140 | 141 |
| 141 void PosixDynamicThreadPool::PostTask( | 142 void PosixDynamicThreadPool::PostTask( |
| 142 const tracked_objects::Location& from_here, | 143 const tracked_objects::Location& from_here, |
| 143 const base::Closure& task) { | 144 OnceClosure task) { |
| 144 PendingTask pending_task(from_here, task); | 145 PendingTask pending_task(from_here, std::move(task)); |
| 145 AddTask(&pending_task); | 146 AddTask(&pending_task); |
| 146 } | 147 } |
| 147 | 148 |
| 148 void PosixDynamicThreadPool::AddTask(PendingTask* pending_task) { | 149 void PosixDynamicThreadPool::AddTask(PendingTask* pending_task) { |
| 149 AutoLock locked(lock_); | 150 AutoLock locked(lock_); |
| 150 DCHECK(!terminated_) | 151 DCHECK(!terminated_) |
| 151 << "This thread pool is already terminated. Do not post new tasks."; | 152 << "This thread pool is already terminated. Do not post new tasks."; |
| 152 | 153 |
| 153 pending_tasks_.push(std::move(*pending_task)); | 154 pending_tasks_.push(std::move(*pending_task)); |
| 154 | 155 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 184 return PendingTask(FROM_HERE, base::Closure()); | 185 return PendingTask(FROM_HERE, base::Closure()); |
| 185 } | 186 } |
| 186 } | 187 } |
| 187 | 188 |
| 188 PendingTask pending_task = std::move(pending_tasks_.front()); | 189 PendingTask pending_task = std::move(pending_tasks_.front()); |
| 189 pending_tasks_.pop(); | 190 pending_tasks_.pop(); |
| 190 return pending_task; | 191 return pending_task; |
| 191 } | 192 } |
| 192 | 193 |
| 193 } // namespace base | 194 } // namespace base |
| OLD | NEW |