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 3 // found in the LICENSE file. 4 5 #include "base/test/sequenced_worker_pool_owner.h" 6 7 #include "base/location.h" 8 #include "base/message_loop/message_loop.h" 9 #include "base/single_thread_task_runner.h" 10 11 namespace base { 12 13 SequencedWorkerPoolOwner::SequencedWorkerPoolOwner( 14 size_t max_threads, 15 const std::string& thread_name_prefix) 16 : constructor_message_loop_(MessageLoop::current()), 17 pool_(new SequencedWorkerPool(max_threads, 18 thread_name_prefix, 19 TaskPriority::USER_VISIBLE, 20 this)), 21 has_work_call_count_(0) {} 22 23 SequencedWorkerPoolOwner::~SequencedWorkerPoolOwner() { 24 pool_->Shutdown(); 25 pool_ = NULL; 26 27 // Spin the current message loop until SWP destruction verified in OnDestruct. 28 exit_loop_.Run(); 29 } 30 31 const scoped_refptr<SequencedWorkerPool>& SequencedWorkerPoolOwner::pool() 32 const { 33 return pool_; 34 } 35 36 void SequencedWorkerPoolOwner::SetWillWaitForShutdownCallback( 37 const Closure& callback) { 38 will_wait_for_shutdown_callback_ = callback; 39 } 40 41 int SequencedWorkerPoolOwner::has_work_call_count() const { 42 AutoLock lock(has_work_lock_); 43 return has_work_call_count_; 44 } 45 46 void SequencedWorkerPoolOwner::OnHasWork() { 47 AutoLock lock(has_work_lock_); 48 ++has_work_call_count_; 49 } 50 51 void SequencedWorkerPoolOwner::WillWaitForShutdown() { 52 if (!will_wait_for_shutdown_callback_.is_null()) { 53 will_wait_for_shutdown_callback_.Run(); 54 55 // Release the reference to the callback to prevent retain cycles. 56 will_wait_for_shutdown_callback_ = Closure(); 57 } 58 } 59 60 void SequencedWorkerPoolOwner::OnDestruct() { 61 constructor_message_loop_->task_runner()->PostTask(FROM_HERE, 62 exit_loop_.QuitClosure()); 63 } 64 65 } // namespace base 66