1// Copyright (c) 2010 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/threading/simple_thread.h" 6 7#include "base/logging.h" 8#include "base/strings/string_number_conversions.h" 9#include "base/threading/platform_thread.h" 10#include "base/threading/thread_restrictions.h" 11 12namespace base { 13 14SimpleThread::SimpleThread(const std::string& name_prefix) 15 : name_prefix_(name_prefix), 16 name_(name_prefix), 17 thread_(), 18 event_(WaitableEvent::ResetPolicy::MANUAL, 19 WaitableEvent::InitialState::NOT_SIGNALED), 20 tid_(0), 21 joined_(false) {} 22 23SimpleThread::SimpleThread(const std::string& name_prefix, 24 const Options& options) 25 : name_prefix_(name_prefix), 26 name_(name_prefix), 27 options_(options), 28 thread_(), 29 event_(WaitableEvent::ResetPolicy::MANUAL, 30 WaitableEvent::InitialState::NOT_SIGNALED), 31 tid_(0), 32 joined_(false) {} 33 34SimpleThread::~SimpleThread() { 35 DCHECK(HasBeenStarted()) << "SimpleThread was never started."; 36 DCHECK(HasBeenJoined()) << "SimpleThread destroyed without being Join()ed."; 37} 38 39void SimpleThread::Start() { 40 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; 41 bool success; 42 if (options_.priority() == ThreadPriority::NORMAL) { 43 success = PlatformThread::Create(options_.stack_size(), this, &thread_); 44 } else { 45 success = PlatformThread::CreateWithPriority(options_.stack_size(), this, 46 &thread_, options_.priority()); 47 } 48 DCHECK(success); 49 base::ThreadRestrictions::ScopedAllowWait allow_wait; 50 event_.Wait(); // Wait for the thread to complete initialization. 51} 52 53void SimpleThread::Join() { 54 DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread."; 55 DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times."; 56 PlatformThread::Join(thread_); 57 joined_ = true; 58} 59 60bool SimpleThread::HasBeenStarted() { 61 base::ThreadRestrictions::ScopedAllowWait allow_wait; 62 return event_.IsSignaled(); 63} 64 65void SimpleThread::ThreadMain() { 66 tid_ = PlatformThread::CurrentId(); 67 // Construct our full name of the form "name_prefix_/TID". 68 name_.push_back('/'); 69 name_.append(IntToString(tid_)); 70 PlatformThread::SetName(name_); 71 72 // We've initialized our new thread, signal that we're done to Start(). 73 event_.Signal(); 74 75 Run(); 76} 77 78DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, 79 const std::string& name_prefix) 80 : SimpleThread(name_prefix), 81 delegate_(delegate) { 82} 83 84DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, 85 const std::string& name_prefix, 86 const Options& options) 87 : SimpleThread(name_prefix, options), 88 delegate_(delegate) { 89} 90 91DelegateSimpleThread::~DelegateSimpleThread() { 92} 93 94void DelegateSimpleThread::Run() { 95 DCHECK(delegate_) << "Tried to call Run without a delegate (called twice?)"; 96 delegate_->Run(); 97 delegate_ = NULL; 98} 99 100DelegateSimpleThreadPool::DelegateSimpleThreadPool( 101 const std::string& name_prefix, 102 int num_threads) 103 : name_prefix_(name_prefix), 104 num_threads_(num_threads), 105 dry_(WaitableEvent::ResetPolicy::MANUAL, 106 WaitableEvent::InitialState::NOT_SIGNALED) {} 107 108DelegateSimpleThreadPool::~DelegateSimpleThreadPool() { 109 DCHECK(threads_.empty()); 110 DCHECK(delegates_.empty()); 111 DCHECK(!dry_.IsSignaled()); 112} 113 114void DelegateSimpleThreadPool::Start() { 115 DCHECK(threads_.empty()) << "Start() called with outstanding threads."; 116 for (int i = 0; i < num_threads_; ++i) { 117 DelegateSimpleThread* thread = new DelegateSimpleThread(this, name_prefix_); 118 thread->Start(); 119 threads_.push_back(thread); 120 } 121} 122 123void DelegateSimpleThreadPool::JoinAll() { 124 DCHECK(!threads_.empty()) << "JoinAll() called with no outstanding threads."; 125 126 // Tell all our threads to quit their worker loop. 127 AddWork(NULL, num_threads_); 128 129 // Join and destroy all the worker threads. 130 for (int i = 0; i < num_threads_; ++i) { 131 threads_[i]->Join(); 132 delete threads_[i]; 133 } 134 threads_.clear(); 135 DCHECK(delegates_.empty()); 136} 137 138void DelegateSimpleThreadPool::AddWork(Delegate* delegate, int repeat_count) { 139 AutoLock locked(lock_); 140 for (int i = 0; i < repeat_count; ++i) 141 delegates_.push(delegate); 142 // If we were empty, signal that we have work now. 143 if (!dry_.IsSignaled()) 144 dry_.Signal(); 145} 146 147void DelegateSimpleThreadPool::Run() { 148 Delegate* work = NULL; 149 150 while (true) { 151 dry_.Wait(); 152 { 153 AutoLock locked(lock_); 154 if (!dry_.IsSignaled()) 155 continue; 156 157 DCHECK(!delegates_.empty()); 158 work = delegates_.front(); 159 delegates_.pop(); 160 161 // Signal to any other threads that we're currently out of work. 162 if (delegates_.empty()) 163 dry_.Reset(); 164 } 165 166 // A NULL delegate pointer signals us to quit. 167 if (!work) 168 break; 169 170 work->Run(); 171 } 172} 173 174} // namespace base 175