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 <memory> | 8 #include <memory> |
9 #include "platform/PlatformExport.h" | 9 #include "platform/PlatformExport.h" |
10 #include "platform/heap/ThreadState.h" | 10 #include "platform/heap/ThreadState.h" |
11 #include "platform/wtf/Allocator.h" | 11 #include "platform/wtf/Allocator.h" |
12 #include "platform/wtf/Assertions.h" | 12 #include "platform/wtf/Assertions.h" |
13 #include "platform/wtf/PtrUtil.h" | 13 #include "platform/wtf/PtrUtil.h" |
14 #include "platform/wtf/ThreadingPrimitives.h" | 14 #include "platform/wtf/ThreadingPrimitives.h" |
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() : self_(nullptr), trace_(nullptr) { ASSERT(IsUnused()); } | 24 PersistentNode() : self_(nullptr), trace_(nullptr) { DCHECK(IsUnused()); } |
25 | 25 |
26 #if DCHECK_IS_ON() | 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(trace_); | 49 DCHECK(trace_); |
50 trace_(visitor, self_); | 50 trace_(visitor, 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 self_ = self; | 55 self_ = self; |
56 trace_ = trace; | 56 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 self_ = node; | 61 self_ = node; |
62 trace_ = nullptr; | 62 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*>(self_); | 68 PersistentNode* node = reinterpret_cast<PersistentNode*>(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 !trace_; } | 73 bool IsUnused() const { return !trace_; } |
74 | 74 |
75 void* Self() const { return self_; } | 75 void* Self() const { return 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 ++persistent_count_; | 120 ++persistent_count_; |
121 #endif | 121 #endif |
122 if (UNLIKELY(!free_list_head_)) | 122 if (UNLIKELY(!free_list_head_)) |
123 EnsurePersistentNodeSlots(self, trace); | 123 EnsurePersistentNodeSlots(self, trace); |
124 ASSERT(free_list_head_); | 124 DCHECK(free_list_head_); |
125 PersistentNode* node = free_list_head_; | 125 PersistentNode* node = free_list_head_; |
126 free_list_head_ = free_list_head_->FreeListNext(); | 126 free_list_head_ = free_list_head_->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* persistent_node) { | 132 void FreePersistentNode(PersistentNode* persistent_node) { |
133 #if DCHECK_IS_ON() | 133 #if DCHECK_IS_ON() |
134 DCHECK_GT(persistent_count_, 0); | 134 DCHECK_GT(persistent_count_, 0); |
135 #endif | 135 #endif |
136 persistent_node->SetFreeListNext(free_list_head_); | 136 persistent_node->SetFreeListNext(free_list_head_); |
137 free_list_head_ = persistent_node; | 137 free_list_head_ = persistent_node; |
138 #if DCHECK_IS_ON() | 138 #if DCHECK_IS_ON() |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 mutex_; | 249 RecursiveMutex mutex_; |
250 }; | 250 }; |
251 | 251 |
252 } // namespace blink | 252 } // namespace blink |
253 | 253 |
254 #endif | 254 #endif |
OLD | NEW |