| 6 |
| 7 | #include "base/logging.h" |
| 8 | #include "base/threading/thread_local.h" |
| 9 | #include "base/threading/thread_task_runner_handle.h" |
| 10 |
| 11 | namespace base { |
| 12 |
| 13 | ThreadCheckerImpl::ThreadCheckerImpl() { |
| 14 | AutoLock auto_lock(lock_); |
| 15 | EnsureAssignedLockRequired(); |
| 16 | } |
| 17 |
| 18 | ThreadCheckerImpl::~ThreadCheckerImpl() = default; |
| 19 |
| 20 | ThreadCheckerImpl::ThreadCheckerImpl(ThreadCheckerImpl&& other) { |
| 21 | // Verify that |other| is called on its associated thread and bind it now if |
| 22 | // it is currently detached (even if this isn't a DCHECK build). |
| 23 | const bool other_called_on_valid_thread = other.CalledOnValidThread(); |
| 24 | DCHECK(other_called_on_valid_thread); |
| 25 |
| 26 | // Intentionally not using |other.lock_| to let TSAN catch racy construct from |
| 27 | // |other|. |
| 28 | thread_id_ = other.thread_id_; |
| 29 | task_token_ = other.task_token_; |
| 30 | sequence_token_ = other.sequence_token_; |
| 31 |
| 32 | other.thread_id_ = PlatformThreadRef(); |
| 33 | other.task_token_ = TaskToken(); |
| 34 | other.sequence_token_ = SequenceToken(); |
| 35 | } |
| 36 |
| 37 | ThreadCheckerImpl& ThreadCheckerImpl::operator=(ThreadCheckerImpl&& other) { |
| 38 | DCHECK(CalledOnValidThread()); |
| 39 |
| 40 | // Verify that |other| is called on its associated thread and bind it now if |
| 41 | // it is currently detached (even if this isn't a DCHECK build). |
| 42 | const bool other_called_on_valid_thread = other.CalledOnValidThread(); |
| 43 | DCHECK(other_called_on_valid_thread); |
| 44 |
| 45 | // Intentionally not using either |lock_| to let TSAN catch racy assign. |
| 46 | TS_UNCHECKED_READ(thread_id_) = TS_UNCHECKED_READ(other.thread_id_); |
| 47 | TS_UNCHECKED_READ(task_token_) = TS_UNCHECKED_READ(other.task_token_); |
| 48 | TS_UNCHECKED_READ(sequence_token_) = TS_UNCHECKED_READ(other.sequence_token_); |
| 49 |
| 50 | TS_UNCHECKED_READ(other.thread_id_) = PlatformThreadRef(); |
| 51 | TS_UNCHECKED_READ(other.task_token_) = TaskToken(); |
| 52 | TS_UNCHECKED_READ(other.sequence_token_) = SequenceToken(); |
| 53 |
| 54 | return *this; |
| 55 | } |
| 56 |
| 57 | bool ThreadCheckerImpl::CalledOnValidThread() const { |
| 58 | const bool has_thread_been_destroyed = ThreadLocalStorage::HasBeenDestroyed(); |
| 59 |
| 60 | AutoLock auto_lock(lock_); |
| 61 | // TaskToken/SequenceToken access thread-local storage. During destruction |
| 62 | // the state of thread-local storage is not guaranteed to be in a consistent |
| 63 | // state. Further, task-runner only installs the tokens when running a task. |
| 64 | if (!has_thread_been_destroyed) { |
| 65 | EnsureAssignedLockRequired(); |
| 66 |
| 67 | // Always return true when called from the task from which this |
| 68 | // ThreadCheckerImpl was assigned to a thread. |
| 69 | if (task_token_ == TaskToken::GetForCurrentThread()) |
| 70 | return true; |
| 71 |
| 72 | // If this ThreadCheckerImpl is bound to a valid SequenceToken, it must be |
| 73 | // equal to the current SequenceToken and there must be a registered |
| 74 | // ThreadTaskRunnerHandle. Otherwise, the fact that the current task runs on |
| 75 | // the thread to which this ThreadCheckerImpl is bound is fortuitous. |
| 76 | if (sequence_token_.IsValid() && |
| 77 | (sequence_token_ != SequenceToken::GetForCurrentThread() || |
| 78 | !ThreadTaskRunnerHandle::IsSet())) { |
| 79 | return false; |
| 80 | } |
| 81 | } else if (thread_id_.is_null()) { |
| 82 | // We're in tls destruction but the |thread_id_| hasn't been assigned yet. |
| 83 | // Assign it now. This doesn't call EnsureAssigned() as to do so while in |
| 84 | // tls destruction may result in the wrong TaskToken/SequenceToken. |
| 85 | thread_id_ = PlatformThread::CurrentRef(); |
| 86 | } |
| 87 |
| 88 | return thread_id_ == PlatformThread::CurrentRef(); |
| 89 | } |
| 90 |
| 91 | void ThreadCheckerImpl::DetachFromThread() { |
| 92 | AutoLock auto_lock(lock_); |
| 93 | thread_id_ = PlatformThreadRef(); |
| 94 | task_token_ = TaskToken(); |
| 95 | sequence_token_ = SequenceToken(); |
| 96 | } |
| 97 |
| 98 | void ThreadCheckerImpl::EnsureAssignedLockRequired() const { |
| 99 | if (!thread_id_.is_null()) |
| 100 | return; |
| 101 |
| 102 | thread_id_ = PlatformThread::CurrentRef(); |
| 103 | task_token_ = TaskToken::GetForCurrentThread(); |
| 104 | sequence_token_ = SequenceToken::GetForCurrentThread(); |
| 105 | } |
| 106 |
| 107 | } // namespace base |
| 108 |