checked_lock_impl.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2016 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_TASK_COMMON_CHECKED_LOCK_IMPL_H_
  5. #define BASE_TASK_COMMON_CHECKED_LOCK_IMPL_H_
  6. #include <memory>
  7. #include "base/base_export.h"
  8. #include "base/macros.h"
  9. #include "base/synchronization/lock.h"
  10. namespace base {
  11. class ConditionVariable;
  12. namespace internal {
  13. struct UniversalPredecessor {};
  14. struct UniversalSuccessor {};
  15. // A regular lock with simple deadlock correctness checking.
  16. // This lock tracks all of the available locks to make sure that any locks are
  17. // acquired in an expected order.
  18. // See scheduler_lock.h for details.
  19. class BASE_EXPORT CheckedLockImpl {
  20. public:
  21. CheckedLockImpl();
  22. explicit CheckedLockImpl(const CheckedLockImpl* predecessor);
  23. explicit CheckedLockImpl(UniversalPredecessor);
  24. explicit CheckedLockImpl(UniversalSuccessor);
  25. ~CheckedLockImpl();
  26. static void AssertNoLockHeldOnCurrentThread();
  27. void Acquire() EXCLUSIVE_LOCK_FUNCTION(lock_);
  28. void Release() UNLOCK_FUNCTION(lock_);
  29. void AssertAcquired() const;
  30. std::unique_ptr<ConditionVariable> CreateConditionVariable();
  31. bool is_universal_predecessor() const { return is_universal_predecessor_; }
  32. bool is_universal_successor() const { return is_universal_successor_; }
  33. private:
  34. Lock lock_;
  35. const bool is_universal_predecessor_ = false;
  36. const bool is_universal_successor_ = false;
  37. DISALLOW_COPY_AND_ASSIGN(CheckedLockImpl);
  38. };
  39. } // namespace internal
  40. } // namespace base
  41. #endif // BASE_TASK_COMMON_CHECKED_LOCK_IMPL_H_