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