xref: /aosp_15_r20/external/cronet/base/task/common/checked_lock_impl.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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() EXCLUSIVE_LOCK_FUNCTION(lock_);
41   void Release() UNLOCK_FUNCTION(lock_);
42 
43   void AssertAcquired() const;
44   void AssertNotHeld() const;
45 
46   ConditionVariable CreateConditionVariable();
47   void CreateConditionVariableAndEmplace(std::optional<ConditionVariable>& opt);
48 
is_universal_predecessor()49   bool is_universal_predecessor() const { return is_universal_predecessor_; }
is_universal_successor()50   bool is_universal_successor() const { return is_universal_successor_; }
51 
52  private:
53   Lock lock_;
54   const bool is_universal_predecessor_ = false;
55   const bool is_universal_successor_ = false;
56 };
57 
58 }  // namespace internal
59 }  // namespace base
60 
61 #endif  // BASE_TASK_COMMON_CHECKED_LOCK_IMPL_H_
62