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
10namespace base {
11
12SequencedWorkerPoolOwner::SequencedWorkerPoolOwner(
13    size_t max_threads,
14    const std::string& thread_name_prefix)
15    : constructor_message_loop_(MessageLoop::current()),
16      pool_(new SequencedWorkerPool(max_threads, thread_name_prefix, this)),
17      has_work_call_count_(0) {}
18
19SequencedWorkerPoolOwner::~SequencedWorkerPoolOwner() {
20  pool_ = NULL;
21  MessageLoop::current()->Run();
22}
23
24const scoped_refptr<SequencedWorkerPool>& SequencedWorkerPoolOwner::pool() {
25  return pool_;
26}
27
28void SequencedWorkerPoolOwner::SetWillWaitForShutdownCallback(
29    const Closure& callback) {
30  will_wait_for_shutdown_callback_ = callback;
31}
32
33int SequencedWorkerPoolOwner::has_work_call_count() const {
34  AutoLock lock(has_work_lock_);
35  return has_work_call_count_;
36}
37
38void SequencedWorkerPoolOwner::OnHasWork() {
39  AutoLock lock(has_work_lock_);
40  ++has_work_call_count_;
41}
42
43void SequencedWorkerPoolOwner::WillWaitForShutdown() {
44  if (!will_wait_for_shutdown_callback_.is_null()) {
45    will_wait_for_shutdown_callback_.Run();
46  }
47}
48
49void SequencedWorkerPoolOwner::OnDestruct() {
50  constructor_message_loop_->PostTask(
51      FROM_HERE,
52      constructor_message_loop_->QuitWhenIdleClosure());
53}
54
55}  // namespace base
56