| 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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 // never sees it before it's set). In practice, many callers are coming from | 182 // never sees it before it's set). In practice, many callers are coming from |
| 183 // unrelated threads but provide their own implicit (e.g. memory barriers | 183 // unrelated threads but provide their own implicit (e.g. memory barriers |
| 184 // from task posting) or explicit (e.g. locks) synchronization making the | 184 // from task posting) or explicit (e.g. locks) synchronization making the |
| 185 // access of |message_loop_| safe... Changing all of those callers is | 185 // access of |message_loop_| safe... Changing all of those callers is |
| 186 // unfeasible; instead verify that they can reliably see | 186 // unfeasible; instead verify that they can reliably see |
| 187 // |message_loop_ != nullptr| without synchronization as a proof that their | 187 // |message_loop_ != nullptr| without synchronization as a proof that their |
| 188 // external synchronization catches the unsynchronized effects of Start(). | 188 // external synchronization catches the unsynchronized effects of Start(). |
| 189 // TODO(gab): Despite all of the above this test has to be disabled for now | 189 // TODO(gab): Despite all of the above this test has to be disabled for now |
| 190 // per crbug.com/629139#c6. | 190 // per crbug.com/629139#c6. |
| 191 // DCHECK(owning_sequence_checker_.CalledOnValidSequence() || | 191 // DCHECK(owning_sequence_checker_.CalledOnValidSequence() || |
| 192 // id_ == PlatformThread::CurrentId() || message_loop_) | 192 // (id_event_.IsSignaled() && id_ == PlatformThread::CurrentId()) || |
| 193 // << id_ << " vs " << PlatformThread::CurrentId(); | 193 // message_loop_); |
| 194 return message_loop_; | 194 return message_loop_; |
| 195 } | 195 } |
| 196 | 196 |
| 197 // Returns a TaskRunner for this thread. Use the TaskRunner's PostTask | 197 // Returns a TaskRunner for this thread. Use the TaskRunner's PostTask |
| 198 // methods to execute code on the thread. Returns nullptr if the thread is not | 198 // methods to execute code on the thread. Returns nullptr if the thread is not |
| 199 // running (e.g. before Start or after Stop have been called). Callers can | 199 // running (e.g. before Start or after Stop have been called). Callers can |
| 200 // hold on to this even after the thread is gone; in this situation, attempts | 200 // hold on to this even after the thread is gone; in this situation, attempts |
| 201 // to PostTask() will fail. | 201 // to PostTask() will fail. |
| 202 // | 202 // |
| 203 // In addition to this Thread's owning sequence, this can also safely be | 203 // In addition to this Thread's owning sequence, this can also safely be |
| 204 // called from the underlying thread itself. | 204 // called from the underlying thread itself. |
| 205 scoped_refptr<SingleThreadTaskRunner> task_runner() const { | 205 scoped_refptr<SingleThreadTaskRunner> task_runner() const { |
| 206 // Refer to the DCHECK and comment inside |message_loop()|. | 206 // Refer to the DCHECK and comment inside |message_loop()|. |
| 207 DCHECK(owning_sequence_checker_.CalledOnValidSequence() || | 207 DCHECK(owning_sequence_checker_.CalledOnValidSequence() || |
| 208 id_ == PlatformThread::CurrentId() || message_loop_) | 208 (id_event_.IsSignaled() && id_ == PlatformThread::CurrentId()) || |
| 209 << id_ << " vs " << PlatformThread::CurrentId(); | 209 message_loop_); |
| 210 return message_loop_ ? message_loop_->task_runner() : nullptr; | 210 return message_loop_ ? message_loop_->task_runner() : nullptr; |
| 211 } | 211 } |
| 212 | 212 |
| 213 // Returns the name of this thread (for display in debugger too). | 213 // Returns the name of this thread (for display in debugger too). |
| 214 const std::string& thread_name() const { return name_; } | 214 const std::string& thread_name() const { return name_; } |
| 215 | 215 |
| 216 // Returns the thread ID. Should not be called before the first Start*() | 216 // Returns the thread ID. Should not be called before the first Start*() |
| 217 // call. Keeps on returning the same ID even after a Stop() call. The next | 217 // call. Keeps on returning the same ID even after a Stop() call. The next |
| 218 // Start*() call renews the ID. | 218 // Start*() call renews the ID. |
| 219 // | 219 // |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 // True while inside of Run(). | 273 // True while inside of Run(). |
| 274 bool running_ = false; | 274 bool running_ = false; |
| 275 mutable base::Lock running_lock_; // Protects |running_|. | 275 mutable base::Lock running_lock_; // Protects |running_|. |
| 276 | 276 |
| 277 // The thread's handle. | 277 // The thread's handle. |
| 278 PlatformThreadHandle thread_; | 278 PlatformThreadHandle thread_; |
| 279 mutable base::Lock thread_lock_; // Protects |thread_|. | 279 mutable base::Lock thread_lock_; // Protects |thread_|. |
| 280 | 280 |
| 281 // The thread's id once it has started. | 281 // The thread's id once it has started. |
| 282 PlatformThreadId id_ = kInvalidThreadId; | 282 PlatformThreadId id_ = kInvalidThreadId; |
| 283 mutable WaitableEvent id_event_; // Protects |id_|. | 283 // Protects |id_| which must only be read while it's signaled. |
| 284 mutable WaitableEvent id_event_; |
| 284 | 285 |
| 285 // The thread's MessageLoop and RunLoop. Valid only while the thread is alive. | 286 // The thread's MessageLoop and RunLoop. Valid only while the thread is alive. |
| 286 // Set by the created thread. | 287 // Set by the created thread. |
| 287 MessageLoop* message_loop_ = nullptr; | 288 MessageLoop* message_loop_ = nullptr; |
| 288 RunLoop* run_loop_ = nullptr; | 289 RunLoop* run_loop_ = nullptr; |
| 289 | 290 |
| 290 // True only if |message_loop_| was externally provided by |SetMessageLoop()| | 291 // True only if |message_loop_| was externally provided by |SetMessageLoop()| |
| 291 // in which case this Thread has no underlying |thread_| and should merely | 292 // in which case this Thread has no underlying |thread_| and should merely |
| 292 // drop |message_loop_| on Stop(). | 293 // drop |message_loop_| on Stop(). |
| 293 bool using_external_message_loop_ = false; | 294 bool using_external_message_loop_ = false; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 305 // This class is not thread-safe, use this to verify access from the owning | 306 // This class is not thread-safe, use this to verify access from the owning |
| 306 // sequence of the Thread. | 307 // sequence of the Thread. |
| 307 SequenceChecker owning_sequence_checker_; | 308 SequenceChecker owning_sequence_checker_; |
| 308 | 309 |
| 309 DISALLOW_COPY_AND_ASSIGN(Thread); | 310 DISALLOW_COPY_AND_ASSIGN(Thread); |
| 310 }; | 311 }; |
| 311 | 312 |
| 312 } // namespace base | 313 } // namespace base |
| 313 | 314 |
| 314 #endif // BASE_THREADING_THREAD_H_ | 315 #endif // BASE_THREADING_THREAD_H_ |
| OLD | NEW |