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 #ifndef BASE_THREADING_THREAD_H_ | 5 #ifndef BASE_THREADING_THREAD_H_ |
6 #define BASE_THREADING_THREAD_H_ | 6 #define BASE_THREADING_THREAD_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 // such only the owner should access it (and the underlying thread which | 171 // such only the owner should access it (and the underlying thread which |
172 // never sees it before it's set). In practice, many callers are coming from | 172 // never sees it before it's set). In practice, many callers are coming from |
173 // unrelated threads but provide their own implicit (e.g. memory barriers | 173 // unrelated threads but provide their own implicit (e.g. memory barriers |
174 // from task posting) or explicit (e.g. locks) synchronization making the | 174 // from task posting) or explicit (e.g. locks) synchronization making the |
175 // access of |message_loop_| safe... Changing all of those callers is | 175 // access of |message_loop_| safe... Changing all of those callers is |
176 // unfeasible; instead verify that they can reliably see | 176 // unfeasible; instead verify that they can reliably see |
177 // |message_loop_ != nullptr| without synchronization as a proof that their | 177 // |message_loop_ != nullptr| without synchronization as a proof that their |
178 // external synchronization catches the unsynchronized effects of Start(). | 178 // external synchronization catches the unsynchronized effects of Start(). |
179 // TODO(gab): Despite all of the above this test has to be disabled for now | 179 // TODO(gab): Despite all of the above this test has to be disabled for now |
180 // per crbug.com/629139#c6. | 180 // per crbug.com/629139#c6. |
181 // DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread() || | 181 // DCHECK(owning_sequence_checker_.CalledOnValidSequence() || |
182 // id_ == PlatformThread::CurrentId() || message_loop_) | 182 // id_ == PlatformThread::CurrentId() || message_loop_) |
183 // << id_ << " vs " << PlatformThread::CurrentId(); | 183 // << id_ << " vs " << PlatformThread::CurrentId(); |
184 return message_loop_; | 184 return message_loop_; |
185 } | 185 } |
186 | 186 |
187 // Returns a TaskRunner for this thread. Use the TaskRunner's PostTask | 187 // Returns a TaskRunner for this thread. Use the TaskRunner's PostTask |
188 // methods to execute code on the thread. Returns nullptr if the thread is not | 188 // methods to execute code on the thread. Returns nullptr if the thread is not |
189 // running (e.g. before Start or after Stop have been called). Callers can | 189 // running (e.g. before Start or after Stop have been called). Callers can |
190 // hold on to this even after the thread is gone; in this situation, attempts | 190 // hold on to this even after the thread is gone; in this situation, attempts |
191 // to PostTask() will fail. | 191 // to PostTask() will fail. |
192 // | 192 // |
193 // In addition to this Thread's owning sequence, this can also safely be | 193 // In addition to this Thread's owning sequence, this can also safely be |
194 // called from the underlying thread itself. | 194 // called from the underlying thread itself. |
195 scoped_refptr<SingleThreadTaskRunner> task_runner() const { | 195 scoped_refptr<SingleThreadTaskRunner> task_runner() const { |
196 // Refer to the DCHECK and comment inside |message_loop()|. | 196 // Refer to the DCHECK and comment inside |message_loop()|. |
197 DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread() || | 197 DCHECK(owning_sequence_checker_.CalledOnValidSequence() || |
198 id_ == PlatformThread::CurrentId() || message_loop_) | 198 id_ == PlatformThread::CurrentId() || message_loop_) |
199 << id_ << " vs " << PlatformThread::CurrentId(); | 199 << id_ << " vs " << PlatformThread::CurrentId(); |
200 return message_loop_ ? message_loop_->task_runner() : nullptr; | 200 return message_loop_ ? message_loop_->task_runner() : nullptr; |
201 } | 201 } |
202 | 202 |
203 // Returns the name of this thread (for display in debugger too). | 203 // Returns the name of this thread (for display in debugger too). |
204 const std::string& thread_name() const { return name_; } | 204 const std::string& thread_name() const { return name_; } |
205 | 205 |
206 // Returns the thread ID. Should not be called before the first Start*() | 206 // Returns the thread ID. Should not be called before the first Start*() |
207 // call. Keeps on returning the same ID even after a Stop() call. The next | 207 // call. Keeps on returning the same ID even after a Stop() call. The next |
(...skipping 14 matching lines...) Expand all Loading... |
222 // Called to start the run loop | 222 // Called to start the run loop |
223 virtual void Run(RunLoop* run_loop); | 223 virtual void Run(RunLoop* run_loop); |
224 | 224 |
225 // Called just after the message loop ends | 225 // Called just after the message loop ends |
226 virtual void CleanUp() {} | 226 virtual void CleanUp() {} |
227 | 227 |
228 static void SetThreadWasQuitProperly(bool flag); | 228 static void SetThreadWasQuitProperly(bool flag); |
229 static bool GetThreadWasQuitProperly(); | 229 static bool GetThreadWasQuitProperly(); |
230 | 230 |
231 void set_message_loop(MessageLoop* message_loop) { | 231 void set_message_loop(MessageLoop* message_loop) { |
232 DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); | 232 DCHECK(owning_sequence_checker_.CalledOnValidSequence()); |
233 message_loop_ = message_loop; | 233 message_loop_ = message_loop; |
234 } | 234 } |
235 | 235 |
236 private: | 236 private: |
237 #if defined(OS_WIN) | 237 #if defined(OS_WIN) |
238 enum ComStatus { | 238 enum ComStatus { |
239 NONE, | 239 NONE, |
240 STA, | 240 STA, |
241 MTA, | 241 MTA, |
242 }; | 242 }; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 // This class is not thread-safe, use this to verify access from the owning | 288 // This class is not thread-safe, use this to verify access from the owning |
289 // sequence of the Thread. | 289 // sequence of the Thread. |
290 SequenceChecker owning_sequence_checker_; | 290 SequenceChecker owning_sequence_checker_; |
291 | 291 |
292 DISALLOW_COPY_AND_ASSIGN(Thread); | 292 DISALLOW_COPY_AND_ASSIGN(Thread); |
293 }; | 293 }; |
294 | 294 |
295 } // namespace base | 295 } // namespace base |
296 | 296 |
297 #endif // BASE_THREADING_THREAD_H_ | 297 #endif // BASE_THREADING_THREAD_H_ |
OLD | NEW |