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 10 matching lines...) Expand all Loading... | |
21 #endif | 21 #endif |
22 | 22 |
23 namespace base { | 23 namespace base { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 // We use this thread-local variable to record whether or not a thread exited | 27 // We use this thread-local variable to record whether or not a thread exited |
28 // because its Stop method was called. This allows us to catch cases where | 28 // because its Stop method was called. This allows us to catch cases where |
29 // MessageLoop::QuitWhenIdle() is called directly, which is unexpected when | 29 // MessageLoop::QuitWhenIdle() is called directly, which is unexpected when |
30 // using a Thread to setup and run a MessageLoop. | 30 // using a Thread to setup and run a MessageLoop. |
31 base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool = | 31 base::LazyInstance<base::ThreadLocalBoolean>::Leaky lazy_tls_bool = |
32 LAZY_INSTANCE_INITIALIZER; | 32 LAZY_INSTANCE_INITIALIZER; |
33 | 33 |
34 } // namespace | 34 } // namespace |
35 | 35 |
36 Thread::Options::Options() = default; | 36 Thread::Options::Options() = default; |
37 | 37 |
38 Thread::Options::Options(MessageLoop::Type type, size_t size) | 38 Thread::Options::Options(MessageLoop::Type type, size_t size) |
39 : message_loop_type(type), stack_size(size) {} | 39 : message_loop_type(type), stack_size(size) {} |
40 | 40 |
41 Thread::Options::Options(const Options& other) = default; | 41 Thread::Options::Options(const Options& other) = default; |
(...skipping 25 matching lines...) Expand all Loading... | |
67 if (com_status_ == STA) | 67 if (com_status_ == STA) |
68 options.message_loop_type = MessageLoop::TYPE_UI; | 68 options.message_loop_type = MessageLoop::TYPE_UI; |
69 #endif | 69 #endif |
70 return StartWithOptions(options); | 70 return StartWithOptions(options); |
71 } | 71 } |
72 | 72 |
73 bool Thread::StartWithOptions(const Options& options) { | 73 bool Thread::StartWithOptions(const Options& options) { |
74 DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); | 74 DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); |
75 DCHECK(!message_loop_); | 75 DCHECK(!message_loop_); |
76 DCHECK(!IsRunning()); | 76 DCHECK(!IsRunning()); |
77 DCHECK(!stopping_) << "Starting a non-joinable thread a second time? That's " | |
78 << "not allowed!"; | |
77 #if defined(OS_WIN) | 79 #if defined(OS_WIN) |
78 DCHECK((com_status_ != STA) || | 80 DCHECK((com_status_ != STA) || |
79 (options.message_loop_type == MessageLoop::TYPE_UI)); | 81 (options.message_loop_type == MessageLoop::TYPE_UI)); |
80 #endif | 82 #endif |
81 | 83 |
82 // Reset |id_| here to support restarting the thread. | 84 // Reset |id_| here to support restarting the thread. |
83 id_event_.Reset(); | 85 id_event_.Reset(); |
84 id_ = kInvalidThreadId; | 86 id_ = kInvalidThreadId; |
85 | 87 |
86 SetThreadWasQuitProperly(false); | 88 SetThreadWasQuitProperly(false); |
87 | 89 |
88 MessageLoop::Type type = options.message_loop_type; | 90 MessageLoop::Type type = options.message_loop_type; |
89 if (!options.message_pump_factory.is_null()) | 91 if (!options.message_pump_factory.is_null()) |
90 type = MessageLoop::TYPE_CUSTOM; | 92 type = MessageLoop::TYPE_CUSTOM; |
91 | 93 |
92 message_loop_timer_slack_ = options.timer_slack; | 94 message_loop_timer_slack_ = options.timer_slack; |
93 std::unique_ptr<MessageLoop> message_loop_owned = | 95 std::unique_ptr<MessageLoop> message_loop_owned = |
94 MessageLoop::CreateUnbound(type, options.message_pump_factory); | 96 MessageLoop::CreateUnbound(type, options.message_pump_factory); |
95 message_loop_ = message_loop_owned.get(); | 97 message_loop_ = message_loop_owned.get(); |
96 start_event_.Reset(); | 98 start_event_.Reset(); |
97 | 99 |
98 // Hold |thread_lock_| while starting the new thread to synchronize with | 100 // Hold |thread_lock_| while starting the new thread to synchronize with |
99 // Stop() while it's not guaranteed to be sequenced (until crbug/629139 is | 101 // Stop() while it's not guaranteed to be sequenced (until crbug/629139 is |
100 // fixed). | 102 // fixed). |
101 { | 103 { |
102 AutoLock lock(thread_lock_); | 104 AutoLock lock(thread_lock_); |
103 if (!PlatformThread::CreateWithPriority(options.stack_size, this, &thread_, | 105 bool success = |
104 options.priority)) { | 106 options.joinable |
107 ? PlatformThread::CreateWithPriority(options.stack_size, this, | |
108 &thread_, options.priority) | |
109 : PlatformThread::CreateNonJoinableWithPriority( | |
110 options.stack_size, this, options.priority); | |
111 if (!success) { | |
105 DLOG(ERROR) << "failed to create thread"; | 112 DLOG(ERROR) << "failed to create thread"; |
106 message_loop_ = nullptr; | 113 message_loop_ = nullptr; |
107 return false; | 114 return false; |
108 } | 115 } |
109 } | 116 } |
110 | 117 |
111 // The ownership of |message_loop_| is managed by the newly created thread | 118 // The ownership of |message_loop_| is managed by the newly created thread |
112 // within the ThreadMain. | 119 // within the ThreadMain. |
113 ignore_result(message_loop_owned.release()); | 120 ignore_result(message_loop_owned.release()); |
114 | 121 |
(...skipping 19 matching lines...) Expand all Loading... | |
134 return true; | 141 return true; |
135 } | 142 } |
136 | 143 |
137 void Thread::Stop() { | 144 void Thread::Stop() { |
138 // TODO(gab): Fix improper usage of this API (http://crbug.com/629139) and | 145 // TODO(gab): Fix improper usage of this API (http://crbug.com/629139) and |
139 // enable this check, until then synchronization with Start() via | 146 // enable this check, until then synchronization with Start() via |
140 // |thread_lock_| is required... | 147 // |thread_lock_| is required... |
141 // DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); | 148 // DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); |
142 AutoLock lock(thread_lock_); | 149 AutoLock lock(thread_lock_); |
143 | 150 |
151 StopSoon(); | |
152 | |
153 // Can't join if the |thread_| is either already gone or is non-joinable. | |
144 if (thread_.is_null()) | 154 if (thread_.is_null()) |
145 return; | 155 return; |
146 | 156 |
147 StopSoon(); | |
148 | |
149 // Wait for the thread to exit. | 157 // Wait for the thread to exit. |
150 // | 158 // |
151 // TODO(darin): Unfortunately, we need to keep |message_loop_| around until | 159 // TODO(darin): Unfortunately, we need to keep |message_loop_| around until |
152 // the thread exits. Some consumers are abusing the API. Make them stop. | 160 // the thread exits. Some consumers are abusing the API. Make them stop. |
153 // | 161 // |
154 PlatformThread::Join(thread_); | 162 PlatformThread::Join(thread_); |
155 thread_ = base::PlatformThreadHandle(); | 163 thread_ = base::PlatformThreadHandle(); |
156 | 164 |
157 // The thread should nullify |message_loop_| on exit (note: Join() adds an | 165 // 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). | 166 // implicit memory barrier and no lock is thus required for this check). |
159 DCHECK(!message_loop_); | 167 DCHECK(!message_loop_); |
160 | 168 |
161 stopping_ = false; | 169 stopping_ = false; |
162 } | 170 } |
163 | 171 |
164 void Thread::StopSoon() { | 172 void Thread::StopSoon() { |
165 // TODO(gab): Fix improper usage of this API (http://crbug.com/629139) and | 173 // TODO(gab): Fix improper usage of this API (http://crbug.com/629139) and |
166 // enable this check. | 174 // enable this check. |
167 // DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); | 175 // DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); |
168 | 176 |
169 if (stopping_ || !message_loop_) | 177 if (stopping_ || !message_loop_) |
170 return; | 178 return; |
171 | 179 |
172 stopping_ = true; | 180 stopping_ = true; |
danakj
2016/07/26 19:54:25
Is it not weird that Thread will leave Stop() with
gab
2016/07/26 20:25:33
For non-joinable threads? I don't think so, attemp
| |
181 | |
182 if (using_external_message_loop_) { | |
183 // Setting |stopping_| to true above should have been sufficient for this | |
184 // thread to be considered "stopped" per it having never set its |running_| | |
185 // bit by lack of its own ThreadMain. | |
186 DCHECK(!IsRunning()); | |
187 message_loop_ = nullptr; | |
188 return; | |
189 } | |
190 | |
173 task_runner()->PostTask( | 191 task_runner()->PostTask( |
174 FROM_HERE, base::Bind(&Thread::ThreadQuitHelper, Unretained(this))); | 192 FROM_HERE, base::Bind(&Thread::ThreadQuitHelper, Unretained(this))); |
175 } | 193 } |
176 | 194 |
177 PlatformThreadId Thread::GetThreadId() const { | 195 PlatformThreadId Thread::GetThreadId() const { |
178 // If the thread is created but not started yet, wait for |id_| being ready. | 196 // If the thread is created but not started yet, wait for |id_| being ready. |
179 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 197 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
180 id_event_.Wait(); | 198 id_event_.Wait(); |
181 return id_; | 199 return id_; |
182 } | 200 } |
(...skipping 29 matching lines...) Expand all Loading... | |
212 | 230 |
213 // static | 231 // static |
214 bool Thread::GetThreadWasQuitProperly() { | 232 bool Thread::GetThreadWasQuitProperly() { |
215 bool quit_properly = true; | 233 bool quit_properly = true; |
216 #ifndef NDEBUG | 234 #ifndef NDEBUG |
217 quit_properly = lazy_tls_bool.Pointer()->Get(); | 235 quit_properly = lazy_tls_bool.Pointer()->Get(); |
218 #endif | 236 #endif |
219 return quit_properly; | 237 return quit_properly; |
220 } | 238 } |
221 | 239 |
240 void Thread::SetMessageLoop(MessageLoop* message_loop) { | |
241 DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); | |
242 | |
243 // Setting |message_loop_| should suffice for this thread to be considered | |
244 // as "running", until Stop() is invoked. | |
245 DCHECK(!IsRunning()); | |
246 message_loop_ = message_loop; | |
247 DCHECK(IsRunning()); | |
248 | |
249 using_external_message_loop_ = true; | |
250 } | |
251 | |
222 void Thread::ThreadMain() { | 252 void Thread::ThreadMain() { |
223 // First, make GetThreadId() available to avoid deadlocks. It could be called | 253 // First, make GetThreadId() available to avoid deadlocks. It could be called |
224 // any place in the following thread initialization code. | 254 // any place in the following thread initialization code. |
225 id_ = PlatformThread::CurrentId(); | 255 id_ = PlatformThread::CurrentId(); |
226 DCHECK_NE(kInvalidThreadId, id_); | 256 DCHECK_NE(kInvalidThreadId, id_); |
227 id_event_.Signal(); | 257 id_event_.Signal(); |
228 | 258 |
229 // Complete the initialization of our Thread object. | 259 // Complete the initialization of our Thread object. |
230 PlatformThread::SetName(name_.c_str()); | 260 PlatformThread::SetName(name_.c_str()); |
231 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. | 261 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 run_loop_ = nullptr; | 314 run_loop_ = nullptr; |
285 } | 315 } |
286 | 316 |
287 void Thread::ThreadQuitHelper() { | 317 void Thread::ThreadQuitHelper() { |
288 DCHECK(run_loop_); | 318 DCHECK(run_loop_); |
289 run_loop_->QuitWhenIdle(); | 319 run_loop_->QuitWhenIdle(); |
290 SetThreadWasQuitProperly(true); | 320 SetThreadWasQuitProperly(true); |
291 } | 321 } |
292 | 322 |
293 } // namespace base | 323 } // namespace base |
OLD | NEW |