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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <list> | 9 #include <list> |
10 #include <map> | 10 #include <map> |
11 #include <set> | 11 #include <set> |
12 #include <utility> | 12 #include <utility> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/atomic_sequence_num.h" | 15 #include "base/atomic_sequence_num.h" |
16 #include "base/callback.h" | 16 #include "base/callback.h" |
17 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
18 #include "base/critical_closure.h" | 18 #include "base/critical_closure.h" |
19 #include "base/lazy_instance.h" | 19 #include "base/lazy_instance.h" |
20 #include "base/logging.h" | 20 #include "base/logging.h" |
21 #include "base/macros.h" | 21 #include "base/macros.h" |
22 #include "base/memory/linked_ptr.h" | 22 #include "base/memory/scoped_ptr.h" |
23 #include "base/stl_util.h" | 23 #include "base/stl_util.h" |
24 #include "base/strings/stringprintf.h" | 24 #include "base/strings/stringprintf.h" |
25 #include "base/synchronization/condition_variable.h" | 25 #include "base/synchronization/condition_variable.h" |
26 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
27 #include "base/thread_task_runner_handle.h" | 27 #include "base/thread_task_runner_handle.h" |
28 #include "base/threading/platform_thread.h" | 28 #include "base/threading/platform_thread.h" |
29 #include "base/threading/simple_thread.h" | 29 #include "base/threading/simple_thread.h" |
30 #include "base/threading/thread_local.h" | 30 #include "base/threading/thread_local.h" |
31 #include "base/threading/thread_restrictions.h" | 31 #include "base/threading/thread_restrictions.h" |
32 #include "base/time/time.h" | 32 #include "base/time/time.h" |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 const size_t max_threads_; | 441 const size_t max_threads_; |
442 | 442 |
443 const std::string thread_name_prefix_; | 443 const std::string thread_name_prefix_; |
444 | 444 |
445 // Associates all known sequence token names with their IDs. | 445 // Associates all known sequence token names with their IDs. |
446 std::map<std::string, int> named_sequence_tokens_; | 446 std::map<std::string, int> named_sequence_tokens_; |
447 | 447 |
448 // Owning pointers to all threads we've created so far, indexed by | 448 // Owning pointers to all threads we've created so far, indexed by |
449 // ID. Since we lazily create threads, this may be less than | 449 // ID. Since we lazily create threads, this may be less than |
450 // max_threads_ and will be initially empty. | 450 // max_threads_ and will be initially empty. |
451 typedef std::map<PlatformThreadId, linked_ptr<Worker> > ThreadMap; | 451 using ThreadMap = std::map<PlatformThreadId, scoped_ptr<Worker>>; |
452 ThreadMap threads_; | 452 ThreadMap threads_; |
453 | 453 |
454 // Set to true when we're in the process of creating another thread. | 454 // Set to true when we're in the process of creating another thread. |
455 // See PrepareToStartAdditionalThreadIfHelpful for more. | 455 // See PrepareToStartAdditionalThreadIfHelpful for more. |
456 bool thread_being_created_; | 456 bool thread_being_created_; |
457 | 457 |
458 // Number of threads currently waiting for work. | 458 // Number of threads currently waiting for work. |
459 size_t waiting_thread_count_; | 459 size_t waiting_thread_count_; |
460 | 460 |
461 // Number of threads currently running tasks that have the BLOCK_SHUTDOWN | 461 // Number of threads currently running tasks that have the BLOCK_SHUTDOWN |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
781 bool SequencedWorkerPool::Inner::IsShutdownInProgress() { | 781 bool SequencedWorkerPool::Inner::IsShutdownInProgress() { |
782 AutoLock lock(lock_); | 782 AutoLock lock(lock_); |
783 return shutdown_called_; | 783 return shutdown_called_; |
784 } | 784 } |
785 | 785 |
786 void SequencedWorkerPool::Inner::ThreadLoop(Worker* this_worker) { | 786 void SequencedWorkerPool::Inner::ThreadLoop(Worker* this_worker) { |
787 { | 787 { |
788 AutoLock lock(lock_); | 788 AutoLock lock(lock_); |
789 DCHECK(thread_being_created_); | 789 DCHECK(thread_being_created_); |
790 thread_being_created_ = false; | 790 thread_being_created_ = false; |
791 std::pair<ThreadMap::iterator, bool> result = | 791 auto result = threads_.insert( |
792 threads_.insert( | 792 std::make_pair(this_worker->tid(), make_scoped_ptr(this_worker))); |
793 std::make_pair(this_worker->tid(), make_linked_ptr(this_worker))); | |
794 DCHECK(result.second); | 793 DCHECK(result.second); |
795 | 794 |
796 while (true) { | 795 while (true) { |
797 #if defined(OS_MACOSX) | 796 #if defined(OS_MACOSX) |
798 base::mac::ScopedNSAutoreleasePool autorelease_pool; | 797 base::mac::ScopedNSAutoreleasePool autorelease_pool; |
799 #endif | 798 #endif |
800 | 799 |
801 HandleCleanup(); | 800 HandleCleanup(); |
802 | 801 |
803 // See GetWork for what delete_these_outside_lock is doing. | 802 // See GetWork for what delete_these_outside_lock is doing. |
(...skipping 595 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1399 void SequencedWorkerPool::Shutdown(int max_new_blocking_tasks_after_shutdown) { | 1398 void SequencedWorkerPool::Shutdown(int max_new_blocking_tasks_after_shutdown) { |
1400 DCHECK(constructor_task_runner_->BelongsToCurrentThread()); | 1399 DCHECK(constructor_task_runner_->BelongsToCurrentThread()); |
1401 inner_->Shutdown(max_new_blocking_tasks_after_shutdown); | 1400 inner_->Shutdown(max_new_blocking_tasks_after_shutdown); |
1402 } | 1401 } |
1403 | 1402 |
1404 bool SequencedWorkerPool::IsShutdownInProgress() { | 1403 bool SequencedWorkerPool::IsShutdownInProgress() { |
1405 return inner_->IsShutdownInProgress(); | 1404 return inner_->IsShutdownInProgress(); |
1406 } | 1405 } |
1407 | 1406 |
1408 } // namespace base | 1407 } // namespace base |
OLD | NEW |