OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef PersistentNode_h | 5 #ifndef PersistentNode_h |
6 #define PersistentNode_h | 6 #define PersistentNode_h |
7 | 7 |
8 #include "platform/PlatformExport.h" | 8 #include "platform/PlatformExport.h" |
9 #include "platform/heap/ThreadState.h" | 9 #include "platform/heap/ThreadState.h" |
10 #include "wtf/Allocator.h" | 10 #include "wtf/Allocator.h" |
11 #include "wtf/Assertions.h" | 11 #include "wtf/Assertions.h" |
12 #include "wtf/PtrUtil.h" | 12 #include "wtf/PtrUtil.h" |
13 #include "wtf/ThreadingPrimitives.h" | 13 #include "wtf/ThreadingPrimitives.h" |
14 #include <memory> | 14 #include <memory> |
15 | 15 |
16 namespace blink { | 16 namespace blink { |
17 | 17 |
18 class CrossThreadPersistentRegion; | 18 class CrossThreadPersistentRegion; |
19 | 19 |
20 class PersistentNode final { | 20 class PersistentNode final { |
21 DISALLOW_NEW(); | 21 DISALLOW_NEW(); |
22 | 22 |
23 public: | 23 public: |
24 PersistentNode() : m_self(nullptr), m_trace(nullptr) { ASSERT(isUnused()); } | 24 PersistentNode() : m_self(nullptr), m_trace(nullptr) { DCHECK(isUnused()); } |
25 | 25 |
26 #if ENABLE(ASSERT) | 26 #if DCHECK_IS_ON() |
27 ~PersistentNode() { | 27 ~PersistentNode() { |
28 // If you hit this assert, it means that the thread finished | 28 // If you hit this assert, it means that the thread finished |
29 // without clearing persistent handles that the thread created. | 29 // without clearing persistent handles that the thread created. |
30 // We don't enable the assert for the main thread because the | 30 // We don't enable the assert for the main thread because the |
31 // main thread finishes without clearing all persistent handles. | 31 // main thread finishes without clearing all persistent handles. |
32 ASSERT(isMainThread() || isUnused()); | 32 DCHECK(isMainThread() || isUnused()); |
33 } | 33 } |
34 #endif | 34 #endif |
35 | 35 |
36 // It is dangerous to copy the PersistentNode because it breaks the | 36 // It is dangerous to copy the PersistentNode because it breaks the |
37 // free list. | 37 // free list. |
38 PersistentNode& operator=(const PersistentNode& otherref) = delete; | 38 PersistentNode& operator=(const PersistentNode& otherref) = delete; |
39 | 39 |
40 // Ideally the trace method should be virtual and automatically dispatch | 40 // Ideally the trace method should be virtual and automatically dispatch |
41 // to the most specific implementation. However having a virtual method | 41 // to the most specific implementation. However having a virtual method |
42 // on PersistentNode leads to too eager template instantiation with MSVC | 42 // on PersistentNode leads to too eager template instantiation with MSVC |
43 // which leads to include cycles. | 43 // which leads to include cycles. |
44 // Instead we call the constructor with a TraceCallback which knows the | 44 // Instead we call the constructor with a TraceCallback which knows the |
45 // type of the most specific child and calls trace directly. See | 45 // type of the most specific child and calls trace directly. See |
46 // TraceMethodDelegate in Visitor.h for how this is done. | 46 // TraceMethodDelegate in Visitor.h for how this is done. |
47 void tracePersistentNode(Visitor* visitor) { | 47 void tracePersistentNode(Visitor* visitor) { |
48 ASSERT(!isUnused()); | 48 DCHECK(!isUnused()); |
49 ASSERT(m_trace); | 49 DCHECK(m_trace); |
50 m_trace(visitor, m_self); | 50 m_trace(visitor, m_self); |
51 } | 51 } |
52 | 52 |
53 void initialize(void* self, TraceCallback trace) { | 53 void initialize(void* self, TraceCallback trace) { |
54 ASSERT(isUnused()); | 54 DCHECK(isUnused()); |
55 m_self = self; | 55 m_self = self; |
56 m_trace = trace; | 56 m_trace = trace; |
57 } | 57 } |
58 | 58 |
59 void setFreeListNext(PersistentNode* node) { | 59 void setFreeListNext(PersistentNode* node) { |
60 ASSERT(!node || node->isUnused()); | 60 DCHECK(!node || node->isUnused()); |
61 m_self = node; | 61 m_self = node; |
62 m_trace = nullptr; | 62 m_trace = nullptr; |
63 ASSERT(isUnused()); | 63 DCHECK(isUnused()); |
64 } | 64 } |
65 | 65 |
66 PersistentNode* freeListNext() { | 66 PersistentNode* freeListNext() { |
67 ASSERT(isUnused()); | 67 DCHECK(isUnused()); |
68 PersistentNode* node = reinterpret_cast<PersistentNode*>(m_self); | 68 PersistentNode* node = reinterpret_cast<PersistentNode*>(m_self); |
69 ASSERT(!node || node->isUnused()); | 69 DCHECK(!node || node->isUnused()); |
70 return node; | 70 return node; |
71 } | 71 } |
72 | 72 |
73 bool isUnused() const { return !m_trace; } | 73 bool isUnused() const { return !m_trace; } |
74 | 74 |
75 void* self() const { return m_self; } | 75 void* self() const { return m_self; } |
76 | 76 |
77 private: | 77 private: |
78 // If this PersistentNode is in use: | 78 // If this PersistentNode is in use: |
79 // - m_self points to the corresponding Persistent handle. | 79 // - m_self points to the corresponding Persistent handle. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 { | 114 { |
115 } | 115 } |
116 ~PersistentRegion(); | 116 ~PersistentRegion(); |
117 | 117 |
118 PersistentNode* allocatePersistentNode(void* self, TraceCallback trace) { | 118 PersistentNode* allocatePersistentNode(void* self, TraceCallback trace) { |
119 #if DCHECK_IS_ON() | 119 #if DCHECK_IS_ON() |
120 ++m_persistentCount; | 120 ++m_persistentCount; |
121 #endif | 121 #endif |
122 if (UNLIKELY(!m_freeListHead)) | 122 if (UNLIKELY(!m_freeListHead)) |
123 ensurePersistentNodeSlots(self, trace); | 123 ensurePersistentNodeSlots(self, trace); |
124 ASSERT(m_freeListHead); | 124 DCHECK(m_freeListHead); |
125 PersistentNode* node = m_freeListHead; | 125 PersistentNode* node = m_freeListHead; |
126 m_freeListHead = m_freeListHead->freeListNext(); | 126 m_freeListHead = m_freeListHead->freeListNext(); |
127 node->initialize(self, trace); | 127 node->initialize(self, trace); |
128 ASSERT(!node->isUnused()); | 128 DCHECK(!node->isUnused()); |
129 return node; | 129 return node; |
130 } | 130 } |
131 | 131 |
132 void freePersistentNode(PersistentNode* persistentNode) { | 132 void freePersistentNode(PersistentNode* persistentNode) { |
133 #if DCHECK_IS_ON() | 133 #if DCHECK_IS_ON() |
134 DCHECK_GT(m_persistentCount, 0); | 134 DCHECK_GT(m_persistentCount, 0); |
135 #endif | 135 #endif |
136 persistentNode->setFreeListNext(m_freeListHead); | 136 persistentNode->setFreeListNext(m_freeListHead); |
137 m_freeListHead = persistentNode; | 137 m_freeListHead = persistentNode; |
138 #if DCHECK_IS_ON() | 138 #if DCHECK_IS_ON() |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 m_persistentRegion.lock(); | 207 m_persistentRegion.lock(); |
208 } | 208 } |
209 ~LockScope() { m_persistentRegion.unlock(); } | 209 ~LockScope() { m_persistentRegion.unlock(); } |
210 | 210 |
211 private: | 211 private: |
212 CrossThreadPersistentRegion& m_persistentRegion; | 212 CrossThreadPersistentRegion& m_persistentRegion; |
213 }; | 213 }; |
214 | 214 |
215 void tracePersistentNodes(Visitor* visitor) { | 215 void tracePersistentNodes(Visitor* visitor) { |
216 // If this assert triggers, you're tracing without being in a LockScope. | 216 // If this assert triggers, you're tracing without being in a LockScope. |
217 #if ENABLE(ASSERT) | 217 #if DCHECK_IS_ON() |
218 DCHECK(m_mutex.locked()); | 218 DCHECK(m_mutex.locked()); |
219 #endif | 219 #endif |
220 m_persistentRegion->tracePersistentNodes( | 220 m_persistentRegion->tracePersistentNodes( |
221 visitor, CrossThreadPersistentRegion::shouldTracePersistentNode); | 221 visitor, CrossThreadPersistentRegion::shouldTracePersistentNode); |
222 } | 222 } |
223 | 223 |
224 void prepareForThreadStateTermination(ThreadState*); | 224 void prepareForThreadStateTermination(ThreadState*); |
225 | 225 |
226 NO_SANITIZE_ADDRESS | 226 NO_SANITIZE_ADDRESS |
227 static bool shouldTracePersistentNode(Visitor*, PersistentNode*); | 227 static bool shouldTracePersistentNode(Visitor*, PersistentNode*); |
(...skipping 17 matching lines...) Expand all Loading... |
245 // Recursive as prepareForThreadStateTermination() clears a PersistentNode's | 245 // Recursive as prepareForThreadStateTermination() clears a PersistentNode's |
246 // associated Persistent<> -- it in turn freeing the PersistentNode. And both | 246 // associated Persistent<> -- it in turn freeing the PersistentNode. And both |
247 // CrossThreadPersistentRegion operations need a lock on the region before | 247 // CrossThreadPersistentRegion operations need a lock on the region before |
248 // mutating. | 248 // mutating. |
249 RecursiveMutex m_mutex; | 249 RecursiveMutex m_mutex; |
250 }; | 250 }; |
251 | 251 |
252 } // namespace blink | 252 } // namespace blink |
253 | 253 |
254 #endif | 254 #endif |
OLD | NEW |