OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef BASE_SEQUENCE_TOKEN_H_ | 5 #ifndef BASE_SEQUENCE_TOKEN_H_ |
6 #define BASE_SEQUENCE_TOKEN_H_ | 6 #define BASE_SEQUENCE_TOKEN_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 | 10 |
(...skipping 27 matching lines...) Expand all Loading... | |
38 // if any. | 38 // if any. |
39 static SequenceToken GetForCurrentThread(); | 39 static SequenceToken GetForCurrentThread(); |
40 | 40 |
41 private: | 41 private: |
42 SequenceToken(int token) : token_(token) {} | 42 SequenceToken(int token) : token_(token) {} |
43 | 43 |
44 static constexpr int kInvalidSequenceToken = -1; | 44 static constexpr int kInvalidSequenceToken = -1; |
45 int token_ = kInvalidSequenceToken; | 45 int token_ = kInvalidSequenceToken; |
46 }; | 46 }; |
47 | 47 |
48 // Throughout its lifetime, determines the value returned by | 48 // A token that identifies a task. |
49 // SequenceToken::GetForCurrentThread(). | 49 // |
50 // This is used by ThreadCheckerImpl to determine whether calls to | |
51 // CalledOnValidThread() come from the same task and hence are deterministically | |
52 // single-threaded (vs. calls coming from different sequenced or parallel tasks, | |
53 // which may or may not run on the same thread). | |
54 class BASE_EXPORT TaskToken { | |
55 public: | |
56 // Instantiates an invalid TaskToken. | |
57 TaskToken() = default; | |
58 | |
59 // Explicitly allow copy. | |
60 TaskToken(const TaskToken& other) = default; | |
61 TaskToken& operator=(const TaskToken& other) = default; | |
62 | |
63 // An invalid TaskToken is not equal to any other TaskToken, including | |
64 // other invalid TaskTokens. | |
65 bool operator==(const TaskToken& other) const; | |
66 bool operator!=(const TaskToken& other) const; | |
67 | |
68 // Returns true if this is a valid TaskToken. | |
69 bool IsValid() const; | |
70 | |
71 // In the scope of a ScopedSetSequenceTokenForCurrentThread, returns a valid | |
72 // TaskToken which isn't equal to any TaskToken returned in the scope of a | |
73 // different ScopedSetSequenceTokenForCurrentThread. Otherwise, returns an | |
74 // invalid TaskToken. | |
75 static TaskToken GetForCurrentThread(); | |
76 | |
77 private: | |
78 friend class ScopedSetSequenceTokenForCurrentThread; | |
79 | |
80 TaskToken(int token) : token_(token) {} | |
Lei Zhang
2016/08/08 23:21:26
explicit
gab
2016/08/09 15:50:15
Done.
| |
81 | |
82 // Returns a valid TaskToken which isn't equal to any previously returned | |
83 // TaskToken. This is private as it only meant to be instantiated by | |
84 // ScopedSetSequenceTokenForCurrentThread. | |
85 static TaskToken Create(); | |
86 | |
87 static constexpr int kInvalidTaskToken = -1; | |
88 int token_ = kInvalidTaskToken; | |
89 }; | |
90 | |
91 // Instantiate this in the scope where a single task runs. | |
50 class BASE_EXPORT ScopedSetSequenceTokenForCurrentThread { | 92 class BASE_EXPORT ScopedSetSequenceTokenForCurrentThread { |
51 public: | 93 public: |
52 ScopedSetSequenceTokenForCurrentThread(const SequenceToken& token); | 94 // Throughout the lifetime of the constructed object, |
95 // SequenceToken::GetForCurrentThread() will return |sequence_token| and | |
96 // TaskToken::GetForCurrentThread() will return a TaskToken which is not equal | |
97 // to any TaskToken returned in the scope of another | |
98 // ScopedSetSequenceTokenForCurrentThread. | |
99 ScopedSetSequenceTokenForCurrentThread(const SequenceToken& sequence_token); | |
53 ~ScopedSetSequenceTokenForCurrentThread(); | 100 ~ScopedSetSequenceTokenForCurrentThread(); |
54 | 101 |
55 private: | 102 private: |
56 friend class SequenceToken; | 103 const SequenceToken sequence_token_; |
57 | 104 const TaskToken task_token_; |
58 const SequenceToken token_; | |
59 | 105 |
60 DISALLOW_COPY_AND_ASSIGN(ScopedSetSequenceTokenForCurrentThread); | 106 DISALLOW_COPY_AND_ASSIGN(ScopedSetSequenceTokenForCurrentThread); |
61 }; | 107 }; |
62 | 108 |
63 } // namespace base | 109 } // namespace base |
64 | 110 |
65 #endif // BASE_SEQUENCE_TOKEN_H_ | 111 #endif // BASE_SEQUENCE_TOKEN_H_ |
OLD | NEW |