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

Side by Side Diff: base/sequence_and_task_token.h

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: similarity 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 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_AND_TASK_TOKEN_H_
gab 2016/08/08 16:34:22 Since TaskToken is more-or-less an implementation
fdoray 2016/08/08 17:45:18 Done.
6 #define BASE_SEQUENCE_TOKEN_H_ 6 #define BASE_SEQUENCE_AND_TASK_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
11 namespace base { 11 namespace base {
12 12
13 // A token that identifies a series of sequenced tasks (i.e. tasks that run one 13 // A token that identifies a series of sequenced tasks (i.e. tasks that run one
14 // at a time in posting order). 14 // at a time in posting order).
15 class BASE_EXPORT SequenceToken { 15 class BASE_EXPORT SequenceToken {
16 public: 16 public:
(...skipping 10 matching lines...) Expand all
27 bool operator!=(const SequenceToken& other) const; 27 bool operator!=(const SequenceToken& other) const;
28 28
29 // Returns true if this is a valid SequenceToken. 29 // Returns true if this is a valid SequenceToken.
30 bool IsValid() const; 30 bool IsValid() const;
31 31
32 // Returns a valid SequenceToken which isn't equal to any previously returned 32 // Returns a valid SequenceToken which isn't equal to any previously returned
33 // SequenceToken. 33 // SequenceToken.
34 static SequenceToken Create(); 34 static SequenceToken Create();
35 35
36 // Returns the SequenceToken associated with the task running on the current 36 // Returns the SequenceToken associated with the task running on the current
37 // thread, as determined by the active ScopedSetSequenceTokenForCurrentThread 37 // thread, as determined by the active
38 // if any. 38 // ScopedSetSequenceAndTaskTokenForCurrentThread 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.
gab 2016/08/08 16:34:22 Expand comment to explain why this is necessary.
fdoray 2016/08/08 17:45:18 Done.
49 // SequenceToken::GetForCurrentThread(). 49 class BASE_EXPORT TaskToken {
50 class BASE_EXPORT ScopedSetSequenceTokenForCurrentThread {
51 public: 50 public:
52 ScopedSetSequenceTokenForCurrentThread(const SequenceToken& token); 51 // Instantiates an invalid TaskToken.
53 ~ScopedSetSequenceTokenForCurrentThread(); 52 TaskToken() = default;
53
54 // Explicitly allow copy.
55 TaskToken(const TaskToken& other) = default;
56 TaskToken& operator=(const TaskToken& other) = default;
57
58 // An invalid TaskToken is not equal to any other TaskToken, including
59 // other invalid TaskTokens.
60 bool operator==(const TaskToken& other) const;
61 bool operator!=(const TaskToken& other) const;
62
63 // Returns true if this is a valid TaskToken.
64 bool IsValid() const;
65
66 // Returns the TaskToken associated with the task running on the current
67 // thread, as determined by the active
68 // ScopedSetSequenceAndTaskTokenForCurrentThread if any.
69 static TaskToken GetForCurrentThread();
70
71 private:
72 friend class ScopedSetSequenceAndTaskTokenForCurrentThread;
73
74 TaskToken(int token) : token_(token) {}
gab 2016/08/08 16:34:22 Comment explaining why this is private (and also j
fdoray 2016/08/08 17:45:18 Done.
75
76 // Returns a valid TaskToken which isn't equal to any previously returned
77 // TaskToken.
78 static TaskToken Create();
79
80 static constexpr int kInvalidTaskToken = -1;
81 int token_ = kInvalidTaskToken;
82 };
83
84 // Instantiate this in the scope where a single task runs.
85 class BASE_EXPORT ScopedSetSequenceAndTaskTokenForCurrentThread {
86 public:
87 // Throughout the lifetime of the constructed object,
88 // SequenceToken::GetForCurrentThread() will return |sequence_token| and
89 // TaskToken::GetForCurrentThread() will return a TaskToken which is not equal
90 // to any TaskToken returned in the scope of another
91 // ScopedSetSequenceAndTaskTokenForCurrentThread.
92 ScopedSetSequenceAndTaskTokenForCurrentThread(
93 const SequenceToken& sequence_token);
94 ~ScopedSetSequenceAndTaskTokenForCurrentThread();
54 95
55 private: 96 private:
56 friend class SequenceToken; 97 friend class SequenceToken;
98 friend class TaskToken;
gab 2016/08/08 16:34:22 Cyclic-friending is weird, why is this needed?
fdoray 2016/08/08 17:45:18 ScopedSetSequenceTokenForCurrentThread needs to be
57 99
58 const SequenceToken token_; 100 const SequenceToken sequence_token_;
101 const TaskToken task_token_;
59 102
60 DISALLOW_COPY_AND_ASSIGN(ScopedSetSequenceTokenForCurrentThread); 103 DISALLOW_COPY_AND_ASSIGN(ScopedSetSequenceAndTaskTokenForCurrentThread);
61 }; 104 };
62 105
63 } // namespace base 106 } // namespace base
64 107
65 #endif // BASE_SEQUENCE_TOKEN_H_ 108 #endif // BASE_SEQUENCE_AND_TASK_TOKEN_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698