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 3 // found in the LICENSE file. 4 5 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_IMPL_THREADING_TRAITS_H_ 6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_IMPL_THREADING_TRAITS_H_ 7 8 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h" 9 #include "third_party/blink/renderer/platform/wtf/deque.h" 10 #include "third_party/blink/renderer/platform/wtf/hash_counted_set.h" 11 #include "third_party/blink/renderer/platform/wtf/hash_map.h" 12 #include "third_party/blink/renderer/platform/wtf/hash_set.h" 13 #include "third_party/blink/renderer/platform/wtf/hash_table.h" 14 #include "third_party/blink/renderer/platform/wtf/type_traits.h" 15 #include "third_party/blink/renderer/platform/wtf/vector.h" 16 17 namespace blink { 18 19 // ThreadAffinity indicates which threads objects can be used on. We 20 // distinguish between objects that can be used on the main thread 21 // only and objects that can be used on any thread. 22 // 23 // For objects that can only be used on the main thread, we avoid going 24 // through thread-local storage to get to the thread state. This is 25 // important for performance. 26 enum ThreadAffinity { 27 kAnyThread, 28 kMainThreadOnly, 29 }; 30 31 // TODO(haraken): These forward declarations violate dependency rules. 32 // Remove them. 33 class Node; 34 class NodeList; 35 class NodeRareData; 36 37 template < 38 typename T, 39 bool mainThreadOnly = 40 WTF::IsSubclass<typename std::remove_const<T>::type, Node>::value || 41 WTF::IsSubclass<typename std::remove_const<T>::type, NodeList>::value || 42 WTF::IsSubclass<typename std::remove_const<T>::type, 43 NodeRareData>::value> 44 struct DefaultThreadingTrait; 45 46 template <typename T> 47 struct DefaultThreadingTrait<T, false> { 48 STATIC_ONLY(DefaultThreadingTrait); 49 static const ThreadAffinity kAffinity = kAnyThread; 50 }; 51 52 template <typename T> 53 struct DefaultThreadingTrait<T, true> { 54 STATIC_ONLY(DefaultThreadingTrait); 55 static const ThreadAffinity kAffinity = kMainThreadOnly; 56 }; 57 58 class HeapAllocator; 59 template <typename T> 60 class Member; 61 template <typename T> 62 class WeakMember; 63 64 template <typename T> 65 struct ThreadingTrait { 66 STATIC_ONLY(ThreadingTrait); 67 static const ThreadAffinity kAffinity = DefaultThreadingTrait<T>::kAffinity; 68 }; 69 70 template <typename U> 71 class ThreadingTrait<const U> : public ThreadingTrait<U> {}; 72 73 template <typename T> 74 struct ThreadingTrait<Member<T>> { 75 STATIC_ONLY(ThreadingTrait); 76 static const ThreadAffinity kAffinity = ThreadingTrait<T>::kAffinity; 77 }; 78 79 template <typename T> 80 struct ThreadingTrait<WeakMember<T>> { 81 STATIC_ONLY(ThreadingTrait); 82 static const ThreadAffinity kAffinity = ThreadingTrait<T>::kAffinity; 83 }; 84 85 template <typename Key, typename Value, typename T, typename U, typename V> 86 struct ThreadingTrait<HashMap<Key, Value, T, U, V, HeapAllocator>> { 87 STATIC_ONLY(ThreadingTrait); 88 static const ThreadAffinity kAffinity = 89 (ThreadingTrait<Key>::kAffinity == kMainThreadOnly) && 90 (ThreadingTrait<Value>::kAffinity == kMainThreadOnly) 91 ? kMainThreadOnly 92 : kAnyThread; 93 }; 94 95 template <typename First, typename Second> 96 struct ThreadingTrait<WTF::KeyValuePair<First, Second>> { 97 STATIC_ONLY(ThreadingTrait); 98 static const ThreadAffinity kAffinity = 99 (ThreadingTrait<First>::kAffinity == kMainThreadOnly) && 100 (ThreadingTrait<Second>::kAffinity == kMainThreadOnly) 101 ? kMainThreadOnly 102 : kAnyThread; 103 }; 104 105 template <typename T, typename U, typename V> 106 struct ThreadingTrait<HashSet<T, U, V, HeapAllocator>> { 107 STATIC_ONLY(ThreadingTrait); 108 static const ThreadAffinity kAffinity = ThreadingTrait<T>::kAffinity; 109 }; 110 111 template <typename T, size_t inlineCapacity> 112 struct ThreadingTrait<Vector<T, inlineCapacity, HeapAllocator>> { 113 STATIC_ONLY(ThreadingTrait); 114 static const ThreadAffinity kAffinity = ThreadingTrait<T>::kAffinity; 115 }; 116 117 template <typename T, size_t inlineCapacity> 118 struct ThreadingTrait<Deque<T, inlineCapacity, HeapAllocator>> { 119 STATIC_ONLY(ThreadingTrait); 120 static const ThreadAffinity kAffinity = ThreadingTrait<T>::kAffinity; 121 }; 122 123 template <typename T, typename U, typename V> 124 struct ThreadingTrait<HashCountedSet<T, U, V, HeapAllocator>> { 125 STATIC_ONLY(ThreadingTrait); 126 static const ThreadAffinity kAffinity = ThreadingTrait<T>::kAffinity; 127 }; 128 129 template <typename T, typename U, typename V, typename W, typename X> 130 class HeapHashMap; 131 template <typename T, typename U, typename V> 132 class HeapHashSet; 133 template <typename T, wtf_size_t inlineCapacity> 134 class HeapVector; 135 template <typename T> 136 class HeapDeque; 137 template <typename T, typename U, typename V> 138 class HeapHashCountedSet; 139 140 template <typename T, typename U, typename V, typename W, typename X> 141 struct ThreadingTrait<HeapHashMap<T, U, V, W, X>> 142 : public ThreadingTrait<HashMap<T, U, V, W, X, HeapAllocator>> { 143 STATIC_ONLY(ThreadingTrait); 144 }; 145 template <typename T, typename U, typename V> 146 struct ThreadingTrait<HeapHashSet<T, U, V>> 147 : public ThreadingTrait<HashSet<T, U, V, HeapAllocator>> { 148 STATIC_ONLY(ThreadingTrait); 149 }; 150 template <typename T, size_t inlineCapacity> 151 struct ThreadingTrait<HeapVector<T, inlineCapacity>> 152 : public ThreadingTrait<Vector<T, inlineCapacity, HeapAllocator>> { 153 STATIC_ONLY(ThreadingTrait); 154 }; 155 template <typename T> 156 struct ThreadingTrait<HeapDeque<T>> 157 : public ThreadingTrait<Deque<T, 0, HeapAllocator>> { 158 STATIC_ONLY(ThreadingTrait); 159 }; 160 template <typename T, typename U, typename V> 161 struct ThreadingTrait<HeapHashCountedSet<T, U, V>> 162 : public ThreadingTrait<HashCountedSet<T, U, V, HeapAllocator>> { 163 STATIC_ONLY(ThreadingTrait); 164 }; 165 166 } // namespace blink 167 168 #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_IMPL_THREADING_TRAITS_H_ 169