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