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

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

Issue 2165663003: TaskScheduler: Add SequenceToken and ScopedSetSequenceTokenForCurrentThread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: improve comment Created 4 years, 5 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"
8
7 namespace base { 9 namespace base {
8 10
9 ThreadCheckerImpl::ThreadCheckerImpl() 11 ThreadCheckerImpl::ThreadCheckerImpl() {
10 : valid_thread_id_() { 12 AutoLock auto_lock(lock_);
11 EnsureThreadIdAssigned(); 13 EnsureAssigned();
12 } 14 }
13 15
14 ThreadCheckerImpl::~ThreadCheckerImpl() {} 16 ThreadCheckerImpl::~ThreadCheckerImpl() = default;
15 17
16 bool ThreadCheckerImpl::CalledOnValidThread() const { 18 bool ThreadCheckerImpl::CalledOnValidThread() const {
17 EnsureThreadIdAssigned();
18 AutoLock auto_lock(lock_); 19 AutoLock auto_lock(lock_);
19 return valid_thread_id_ == PlatformThread::CurrentRef(); 20 EnsureAssigned();
21
22 // If this ThreadCheckerImpl is bound to a valid SequenceToken, it must be
23 // equal to the current SequenceToken and there must be a registered
24 // ThreadTaskRunnerHandle. Otherwise, the fact that the current task runs on
25 // the thread to which this ThreadCheckerImpl is bound is fortuitous.
26 if (sequence_token_.IsValid() &&
27 (sequence_token_ != internal::SequenceToken::GetCurrent() ||
28 !ThreadTaskRunnerHandle::IsSet())) {
29 return false;
30 }
31
32 return thread_id_ == PlatformThread::CurrentRef();
20 } 33 }
21 34
22 void ThreadCheckerImpl::DetachFromThread() { 35 void ThreadCheckerImpl::DetachFromThread() {
23 AutoLock auto_lock(lock_); 36 AutoLock auto_lock(lock_);
24 valid_thread_id_ = PlatformThreadRef(); 37 thread_id_ = PlatformThreadRef();
38 sequence_token_ = internal::SequenceToken();
25 } 39 }
26 40
27 void ThreadCheckerImpl::EnsureThreadIdAssigned() const { 41 void ThreadCheckerImpl::EnsureAssigned() const {
28 AutoLock auto_lock(lock_); 42 lock_.AssertAcquired();
29 if (valid_thread_id_.is_null()) { 43 if (!thread_id_.is_null())
30 valid_thread_id_ = PlatformThread::CurrentRef(); 44 return;
31 } 45
46 thread_id_ = PlatformThread::CurrentRef();
47 sequence_token_ = internal::SequenceToken::GetCurrent();
32 } 48 }
33 49
34 } // namespace base 50 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698