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 11namespace base { 12 13SequencedWorkerPoolOwner::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, thread_name_prefix, this)), 18 has_work_call_count_(0) {} 19 20SequencedWorkerPoolOwner::~SequencedWorkerPoolOwner() { 21 pool_->Shutdown(); 22 pool_ = NULL; 23 24 // Spin the current message loop until SWP destruction verified in OnDestruct. 25 exit_loop_.Run(); 26} 27 28const scoped_refptr<SequencedWorkerPool>& SequencedWorkerPoolOwner::pool() { 29 return pool_; 30} 31 32void SequencedWorkerPoolOwner::SetWillWaitForShutdownCallback( 33 const Closure& callback) { 34 will_wait_for_shutdown_callback_ = callback; 35} 36 37int SequencedWorkerPoolOwner::has_work_call_count() const { 38 AutoLock lock(has_work_lock_); 39 return has_work_call_count_; 40} 41 42void SequencedWorkerPoolOwner::OnHasWork() { 43 AutoLock lock(has_work_lock_); 44 ++has_work_call_count_; 45} 46 47void SequencedWorkerPoolOwner::WillWaitForShutdown() { 48 if (!will_wait_for_shutdown_callback_.is_null()) { 49 will_wait_for_shutdown_callback_.Run(); 50 51 // Release the reference to the callback to prevent retain cycles. 52 will_wait_for_shutdown_callback_ = Closure(); 53 } 54} 55 56void SequencedWorkerPoolOwner::OnDestruct() { 57 constructor_message_loop_->PostTask(FROM_HERE, exit_loop_.QuitClosure()); 58} 59 60} // namespace base 61