Chromium Code Reviews| 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 options_(options), | 20 options_(options), |
| 21 event_(WaitableEvent::ResetPolicy::MANUAL, | 21 tid_set_event_(WaitableEvent::ResetPolicy::MANUAL, |
| 22 WaitableEvent::InitialState::NOT_SIGNALED) {} | 22 WaitableEvent::InitialState::NOT_SIGNALED) {} |
| 23 | 23 |
| 24 SimpleThread::~SimpleThread() { | 24 SimpleThread::~SimpleThread() { |
| 25 DCHECK(HasBeenStarted()) << "SimpleThread was never started."; | 25 #if DCHECK_IS_ON() |
| 26 DCHECK(!options_.joinable || HasBeenJoined()) | 26 DCHECK(has_been_started_) << "SimpleThread was never started."; |
| 27 DCHECK(!options_.joinable || has_been_joined_) | |
| 27 << "Joinable SimpleThread destroyed without being Join()ed."; | 28 << "Joinable SimpleThread destroyed without being Join()ed."; |
| 29 #endif | |
| 28 } | 30 } |
| 29 | 31 |
| 30 void SimpleThread::Start() { | 32 void SimpleThread::Start() { |
| 31 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; | 33 #if DCHECK_IS_ON() |
| 34 DCHECK(!has_been_started_) << "Tried to Start a thread multiple times."; | |
| 35 | |
| 36 // Set |has_been_started_| before creating the thread so that the thread's | |
| 37 // main function sees it. Otherwise, the thread's main function couldn't | |
|
gab
2017/02/06 20:29:10
couldn't... ?
fdoray
2017/02/06 20:46:48
Done.
| |
| 38 has_been_started_ = true; | |
| 39 #endif | |
| 32 bool success = | 40 bool success = |
| 33 options_.joinable | 41 options_.joinable |
| 34 ? PlatformThread::CreateWithPriority(options_.stack_size, this, | 42 ? PlatformThread::CreateWithPriority(options_.stack_size, this, |
| 35 &thread_, options_.priority) | 43 &thread_, options_.priority) |
| 36 : PlatformThread::CreateNonJoinableWithPriority( | 44 : PlatformThread::CreateNonJoinableWithPriority( |
| 37 options_.stack_size, this, options_.priority); | 45 options_.stack_size, this, options_.priority); |
| 38 DCHECK(success); | 46 DCHECK(success); |
| 39 ThreadRestrictions::ScopedAllowWait allow_wait; | |
|
gab
2017/02/06 20:29:10
Remove
friend class SimpleThread;
from thread_r
fdoray
2017/02/06 20:46:48
Done.
| |
| 40 event_.Wait(); // Wait for the thread to complete initialization. | |
| 41 } | 47 } |
|
gab
2017/02/06 20:29:10
Add after DCHECK(success):
// No member access af
fdoray
2017/02/06 20:46:48
Done.
| |
| 42 | 48 |
| 43 void SimpleThread::Join() { | 49 void SimpleThread::Join() { |
| 50 #if DCHECK_IS_ON() | |
| 44 DCHECK(options_.joinable) << "A non-joinable thread can't be joined."; | 51 DCHECK(options_.joinable) << "A non-joinable thread can't be joined."; |
| 45 DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread."; | 52 DCHECK(has_been_started_) << "Tried to Join a never-started thread."; |
| 46 DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times."; | 53 DCHECK(!has_been_joined_) << "Tried to Join a thread multiple times."; |
| 54 #endif | |
| 47 PlatformThread::Join(thread_); | 55 PlatformThread::Join(thread_); |
| 48 thread_ = PlatformThreadHandle(); | 56 thread_ = PlatformThreadHandle(); |
| 49 joined_ = true; | 57 #if DCHECK_IS_ON() |
| 58 has_been_joined_ = true; | |
| 59 #endif | |
| 50 } | 60 } |
| 51 | 61 |
| 52 bool SimpleThread::HasBeenStarted() { | 62 PlatformThreadId SimpleThread::GetTid() const { |
| 53 ThreadRestrictions::ScopedAllowWait allow_wait; | 63 tid_set_event_.Wait(); |
|
gab
2017/02/06 20:29:10
Nice, always thought it made no sense to blindly a
fdoray
2017/02/06 20:46:48
Acknowledged.
| |
| 54 return event_.IsSignaled(); | 64 return tid_; |
| 55 } | 65 } |
| 56 | 66 |
| 57 void SimpleThread::ThreadMain() { | 67 void SimpleThread::ThreadMain() { |
| 58 tid_ = PlatformThread::CurrentId(); | 68 tid_ = PlatformThread::CurrentId(); |
| 69 tid_set_event_.Signal(); | |
| 70 | |
| 59 // Construct our full name of the form "name_prefix_/TID". | 71 // Construct our full name of the form "name_prefix_/TID". |
| 60 std::string name(name_prefix_); | 72 std::string name(name_prefix_); |
| 61 name.push_back('/'); | 73 name.push_back('/'); |
| 62 name.append(IntToString(tid_)); | 74 name.append(IntToString(tid_)); |
| 63 PlatformThread::SetName(name); | 75 PlatformThread::SetName(name); |
| 64 | 76 |
| 65 // We've initialized our new thread, signal that we're done to Start(). | |
| 66 event_.Signal(); | |
| 67 | |
| 68 Run(); | 77 Run(); |
| 69 } | 78 } |
| 70 | 79 |
| 71 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, | 80 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, |
| 72 const std::string& name_prefix) | 81 const std::string& name_prefix) |
| 73 : DelegateSimpleThread(delegate, name_prefix, Options()) {} | 82 : DelegateSimpleThread(delegate, name_prefix, Options()) {} |
| 74 | 83 |
| 75 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, | 84 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, |
| 76 const std::string& name_prefix, | 85 const std::string& name_prefix, |
| 77 const Options& options) | 86 const Options& options) |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 | 169 |
| 161 // A NULL delegate pointer signals us to quit. | 170 // A NULL delegate pointer signals us to quit. |
| 162 if (!work) | 171 if (!work) |
| 163 break; | 172 break; |
| 164 | 173 |
| 165 work->Run(); | 174 work->Run(); |
| 166 } | 175 } |
| 167 } | 176 } |
| 168 | 177 |
| 169 } // namespace base | 178 } // namespace base |
| OLD | NEW |