diff options
author | fdoray <fdoray@chromium.org> | 2016-08-10 06:49:26 +0900 |
---|---|---|
committer | Qijiang Fan <fqj@google.com> | 2020-06-05 03:53:04 +0900 |
commit | 0f73bd902bac49a499404d5589d82ffaa2d18234 (patch) | |
tree | 72f6e8daf798e980b481e3aebdc7dd7b7deb3ad1 /base/sequence_token.h | |
parent | 58ec55723b76c398d685c894ea0d06d4278da758 (diff) | |
download | libchrome-0f73bd902bac49a499404d5589d82ffaa2d18234.tar.gz |
Make ThreadChecker::CalledOnValidThread() return true when called from the same task.
This CL introduces TaskToken to identify individual tasks. In the scope
of a ScopedSetSequenceTokenForCurrentThread, a unique TaskToken is set
in TLS. This unique TaskToken can be retrieved using
TaskToken::GetForCurrentThread().
ThreadCheckerImpl uses TaskToken to make CalledOnValidThread() return
true when called multiple times from the same task. This allows usage
of ThreadChecker/NonThreadSafe objects from the stack on tasks not
otherwise running in a single-threaded context.
BUG=553459
Review-Url: https://codereview.chromium.org/2213263002
Cr-Commit-Position: refs/heads/master@{#410839}
CrOS-Libchrome-Original-Commit: b339954beda5b00c4a482d74594cbe2e7cd39516
Diffstat (limited to 'base/sequence_token.h')
-rw-r--r-- | base/sequence_token.h | 58 |
1 files changed, 52 insertions, 6 deletions
diff --git a/base/sequence_token.h b/base/sequence_token.h index 8d1398ea3c..1f33528128 100644 --- a/base/sequence_token.h +++ b/base/sequence_token.h @@ -45,17 +45,63 @@ class BASE_EXPORT SequenceToken { int token_ = kInvalidSequenceToken; }; -// Throughout its lifetime, determines the value returned by -// SequenceToken::GetForCurrentThread(). +// A token that identifies a task. +// +// This is used by ThreadCheckerImpl to determine whether calls to +// CalledOnValidThread() come from the same task and hence are deterministically +// single-threaded (vs. calls coming from different sequenced or parallel tasks, +// which may or may not run on the same thread). +class BASE_EXPORT TaskToken { + public: + // Instantiates an invalid TaskToken. + TaskToken() = default; + + // Explicitly allow copy. + TaskToken(const TaskToken& other) = default; + TaskToken& operator=(const TaskToken& other) = default; + + // An invalid TaskToken is not equal to any other TaskToken, including + // other invalid TaskTokens. + bool operator==(const TaskToken& other) const; + bool operator!=(const TaskToken& other) const; + + // Returns true if this is a valid TaskToken. + bool IsValid() const; + + // In the scope of a ScopedSetSequenceTokenForCurrentThread, returns a valid + // TaskToken which isn't equal to any TaskToken returned in the scope of a + // different ScopedSetSequenceTokenForCurrentThread. Otherwise, returns an + // invalid TaskToken. + static TaskToken GetForCurrentThread(); + + private: + friend class ScopedSetSequenceTokenForCurrentThread; + + TaskToken(int token) : token_(token) {} + + // Returns a valid TaskToken which isn't equal to any previously returned + // TaskToken. This is private as it only meant to be instantiated by + // ScopedSetSequenceTokenForCurrentThread. + static TaskToken Create(); + + static constexpr int kInvalidTaskToken = -1; + int token_ = kInvalidTaskToken; +}; + +// Instantiate this in the scope where a single task runs. class BASE_EXPORT ScopedSetSequenceTokenForCurrentThread { public: - ScopedSetSequenceTokenForCurrentThread(const SequenceToken& token); + // Throughout the lifetime of the constructed object, + // SequenceToken::GetForCurrentThread() will return |sequence_token| and + // TaskToken::GetForCurrentThread() will return a TaskToken which is not equal + // to any TaskToken returned in the scope of another + // ScopedSetSequenceTokenForCurrentThread. + ScopedSetSequenceTokenForCurrentThread(const SequenceToken& sequence_token); ~ScopedSetSequenceTokenForCurrentThread(); private: - friend class SequenceToken; - - const SequenceToken token_; + const SequenceToken sequence_token_; + const TaskToken task_token_; DISALLOW_COPY_AND_ASSIGN(ScopedSetSequenceTokenForCurrentThread); }; |