OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/task_scheduler/scheduler_lock.h" | |
6 | |
7 #include <algorithm> | |
8 #include <unordered_map> | |
9 #include <vector> | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/memory/singleton.h" | |
13 #include "base/threading/platform_thread.h" | |
14 | |
15 #if DCHECK_IS_ON() | |
16 #define DCHECK_ON_ONLY(statement) statement | |
17 #else | |
18 #define DCHECK_ON_ONLY(statement) | |
19 #endif | |
20 | |
21 namespace base { | |
22 namespace task_scheduler { | |
23 | |
24 namespace { | |
25 | |
26 class SafeAcquisitionTracker { | |
27 public: | |
28 static SafeAcquisitionTracker* GetInstance() { | |
29 return base::Singleton<SafeAcquisitionTracker>::get(); | |
30 } | |
31 | |
32 void RegisterLock( | |
33 const SchedulerLock* const lock, | |
34 const SchedulerLock* const predecessor) { | |
35 AutoLock auto_lock(metadata_lock_); | |
36 allowed_predecessors_[lock] = predecessor; | |
37 } | |
38 | |
39 void UnregisterLock(const SchedulerLock* const lock) { | |
40 AutoLock auto_lock(metadata_lock_); | |
41 allowed_predecessors_.erase(lock); | |
42 } | |
43 | |
44 void RecordAcquisition(const SchedulerLock* const lock) { | |
45 AutoLock auto_lock(metadata_lock_); | |
46 const PlatformThreadId id = PlatformThread::CurrentId(); | |
47 AssertSafeAcquire(id, lock); | |
48 LockVector& thread_locks = acquired_locks_[id]; | |
49 const auto& iter = | |
fdoray
2016/02/11 17:30:33
should it be const auto (no &)?
robliao
2016/02/11 22:59:04
Looks like it. Done.
| |
50 std::find(thread_locks.begin(), thread_locks.end(), lock); | |
51 // We don't allow for reentrant locks. | |
52 DCHECK(iter == thread_locks.end()); | |
fdoray
2016/02/11 17:30:33
Since predecessors are specified when locks are co
robliao
2016/02/11 22:59:04
That's a good point. When ahead and added a hard C
| |
53 thread_locks.push_back(lock); | |
54 } | |
55 | |
56 void RecordRelease(const SchedulerLock* const lock) { | |
57 AutoLock auto_lock(metadata_lock_); | |
58 const PlatformThreadId id = PlatformThread::CurrentId(); | |
59 LockVector& thread_locks = acquired_locks_[id]; | |
60 const auto& iter = | |
fdoray
2016/02/11 17:30:33
should it be const auto (no &)?
robliao
2016/02/11 22:59:04
Done.
| |
61 std::find(thread_locks.begin(), thread_locks.end(), lock); | |
62 DCHECK(iter != thread_locks.end()); | |
63 thread_locks.erase(iter); | |
64 } | |
65 | |
66 private: | |
67 friend struct base::DefaultSingletonTraits<SafeAcquisitionTracker>; | |
68 using LockVector = std::vector<const SchedulerLock*>; | |
69 using PredecessorMap = std::unordered_map< | |
70 const SchedulerLock*, const SchedulerLock*>; | |
71 using AcquisitionMap = | |
72 std::unordered_map<base::PlatformThreadId, LockVector>; | |
73 | |
74 SafeAcquisitionTracker() = default; | |
75 | |
76 void AssertSafeAcquire(PlatformThreadId id, const SchedulerLock* const lock) { | |
77 metadata_lock_.AssertAcquired(); | |
78 LockVector& thread_locks = acquired_locks_[id]; | |
fdoray
2016/02/11 17:30:33
const LockVector&
robliao
2016/02/11 22:59:04
Done.
| |
79 | |
80 // If the thread hasn't ever acquired a lock, this is inherently safe. | |
81 if (thread_locks.empty()) | |
82 return; | |
83 | |
84 // Otherwise, make sure that the previous lock acquired is an allowed | |
85 // predecessor. | |
86 const SchedulerLock* allowed_predecessor = allowed_predecessors_[lock]; | |
87 DCHECK(thread_locks.back() == allowed_predecessor); | |
88 } | |
89 | |
90 // Synchronizes everything in SafeAcquisitionTracker. | |
91 base::Lock metadata_lock_; | |
92 | |
93 PredecessorMap allowed_predecessors_; | |
94 AcquisitionMap acquired_locks_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(SafeAcquisitionTracker); | |
97 }; | |
98 | |
99 } // namespace | |
100 | |
101 SchedulerLock::SchedulerLock() : SchedulerLock(nullptr) {} | |
102 | |
103 SchedulerLock::SchedulerLock(const SchedulerLock* predecessor) { | |
104 DCHECK_ON_ONLY( | |
105 SafeAcquisitionTracker::GetInstance()->RegisterLock(this, predecessor)); | |
106 } | |
107 | |
108 SchedulerLock::~SchedulerLock() { | |
109 DCHECK_ON_ONLY(SafeAcquisitionTracker::GetInstance()->UnregisterLock(this)); | |
110 } | |
111 | |
112 void SchedulerLock::Acquire() { | |
113 lock.Acquire(); | |
114 DCHECK_ON_ONLY( | |
115 SafeAcquisitionTracker::GetInstance()->RecordAcquisition(this)); | |
116 } | |
117 | |
118 void SchedulerLock::Release() { | |
119 lock.Release(); | |
120 DCHECK_ON_ONLY(SafeAcquisitionTracker::GetInstance()->RecordRelease(this)); | |
121 } | |
122 | |
123 Lock* SchedulerLock::RawLockForConditionVariable() { | |
124 return &lock; | |
125 } | |
126 | |
127 } // namespace task_scheduler | |
128 } // base | |
OLD | NEW |