Chromium Code Reviews| 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/atomicops.h" | 13 #include "base/atomicops.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/logging.h" | 19 #include "base/logging.h" |
| 19 #include "base/memory/linked_ptr.h" | 20 #include "base/memory/linked_ptr.h" |
| 20 #include "base/message_loop/message_loop_proxy.h" | 21 #include "base/message_loop/message_loop_proxy.h" |
| 21 #include "base/metrics/histogram.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/threading/platform_thread.h" | 26 #include "base/threading/platform_thread.h" |
| 27 #include "base/threading/simple_thread.h" | 27 #include "base/threading/simple_thread.h" |
| 28 #include "base/threading/thread_local.h" | |
| 28 #include "base/threading/thread_restrictions.h" | 29 #include "base/threading/thread_restrictions.h" |
| 29 #include "base/time/time.h" | 30 #include "base/time/time.h" |
| 30 #include "base/tracked_objects.h" | 31 #include "base/tracked_objects.h" |
| 31 | 32 |
| 32 #if defined(OS_MACOSX) | 33 #if defined(OS_MACOSX) |
| 33 #include "base/mac/scoped_nsautorelease_pool.h" | 34 #include "base/mac/scoped_nsautorelease_pool.h" |
| 34 #endif | 35 #endif |
| 35 | 36 |
| 37 #if !defined(OS_NACL) | |
| 38 #include "base/metrics/histogram.h" | |
| 39 #endif | |
| 40 | |
| 36 namespace base { | 41 namespace base { |
| 37 | 42 |
| 38 namespace { | 43 namespace { |
| 39 | 44 |
| 45 base::LazyInstance<base::ThreadLocalPointer<SequencedWorkerPool> > | |
| 46 lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; | |
| 47 | |
| 40 struct SequencedTask : public TrackingInfo { | 48 struct SequencedTask : public TrackingInfo { |
| 41 SequencedTask() | 49 SequencedTask() |
| 42 : sequence_token_id(0), | 50 : sequence_token_id(0), |
| 43 trace_id(0), | 51 trace_id(0), |
| 44 sequence_task_number(0), | 52 sequence_task_number(0), |
| 45 shutdown_behavior(SequencedWorkerPool::BLOCK_SHUTDOWN) {} | 53 shutdown_behavior(SequencedWorkerPool::BLOCK_SHUTDOWN) {} |
| 46 | 54 |
| 47 explicit SequencedTask(const tracked_objects::Location& from_here) | 55 explicit SequencedTask(const tracked_objects::Location& from_here) |
| 48 : base::TrackingInfo(from_here, TimeTicks()), | 56 : base::TrackingInfo(from_here, TimeTicks()), |
| 49 sequence_token_id(0), | 57 sequence_token_id(0), |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 260 Inner(SequencedWorkerPool* worker_pool, size_t max_threads, | 268 Inner(SequencedWorkerPool* worker_pool, size_t max_threads, |
| 261 const std::string& thread_name_prefix, | 269 const std::string& thread_name_prefix, |
| 262 TestingObserver* observer); | 270 TestingObserver* observer); |
| 263 | 271 |
| 264 ~Inner(); | 272 ~Inner(); |
| 265 | 273 |
| 266 SequenceToken GetSequenceToken(); | 274 SequenceToken GetSequenceToken(); |
| 267 | 275 |
| 268 SequenceToken GetNamedSequenceToken(const std::string& name); | 276 SequenceToken GetNamedSequenceToken(const std::string& name); |
| 269 | 277 |
| 278 bool GetCurrentThreadSequenceToken(SequenceToken* result_token) const; | |
| 279 | |
| 270 // This function accepts a name and an ID. If the name is null, the | 280 // This function accepts a name and an ID. If the name is null, the |
| 271 // token ID is used. This allows us to implement the optional name lookup | 281 // token ID is used. This allows us to implement the optional name lookup |
| 272 // from a single function without having to enter the lock a separate time. | 282 // from a single function without having to enter the lock a separate time. |
| 273 bool PostTask(const std::string* optional_token_name, | 283 bool PostTask(const std::string* optional_token_name, |
| 274 SequenceToken sequence_token, | 284 SequenceToken sequence_token, |
| 275 WorkerShutdown shutdown_behavior, | 285 WorkerShutdown shutdown_behavior, |
| 276 const tracked_objects::Location& from_here, | 286 const tracked_objects::Location& from_here, |
| 277 const Closure& task, | 287 const Closure& task, |
| 278 TimeDelta delay); | 288 TimeDelta delay); |
| 279 | 289 |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 472 prefix + StringPrintf("Worker%d", thread_number).c_str()), | 482 prefix + StringPrintf("Worker%d", thread_number).c_str()), |
| 473 worker_pool_(worker_pool), | 483 worker_pool_(worker_pool), |
| 474 running_shutdown_behavior_(CONTINUE_ON_SHUTDOWN) { | 484 running_shutdown_behavior_(CONTINUE_ON_SHUTDOWN) { |
| 475 Start(); | 485 Start(); |
| 476 } | 486 } |
| 477 | 487 |
| 478 SequencedWorkerPool::Worker::~Worker() { | 488 SequencedWorkerPool::Worker::~Worker() { |
| 479 } | 489 } |
| 480 | 490 |
| 481 void SequencedWorkerPool::Worker::Run() { | 491 void SequencedWorkerPool::Worker::Run() { |
| 492 // Store the SequencedWorkerPool associated with this worker thread. | |
| 493 lazy_tls_ptr.Get().Set(worker_pool_.get()); | |
| 494 | |
| 482 // Just jump back to the Inner object to run the thread, since it has all the | 495 // Just jump back to the Inner object to run the thread, since it has all the |
| 483 // tracking information and queues. It might be more natural to implement | 496 // tracking information and queues. It might be more natural to implement |
| 484 // using DelegateSimpleThread and have Inner implement the Delegate to avoid | 497 // using DelegateSimpleThread and have Inner implement the Delegate to avoid |
| 485 // having these worker objects at all, but that method lacks the ability to | 498 // having these worker objects at all, but that method lacks the ability to |
| 486 // send thread-specific information easily to the thread loop. | 499 // send thread-specific information easily to the thread loop. |
| 487 worker_pool_->inner_->ThreadLoop(this); | 500 worker_pool_->inner_->ThreadLoop(this); |
| 488 // Release our cyclic reference once we're done. | 501 // Release our cyclic reference once we're done. |
| 489 worker_pool_ = NULL; | 502 worker_pool_ = NULL; |
| 490 } | 503 } |
| 491 | 504 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 536 subtle::NoBarrier_AtomicIncrement(&last_sequence_number_, 1); | 549 subtle::NoBarrier_AtomicIncrement(&last_sequence_number_, 1); |
| 537 return SequenceToken(static_cast<int>(result)); | 550 return SequenceToken(static_cast<int>(result)); |
| 538 } | 551 } |
| 539 | 552 |
| 540 SequencedWorkerPool::SequenceToken | 553 SequencedWorkerPool::SequenceToken |
| 541 SequencedWorkerPool::Inner::GetNamedSequenceToken(const std::string& name) { | 554 SequencedWorkerPool::Inner::GetNamedSequenceToken(const std::string& name) { |
| 542 AutoLock lock(lock_); | 555 AutoLock lock(lock_); |
| 543 return SequenceToken(LockedGetNamedTokenID(name)); | 556 return SequenceToken(LockedGetNamedTokenID(name)); |
| 544 } | 557 } |
| 545 | 558 |
| 559 bool SequencedWorkerPool::Inner::GetCurrentThreadSequenceToken( | |
| 560 SequenceToken* result_token) const { | |
| 561 DCHECK(result_token); | |
| 562 AutoLock lock(lock_); | |
| 563 ThreadMap::const_iterator found = threads_.find(PlatformThread::CurrentId()); | |
|
darin (slow to review)
2013/07/02 13:54:23
Why do we need TLS to find the owner and this map
tommycli
2013/07/02 17:22:58
Sequence tokens are assigned from positive integer
| |
| 564 if (found == threads_.end()) | |
| 565 return false; | |
| 566 *result_token = found->second->running_sequence(); | |
| 567 return true; | |
| 568 } | |
| 569 | |
| 546 bool SequencedWorkerPool::Inner::PostTask( | 570 bool SequencedWorkerPool::Inner::PostTask( |
| 547 const std::string* optional_token_name, | 571 const std::string* optional_token_name, |
| 548 SequenceToken sequence_token, | 572 SequenceToken sequence_token, |
| 549 WorkerShutdown shutdown_behavior, | 573 WorkerShutdown shutdown_behavior, |
| 550 const tracked_objects::Location& from_here, | 574 const tracked_objects::Location& from_here, |
| 551 const Closure& task, | 575 const Closure& task, |
| 552 TimeDelta delay) { | 576 TimeDelta delay) { |
| 553 DCHECK(delay == TimeDelta() || shutdown_behavior == SKIP_ON_SHUTDOWN); | 577 DCHECK(delay == TimeDelta() || shutdown_behavior == SKIP_ON_SHUTDOWN); |
| 554 SequencedTask sequenced(from_here); | 578 SequencedTask sequenced(from_here); |
| 555 sequenced.sequence_token_id = sequence_token.id_; | 579 sequenced.sequence_token_id = sequence_token.id_; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 604 return true; | 628 return true; |
| 605 } | 629 } |
| 606 | 630 |
| 607 bool SequencedWorkerPool::Inner::RunsTasksOnCurrentThread() const { | 631 bool SequencedWorkerPool::Inner::RunsTasksOnCurrentThread() const { |
| 608 AutoLock lock(lock_); | 632 AutoLock lock(lock_); |
| 609 return ContainsKey(threads_, PlatformThread::CurrentId()); | 633 return ContainsKey(threads_, PlatformThread::CurrentId()); |
| 610 } | 634 } |
| 611 | 635 |
| 612 bool SequencedWorkerPool::Inner::IsRunningSequenceOnCurrentThread( | 636 bool SequencedWorkerPool::Inner::IsRunningSequenceOnCurrentThread( |
| 613 SequenceToken sequence_token) const { | 637 SequenceToken sequence_token) const { |
| 614 AutoLock lock(lock_); | 638 SequenceToken current_thread_token; |
| 615 ThreadMap::const_iterator found = threads_.find(PlatformThread::CurrentId()); | 639 if (!GetCurrentThreadSequenceToken(¤t_thread_token)) |
| 616 if (found == threads_.end()) | |
| 617 return false; | 640 return false; |
| 618 return found->second->running_sequence().Equals(sequence_token); | 641 return current_thread_token.Equals(sequence_token); |
| 619 } | 642 } |
| 620 | 643 |
| 621 // See https://code.google.com/p/chromium/issues/detail?id=168415 | 644 // See https://code.google.com/p/chromium/issues/detail?id=168415 |
| 622 void SequencedWorkerPool::Inner::CleanupForTesting() { | 645 void SequencedWorkerPool::Inner::CleanupForTesting() { |
| 623 DCHECK(!RunsTasksOnCurrentThread()); | 646 DCHECK(!RunsTasksOnCurrentThread()); |
| 624 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 647 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 625 AutoLock lock(lock_); | 648 AutoLock lock(lock_); |
| 626 CHECK_EQ(CLEANUP_DONE, cleanup_state_); | 649 CHECK_EQ(CLEANUP_DONE, cleanup_state_); |
| 627 if (shutdown_called_) | 650 if (shutdown_called_) |
| 628 return; | 651 return; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 667 testing_observer_->WillWaitForShutdown(); | 690 testing_observer_->WillWaitForShutdown(); |
| 668 | 691 |
| 669 TimeTicks shutdown_wait_begin = TimeTicks::Now(); | 692 TimeTicks shutdown_wait_begin = TimeTicks::Now(); |
| 670 | 693 |
| 671 { | 694 { |
| 672 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 695 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 673 AutoLock lock(lock_); | 696 AutoLock lock(lock_); |
| 674 while (!CanShutdown()) | 697 while (!CanShutdown()) |
| 675 can_shutdown_cv_.Wait(); | 698 can_shutdown_cv_.Wait(); |
| 676 } | 699 } |
| 700 #if !defined(OS_NACL) | |
| 677 UMA_HISTOGRAM_TIMES("SequencedWorkerPool.ShutdownDelayTime", | 701 UMA_HISTOGRAM_TIMES("SequencedWorkerPool.ShutdownDelayTime", |
| 678 TimeTicks::Now() - shutdown_wait_begin); | 702 TimeTicks::Now() - shutdown_wait_begin); |
| 703 #endif | |
| 679 } | 704 } |
| 680 | 705 |
| 681 void SequencedWorkerPool::Inner::ThreadLoop(Worker* this_worker) { | 706 void SequencedWorkerPool::Inner::ThreadLoop(Worker* this_worker) { |
| 682 { | 707 { |
| 683 AutoLock lock(lock_); | 708 AutoLock lock(lock_); |
| 684 DCHECK(thread_being_created_); | 709 DCHECK(thread_being_created_); |
| 685 thread_being_created_ = false; | 710 thread_being_created_ = false; |
| 686 std::pair<ThreadMap::iterator, bool> result = | 711 std::pair<ThreadMap::iterator, bool> result = |
| 687 threads_.insert( | 712 threads_.insert( |
| 688 std::make_pair(this_worker->tid(), make_linked_ptr(this_worker))); | 713 std::make_pair(this_worker->tid(), make_linked_ptr(this_worker))); |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 865 return CONTINUE_ON_SHUTDOWN; | 890 return CONTINUE_ON_SHUTDOWN; |
| 866 return found->second->running_shutdown_behavior(); | 891 return found->second->running_shutdown_behavior(); |
| 867 } | 892 } |
| 868 | 893 |
| 869 SequencedWorkerPool::Inner::GetWorkStatus SequencedWorkerPool::Inner::GetWork( | 894 SequencedWorkerPool::Inner::GetWorkStatus SequencedWorkerPool::Inner::GetWork( |
| 870 SequencedTask* task, | 895 SequencedTask* task, |
| 871 TimeDelta* wait_time, | 896 TimeDelta* wait_time, |
| 872 std::vector<Closure>* delete_these_outside_lock) { | 897 std::vector<Closure>* delete_these_outside_lock) { |
| 873 lock_.AssertAcquired(); | 898 lock_.AssertAcquired(); |
| 874 | 899 |
| 900 #if !defined(OS_NACL) | |
| 875 UMA_HISTOGRAM_COUNTS_100("SequencedWorkerPool.TaskCount", | 901 UMA_HISTOGRAM_COUNTS_100("SequencedWorkerPool.TaskCount", |
| 876 static_cast<int>(pending_tasks_.size())); | 902 static_cast<int>(pending_tasks_.size())); |
| 903 #endif | |
| 877 | 904 |
| 878 // Find the next task with a sequence token that's not currently in use. | 905 // Find the next task with a sequence token that's not currently in use. |
| 879 // If the token is in use, that means another thread is running something | 906 // If the token is in use, that means another thread is running something |
| 880 // in that sequence, and we can't run it without going out-of-order. | 907 // in that sequence, and we can't run it without going out-of-order. |
| 881 // | 908 // |
| 882 // This algorithm is simple and fair, but inefficient in some cases. For | 909 // This algorithm is simple and fair, but inefficient in some cases. For |
| 883 // example, say somebody schedules 1000 slow tasks with the same sequence | 910 // example, say somebody schedules 1000 slow tasks with the same sequence |
| 884 // number. We'll have to go through all those tasks each time we feel like | 911 // number. We'll have to go through all those tasks each time we feel like |
| 885 // there might be work to schedule. If this proves to be a problem, we | 912 // there might be work to schedule. If this proves to be a problem, we |
| 886 // should make this more efficient. | 913 // should make this more efficient. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 951 blocking_shutdown_pending_task_count_--; | 978 blocking_shutdown_pending_task_count_--; |
| 952 } | 979 } |
| 953 | 980 |
| 954 status = GET_WORK_FOUND; | 981 status = GET_WORK_FOUND; |
| 955 break; | 982 break; |
| 956 } | 983 } |
| 957 | 984 |
| 958 // Track the number of tasks we had to skip over to see if we should be | 985 // Track the number of tasks we had to skip over to see if we should be |
| 959 // making this more efficient. If this number ever becomes large or is | 986 // making this more efficient. If this number ever becomes large or is |
| 960 // frequently "some", we should consider the optimization above. | 987 // frequently "some", we should consider the optimization above. |
| 988 #if !defined(OS_NACL) | |
| 961 UMA_HISTOGRAM_COUNTS_100("SequencedWorkerPool.UnrunnableTaskCount", | 989 UMA_HISTOGRAM_COUNTS_100("SequencedWorkerPool.UnrunnableTaskCount", |
| 962 unrunnable_tasks); | 990 unrunnable_tasks); |
| 991 #endif | |
| 963 return status; | 992 return status; |
| 964 } | 993 } |
| 965 | 994 |
| 966 int SequencedWorkerPool::Inner::WillRunWorkerTask(const SequencedTask& task) { | 995 int SequencedWorkerPool::Inner::WillRunWorkerTask(const SequencedTask& task) { |
| 967 lock_.AssertAcquired(); | 996 lock_.AssertAcquired(); |
| 968 | 997 |
| 969 // Mark the task's sequence number as in use. | 998 // Mark the task's sequence number as in use. |
| 970 if (task.sequence_token_id) | 999 if (task.sequence_token_id) |
| 971 current_sequences_.insert(task.sequence_token_id); | 1000 current_sequences_.insert(task.sequence_token_id); |
| 972 | 1001 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1120 | 1149 |
| 1121 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetSequenceToken() { | 1150 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetSequenceToken() { |
| 1122 return inner_->GetSequenceToken(); | 1151 return inner_->GetSequenceToken(); |
| 1123 } | 1152 } |
| 1124 | 1153 |
| 1125 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetNamedSequenceToken( | 1154 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetNamedSequenceToken( |
| 1126 const std::string& name) { | 1155 const std::string& name) { |
| 1127 return inner_->GetNamedSequenceToken(name); | 1156 return inner_->GetNamedSequenceToken(name); |
| 1128 } | 1157 } |
| 1129 | 1158 |
| 1159 bool SequencedWorkerPool::GetCurrentThreadSequenceToken( | |
| 1160 SequenceToken* result_token) const { | |
| 1161 return inner_->GetCurrentThreadSequenceToken(result_token); | |
| 1162 } | |
| 1163 | |
| 1130 scoped_refptr<SequencedTaskRunner> SequencedWorkerPool::GetSequencedTaskRunner( | 1164 scoped_refptr<SequencedTaskRunner> SequencedWorkerPool::GetSequencedTaskRunner( |
| 1131 SequenceToken token) { | 1165 SequenceToken token) { |
| 1132 return GetSequencedTaskRunnerWithShutdownBehavior(token, BLOCK_SHUTDOWN); | 1166 return GetSequencedTaskRunnerWithShutdownBehavior(token, BLOCK_SHUTDOWN); |
| 1133 } | 1167 } |
| 1134 | 1168 |
| 1135 scoped_refptr<SequencedTaskRunner> | 1169 scoped_refptr<SequencedTaskRunner> |
| 1136 SequencedWorkerPool::GetSequencedTaskRunnerWithShutdownBehavior( | 1170 SequencedWorkerPool::GetSequencedTaskRunnerWithShutdownBehavior( |
| 1137 SequenceToken token, WorkerShutdown shutdown_behavior) { | 1171 SequenceToken token, WorkerShutdown shutdown_behavior) { |
| 1138 return new SequencedWorkerPoolSequencedTaskRunner( | 1172 return new SequencedWorkerPoolSequencedTaskRunner( |
| 1139 this, token, shutdown_behavior); | 1173 this, token, shutdown_behavior); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1229 | 1263 |
| 1230 void SequencedWorkerPool::SignalHasWorkForTesting() { | 1264 void SequencedWorkerPool::SignalHasWorkForTesting() { |
| 1231 inner_->SignalHasWorkForTesting(); | 1265 inner_->SignalHasWorkForTesting(); |
| 1232 } | 1266 } |
| 1233 | 1267 |
| 1234 void SequencedWorkerPool::Shutdown(int max_new_blocking_tasks_after_shutdown) { | 1268 void SequencedWorkerPool::Shutdown(int max_new_blocking_tasks_after_shutdown) { |
| 1235 DCHECK(constructor_message_loop_->BelongsToCurrentThread()); | 1269 DCHECK(constructor_message_loop_->BelongsToCurrentThread()); |
| 1236 inner_->Shutdown(max_new_blocking_tasks_after_shutdown); | 1270 inner_->Shutdown(max_new_blocking_tasks_after_shutdown); |
| 1237 } | 1271 } |
| 1238 | 1272 |
| 1273 // static | |
| 1274 SequencedWorkerPool* SequencedWorkerPool::Owner() { | |
| 1275 return lazy_tls_ptr.Get().Get(); | |
| 1276 } | |
| 1277 | |
| 1239 } // namespace base | 1278 } // namespace base |
| OLD | NEW |