[M108 Migration][VD] Support set time and time zone offset
[platform/framework/web/chromium-efl.git] / base / sequence_token.h
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 #include "base/base_export.h"
9
10 namespace base {
11
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 {
15  public:
16   // Instantiates an invalid SequenceToken.
17   SequenceToken() = default;
18
19   // Explicitly allow copy.
20   SequenceToken(const SequenceToken& other) = default;
21   SequenceToken& operator=(const SequenceToken& other) = default;
22
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;
27
28   // Returns true if this is a valid SequenceToken.
29   bool IsValid() const;
30
31   // Returns the integer uniquely representing this SequenceToken. This method
32   // should only be used for tracing and debugging.
33   int ToInternalValue() const;
34
35   // Returns a valid SequenceToken which isn't equal to any previously returned
36   // SequenceToken.
37   static SequenceToken Create();
38
39   // Returns the SequenceToken associated with the task running on the current
40   // thread, as determined by the active ScopedSetSequenceTokenForCurrentThread
41   // if any.
42   static SequenceToken GetForCurrentThread();
43
44  private:
45   explicit SequenceToken(int token) : token_(token) {}
46
47   static constexpr int kInvalidSequenceToken = -1;
48   int token_ = kInvalidSequenceToken;
49 };
50
51 // A token that identifies a task.
52 //
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 {
58  public:
59   // Instantiates an invalid TaskToken.
60   TaskToken() = default;
61
62   // Explicitly allow copy.
63   TaskToken(const TaskToken& other) = default;
64   TaskToken& operator=(const TaskToken& other) = default;
65
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;
70
71   // Returns true if this is a valid TaskToken.
72   bool IsValid() const;
73
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
77   // invalid TaskToken.
78   static TaskToken GetForCurrentThread();
79
80  private:
81   friend class ScopedSetSequenceTokenForCurrentThread;
82
83   explicit TaskToken(int token) : token_(token) {}
84
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();
89
90   static constexpr int kInvalidTaskToken = -1;
91   int token_ = kInvalidTaskToken;
92 };
93
94 // Instantiate this in the scope where a single task runs.
95 class BASE_EXPORT ScopedSetSequenceTokenForCurrentThread {
96  public:
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();
109
110  private:
111   const SequenceToken sequence_token_;
112   const TaskToken task_token_;
113 };
114
115 }  // namespace base
116
117 #endif  // BASE_SEQUENCE_TOKEN_H_