| 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 : SimpleThread(name_prefix, Options()) {} | 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) {} |
| 16 | 22 |
| 17 SimpleThread::SimpleThread(const std::string& name_prefix, | 23 SimpleThread::SimpleThread(const std::string& name_prefix, |
| 18 const Options& options) | 24 const Options& options) |
| 19 : name_prefix_(name_prefix), | 25 : name_prefix_(name_prefix), |
| 20 name_(name_prefix), | 26 name_(name_prefix), |
| 21 options_(options), | 27 options_(options), |
| 28 thread_(), |
| 22 event_(WaitableEvent::ResetPolicy::MANUAL, | 29 event_(WaitableEvent::ResetPolicy::MANUAL, |
| 23 WaitableEvent::InitialState::NOT_SIGNALED) {} | 30 WaitableEvent::InitialState::NOT_SIGNALED), |
| 31 tid_(0), |
| 32 joined_(false) {} |
| 24 | 33 |
| 25 SimpleThread::~SimpleThread() { | 34 SimpleThread::~SimpleThread() { |
| 26 DCHECK(HasBeenStarted()) << "SimpleThread was never started."; | 35 DCHECK(HasBeenStarted()) << "SimpleThread was never started."; |
| 27 DCHECK(thread_.is_null()) << "Joinable SimpleThread destroyed without being " | 36 DCHECK(HasBeenJoined()) << "SimpleThread destroyed without being Join()ed."; |
| 28 << "Join()ed."; | |
| 29 } | 37 } |
| 30 | 38 |
| 31 void SimpleThread::Start() { | 39 void SimpleThread::Start() { |
| 32 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; | 40 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; |
| 33 bool success = options_.joinable | 41 bool success; |
| 34 ? PlatformThread::CreateWithPriority(options_.stack_size, this, | 42 if (options_.priority() == ThreadPriority::NORMAL) { |
| 35 &thread_, options_.priority) | 43 success = PlatformThread::Create(options_.stack_size(), this, &thread_); |
| 36 : PlatformThread::CreateNonJoinableWithPriority( | 44 } else { |
| 37 options_.stack_size, this, options_.priority); | 45 success = PlatformThread::CreateWithPriority(options_.stack_size(), this, |
| 46 &thread_, options_.priority()); |
| 47 } |
| 38 DCHECK(success); | 48 DCHECK(success); |
| 39 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 49 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 40 event_.Wait(); // Wait for the thread to complete initialization. | 50 event_.Wait(); // Wait for the thread to complete initialization. |
| 41 } | 51 } |
| 42 | 52 |
| 43 void SimpleThread::Join() { | 53 void SimpleThread::Join() { |
| 44 DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread."; | 54 DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread."; |
| 45 DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times."; | 55 DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times."; |
| 46 DCHECK(!thread_.is_null()) << "A non-joinable thread can't be joined."; | |
| 47 PlatformThread::Join(thread_); | 56 PlatformThread::Join(thread_); |
| 48 thread_ = PlatformThreadHandle(); | |
| 49 joined_ = true; | 57 joined_ = true; |
| 50 } | 58 } |
| 51 | 59 |
| 52 bool SimpleThread::HasBeenStarted() { | 60 bool SimpleThread::HasBeenStarted() { |
| 53 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 61 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 54 return event_.IsSignaled(); | 62 return event_.IsSignaled(); |
| 55 } | 63 } |
| 56 | 64 |
| 57 void SimpleThread::ThreadMain() { | 65 void SimpleThread::ThreadMain() { |
| 58 tid_ = PlatformThread::CurrentId(); | 66 tid_ = PlatformThread::CurrentId(); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 | 165 |
| 158 // A NULL delegate pointer signals us to quit. | 166 // A NULL delegate pointer signals us to quit. |
| 159 if (!work) | 167 if (!work) |
| 160 break; | 168 break; |
| 161 | 169 |
| 162 work->Run(); | 170 work->Run(); |
| 163 } | 171 } |
| 164 } | 172 } |
| 165 | 173 |
| 166 } // namespace base | 174 } // namespace base |
| OLD | NEW |