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 <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 class WorkerPoolImpl { | 43 class WorkerPoolImpl { |
44 public: | 44 public: |
45 WorkerPoolImpl(); | 45 WorkerPoolImpl(); |
46 | 46 |
47 // WorkerPoolImpl is only instantiated as a leaky LazyInstance, so the | 47 // WorkerPoolImpl is only instantiated as a leaky LazyInstance, so the |
48 // destructor is never called. | 48 // destructor is never called. |
49 ~WorkerPoolImpl() = delete; | 49 ~WorkerPoolImpl() = delete; |
50 | 50 |
51 void PostTask(const tracked_objects::Location& from_here, | 51 void PostTask(const tracked_objects::Location& from_here, |
52 base::Closure task, | 52 base::OnceClosure task, |
53 bool task_is_slow); | 53 bool task_is_slow); |
54 | 54 |
55 private: | 55 private: |
56 scoped_refptr<base::PosixDynamicThreadPool> pool_; | 56 scoped_refptr<base::PosixDynamicThreadPool> pool_; |
57 }; | 57 }; |
58 | 58 |
59 WorkerPoolImpl::WorkerPoolImpl() | 59 WorkerPoolImpl::WorkerPoolImpl() |
60 : pool_(new base::PosixDynamicThreadPool("WorkerPool", | 60 : pool_(new base::PosixDynamicThreadPool("WorkerPool", |
61 kIdleSecondsBeforeExit)) {} | 61 kIdleSecondsBeforeExit)) {} |
62 | 62 |
63 void WorkerPoolImpl::PostTask(const tracked_objects::Location& from_here, | 63 void WorkerPoolImpl::PostTask(const tracked_objects::Location& from_here, |
64 base::Closure task, | 64 base::OnceClosure task, |
65 bool task_is_slow) { | 65 bool task_is_slow) { |
66 pool_->PostTask(from_here, std::move(task)); | 66 pool_->PostTask(from_here, std::move(task)); |
67 } | 67 } |
68 | 68 |
69 base::LazyInstance<WorkerPoolImpl>::Leaky g_lazy_worker_pool = | 69 base::LazyInstance<WorkerPoolImpl>::Leaky g_lazy_worker_pool = |
70 LAZY_INSTANCE_INITIALIZER; | 70 LAZY_INSTANCE_INITIALIZER; |
71 | 71 |
72 class WorkerThread : public PlatformThread::Delegate { | 72 class WorkerThread : public PlatformThread::Delegate { |
73 public: | 73 public: |
74 WorkerThread(const std::string& name_prefix, | 74 WorkerThread(const std::string& name_prefix, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 } | 107 } |
108 | 108 |
109 // The WorkerThread is non-joinable, so it deletes itself. | 109 // The WorkerThread is non-joinable, so it deletes itself. |
110 delete this; | 110 delete this; |
111 } | 111 } |
112 | 112 |
113 } // namespace | 113 } // namespace |
114 | 114 |
115 // static | 115 // static |
116 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, | 116 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, |
117 base::Closure task, | 117 base::OnceClosure task, |
118 bool task_is_slow) { | 118 bool task_is_slow) { |
119 g_lazy_worker_pool.Pointer()->PostTask(from_here, std::move(task), | 119 g_lazy_worker_pool.Pointer()->PostTask(from_here, std::move(task), |
120 task_is_slow); | 120 task_is_slow); |
121 return true; | 121 return true; |
122 } | 122 } |
123 | 123 |
124 // static | 124 // static |
125 bool WorkerPool::RunsTasksOnCurrentThread() { | 125 bool WorkerPool::RunsTasksOnCurrentThread() { |
126 return g_worker_pool_running_on_this_thread.Get().Get(); | 126 return g_worker_pool_running_on_this_thread.Get().Get(); |
127 } | 127 } |
128 | 128 |
129 PosixDynamicThreadPool::PosixDynamicThreadPool(const std::string& name_prefix, | 129 PosixDynamicThreadPool::PosixDynamicThreadPool(const std::string& name_prefix, |
130 int idle_seconds_before_exit) | 130 int idle_seconds_before_exit) |
131 : name_prefix_(name_prefix), | 131 : name_prefix_(name_prefix), |
132 idle_seconds_before_exit_(idle_seconds_before_exit), | 132 idle_seconds_before_exit_(idle_seconds_before_exit), |
133 pending_tasks_available_cv_(&lock_), | 133 pending_tasks_available_cv_(&lock_), |
134 num_idle_threads_(0) {} | 134 num_idle_threads_(0) {} |
135 | 135 |
136 PosixDynamicThreadPool::~PosixDynamicThreadPool() { | 136 PosixDynamicThreadPool::~PosixDynamicThreadPool() { |
137 while (!pending_tasks_.empty()) | 137 while (!pending_tasks_.empty()) |
138 pending_tasks_.pop(); | 138 pending_tasks_.pop(); |
139 } | 139 } |
140 | 140 |
141 void PosixDynamicThreadPool::PostTask( | 141 void PosixDynamicThreadPool::PostTask( |
142 const tracked_objects::Location& from_here, | 142 const tracked_objects::Location& from_here, |
143 base::Closure task) { | 143 base::OnceClosure task) { |
144 PendingTask pending_task(from_here, std::move(task)); | 144 PendingTask pending_task(from_here, std::move(task)); |
145 AddTask(&pending_task); | 145 AddTask(&pending_task); |
146 } | 146 } |
147 | 147 |
148 void PosixDynamicThreadPool::AddTask(PendingTask* pending_task) { | 148 void PosixDynamicThreadPool::AddTask(PendingTask* pending_task) { |
149 DCHECK(pending_task); | 149 DCHECK(pending_task); |
150 DCHECK(pending_task->task); | 150 DCHECK(pending_task->task); |
151 AutoLock locked(lock_); | 151 AutoLock locked(lock_); |
152 | 152 |
153 pending_tasks_.push(std::move(*pending_task)); | 153 pending_tasks_.push(std::move(*pending_task)); |
(...skipping 27 matching lines...) Expand all Loading... |
181 return PendingTask(FROM_HERE, base::Closure()); | 181 return PendingTask(FROM_HERE, base::Closure()); |
182 } | 182 } |
183 } | 183 } |
184 | 184 |
185 PendingTask pending_task = std::move(pending_tasks_.front()); | 185 PendingTask pending_task = std::move(pending_tasks_.front()); |
186 pending_tasks_.pop(); | 186 pending_tasks_.pop(); |
187 return pending_task; | 187 return pending_task; |
188 } | 188 } |
189 | 189 |
190 } // namespace base | 190 } // namespace base |
OLD | NEW |