xref: /aosp_15_r20/external/cronet/base/sequence_token.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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.
4 
5 #ifndef BASE_SEQUENCE_TOKEN_H_
6 #define BASE_SEQUENCE_TOKEN_H_
7 
8 // TODO: Remove this unused include once no other file indirectly depends on it.
9 #include "base/auto_reset.h"
10 #include "base/base_export.h"
11 
12 namespace base {
13 namespace internal {
14 
15 // A token that identifies a series of sequenced work items (i.e. tasks, native
16 // message handlers, code blocks running outside or a `RunLoop`, etc. that are
17 // mutually exclusive).
18 class BASE_EXPORT SequenceToken {
19  public:
20   // Instantiates an invalid SequenceToken.
21   constexpr SequenceToken() = default;
22 
23   // Explicitly allow copy.
24   SequenceToken(const SequenceToken& other) = default;
25   SequenceToken& operator=(const SequenceToken& other) = default;
26 
27   // An invalid SequenceToken is not equal to any other SequenceToken, including
28   // other invalid SequenceTokens.
29   bool operator==(const SequenceToken& other) const;
30   bool operator!=(const SequenceToken& other) const;
31 
32   // Returns true if this is a valid SequenceToken.
33   bool IsValid() const;
34 
35   // Returns the integer uniquely representing this SequenceToken. This method
36   // should only be used for tracing and debugging.
37   int ToInternalValue() const;
38 
39   // Returns a valid SequenceToken which isn't equal to any previously returned
40   // SequenceToken.
41   static SequenceToken Create();
42 
43   // Returns the `SequenceToken` for the work item currently running on this
44   // thread. A valid and unique `SequenceToken` is assigned to each thread. It
45   // can be overridden in a scope with `TaskScope`.
46   static SequenceToken GetForCurrentThread();
47 
48  private:
SequenceToken(int token)49   explicit SequenceToken(int token) : token_(token) {}
50 
51   static constexpr int kInvalidSequenceToken = -1;
52   int token_ = kInvalidSequenceToken;
53 };
54 
55 // A token that identifies a task.
56 //
57 // This is used by ThreadCheckerImpl to determine whether calls to
58 // CalledOnValidThread() come from the same task and hence are deterministically
59 // single-threaded (vs. calls coming from different sequenced or parallel tasks,
60 // which may or may not run on the same thread).
61 class BASE_EXPORT TaskToken {
62  public:
63   // Instantiates an invalid TaskToken.
64   constexpr TaskToken() = default;
65 
66   // Explicitly allow copy.
67   TaskToken(const TaskToken& other) = default;
68   TaskToken& operator=(const TaskToken& other) = default;
69 
70   // An invalid TaskToken is not equal to any other TaskToken, including
71   // other invalid TaskTokens.
72   bool operator==(const TaskToken& other) const;
73   bool operator!=(const TaskToken& other) const;
74 
75   // Returns true if this is a valid TaskToken.
76   bool IsValid() const;
77 
78   // In the scope of a `TaskScope`, returns a valid `TaskToken` which isn't
79   // equal to any `TaskToken` returned in the scope of a different `TaskScope`.
80   // Otherwise, returns an invalid `TaskToken`.
81   static TaskToken GetForCurrentThread();
82 
83  private:
84   friend class TaskScope;
85 
TaskToken(int token)86   explicit TaskToken(int token) : token_(token) {}
87 
88   // Returns a valid `TaskToken` which isn't equal to any previously returned
89   // `TaskToken`. Private as it is only meant to be instantiated by `TaskScope`.
90   static TaskToken Create();
91 
92   static constexpr int kInvalidTaskToken = -1;
93   int token_ = kInvalidTaskToken;
94 };
95 
96 // Returns true if a thread checker bound in a different task than the current
97 // one but on the same sequence and thread may return true from
98 // `CalledOnValidSequence()`.
99 bool BASE_EXPORT CurrentTaskIsThreadBound();
100 
101 // Identifies a scope in which a task runs.
102 class BASE_EXPORT [[maybe_unused, nodiscard]] TaskScope {
103  public:
104   // `sequence_token` identifies the series of mutually exclusive work items
105   // that this task is part of (may be unique if this task isn't mutually
106   // exclusive with any other work item). `is_thread_bound` sets the value
107   // returned by `CurrentTaskIsThreadBound()` within the scope.
108   // `is_running_synchronously` is true iff this is instantiated for a task run
109   // synchronously by `RunOrPostTask()`.
110   explicit TaskScope(SequenceToken sequence_token,
111                      bool is_thread_bound,
112                      bool is_running_synchronously = false);
113   TaskScope(const TaskScope&) = delete;
114   TaskScope& operator=(const TaskScope&) = delete;
115   ~TaskScope();
116 
117  private:
118   const TaskToken previous_task_token_;
119   const SequenceToken previous_sequence_token_;
120   const bool previous_task_is_thread_bound_;
121   const bool previous_task_is_running_synchronously_;
122 };
123 
124 }  // namespace internal
125 
126 // Returns true if the current task is run synchronously by `RunOrPostTask()`.
127 bool BASE_EXPORT CurrentTaskIsRunningSynchronously();
128 
129 }  // namespace base
130 
131 #endif  // BASE_SEQUENCE_TOKEN_H_
132