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 lock | |
robliao
2016/08/04 18:15:53
s/registered lock/registered SchedulerLock/
| |
79 // in a potential chain must have a null predecessor and is thus cycle-free. | |
80 // Any subsequent lock with a predecessor must come from the set of registered | |
robliao
2016/08/04 18:15:53
s/subsequent lock/subsequent SchedulerLock/, and s
| |
81 // locks. Since the registered locks only contain cycle-free locks, this | |
82 // subsequent lock is itself cycle-free and may be safely added to the | |
83 // registered lock set. | |
75 void AssertSafePredecessor(const SchedulerLockImpl* lock) const { | 84 void AssertSafePredecessor(const SchedulerLockImpl* lock) const { |
76 allowed_predecessor_map_lock_.AssertAcquired(); | 85 allowed_predecessor_map_lock_.AssertAcquired(); |
77 for (const SchedulerLockImpl* predecessor = | 86 // Using at() is exception-safe here as |lock| was registered already. |
78 allowed_predecessor_map_.at(lock); | 87 const SchedulerLockImpl* predecessor = allowed_predecessor_map_.at(lock); |
79 predecessor != nullptr; | 88 if (predecessor) { |
80 predecessor = allowed_predecessor_map_.at(predecessor)) { | 89 DCHECK(allowed_predecessor_map_.find(predecessor) != |
81 DCHECK_NE(predecessor, lock) << | 90 allowed_predecessor_map_.end()) |
82 "Scheduler lock predecessor cycle detected."; | 91 << "SchedulerLock was registered before its predecessor. " |
92 << "Potential cycle detected"; | |
83 } | 93 } |
84 } | 94 } |
85 | 95 |
86 LockVector* GetAcquiredLocksOnCurrentThread() { | 96 LockVector* GetAcquiredLocksOnCurrentThread() { |
87 if (!tls_acquired_locks_.Get()) | 97 if (!tls_acquired_locks_.Get()) |
88 tls_acquired_locks_.Set(new LockVector); | 98 tls_acquired_locks_.Set(new LockVector); |
89 | 99 |
90 return reinterpret_cast<LockVector*>(tls_acquired_locks_.Get()); | 100 return reinterpret_cast<LockVector*>(tls_acquired_locks_.Get()); |
91 } | 101 } |
92 | 102 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 lock_.AssertAcquired(); | 146 lock_.AssertAcquired(); |
137 } | 147 } |
138 | 148 |
139 std::unique_ptr<ConditionVariable> | 149 std::unique_ptr<ConditionVariable> |
140 SchedulerLockImpl::CreateConditionVariable() { | 150 SchedulerLockImpl::CreateConditionVariable() { |
141 return std::unique_ptr<ConditionVariable>(new ConditionVariable(&lock_)); | 151 return std::unique_ptr<ConditionVariable>(new ConditionVariable(&lock_)); |
142 } | 152 } |
143 | 153 |
144 } // namespace internal | 154 } // namespace internal |
145 } // base | 155 } // base |
OLD | NEW |