| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/threading/simple_thread.h" | 5 #include "base/threading/simple_thread.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
| 10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| 11 | 11 |
| 12 namespace base { | 12 namespace base { |
| 13 | 13 |
| 14 SimpleThread::SimpleThread(const std::string& name_prefix) | 14 SimpleThread::SimpleThread(const std::string& name_prefix) |
| 15 : name_prefix_(name_prefix), | 15 : SimpleThread(name_prefix, Options()) {} |
| 16 name_(name_prefix), | |
| 17 thread_(), | |
| 18 event_(WaitableEvent::ResetPolicy::MANUAL, | |
| 19 WaitableEvent::InitialState::NOT_SIGNALED), | |
| 20 tid_(0), | |
| 21 joined_(false) {} | |
| 22 | 16 |
| 23 SimpleThread::SimpleThread(const std::string& name_prefix, | 17 SimpleThread::SimpleThread(const std::string& name_prefix, |
| 24 const Options& options) | 18 const Options& options) |
| 25 : name_prefix_(name_prefix), | 19 : name_prefix_(name_prefix), |
| 26 name_(name_prefix), | 20 name_(name_prefix), |
| 27 options_(options), | 21 options_(options), |
| 28 thread_(), | |
| 29 event_(WaitableEvent::ResetPolicy::MANUAL, | 22 event_(WaitableEvent::ResetPolicy::MANUAL, |
| 30 WaitableEvent::InitialState::NOT_SIGNALED), | 23 WaitableEvent::InitialState::NOT_SIGNALED) {} |
| 31 tid_(0), | |
| 32 joined_(false) {} | |
| 33 | 24 |
| 34 SimpleThread::~SimpleThread() { | 25 SimpleThread::~SimpleThread() { |
| 35 DCHECK(HasBeenStarted()) << "SimpleThread was never started."; | 26 DCHECK(HasBeenStarted()) << "SimpleThread was never started."; |
| 36 DCHECK(HasBeenJoined()) << "SimpleThread destroyed without being Join()ed."; | 27 DCHECK(!options_.joinable || HasBeenJoined()) |
| 28 << "Joinable SimpleThread destroyed without being Join()ed."; |
| 37 } | 29 } |
| 38 | 30 |
| 39 void SimpleThread::Start() { | 31 void SimpleThread::Start() { |
| 40 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; | 32 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; |
| 41 bool success; | 33 bool success = |
| 42 if (options_.priority() == ThreadPriority::NORMAL) { | 34 options_.joinable |
| 43 success = PlatformThread::Create(options_.stack_size(), this, &thread_); | 35 ? PlatformThread::CreateWithPriority(options_.stack_size, this, |
| 44 } else { | 36 &thread_, options_.priority) |
| 45 success = PlatformThread::CreateWithPriority(options_.stack_size(), this, | 37 : PlatformThread::CreateNonJoinableWithPriority( |
| 46 &thread_, options_.priority()); | 38 options_.stack_size, this, options_.priority); |
| 47 } | |
| 48 DCHECK(success); | 39 DCHECK(success); |
| 49 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 40 ThreadRestrictions::ScopedAllowWait allow_wait; |
| 50 event_.Wait(); // Wait for the thread to complete initialization. | 41 event_.Wait(); // Wait for the thread to complete initialization. |
| 51 } | 42 } |
| 52 | 43 |
| 53 void SimpleThread::Join() { | 44 void SimpleThread::Join() { |
| 45 DCHECK(options_.joinable) << "A non-joinable thread can't be joined."; |
| 54 DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread."; | 46 DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread."; |
| 55 DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times."; | 47 DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times."; |
| 56 PlatformThread::Join(thread_); | 48 PlatformThread::Join(thread_); |
| 49 thread_ = PlatformThreadHandle(); |
| 57 joined_ = true; | 50 joined_ = true; |
| 58 } | 51 } |
| 59 | 52 |
| 60 bool SimpleThread::HasBeenStarted() { | 53 bool SimpleThread::HasBeenStarted() { |
| 61 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 54 ThreadRestrictions::ScopedAllowWait allow_wait; |
| 62 return event_.IsSignaled(); | 55 return event_.IsSignaled(); |
| 63 } | 56 } |
| 64 | 57 |
| 65 void SimpleThread::ThreadMain() { | 58 void SimpleThread::ThreadMain() { |
| 66 tid_ = PlatformThread::CurrentId(); | 59 tid_ = PlatformThread::CurrentId(); |
| 67 // Construct our full name of the form "name_prefix_/TID". | 60 // Construct our full name of the form "name_prefix_/TID". |
| 68 name_.push_back('/'); | 61 name_.push_back('/'); |
| 69 name_.append(IntToString(tid_)); | 62 name_.append(IntToString(tid_)); |
| 70 PlatformThread::SetName(name_); | 63 PlatformThread::SetName(name_); |
| 71 | 64 |
| 72 // We've initialized our new thread, signal that we're done to Start(). | 65 // We've initialized our new thread, signal that we're done to Start(). |
| 73 event_.Signal(); | 66 event_.Signal(); |
| 74 | 67 |
| 75 Run(); | 68 Run(); |
| 76 } | 69 } |
| 77 | 70 |
| 78 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, | 71 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, |
| 79 const std::string& name_prefix) | 72 const std::string& name_prefix) |
| 80 : SimpleThread(name_prefix), | 73 : DelegateSimpleThread(delegate, name_prefix, Options()) {} |
| 81 delegate_(delegate) { | |
| 82 } | |
| 83 | 74 |
| 84 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, | 75 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, |
| 85 const std::string& name_prefix, | 76 const std::string& name_prefix, |
| 86 const Options& options) | 77 const Options& options) |
| 87 : SimpleThread(name_prefix, options), | 78 : SimpleThread(name_prefix, options), |
| 88 delegate_(delegate) { | 79 delegate_(delegate) { |
| 80 DCHECK(delegate_); |
| 89 } | 81 } |
| 90 | 82 |
| 91 DelegateSimpleThread::~DelegateSimpleThread() { | 83 DelegateSimpleThread::~DelegateSimpleThread() = default; |
| 92 } | |
| 93 | 84 |
| 94 void DelegateSimpleThread::Run() { | 85 void DelegateSimpleThread::Run() { |
| 95 DCHECK(delegate_) << "Tried to call Run without a delegate (called twice?)"; | 86 DCHECK(delegate_) << "Tried to call Run without a delegate (called twice?)"; |
| 96 delegate_->Run(); | 87 delegate_->Run(); |
| 97 delegate_ = NULL; | 88 delegate_ = nullptr; |
| 98 } | 89 } |
| 99 | 90 |
| 100 DelegateSimpleThreadPool::DelegateSimpleThreadPool( | 91 DelegateSimpleThreadPool::DelegateSimpleThreadPool( |
| 101 const std::string& name_prefix, | 92 const std::string& name_prefix, |
| 102 int num_threads) | 93 int num_threads) |
| 103 : name_prefix_(name_prefix), | 94 : name_prefix_(name_prefix), |
| 104 num_threads_(num_threads), | 95 num_threads_(num_threads), |
| 105 dry_(WaitableEvent::ResetPolicy::MANUAL, | 96 dry_(WaitableEvent::ResetPolicy::MANUAL, |
| 106 WaitableEvent::InitialState::NOT_SIGNALED) {} | 97 WaitableEvent::InitialState::NOT_SIGNALED) {} |
| 107 | 98 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 | 156 |
| 166 // A NULL delegate pointer signals us to quit. | 157 // A NULL delegate pointer signals us to quit. |
| 167 if (!work) | 158 if (!work) |
| 168 break; | 159 break; |
| 169 | 160 |
| 170 work->Run(); | 161 work->Run(); |
| 171 } | 162 } |
| 172 } | 163 } |
| 173 | 164 |
| 174 } // namespace base | 165 } // namespace base |
| OLD | NEW |