OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/task_scheduler/scheduler_lock_impl.h" | 5 #include "base/task_scheduler/scheduler_lock_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <unordered_map> | 8 #include <unordered_map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 void AssertSafeAcquire(const SchedulerLockImpl* const lock) { | 60 void AssertSafeAcquire(const SchedulerLockImpl* const lock) { |
61 const LockVector* acquired_locks = GetAcquiredLocksOnCurrentThread(); | 61 const LockVector* acquired_locks = GetAcquiredLocksOnCurrentThread(); |
62 | 62 |
63 // If the thread currently holds no locks, this is inherently safe. | 63 // If the thread currently holds no locks, this is inherently safe. |
64 if (acquired_locks->empty()) | 64 if (acquired_locks->empty()) |
65 return; | 65 return; |
66 | 66 |
67 // Otherwise, make sure that the previous lock acquired is an allowed | 67 // Otherwise, make sure that the previous lock acquired is an allowed |
68 // predecessor. | 68 // predecessor. |
69 AutoLock auto_lock(allowed_predecessor_map_lock_); | 69 AutoLock auto_lock(allowed_predecessor_map_lock_); |
| 70 // Using at() is exception-safe here as |lock| was registered already. |
70 const SchedulerLockImpl* allowed_predecessor = | 71 const SchedulerLockImpl* allowed_predecessor = |
71 allowed_predecessor_map_.at(lock); | 72 allowed_predecessor_map_.at(lock); |
72 DCHECK_EQ(acquired_locks->back(), allowed_predecessor); | 73 DCHECK_EQ(acquired_locks->back(), allowed_predecessor); |
73 } | 74 } |
74 | 75 |
| 76 // Asserts that |lock|'s registered predecessor is safe. Because |
| 77 // SchedulerLocks are registered at construction time and any predecessor |
| 78 // specified on a SchedulerLock must already exist, the first registered |
| 79 // SchedulerLock in a potential chain must have a null predecessor and is thus |
| 80 // cycle-free. Any subsequent SchedulerLock with a predecessor must come from |
| 81 // the set of registered SchedulerLocks. Since the registered SchedulerLocks |
| 82 // only contain cycle-free SchedulerLocks, this subsequent SchedulerLock is |
| 83 // itself cycle-free and may be safely added to the registered SchedulerLock |
| 84 // set. |
75 void AssertSafePredecessor(const SchedulerLockImpl* lock) const { | 85 void AssertSafePredecessor(const SchedulerLockImpl* lock) const { |
76 allowed_predecessor_map_lock_.AssertAcquired(); | 86 allowed_predecessor_map_lock_.AssertAcquired(); |
77 for (const SchedulerLockImpl* predecessor = | 87 // Using at() is exception-safe here as |lock| was registered already. |
78 allowed_predecessor_map_.at(lock); | 88 const SchedulerLockImpl* predecessor = allowed_predecessor_map_.at(lock); |
79 predecessor != nullptr; | 89 if (predecessor) { |
80 predecessor = allowed_predecessor_map_.at(predecessor)) { | 90 DCHECK(allowed_predecessor_map_.find(predecessor) != |
81 DCHECK_NE(predecessor, lock) << | 91 allowed_predecessor_map_.end()) |
82 "Scheduler lock predecessor cycle detected."; | 92 << "SchedulerLock was registered before its predecessor. " |
| 93 << "Potential cycle detected"; |
83 } | 94 } |
84 } | 95 } |
85 | 96 |
86 LockVector* GetAcquiredLocksOnCurrentThread() { | 97 LockVector* GetAcquiredLocksOnCurrentThread() { |
87 if (!tls_acquired_locks_.Get()) | 98 if (!tls_acquired_locks_.Get()) |
88 tls_acquired_locks_.Set(new LockVector); | 99 tls_acquired_locks_.Set(new LockVector); |
89 | 100 |
90 return reinterpret_cast<LockVector*>(tls_acquired_locks_.Get()); | 101 return reinterpret_cast<LockVector*>(tls_acquired_locks_.Get()); |
91 } | 102 } |
92 | 103 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 lock_.AssertAcquired(); | 147 lock_.AssertAcquired(); |
137 } | 148 } |
138 | 149 |
139 std::unique_ptr<ConditionVariable> | 150 std::unique_ptr<ConditionVariable> |
140 SchedulerLockImpl::CreateConditionVariable() { | 151 SchedulerLockImpl::CreateConditionVariable() { |
141 return std::unique_ptr<ConditionVariable>(new ConditionVariable(&lock_)); | 152 return std::unique_ptr<ConditionVariable>(new ConditionVariable(&lock_)); |
142 } | 153 } |
143 | 154 |
144 } // namespace internal | 155 } // namespace internal |
145 } // base | 156 } // base |
OLD | NEW |