sequence_checker_impl.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/compiler_specific.h"
  9. #include "base/macros.h"
  10. #include "base/synchronization/lock.h"
  11. #include "base/thread_annotations.h"
  12. namespace base {
  13. // Real implementation of SequenceChecker for use in debug mode or for temporary
  14. // use in release mode (e.g. to CHECK on a threading issue seen only in the
  15. // wild).
  16. //
  17. // Note: You should almost always use the SequenceChecker class to get the right
  18. // version for your build configuration.
  19. // Note: This is only a check, not a "lock". It is marked "LOCKABLE" only in
  20. // order to support thread_annotations.h.
  21. class LOCKABLE BASE_EXPORT SequenceCheckerImpl {
  22. public:
  23. SequenceCheckerImpl();
  24. ~SequenceCheckerImpl();
  25. // Allow move construct/assign. This must be called on |other|'s associated
  26. // sequence and assignment can only be made into a SequenceCheckerImpl which
  27. // is detached or already associated with the current sequence. This isn't
  28. // thread-safe (|this| and |other| shouldn't be in use while this move is
  29. // performed). If the assignment was legal, the resulting SequenceCheckerImpl
  30. // will be bound to the current sequence and |other| will be detached.
  31. SequenceCheckerImpl(SequenceCheckerImpl&& other);
  32. SequenceCheckerImpl& operator=(SequenceCheckerImpl&& other);
  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. DISALLOW_COPY_AND_ASSIGN(SequenceCheckerImpl);
  47. };
  48. } // namespace base
  49. #endif // BASE_SEQUENCE_CHECKER_IMPL_H_