| 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/sequenced_worker_pool.h" | 5 #include "base/threading/sequenced_worker_pool.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/atomic_sequence_num.h" | 13 #include "base/atomic_sequence_num.h" |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/critical_closure.h" | 16 #include "base/critical_closure.h" |
| 17 #include "base/debug/trace_event.h" | 17 #include "base/debug/trace_event.h" |
| 18 #include "base/lazy_instance.h" | 18 #include "base/lazy_instance.h" |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/memory/linked_ptr.h" | 20 #include "base/memory/linked_ptr.h" |
| 21 #include "base/message_loop/message_loop_proxy.h" | 21 #include "base/message_loop/message_loop_proxy.h" |
| 22 #include "base/stl_util.h" | 22 #include "base/stl_util.h" |
| 23 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
| 24 #include "base/synchronization/condition_variable.h" | 24 #include "base/synchronization/condition_variable.h" |
| 25 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
| 26 #include "base/thread_task_runner_handle.h" |
| 26 #include "base/threading/platform_thread.h" | 27 #include "base/threading/platform_thread.h" |
| 27 #include "base/threading/simple_thread.h" | 28 #include "base/threading/simple_thread.h" |
| 28 #include "base/threading/thread_local.h" | 29 #include "base/threading/thread_local.h" |
| 29 #include "base/threading/thread_restrictions.h" | 30 #include "base/threading/thread_restrictions.h" |
| 30 #include "base/time/time.h" | 31 #include "base/time/time.h" |
| 31 #include "base/tracked_objects.h" | 32 #include "base/tracked_objects.h" |
| 32 | 33 |
| 33 #if defined(OS_MACOSX) | 34 #if defined(OS_MACOSX) |
| 34 #include "base/mac/scoped_nsautorelease_pool.h" | 35 #include "base/mac/scoped_nsautorelease_pool.h" |
| 35 #elif defined(OS_WIN) | 36 #elif defined(OS_WIN) |
| (...skipping 1086 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1122 // Don't construct lazy instance on check. | 1123 // Don't construct lazy instance on check. |
| 1123 if (g_lazy_tls_ptr == NULL) | 1124 if (g_lazy_tls_ptr == NULL) |
| 1124 return SequenceToken(); | 1125 return SequenceToken(); |
| 1125 | 1126 |
| 1126 SequencedWorkerPool::SequenceToken* token = g_lazy_tls_ptr.Get().Get(); | 1127 SequencedWorkerPool::SequenceToken* token = g_lazy_tls_ptr.Get().Get(); |
| 1127 if (!token) | 1128 if (!token) |
| 1128 return SequenceToken(); | 1129 return SequenceToken(); |
| 1129 return *token; | 1130 return *token; |
| 1130 } | 1131 } |
| 1131 | 1132 |
| 1132 SequencedWorkerPool::SequencedWorkerPool( | 1133 SequencedWorkerPool::SequencedWorkerPool(size_t max_threads, |
| 1133 size_t max_threads, | 1134 const std::string& thread_name_prefix) |
| 1134 const std::string& thread_name_prefix) | 1135 : constructor_task_runner_(ThreadTaskRunnerHandle::IsSet() |
| 1135 : constructor_message_loop_(MessageLoopProxy::current()), | 1136 ? ThreadTaskRunnerHandle::Get() |
| 1137 : NULL), |
| 1136 inner_(new Inner(this, max_threads, thread_name_prefix, NULL)) { | 1138 inner_(new Inner(this, max_threads, thread_name_prefix, NULL)) { |
| 1137 } | 1139 } |
| 1138 | 1140 |
| 1139 SequencedWorkerPool::SequencedWorkerPool( | 1141 SequencedWorkerPool::SequencedWorkerPool(size_t max_threads, |
| 1140 size_t max_threads, | 1142 const std::string& thread_name_prefix, |
| 1141 const std::string& thread_name_prefix, | 1143 TestingObserver* observer) |
| 1142 TestingObserver* observer) | 1144 : constructor_task_runner_(ThreadTaskRunnerHandle::IsSet() |
| 1143 : constructor_message_loop_(MessageLoopProxy::current()), | 1145 ? ThreadTaskRunnerHandle::Get() |
| 1146 : NULL), |
| 1144 inner_(new Inner(this, max_threads, thread_name_prefix, observer)) { | 1147 inner_(new Inner(this, max_threads, thread_name_prefix, observer)) { |
| 1145 } | 1148 } |
| 1146 | 1149 |
| 1147 SequencedWorkerPool::~SequencedWorkerPool() {} | 1150 SequencedWorkerPool::~SequencedWorkerPool() {} |
| 1148 | 1151 |
| 1149 void SequencedWorkerPool::OnDestruct() const { | 1152 void SequencedWorkerPool::OnDestruct() const { |
| 1150 DCHECK(constructor_message_loop_.get()); | 1153 // In unit tests, there may not be a constructor/main task runner set. |
| 1154 // However, in these cases, the task runner is leaked on shutdown, thus this |
| 1155 // code is not hit. |
| 1156 DCHECK(constructor_task_runner_.get()); |
| 1151 // Avoid deleting ourselves on a worker thread (which would | 1157 // Avoid deleting ourselves on a worker thread (which would |
| 1152 // deadlock). | 1158 // deadlock). |
| 1153 if (RunsTasksOnCurrentThread()) { | 1159 if (RunsTasksOnCurrentThread()) { |
| 1154 constructor_message_loop_->DeleteSoon(FROM_HERE, this); | 1160 constructor_task_runner_->DeleteSoon(FROM_HERE, this); |
| 1155 } else { | 1161 } else { |
| 1156 delete this; | 1162 delete this; |
| 1157 } | 1163 } |
| 1158 } | 1164 } |
| 1159 | 1165 |
| 1160 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetSequenceToken() { | 1166 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetSequenceToken() { |
| 1161 return inner_->GetSequenceToken(); | 1167 return inner_->GetSequenceToken(); |
| 1162 } | 1168 } |
| 1163 | 1169 |
| 1164 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetNamedSequenceToken( | 1170 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetNamedSequenceToken( |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1264 | 1270 |
| 1265 void SequencedWorkerPool::FlushForTesting() { | 1271 void SequencedWorkerPool::FlushForTesting() { |
| 1266 inner_->CleanupForTesting(); | 1272 inner_->CleanupForTesting(); |
| 1267 } | 1273 } |
| 1268 | 1274 |
| 1269 void SequencedWorkerPool::SignalHasWorkForTesting() { | 1275 void SequencedWorkerPool::SignalHasWorkForTesting() { |
| 1270 inner_->SignalHasWorkForTesting(); | 1276 inner_->SignalHasWorkForTesting(); |
| 1271 } | 1277 } |
| 1272 | 1278 |
| 1273 void SequencedWorkerPool::Shutdown(int max_new_blocking_tasks_after_shutdown) { | 1279 void SequencedWorkerPool::Shutdown(int max_new_blocking_tasks_after_shutdown) { |
| 1274 DCHECK(constructor_message_loop_->BelongsToCurrentThread()); | 1280 DCHECK(constructor_task_runner_->RunsTasksOnCurrentThread()); |
| 1275 inner_->Shutdown(max_new_blocking_tasks_after_shutdown); | 1281 inner_->Shutdown(max_new_blocking_tasks_after_shutdown); |
| 1276 } | 1282 } |
| 1277 | 1283 |
| 1278 bool SequencedWorkerPool::IsShutdownInProgress() { | 1284 bool SequencedWorkerPool::IsShutdownInProgress() { |
| 1279 return inner_->IsShutdownInProgress(); | 1285 return inner_->IsShutdownInProgress(); |
| 1280 } | 1286 } |
| 1281 | 1287 |
| 1282 } // namespace base | 1288 } // namespace base |
| OLD | NEW |