sequence_checker_impl.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #ifndef BASE_SEQUENCE_CHECKER_IMPL_H_
  5. #define BASE_SEQUENCE_CHECKER_IMPL_H_
  6. #include <memory>
  7. #include "base/base_export.h"
  8. #include "base/synchronization/lock.h"
  9. #include "base/thread_annotations.h"
  10. namespace base {
  11. namespace debug {
  12. class StackTrace;
  13. }
  14. // Real implementation of SequenceChecker for use in debug mode or for temporary
  15. // use in release mode (e.g. to CHECK on a threading issue seen only in the
  16. // wild).
  17. //
  18. // Note: You should almost always use the SequenceChecker class to get the right
  19. // version for your build configuration.
  20. // Note: This is marked with "context" capability in order to support
  21. // thread_annotations.h.
  22. class THREAD_ANNOTATION_ATTRIBUTE__(capability("context"))
  23. BASE_EXPORT SequenceCheckerImpl {
  24. public:
  25. static void EnableStackLogging();
  26. SequenceCheckerImpl();
  27. // Allow move construct/assign. This must be called on |other|'s associated
  28. // sequence and assignment can only be made into a SequenceCheckerImpl which
  29. // is detached or already associated with the current sequence. This isn't
  30. // thread-safe (|this| and |other| shouldn't be in use while this move is
  31. // performed). If the assignment was legal, the resulting SequenceCheckerImpl
  32. // will be bound to the current sequence and |other| will be detached.
  33. SequenceCheckerImpl(SequenceCheckerImpl&& other);
  34. SequenceCheckerImpl& operator=(SequenceCheckerImpl&& other);
  35. SequenceCheckerImpl(const SequenceCheckerImpl&) = delete;
  36. SequenceCheckerImpl& operator=(const SequenceCheckerImpl&) = delete;
  37. ~SequenceCheckerImpl();
  38. // Returns true if called in sequence with previous calls to this method and
  39. // the constructor.
  40. // On returning false, if logging is enabled with EnableStackLogging() and
  41. // `out_bound_at` is not null, this method allocates a StackTrace and returns
  42. // it in the out-parameter, storing inside it the stack from where the failing
  43. // SequenceChecker was bound to its sequence. Otherwise, out_bound_at is left
  44. // untouched.
  45. [[nodiscard]] bool CalledOnValidSequence(
  46. std::unique_ptr<debug::StackTrace>* out_bound_at = nullptr) const;
  47. // Unbinds the checker from the currently associated sequence. The checker
  48. // will be re-bound on the next call to CalledOnValidSequence().
  49. void DetachFromSequence();
  50. private:
  51. class Core;
  52. // Calls straight to ThreadLocalStorage::HasBeenDestroyed(). Exposed purely
  53. // for 'friend' to work.
  54. static bool HasThreadLocalStorageBeenDestroyed();
  55. mutable Lock lock_;
  56. mutable std::unique_ptr<Core> core_ GUARDED_BY(lock_);
  57. };
  58. } // namespace base
  59. #endif // BASE_SEQUENCE_CHECKER_IMPL_H_