• 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 <memory>
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   std::unique_ptr<ConditionVariable> CreateConditionVariable();
47 
is_universal_predecessor()48   bool is_universal_predecessor() const { return is_universal_predecessor_; }
is_universal_successor()49   bool is_universal_successor() const { return is_universal_successor_; }
50 
51  private:
52   Lock lock_;
53   const bool is_universal_predecessor_ = false;
54   const bool is_universal_successor_ = false;
55 };
56 
57 }  // namespace internal
58 }  // namespace base
59 
60 #endif  // BASE_TASK_COMMON_CHECKED_LOCK_IMPL_H_
61