Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(604)

Side by Side Diff: base/threading/sequenced_worker_pool.cc

Issue 9558007: Ensure that SequencedWorkerPools in tests don't outlive their tests (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix test failures Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <vector> 10 #include <vector>
11 11
12 #include "base/atomicops.h" 12 #include "base/atomicops.h"
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/compiler_specific.h" 14 #include "base/compiler_specific.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/memory/linked_ptr.h" 16 #include "base/memory/linked_ptr.h"
17 #include "base/message_loop_proxy.h"
17 #include "base/metrics/histogram.h" 18 #include "base/metrics/histogram.h"
18 #include "base/stringprintf.h" 19 #include "base/stringprintf.h"
19 #include "base/synchronization/condition_variable.h" 20 #include "base/synchronization/condition_variable.h"
20 #include "base/synchronization/lock.h" 21 #include "base/synchronization/lock.h"
21 #include "base/threading/simple_thread.h" 22 #include "base/threading/simple_thread.h"
22 #include "base/time.h" 23 #include "base/time.h"
23 #include "base/tracked_objects.h" 24 #include "base/tracked_objects.h"
24 25
25 namespace base { 26 namespace base {
26 27
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 252
252 SequencedWorkerPool::Inner::~Inner() { 253 SequencedWorkerPool::Inner::~Inner() {
253 // You must call Shutdown() before destroying the pool. 254 // You must call Shutdown() before destroying the pool.
254 DCHECK(shutdown_called_); 255 DCHECK(shutdown_called_);
255 256
256 // Need to explicitly join with the threads before they're destroyed or else 257 // Need to explicitly join with the threads before they're destroyed or else
257 // they will be running when our object is half torn down. 258 // they will be running when our object is half torn down.
258 for (size_t i = 0; i < threads_.size(); i++) 259 for (size_t i = 0; i < threads_.size(); i++)
259 threads_[i]->Join(); 260 threads_[i]->Join();
260 threads_.clear(); 261 threads_.clear();
262
263 if (testing_observer_)
264 testing_observer_->OnDestruct();
261 } 265 }
262 266
263 SequencedWorkerPool::SequenceToken 267 SequencedWorkerPool::SequenceToken
264 SequencedWorkerPool::Inner::GetSequenceToken() { 268 SequencedWorkerPool::Inner::GetSequenceToken() {
265 subtle::Atomic32 result = 269 subtle::Atomic32 result =
266 subtle::NoBarrier_AtomicIncrement(&last_sequence_number_, 1); 270 subtle::NoBarrier_AtomicIncrement(&last_sequence_number_, 1);
267 return SequenceToken(static_cast<int>(result)); 271 return SequenceToken(static_cast<int>(result));
268 } 272 }
269 273
270 SequencedWorkerPool::SequenceToken 274 SequencedWorkerPool::SequenceToken
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
628 return !thread_being_created_ && 632 return !thread_being_created_ &&
629 blocking_shutdown_thread_count_ == 0 && 633 blocking_shutdown_thread_count_ == 0 &&
630 blocking_shutdown_pending_task_count_ == 0; 634 blocking_shutdown_pending_task_count_ == 0;
631 } 635 }
632 636
633 // SequencedWorkerPool -------------------------------------------------------- 637 // SequencedWorkerPool --------------------------------------------------------
634 638
635 SequencedWorkerPool::SequencedWorkerPool( 639 SequencedWorkerPool::SequencedWorkerPool(
636 size_t max_threads, 640 size_t max_threads,
637 const std::string& thread_name_prefix) 641 const std::string& thread_name_prefix)
638 : inner_(new Inner(ALLOW_THIS_IN_INITIALIZER_LIST(this), 642 : constructor_message_loop_(MessageLoopProxy::current()),
639 max_threads, thread_name_prefix)) {} 643 inner_(new Inner(ALLOW_THIS_IN_INITIALIZER_LIST(this),
644 max_threads, thread_name_prefix)) {
645 DCHECK(constructor_message_loop_.get());
646 }
640 647
641 SequencedWorkerPool::~SequencedWorkerPool() {} 648 SequencedWorkerPool::~SequencedWorkerPool() {}
642 649
650 void SequencedWorkerPool::OnDestruct() const {
651 // TODO(akalin): Once we can easily check if we're on a worker
652 // thread or not, use that instead of restricting destruction to
653 // only the constructor message loop.
654 if (constructor_message_loop_->BelongsToCurrentThread()) {
655 LOG(INFO) << "Deleting on this thread";
656 delete this;
657 } else {
658 LOG(INFO) << "Deleting soon";
659 constructor_message_loop_->DeleteSoon(FROM_HERE, this);
660 }
661 }
662
643 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetSequenceToken() { 663 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetSequenceToken() {
644 return inner_->GetSequenceToken(); 664 return inner_->GetSequenceToken();
645 } 665 }
646 666
647 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetNamedSequenceToken( 667 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetNamedSequenceToken(
648 const std::string& name) { 668 const std::string& name) {
649 return inner_->GetNamedSequenceToken(name); 669 return inner_->GetNamedSequenceToken(name);
650 } 670 }
651 671
652 bool SequencedWorkerPool::PostWorkerTask( 672 bool SequencedWorkerPool::PostWorkerTask(
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 741
722 void SequencedWorkerPool::Shutdown() { 742 void SequencedWorkerPool::Shutdown() {
723 inner_->Shutdown(); 743 inner_->Shutdown();
724 } 744 }
725 745
726 void SequencedWorkerPool::SetTestingObserver(TestingObserver* observer) { 746 void SequencedWorkerPool::SetTestingObserver(TestingObserver* observer) {
727 inner_->SetTestingObserver(observer); 747 inner_->SetTestingObserver(observer);
728 } 748 }
729 749
730 } // namespace base 750 } // namespace base
OLDNEW
« no previous file with comments | « base/threading/sequenced_worker_pool.h ('k') | base/threading/sequenced_worker_pool_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698