| 6 | #define BASE_THREADING_THREAD_CHECKER_IMPL_H_ |
| 7 |
| 8 | #include "base/base_export.h" |
| 9 | #include "base/compiler_specific.h" |
| 10 | #include "base/sequence_token.h" |
| 11 | #include "base/synchronization/lock.h" |
| 12 | #include "base/thread_annotations.h" |
| 13 | #include "base/threading/platform_thread.h" |
| 14 |
| 15 | namespace base { |
| 16 |
| 17 | // Real implementation of ThreadChecker, for use in debug mode, or for temporary |
| 18 | // use in release mode (e.g. to CHECK on a threading issue seen only in the |
| 19 | // wild). |
| 20 | // |
| 21 | // Note: You should almost always use the ThreadChecker class to get the right |
| 22 | // version for your build configuration. |
| 23 | // Note: This is only a check, not a "lock". It is marked "LOCKABLE" only in |
| 24 | // order to support thread_annotations.h. |
| 25 | class LOCKABLE BASE_EXPORT ThreadCheckerImpl { |
| 26 | public: |
| 27 | ThreadCheckerImpl(); |
| 28 | ~ThreadCheckerImpl(); |
| 29 |
| 30 | // Allow move construct/assign. This must be called on |other|'s associated |
| 31 | // thread and assignment can only be made into a ThreadCheckerImpl which is |
| 32 | // detached or already associated with the current thread. This isn't |
| 33 | // thread-safe (|this| and |other| shouldn't be in use while this move is |
| 34 | // performed). If the assignment was legal, the resulting ThreadCheckerImpl |
| 35 | // will be bound to the current thread and |other| will be detached. |
| 36 | ThreadCheckerImpl(ThreadCheckerImpl&& other); |
| 37 | ThreadCheckerImpl& operator=(ThreadCheckerImpl&& other); |
| 38 |
| 39 | bool CalledOnValidThread() const WARN_UNUSED_RESULT; |
| 40 |
| 41 | // Changes the thread that is checked for in CalledOnValidThread. This may |
| 42 | // be useful when an object may be created on one thread and then used |
| 43 | // exclusively on another thread. |
| 44 | void DetachFromThread(); |
| 45 |
| 46 | private: |
| 47 | void EnsureAssignedLockRequired() const EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 48 |
| 49 | // Members are mutable so that CalledOnValidThread() can set them. |
| 50 |
| 51 | // Synchronizes access to all members. |
| 52 | mutable base::Lock lock_; |
| 53 |
| 54 | // Thread on which CalledOnValidThread() may return true. |
| 55 | mutable PlatformThreadRef thread_id_ GUARDED_BY(lock_); |
| 56 |
| 57 | // TaskToken for which CalledOnValidThread() always returns true. This allows |
| 58 | // CalledOnValidThread() to return true when called multiple times from the |
| 59 | // same task, even if it's not running in a single-threaded context itself |
| 60 | // (allowing usage of ThreadChecker objects on the stack in the scope of one- |
| 61 | // off tasks). Note: CalledOnValidThread() may return true even if the current |
| 62 | // TaskToken is not equal to this. |
| 63 | mutable TaskToken task_token_ GUARDED_BY(lock_); |
| 64 |
| 65 | // SequenceToken for which CalledOnValidThread() may return true. Used to |
| 66 | // ensure that CalledOnValidThread() doesn't return true for ThreadPool |
| 67 | // tasks that happen to run on the same thread but weren't posted to the same |
| 68 | // SingleThreadTaskRunner. |
| 69 | mutable SequenceToken sequence_token_ GUARDED_BY(lock_); |
| 70 | }; |
| 71 |
| 72 | } // namespace base |
| 73 |
| 74 | #endif // BASE_THREADING_THREAD_CHECKER_IMPL_H_ |
| 75 |