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