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