sequence_checker_impl.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) 2012 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. #include "base/sequence_checker_impl.h"
  5. #include <utility>
  6. #include "base/check.h"
  7. #include "base/debug/stack_trace.h"
  8. #include "base/memory/ptr_util.h"
  9. #include "base/sequence_token.h"
  10. #include "base/threading/thread_checker.h"
  11. #include "base/threading/thread_checker_impl.h"
  12. #include "base/threading/thread_local_storage.h"
  13. namespace base {
  14. // static
  15. void SequenceCheckerImpl::EnableStackLogging() {
  16. ThreadChecker::EnableStackLogging();
  17. }
  18. class SequenceCheckerImpl::Core {
  19. public:
  20. Core() : sequence_token_(SequenceToken::GetForCurrentThread()) {}
  21. ~Core() = default;
  22. bool CalledOnValidSequence(
  23. std::unique_ptr<debug::StackTrace>* out_bound_at) const {
  24. // SequenceToken::GetForCurrentThread() accesses thread-local storage.
  25. // During destruction the state of thread-local storage is not guaranteed to
  26. // be in a consistent state. Further, task-runner only installs the
  27. // SequenceToken when running a task. For this reason, |sequence_token_| is
  28. // not checked during thread destruction.
  29. if (!SequenceCheckerImpl::HasThreadLocalStorageBeenDestroyed() &&
  30. sequence_token_.IsValid()) {
  31. if (sequence_token_ != SequenceToken::GetForCurrentThread()) {
  32. if (out_bound_at)
  33. *out_bound_at = thread_checker_.GetBoundAt();
  34. return false;
  35. }
  36. return true;
  37. }
  38. // SequenceChecker behaves as a ThreadChecker when it is not bound to a
  39. // valid sequence token.
  40. return thread_checker_.CalledOnValidThread(out_bound_at);
  41. }
  42. private:
  43. SequenceToken sequence_token_{SequenceToken::GetForCurrentThread()};
  44. // Used when |sequence_token_| is invalid, or during thread destruction.
  45. ThreadCheckerImpl thread_checker_;
  46. };
  47. SequenceCheckerImpl::SequenceCheckerImpl() : core_(std::make_unique<Core>()) {}
  48. SequenceCheckerImpl::~SequenceCheckerImpl() = default;
  49. SequenceCheckerImpl::SequenceCheckerImpl(SequenceCheckerImpl&& other) {
  50. // Verify that |other| is called on its associated sequence and bind it now if
  51. // it is currently detached (even if this isn't a DCHECK build).
  52. const bool other_called_on_valid_sequence = other.CalledOnValidSequence();
  53. DCHECK(other_called_on_valid_sequence);
  54. core_ = std::move(other.core_);
  55. }
  56. SequenceCheckerImpl& SequenceCheckerImpl::operator=(
  57. SequenceCheckerImpl&& other) {
  58. // If |this| is not in a detached state it needs to be bound to the current
  59. // sequence.
  60. DCHECK(CalledOnValidSequence());
  61. // Verify that |other| is called on its associated sequence and bind it now if
  62. // it is currently detached (even if this isn't a DCHECK build).
  63. const bool other_called_on_valid_sequence = other.CalledOnValidSequence();
  64. DCHECK(other_called_on_valid_sequence);
  65. // Intentionally not using either |lock_| in this method to let TSAN catch
  66. // racy assign.
  67. TS_UNCHECKED_READ(core_) = std::move(TS_UNCHECKED_READ(other.core_));
  68. return *this;
  69. }
  70. bool SequenceCheckerImpl::CalledOnValidSequence(
  71. std::unique_ptr<debug::StackTrace>* bound_at) const {
  72. AutoLock auto_lock(lock_);
  73. if (!core_)
  74. core_ = std::make_unique<Core>();
  75. return core_->CalledOnValidSequence(bound_at);
  76. }
  77. void SequenceCheckerImpl::DetachFromSequence() {
  78. AutoLock auto_lock(lock_);
  79. core_.reset();
  80. }
  81. // static
  82. bool SequenceCheckerImpl::HasThreadLocalStorageBeenDestroyed() {
  83. return ThreadLocalStorage::HasBeenDestroyed();
  84. }
  85. } // namespace base