sequence_token.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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
  3. // found in the LICENSE file.
  4. #ifndef BASE_SEQUENCE_TOKEN_H_
  5. #define BASE_SEQUENCE_TOKEN_H_
  6. #include "base/base_export.h"
  7. namespace base {
  8. // A token that identifies a series of sequenced tasks (i.e. tasks that run one
  9. // at a time in posting order).
  10. class BASE_EXPORT SequenceToken {
  11. public:
  12. // Instantiates an invalid SequenceToken.
  13. SequenceToken() = default;
  14. // Explicitly allow copy.
  15. SequenceToken(const SequenceToken& other) = default;
  16. SequenceToken& operator=(const SequenceToken& other) = default;
  17. // An invalid SequenceToken is not equal to any other SequenceToken, including
  18. // other invalid SequenceTokens.
  19. bool operator==(const SequenceToken& other) const;
  20. bool operator!=(const SequenceToken& other) const;
  21. // Returns true if this is a valid SequenceToken.
  22. bool IsValid() const;
  23. // Returns the integer uniquely representing this SequenceToken. This method
  24. // should only be used for tracing and debugging.
  25. int ToInternalValue() const;
  26. // Returns a valid SequenceToken which isn't equal to any previously returned
  27. // SequenceToken.
  28. static SequenceToken Create();
  29. // Returns the SequenceToken associated with the task running on the current
  30. // thread, as determined by the active ScopedSetSequenceTokenForCurrentThread
  31. // if any.
  32. static SequenceToken GetForCurrentThread();
  33. private:
  34. explicit SequenceToken(int token) : token_(token) {}
  35. static constexpr int kInvalidSequenceToken = -1;
  36. int token_ = kInvalidSequenceToken;
  37. };
  38. // A token that identifies a task.
  39. //
  40. // This is used by ThreadCheckerImpl to determine whether calls to
  41. // CalledOnValidThread() come from the same task and hence are deterministically
  42. // single-threaded (vs. calls coming from different sequenced or parallel tasks,
  43. // which may or may not run on the same thread).
  44. class BASE_EXPORT TaskToken {
  45. public:
  46. // Instantiates an invalid TaskToken.
  47. TaskToken() = default;
  48. // Explicitly allow copy.
  49. TaskToken(const TaskToken& other) = default;
  50. TaskToken& operator=(const TaskToken& other) = default;
  51. // An invalid TaskToken is not equal to any other TaskToken, including
  52. // other invalid TaskTokens.
  53. bool operator==(const TaskToken& other) const;
  54. bool operator!=(const TaskToken& other) const;
  55. // Returns true if this is a valid TaskToken.
  56. bool IsValid() const;
  57. // In the scope of a ScopedSetSequenceTokenForCurrentThread, returns a valid
  58. // TaskToken which isn't equal to any TaskToken returned in the scope of a
  59. // different ScopedSetSequenceTokenForCurrentThread. Otherwise, returns an
  60. // invalid TaskToken.
  61. static TaskToken GetForCurrentThread();
  62. private:
  63. friend class ScopedSetSequenceTokenForCurrentThread;
  64. explicit TaskToken(int token) : token_(token) {}
  65. // Returns a valid TaskToken which isn't equal to any previously returned
  66. // TaskToken. This is private as it only meant to be instantiated by
  67. // ScopedSetSequenceTokenForCurrentThread.
  68. static TaskToken Create();
  69. static constexpr int kInvalidTaskToken = -1;
  70. int token_ = kInvalidTaskToken;
  71. };
  72. // Instantiate this in the scope where a single task runs.
  73. class BASE_EXPORT ScopedSetSequenceTokenForCurrentThread {
  74. public:
  75. // Throughout the lifetime of the constructed object,
  76. // SequenceToken::GetForCurrentThread() will return |sequence_token| and
  77. // TaskToken::GetForCurrentThread() will return a TaskToken which is not equal
  78. // to any TaskToken returned in the scope of another
  79. // ScopedSetSequenceTokenForCurrentThread.
  80. explicit ScopedSetSequenceTokenForCurrentThread(
  81. const SequenceToken& sequence_token);
  82. ScopedSetSequenceTokenForCurrentThread(
  83. const ScopedSetSequenceTokenForCurrentThread&) = delete;
  84. ScopedSetSequenceTokenForCurrentThread& operator=(
  85. const ScopedSetSequenceTokenForCurrentThread&) = delete;
  86. ~ScopedSetSequenceTokenForCurrentThread();
  87. private:
  88. const SequenceToken sequence_token_;
  89. const TaskToken task_token_;
  90. };
  91. } // namespace base
  92. #endif // BASE_SEQUENCE_TOKEN_H_