Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(361)

Side by Side Diff: base/threading/thread_checker_impl.cc

Issue 2213263002: Make ThreadChecker::CalledOnValidThread() return true when called from the same task. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR gab #6 (fix comment) Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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_checker_impl.h" 5 #include "base/threading/thread_checker_impl.h"
6 6
7 #include "base/threading/thread_task_runner_handle.h" 7 #include "base/threading/thread_task_runner_handle.h"
8 8
9 namespace base { 9 namespace base {
10 10
11 ThreadCheckerImpl::ThreadCheckerImpl() { 11 ThreadCheckerImpl::ThreadCheckerImpl() {
12 AutoLock auto_lock(lock_); 12 AutoLock auto_lock(lock_);
13 EnsureAssigned(); 13 EnsureAssigned();
14 } 14 }
15 15
16 ThreadCheckerImpl::~ThreadCheckerImpl() = default; 16 ThreadCheckerImpl::~ThreadCheckerImpl() = default;
17 17
18 bool ThreadCheckerImpl::CalledOnValidThread() const { 18 bool ThreadCheckerImpl::CalledOnValidThread() const {
19 AutoLock auto_lock(lock_); 19 AutoLock auto_lock(lock_);
20 EnsureAssigned(); 20 EnsureAssigned();
21 21
22 // Always return true when called from the task from which this
23 // ThreadCheckerImpl was assigned to a thread.
24 if (task_token_ == TaskToken::GetForCurrentThread())
25 return true;
26
22 // If this ThreadCheckerImpl is bound to a valid SequenceToken, it must be 27 // If this ThreadCheckerImpl is bound to a valid SequenceToken, it must be
23 // equal to the current SequenceToken and there must be a registered 28 // equal to the current SequenceToken and there must be a registered
24 // ThreadTaskRunnerHandle. Otherwise, the fact that the current task runs on 29 // ThreadTaskRunnerHandle. Otherwise, the fact that the current task runs on
25 // the thread to which this ThreadCheckerImpl is bound is fortuitous. 30 // the thread to which this ThreadCheckerImpl is bound is fortuitous.
26 if (sequence_token_.IsValid() && 31 if (sequence_token_.IsValid() &&
27 (sequence_token_ != SequenceToken::GetForCurrentThread() || 32 (sequence_token_ != SequenceToken::GetForCurrentThread() ||
28 !ThreadTaskRunnerHandle::IsSet())) { 33 !ThreadTaskRunnerHandle::IsSet())) {
29 return false; 34 return false;
30 } 35 }
31 36
32 return thread_id_ == PlatformThread::CurrentRef(); 37 return thread_id_ == PlatformThread::CurrentRef();
33 } 38 }
34 39
35 void ThreadCheckerImpl::DetachFromThread() { 40 void ThreadCheckerImpl::DetachFromThread() {
36 AutoLock auto_lock(lock_); 41 AutoLock auto_lock(lock_);
37 thread_id_ = PlatformThreadRef(); 42 thread_id_ = PlatformThreadRef();
43 task_token_ = TaskToken();
38 sequence_token_ = SequenceToken(); 44 sequence_token_ = SequenceToken();
39 } 45 }
40 46
41 void ThreadCheckerImpl::EnsureAssigned() const { 47 void ThreadCheckerImpl::EnsureAssigned() const {
42 lock_.AssertAcquired(); 48 lock_.AssertAcquired();
43 if (!thread_id_.is_null()) 49 if (!thread_id_.is_null())
44 return; 50 return;
45 51
46 thread_id_ = PlatformThread::CurrentRef(); 52 thread_id_ = PlatformThread::CurrentRef();
53 task_token_ = TaskToken::GetForCurrentThread();
47 sequence_token_ = SequenceToken::GetForCurrentThread(); 54 sequence_token_ = SequenceToken::GetForCurrentThread();
48 } 55 }
49 56
50 } // namespace base 57 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698