1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_SEQUENCE_TOKEN_H_
6 #define BASE_SEQUENCE_TOKEN_H_
8 #include "base/base_export.h"
12 // A token that identifies a series of sequenced tasks (i.e. tasks that run one
13 // at a time in posting order).
14 class BASE_EXPORT SequenceToken {
16 // Instantiates an invalid SequenceToken.
17 SequenceToken() = default;
19 // Explicitly allow copy.
20 SequenceToken(const SequenceToken& other) = default;
21 SequenceToken& operator=(const SequenceToken& other) = default;
23 // An invalid SequenceToken is not equal to any other SequenceToken, including
24 // other invalid SequenceTokens.
25 bool operator==(const SequenceToken& other) const;
26 bool operator!=(const SequenceToken& other) const;
28 // Returns true if this is a valid SequenceToken.
31 // Returns the integer uniquely representing this SequenceToken. This method
32 // should only be used for tracing and debugging.
33 int ToInternalValue() const;
35 // Returns a valid SequenceToken which isn't equal to any previously returned
37 static SequenceToken Create();
39 // Returns the SequenceToken associated with the task running on the current
40 // thread, as determined by the active ScopedSetSequenceTokenForCurrentThread
42 static SequenceToken GetForCurrentThread();
45 explicit SequenceToken(int token) : token_(token) {}
47 static constexpr int kInvalidSequenceToken = -1;
48 int token_ = kInvalidSequenceToken;
51 // A token that identifies a task.
53 // This is used by ThreadCheckerImpl to determine whether calls to
54 // CalledOnValidThread() come from the same task and hence are deterministically
55 // single-threaded (vs. calls coming from different sequenced or parallel tasks,
56 // which may or may not run on the same thread).
57 class BASE_EXPORT TaskToken {
59 // Instantiates an invalid TaskToken.
60 TaskToken() = default;
62 // Explicitly allow copy.
63 TaskToken(const TaskToken& other) = default;
64 TaskToken& operator=(const TaskToken& other) = default;
66 // An invalid TaskToken is not equal to any other TaskToken, including
67 // other invalid TaskTokens.
68 bool operator==(const TaskToken& other) const;
69 bool operator!=(const TaskToken& other) const;
71 // Returns true if this is a valid TaskToken.
74 // In the scope of a ScopedSetSequenceTokenForCurrentThread, returns a valid
75 // TaskToken which isn't equal to any TaskToken returned in the scope of a
76 // different ScopedSetSequenceTokenForCurrentThread. Otherwise, returns an
78 static TaskToken GetForCurrentThread();
81 friend class ScopedSetSequenceTokenForCurrentThread;
83 explicit TaskToken(int token) : token_(token) {}
85 // Returns a valid TaskToken which isn't equal to any previously returned
86 // TaskToken. This is private as it only meant to be instantiated by
87 // ScopedSetSequenceTokenForCurrentThread.
88 static TaskToken Create();
90 static constexpr int kInvalidTaskToken = -1;
91 int token_ = kInvalidTaskToken;
94 // Instantiate this in the scope where a single task runs.
95 class BASE_EXPORT ScopedSetSequenceTokenForCurrentThread {
97 // Throughout the lifetime of the constructed object,
98 // SequenceToken::GetForCurrentThread() will return |sequence_token| and
99 // TaskToken::GetForCurrentThread() will return a TaskToken which is not equal
100 // to any TaskToken returned in the scope of another
101 // ScopedSetSequenceTokenForCurrentThread.
102 explicit ScopedSetSequenceTokenForCurrentThread(
103 const SequenceToken& sequence_token);
104 ScopedSetSequenceTokenForCurrentThread(
105 const ScopedSetSequenceTokenForCurrentThread&) = delete;
106 ScopedSetSequenceTokenForCurrentThread& operator=(
107 const ScopedSetSequenceTokenForCurrentThread&) = delete;
108 ~ScopedSetSequenceTokenForCurrentThread();
111 const SequenceToken sequence_token_;
112 const TaskToken task_token_;
117 #endif // BASE_SEQUENCE_TOKEN_H_