• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_TASK_COMMON_CHECKED_LOCK_IMPL_H_
6 #define BASE_TASK_COMMON_CHECKED_LOCK_IMPL_H_
7 
8 #include <optional>
9 
10 #include "base/base_export.h"
11 #include "base/synchronization/lock.h"
12 
13 namespace base {
14 
15 class ConditionVariable;
16 
17 namespace internal {
18 
19 struct UniversalPredecessor {};
20 struct UniversalSuccessor {};
21 
22 // A regular lock with simple deadlock correctness checking.
23 // This lock tracks all of the available locks to make sure that any locks are
24 // acquired in an expected order.
25 // See scheduler_lock.h for details.
26 class BASE_EXPORT CheckedLockImpl {
27  public:
28   CheckedLockImpl();
29   explicit CheckedLockImpl(const CheckedLockImpl* predecessor);
30   explicit CheckedLockImpl(UniversalPredecessor);
31   explicit CheckedLockImpl(UniversalSuccessor);
32 
33   CheckedLockImpl(const CheckedLockImpl&) = delete;
34   CheckedLockImpl& operator=(const CheckedLockImpl&) = delete;
35 
36   ~CheckedLockImpl();
37 
38   static void AssertNoLockHeldOnCurrentThread();
39 
40   void Acquire(subtle::LockTracking tracking = subtle::LockTracking::kDisabled)
41       EXCLUSIVE_LOCK_FUNCTION(lock_);
42   void Release() UNLOCK_FUNCTION(lock_);
43 
44   void AssertAcquired() const;
45   void AssertNotHeld() const;
46 
47   ConditionVariable CreateConditionVariable();
48   void CreateConditionVariableAndEmplace(std::optional<ConditionVariable>& opt);
49 
is_universal_predecessor()50   bool is_universal_predecessor() const { return is_universal_predecessor_; }
is_universal_successor()51   bool is_universal_successor() const { return is_universal_successor_; }
52 
53  private:
54   Lock lock_;
55   const bool is_universal_predecessor_ = false;
56   const bool is_universal_successor_ = false;
57 };
58 
59 }  // namespace internal
60 }  // namespace base
61 
62 #endif  // BASE_TASK_COMMON_CHECKED_LOCK_IMPL_H_
63