thread_checker_impl.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) 2011 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_THREADING_THREAD_CHECKER_IMPL_H_
  5. #define BASE_THREADING_THREAD_CHECKER_IMPL_H_
  6. #include "base/base_export.h"
  7. #include "base/compiler_specific.h"
  8. #include "base/sequence_token.h"
  9. #include "base/synchronization/lock.h"
  10. #include "base/thread_annotations.h"
  11. #include "base/threading/platform_thread.h"
  12. namespace base {
  13. // Real implementation of ThreadChecker, 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 ThreadChecker 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 ThreadCheckerImpl {
  22. public:
  23. ThreadCheckerImpl();
  24. ~ThreadCheckerImpl();
  25. // Allow move construct/assign. This must be called on |other|'s associated
  26. // thread and assignment can only be made into a ThreadCheckerImpl which is
  27. // detached or already associated with the current thread. 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 ThreadCheckerImpl
  30. // will be bound to the current thread and |other| will be detached.
  31. ThreadCheckerImpl(ThreadCheckerImpl&& other);
  32. ThreadCheckerImpl& operator=(ThreadCheckerImpl&& other);
  33. bool CalledOnValidThread() const WARN_UNUSED_RESULT;
  34. // Changes the thread that is checked for in CalledOnValidThread. This may
  35. // be useful when an object may be created on one thread and then used
  36. // exclusively on another thread.
  37. void DetachFromThread();
  38. private:
  39. void EnsureAssignedLockRequired() const EXCLUSIVE_LOCKS_REQUIRED(lock_);
  40. // Members are mutable so that CalledOnValidThread() can set them.
  41. // Synchronizes access to all members.
  42. mutable base::Lock lock_;
  43. // Thread on which CalledOnValidThread() may return true.
  44. mutable PlatformThreadRef thread_id_ GUARDED_BY(lock_);
  45. // TaskToken for which CalledOnValidThread() always returns true. This allows
  46. // CalledOnValidThread() to return true when called multiple times from the
  47. // same task, even if it's not running in a single-threaded context itself
  48. // (allowing usage of ThreadChecker objects on the stack in the scope of one-
  49. // off tasks). Note: CalledOnValidThread() may return true even if the current
  50. // TaskToken is not equal to this.
  51. mutable TaskToken task_token_ GUARDED_BY(lock_);
  52. // SequenceToken for which CalledOnValidThread() may return true. Used to
  53. // ensure that CalledOnValidThread() doesn't return true for ThreadPool
  54. // tasks that happen to run on the same thread but weren't posted to the same
  55. // SingleThreadTaskRunner.
  56. mutable SequenceToken sequence_token_ GUARDED_BY(lock_);
  57. };
  58. } // namespace base
  59. #endif // BASE_THREADING_THREAD_CHECKER_IMPL_H_