sequence_token.h 3.9 KB

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