Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: base/task_scheduler/scheduler_lock_impl.cc

Issue 2218443002: Fix SchedulerLock unittests to actually generate a DCHECK, not an exception (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@b2b0_simplethreadJoinable
Patch Set: tweak comment Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // This asserts that |lock|'s registered predecessor is safe (and by
robliao 2016/08/04 18:13:29 Iterating over chat.
gab 2016/08/04 18:15:06 Done.
77 // indicution that the predecessor chain is safe -- cycle free). Since
78 // registration only happens at construction time, the predecessor is safe as
79 // long as it is either null or already registered.
75 void AssertSafePredecessor(const SchedulerLockImpl* lock) const { 80 void AssertSafePredecessor(const SchedulerLockImpl* lock) const {
76 allowed_predecessor_map_lock_.AssertAcquired(); 81 allowed_predecessor_map_lock_.AssertAcquired();
77 for (const SchedulerLockImpl* predecessor = 82 // Using at() is exception-safe here as |lock| was registered already.
78 allowed_predecessor_map_.at(lock); 83 const SchedulerLockImpl* predecessor = allowed_predecessor_map_.at(lock);
79 predecessor != nullptr; 84 if (predecessor) {
80 predecessor = allowed_predecessor_map_.at(predecessor)) { 85 DCHECK(allowed_predecessor_map_.find(predecessor) !=
81 DCHECK_NE(predecessor, lock) << 86 allowed_predecessor_map_.end())
82 "Scheduler lock predecessor cycle detected."; 87 << "SchedulerLock was registered before its predecessor. "
88 << "Potential cycle detected";
83 } 89 }
84 } 90 }
85 91
86 LockVector* GetAcquiredLocksOnCurrentThread() { 92 LockVector* GetAcquiredLocksOnCurrentThread() {
87 if (!tls_acquired_locks_.Get()) 93 if (!tls_acquired_locks_.Get())
88 tls_acquired_locks_.Set(new LockVector); 94 tls_acquired_locks_.Set(new LockVector);
89 95
90 return reinterpret_cast<LockVector*>(tls_acquired_locks_.Get()); 96 return reinterpret_cast<LockVector*>(tls_acquired_locks_.Get());
91 } 97 }
92 98
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 lock_.AssertAcquired(); 142 lock_.AssertAcquired();
137 } 143 }
138 144
139 std::unique_ptr<ConditionVariable> 145 std::unique_ptr<ConditionVariable>
140 SchedulerLockImpl::CreateConditionVariable() { 146 SchedulerLockImpl::CreateConditionVariable() {
141 return std::unique_ptr<ConditionVariable>(new ConditionVariable(&lock_)); 147 return std::unique_ptr<ConditionVariable>(new ConditionVariable(&lock_));
142 } 148 }
143 149
144 } // namespace internal 150 } // namespace internal
145 } // base 151 } // base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698