| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/thread.h" | 5 #include "base/threading/thread.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 MessageLoop::Type type = options.message_loop_type; | 88 MessageLoop::Type type = options.message_loop_type; |
| 89 if (!options.message_pump_factory.is_null()) | 89 if (!options.message_pump_factory.is_null()) |
| 90 type = MessageLoop::TYPE_CUSTOM; | 90 type = MessageLoop::TYPE_CUSTOM; |
| 91 | 91 |
| 92 message_loop_timer_slack_ = options.timer_slack; | 92 message_loop_timer_slack_ = options.timer_slack; |
| 93 std::unique_ptr<MessageLoop> message_loop_owned = | 93 std::unique_ptr<MessageLoop> message_loop_owned = |
| 94 MessageLoop::CreateUnbound(type, options.message_pump_factory); | 94 MessageLoop::CreateUnbound(type, options.message_pump_factory); |
| 95 message_loop_ = message_loop_owned.get(); | 95 message_loop_ = message_loop_owned.get(); |
| 96 start_event_.Reset(); | 96 start_event_.Reset(); |
| 97 | 97 |
| 98 // Hold |thread_lock_| while starting the new thread to synchronize with | 98 if (!PlatformThread::CreateWithPriority(options.stack_size, this, &thread_, |
| 99 // Stop() while it's not guaranteed to be sequenced (until crbug/629139 is | 99 options.priority)) { |
| 100 // fixed). | 100 DLOG(ERROR) << "failed to create thread"; |
| 101 { | 101 message_loop_ = nullptr; |
| 102 AutoLock lock(thread_lock_); | 102 return false; |
| 103 if (!PlatformThread::CreateWithPriority(options.stack_size, this, &thread_, | |
| 104 options.priority)) { | |
| 105 DLOG(ERROR) << "failed to create thread"; | |
| 106 message_loop_ = nullptr; | |
| 107 return false; | |
| 108 } | |
| 109 } | 103 } |
| 110 | 104 |
| 111 // The ownership of |message_loop_| is managed by the newly created thread | 105 // The ownership of |message_loop_| is managed by the newly created thread |
| 112 // within the ThreadMain. | 106 // within the ThreadMain. |
| 113 ignore_result(message_loop_owned.release()); | 107 ignore_result(message_loop_owned.release()); |
| 114 | 108 |
| 115 DCHECK(message_loop_); | 109 DCHECK(message_loop_); |
| 116 return true; | 110 return true; |
| 117 } | 111 } |
| 118 | 112 |
| 119 bool Thread::StartAndWaitForTesting() { | 113 bool Thread::StartAndWaitForTesting() { |
| 120 DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); | 114 DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); |
| 121 bool result = Start(); | 115 bool result = Start(); |
| 122 if (!result) | 116 if (!result) |
| 123 return false; | 117 return false; |
| 124 WaitUntilThreadStarted(); | 118 WaitUntilThreadStarted(); |
| 125 return true; | 119 return true; |
| 126 } | 120 } |
| 127 | 121 |
| 128 bool Thread::WaitUntilThreadStarted() const { | 122 bool Thread::WaitUntilThreadStarted() const { |
| 129 DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); | 123 DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); |
| 130 if (!message_loop_) | 124 if (!message_loop_) |
| 131 return false; | 125 return false; |
| 132 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 126 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 133 start_event_.Wait(); | 127 start_event_.Wait(); |
| 134 return true; | 128 return true; |
| 135 } | 129 } |
| 136 | 130 |
| 137 void Thread::Stop() { | 131 void Thread::Stop() { |
| 138 // TODO(gab): Fix improper usage of this API (http://crbug.com/629139) and | 132 DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); |
| 139 // enable this check, until then synchronization with Start() via | |
| 140 // |thread_lock_| is required... | |
| 141 // DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); | |
| 142 AutoLock lock(thread_lock_); | |
| 143 | 133 |
| 144 if (thread_.is_null()) | 134 if (thread_.is_null()) |
| 145 return; | 135 return; |
| 146 | 136 |
| 147 StopSoon(); | 137 StopSoon(); |
| 148 | 138 |
| 149 // Wait for the thread to exit. | 139 // Wait for the thread to exit. |
| 150 // | 140 // |
| 151 // TODO(darin): Unfortunately, we need to keep |message_loop_| around until | 141 // TODO(darin): Unfortunately, we need to keep |message_loop_| around until |
| 152 // the thread exits. Some consumers are abusing the API. Make them stop. | 142 // the thread exits. Some consumers are abusing the API. Make them stop. |
| 153 // | 143 // |
| 154 PlatformThread::Join(thread_); | 144 PlatformThread::Join(thread_); |
| 155 thread_ = base::PlatformThreadHandle(); | 145 thread_ = base::PlatformThreadHandle(); |
| 156 | 146 |
| 157 // The thread should nullify |message_loop_| on exit (note: Join() adds an | 147 // The thread should nullify |message_loop_| on exit (note: Join() adds an |
| 158 // implicit memory barrier and no lock is thus required for this check). | 148 // implicit memory barrier and no lock is thus required for this check). |
| 159 DCHECK(!message_loop_); | 149 DCHECK(!message_loop_); |
| 160 | 150 |
| 161 stopping_ = false; | 151 stopping_ = false; |
| 162 } | 152 } |
| 163 | 153 |
| 164 void Thread::StopSoon() { | 154 void Thread::StopSoon() { |
| 165 // TODO(gab): Fix improper usage of this API (http://crbug.com/629139) and | 155 DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); |
| 166 // enable this check. | |
| 167 // DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); | |
| 168 | 156 |
| 169 if (stopping_ || !message_loop_) | 157 if (stopping_ || !message_loop_) |
| 170 return; | 158 return; |
| 171 | 159 |
| 172 stopping_ = true; | 160 stopping_ = true; |
| 173 task_runner()->PostTask( | 161 task_runner()->PostTask( |
| 174 FROM_HERE, base::Bind(&Thread::ThreadQuitHelper, Unretained(this))); | 162 FROM_HERE, base::Bind(&Thread::ThreadQuitHelper, Unretained(this))); |
| 175 } | 163 } |
| 176 | 164 |
| 177 PlatformThreadId Thread::GetThreadId() const { | 165 PlatformThreadId Thread::GetThreadId() const { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 run_loop_ = nullptr; | 272 run_loop_ = nullptr; |
| 285 } | 273 } |
| 286 | 274 |
| 287 void Thread::ThreadQuitHelper() { | 275 void Thread::ThreadQuitHelper() { |
| 288 DCHECK(run_loop_); | 276 DCHECK(run_loop_); |
| 289 run_loop_->QuitWhenIdle(); | 277 run_loop_->QuitWhenIdle(); |
| 290 SetThreadWasQuitProperly(true); | 278 SetThreadWasQuitProperly(true); |
| 291 } | 279 } |
| 292 | 280 |
| 293 } // namespace base | 281 } // namespace base |
| OLD | NEW |