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/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/memory/linked_ptr.h" | 19 #include "base/memory/linked_ptr.h" |
| 20 #include "base/message_loop/message_loop_proxy.h" |
20 #include "base/stl_util.h" | 21 #include "base/stl_util.h" |
21 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
22 #include "base/synchronization/condition_variable.h" | 23 #include "base/synchronization/condition_variable.h" |
23 #include "base/synchronization/lock.h" | 24 #include "base/synchronization/lock.h" |
24 #include "base/thread_task_runner_handle.h" | |
25 #include "base/threading/platform_thread.h" | 25 #include "base/threading/platform_thread.h" |
26 #include "base/threading/simple_thread.h" | 26 #include "base/threading/simple_thread.h" |
27 #include "base/threading/thread_local.h" | 27 #include "base/threading/thread_local.h" |
28 #include "base/threading/thread_restrictions.h" | 28 #include "base/threading/thread_restrictions.h" |
29 #include "base/time/time.h" | 29 #include "base/time/time.h" |
30 #include "base/trace_event/trace_event.h" | 30 #include "base/trace_event/trace_event.h" |
31 #include "base/tracked_objects.h" | 31 #include "base/tracked_objects.h" |
32 | 32 |
33 #if defined(OS_MACOSX) | 33 #if defined(OS_MACOSX) |
34 #include "base/mac/scoped_nsautorelease_pool.h" | 34 #include "base/mac/scoped_nsautorelease_pool.h" |
(...skipping 1116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1151 // Don't construct lazy instance on check. | 1151 // Don't construct lazy instance on check. |
1152 if (g_lazy_tls_ptr == NULL) | 1152 if (g_lazy_tls_ptr == NULL) |
1153 return SequenceToken(); | 1153 return SequenceToken(); |
1154 | 1154 |
1155 SequencedWorkerPool::SequenceToken* token = g_lazy_tls_ptr.Get().Get(); | 1155 SequencedWorkerPool::SequenceToken* token = g_lazy_tls_ptr.Get().Get(); |
1156 if (!token) | 1156 if (!token) |
1157 return SequenceToken(); | 1157 return SequenceToken(); |
1158 return *token; | 1158 return *token; |
1159 } | 1159 } |
1160 | 1160 |
1161 SequencedWorkerPool::SequencedWorkerPool(size_t max_threads, | 1161 SequencedWorkerPool::SequencedWorkerPool( |
1162 const std::string& thread_name_prefix) | 1162 size_t max_threads, |
1163 : constructor_task_runner_(ThreadTaskRunnerHandle::Get()), | 1163 const std::string& thread_name_prefix) |
| 1164 : constructor_message_loop_(MessageLoopProxy::current()), |
1164 inner_(new Inner(this, max_threads, thread_name_prefix, NULL)) { | 1165 inner_(new Inner(this, max_threads, thread_name_prefix, NULL)) { |
1165 } | 1166 } |
1166 | 1167 |
1167 SequencedWorkerPool::SequencedWorkerPool(size_t max_threads, | 1168 SequencedWorkerPool::SequencedWorkerPool( |
1168 const std::string& thread_name_prefix, | 1169 size_t max_threads, |
1169 TestingObserver* observer) | 1170 const std::string& thread_name_prefix, |
1170 : constructor_task_runner_(ThreadTaskRunnerHandle::Get()), | 1171 TestingObserver* observer) |
| 1172 : constructor_message_loop_(MessageLoopProxy::current()), |
1171 inner_(new Inner(this, max_threads, thread_name_prefix, observer)) { | 1173 inner_(new Inner(this, max_threads, thread_name_prefix, observer)) { |
1172 } | 1174 } |
1173 | 1175 |
1174 SequencedWorkerPool::~SequencedWorkerPool() {} | 1176 SequencedWorkerPool::~SequencedWorkerPool() {} |
1175 | 1177 |
1176 void SequencedWorkerPool::OnDestruct() const { | 1178 void SequencedWorkerPool::OnDestruct() const { |
1177 DCHECK(constructor_task_runner_); | 1179 DCHECK(constructor_message_loop_.get()); |
1178 // Avoid deleting ourselves on a worker thread (which would | 1180 // Avoid deleting ourselves on a worker thread (which would |
1179 // deadlock). | 1181 // deadlock). |
1180 if (RunsTasksOnCurrentThread()) { | 1182 if (RunsTasksOnCurrentThread()) { |
1181 constructor_task_runner_->DeleteSoon(FROM_HERE, this); | 1183 constructor_message_loop_->DeleteSoon(FROM_HERE, this); |
1182 } else { | 1184 } else { |
1183 delete this; | 1185 delete this; |
1184 } | 1186 } |
1185 } | 1187 } |
1186 | 1188 |
1187 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetSequenceToken() { | 1189 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetSequenceToken() { |
1188 return inner_->GetSequenceToken(); | 1190 return inner_->GetSequenceToken(); |
1189 } | 1191 } |
1190 | 1192 |
1191 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetNamedSequenceToken( | 1193 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetNamedSequenceToken( |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1291 | 1293 |
1292 void SequencedWorkerPool::FlushForTesting() { | 1294 void SequencedWorkerPool::FlushForTesting() { |
1293 inner_->CleanupForTesting(); | 1295 inner_->CleanupForTesting(); |
1294 } | 1296 } |
1295 | 1297 |
1296 void SequencedWorkerPool::SignalHasWorkForTesting() { | 1298 void SequencedWorkerPool::SignalHasWorkForTesting() { |
1297 inner_->SignalHasWorkForTesting(); | 1299 inner_->SignalHasWorkForTesting(); |
1298 } | 1300 } |
1299 | 1301 |
1300 void SequencedWorkerPool::Shutdown(int max_new_blocking_tasks_after_shutdown) { | 1302 void SequencedWorkerPool::Shutdown(int max_new_blocking_tasks_after_shutdown) { |
1301 DCHECK(constructor_task_runner_->BelongsToCurrentThread()); | 1303 DCHECK(constructor_message_loop_->BelongsToCurrentThread()); |
1302 inner_->Shutdown(max_new_blocking_tasks_after_shutdown); | 1304 inner_->Shutdown(max_new_blocking_tasks_after_shutdown); |
1303 } | 1305 } |
1304 | 1306 |
1305 bool SequencedWorkerPool::IsShutdownInProgress() { | 1307 bool SequencedWorkerPool::IsShutdownInProgress() { |
1306 return inner_->IsShutdownInProgress(); | 1308 return inner_->IsShutdownInProgress(); |
1307 } | 1309 } |
1308 | 1310 |
1309 } // namespace base | 1311 } // namespace base |
OLD | NEW |