sequence_checker_impl.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // Real implementation of SequenceChecker for use in debug mode or for temporary
  12. // use in release mode (e.g. to CHECK on a threading issue seen only in the
  13. // wild).
  14. //
  15. // Note: You should almost always use the SequenceChecker class to get the right
  16. // version for your build configuration.
  17. // Note: This is only a check, not a "lock". It is marked "LOCKABLE" only in
  18. // order to support thread_annotations.h.
  19. class LOCKABLE BASE_EXPORT SequenceCheckerImpl {
  20. public:
  21. SequenceCheckerImpl();
  22. // Allow move construct/assign. This must be called on |other|'s associated
  23. // sequence and assignment can only be made into a SequenceCheckerImpl which
  24. // is detached or already associated with the current sequence. This isn't
  25. // thread-safe (|this| and |other| shouldn't be in use while this move is
  26. // performed). If the assignment was legal, the resulting SequenceCheckerImpl
  27. // will be bound to the current sequence and |other| will be detached.
  28. SequenceCheckerImpl(SequenceCheckerImpl&& other);
  29. SequenceCheckerImpl& operator=(SequenceCheckerImpl&& other);
  30. SequenceCheckerImpl(const SequenceCheckerImpl&) = delete;
  31. SequenceCheckerImpl& operator=(const SequenceCheckerImpl&) = delete;
  32. ~SequenceCheckerImpl();
  33. // Returns true if called in sequence with previous calls to this method and
  34. // the constructor.
  35. bool CalledOnValidSequence() const WARN_UNUSED_RESULT;
  36. // Unbinds the checker from the currently associated sequence. The checker
  37. // will be re-bound on the next call to CalledOnValidSequence().
  38. void DetachFromSequence();
  39. private:
  40. class Core;
  41. // Calls straight to ThreadLocalStorage::HasBeenDestroyed(). Exposed purely
  42. // for 'friend' to work.
  43. static bool HasThreadLocalStorageBeenDestroyed();
  44. mutable Lock lock_;
  45. mutable std::unique_ptr<Core> core_ GUARDED_BY(lock_);
  46. };
  47. } // namespace base
  48. #endif // BASE_SEQUENCE_CHECKER_IMPL_H_