OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google, Inc. All Rights Reserved. | 2 * Copyright (C) 2012 Google, Inc. All Rights Reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #ifndef Supplementable_h | 26 #ifndef Supplementable_h |
27 #define Supplementable_h | 27 #define Supplementable_h |
28 | 28 |
29 #include "platform/PlatformExport.h" | |
30 #include "platform/heap/Handle.h" | 29 #include "platform/heap/Handle.h" |
31 #include "wtf/Assertions.h" | 30 #include "wtf/Assertions.h" |
32 #include "wtf/HashMap.h" | 31 #include "wtf/HashMap.h" |
33 #include "wtf/Noncopyable.h" | 32 #include "wtf/Noncopyable.h" |
34 #include "wtf/OwnPtr.h" | |
35 #include "wtf/PassOwnPtr.h" | |
36 | 33 |
37 #if ENABLE(ASSERT) | 34 #if ENABLE(ASSERT) |
38 #include "wtf/Threading.h" | 35 #include "wtf/Threading.h" |
39 #endif | 36 #endif |
40 | 37 |
41 namespace blink { | 38 namespace blink { |
42 | 39 |
43 // What you should know about Supplementable and Supplement | 40 // What you should know about Supplementable and Supplement |
44 // ======================================================== | 41 // ======================================================== |
45 // Supplementable and Supplement instances are meant to be thread local. They | 42 // Supplementable and Supplement instances are meant to be thread local. They |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 // since in most case you'd provide the value while worker preparation is | 80 // since in most case you'd provide the value while worker preparation is |
84 // being done on the main thread, even before the worker thread is started. | 81 // being done on the main thread, even before the worker thread is started. |
85 // If that's the case you can explicitly call reattachThread() when the | 82 // If that's the case you can explicitly call reattachThread() when the |
86 // Supplementable object is passed to the final destination thread (i.e. | 83 // Supplementable object is passed to the final destination thread (i.e. |
87 // worker thread). Please be extremely careful to use the method though, | 84 // worker thread). Please be extremely careful to use the method though, |
88 // as randomly calling the method could easily cause racy condition. | 85 // as randomly calling the method could easily cause racy condition. |
89 // | 86 // |
90 // Note that reattachThread() does nothing if assertion is not enabled. | 87 // Note that reattachThread() does nothing if assertion is not enabled. |
91 // | 88 // |
92 | 89 |
93 template<typename T, bool isGarbageCollected> | 90 template<typename T> |
94 class SupplementBase; | 91 class Supplementable; |
95 | |
96 template<typename T, bool isGarbageCollected> | |
97 class SupplementableBase; | |
98 | |
99 template<typename T, bool isGarbageCollected> | |
100 struct SupplementableTraits; | |
101 | 92 |
102 template<typename T> | 93 template<typename T> |
103 struct SupplementableTraits<T, true> { | 94 class Supplement : public GarbageCollectedMixin { |
104 typedef RawPtr<SupplementBase<T, true>> SupplementArgumentType; | |
105 }; | |
106 | |
107 template<typename T> | |
108 struct SupplementableTraits<T, false> { | |
109 typedef PassOwnPtr<SupplementBase<T, false>> SupplementArgumentType; | |
110 }; | |
111 | |
112 template<bool> | |
113 class SupplementTracing; | |
114 | |
115 template<> | |
116 class PLATFORM_EXPORT SupplementTracing<true> : public GarbageCollectedMixin { }
; | |
117 | |
118 template<> | |
119 class GC_PLUGIN_IGNORE("crbug.com/476419") PLATFORM_EXPORT SupplementTracing<fal
se> { | |
120 public: | 95 public: |
121 virtual ~SupplementTracing() { } | 96 static void provideTo(Supplementable<T>& host, const char* key, Supplement<T
>* supplement) |
122 // FIXME: Oilpan: this trace() method is only provided to minimize | |
123 // the use of ENABLE(OILPAN) for Supplements deriving from the | |
124 // transition type HeapSupplement<>. | |
125 // | |
126 // When that transition type is removed (or its use is substantially | |
127 // reduced), remove this dummy trace method also. | |
128 DEFINE_INLINE_VIRTUAL_TRACE() { } | |
129 }; | |
130 | |
131 template<typename T, bool isGarbageCollected = false> | |
132 class SupplementBase : public SupplementTracing<isGarbageCollected> { | |
133 public: | |
134 #if ENABLE(SECURITY_ASSERT) | |
135 virtual bool isRefCountedWrapper() const { return false; } | |
136 #endif | |
137 | |
138 static void provideTo(SupplementableBase<T, isGarbageCollected>& host, const
char* key, typename SupplementableTraits<T, isGarbageCollected>::SupplementArgu
mentType supplement) | |
139 { | 97 { |
140 host.provideSupplement(key, supplement); | 98 host.provideSupplement(key, supplement); |
141 } | 99 } |
142 | 100 |
143 static SupplementBase<T, isGarbageCollected>* from(SupplementableBase<T, isG
arbageCollected>& host, const char* key) | 101 static Supplement<T>* from(Supplementable<T>& host, const char* key) |
144 { | 102 { |
145 return host.requireSupplement(key); | 103 return host.requireSupplement(key); |
146 } | 104 } |
147 | 105 |
148 static SupplementBase<T, isGarbageCollected>* from(SupplementableBase<T, isG
arbageCollected>* host, const char* key) | 106 static Supplement<T>* from(Supplementable<T>* host, const char* key) |
149 { | 107 { |
150 return host ? host->requireSupplement(key) : 0; | 108 return host ? host->requireSupplement(key) : 0; |
151 } | 109 } |
152 }; | 110 }; |
153 | 111 |
154 template<typename T, bool isGarbageCollected> | 112 // Supplementable<T> inherits from GarbageCollectedMixin virtually |
155 class SupplementableTracing; | |
156 | |
157 // SupplementableTracing<T,true> inherits GarbageCollectedMixin virtually | |
158 // to allow ExecutionContext to derive from two GC mixin classes. | 113 // to allow ExecutionContext to derive from two GC mixin classes. |
159 template<typename T> | 114 template<typename T> |
160 class SupplementableTracing<T, true> : public virtual GarbageCollectedMixin { | 115 class Supplementable : public virtual GarbageCollectedMixin { |
161 WTF_MAKE_NONCOPYABLE(SupplementableTracing); | 116 WTF_MAKE_NONCOPYABLE(Supplementable); |
162 public: | 117 public: |
163 DEFINE_INLINE_VIRTUAL_TRACE() | 118 void provideSupplement(const char* key, Supplement<T>* supplement) |
164 { | |
165 visitor->trace(m_supplements); | |
166 } | |
167 | |
168 protected: | |
169 SupplementableTracing() { } | |
170 typedef HeapHashMap<const char*, Member<SupplementBase<T, true>>, PtrHash<co
nst char>> SupplementMap; | |
171 SupplementMap m_supplements; | |
172 }; | |
173 | |
174 template<typename T> | |
175 class SupplementableTracing<T, false> { | |
176 WTF_MAKE_NONCOPYABLE(SupplementableTracing); | |
177 protected: | |
178 SupplementableTracing() { } | |
179 typedef HashMap<const char*, OwnPtr<SupplementBase<T, false>>, PtrHash<const
char>> SupplementMap; | |
180 SupplementMap m_supplements; | |
181 }; | |
182 | |
183 // Helper class for implementing Supplementable and HeapSupplementable. | |
184 template<typename T, bool isGarbageCollected = false> | |
185 class SupplementableBase : public SupplementableTracing<T, isGarbageCollected> { | |
186 public: | |
187 void provideSupplement(const char* key, typename SupplementableTraits<T, isG
arbageCollected>::SupplementArgumentType supplement) | |
188 { | 119 { |
189 ASSERT(m_threadId == currentThread()); | 120 ASSERT(m_threadId == currentThread()); |
190 ASSERT(!this->m_supplements.get(key)); | 121 ASSERT(!this->m_supplements.get(key)); |
191 this->m_supplements.set(key, supplement); | 122 this->m_supplements.set(key, supplement); |
192 } | 123 } |
193 | 124 |
194 void removeSupplement(const char* key) | 125 void removeSupplement(const char* key) |
195 { | 126 { |
196 ASSERT(m_threadId == currentThread()); | 127 ASSERT(m_threadId == currentThread()); |
197 this->m_supplements.remove(key); | 128 this->m_supplements.remove(key); |
198 } | 129 } |
199 | 130 |
200 SupplementBase<T, isGarbageCollected>* requireSupplement(const char* key) | 131 Supplement<T>* requireSupplement(const char* key) |
201 { | 132 { |
202 ASSERT(m_threadId == currentThread()); | 133 ASSERT(m_threadId == currentThread()); |
203 return this->m_supplements.get(key); | 134 return this->m_supplements.get(key); |
204 } | 135 } |
205 | 136 |
206 void reattachThread() | 137 void reattachThread() |
207 { | 138 { |
208 #if ENABLE(ASSERT) | 139 #if ENABLE(ASSERT) |
209 m_threadId = currentThread(); | 140 m_threadId = currentThread(); |
210 #endif | 141 #endif |
211 } | 142 } |
212 | 143 |
| 144 DEFINE_INLINE_VIRTUAL_TRACE() |
| 145 { |
| 146 visitor->trace(m_supplements); |
| 147 } |
| 148 |
| 149 protected: |
| 150 using SupplementMap = HeapHashMap<const char*, Member<Supplement<T>>, PtrHas
h<const char>>; |
| 151 SupplementMap m_supplements; |
| 152 |
| 153 Supplementable() |
213 #if ENABLE(ASSERT) | 154 #if ENABLE(ASSERT) |
214 protected: | 155 : m_threadId(currentThread()) |
215 SupplementableBase() : m_threadId(currentThread()) { } | 156 #endif |
| 157 { |
| 158 } |
216 | 159 |
| 160 #if ENABLE(ASSERT) |
217 private: | 161 private: |
218 ThreadIdentifier m_threadId; | 162 ThreadIdentifier m_threadId; |
219 #endif | 163 #endif |
220 }; | 164 }; |
221 | 165 |
222 template<typename T> | 166 // TODO(sof): replace all HeapSupplement<T> uses with Supplement<T>. |
223 class HeapSupplement : public SupplementBase<T, true> { }; | 167 #define HeapSupplement Supplement |
224 | 168 |
225 template<typename T> | 169 template<typename T> |
226 class HeapSupplementable : public SupplementableBase<T, true> { }; | 170 struct ThreadingTrait<Supplement<T>> { |
227 | |
228 template<typename T> | |
229 class Supplement : public SupplementBase<T, false> { }; | |
230 | |
231 template<typename T> | |
232 class Supplementable : public SupplementableBase<T, false> { }; | |
233 | |
234 template<typename T> | |
235 struct ThreadingTrait<SupplementBase<T, true>> { | |
236 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity; | 171 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity; |
237 }; | 172 }; |
238 | 173 |
239 template<typename T> | 174 template<typename T> |
240 struct ThreadingTrait<SupplementableBase<T, true>> { | 175 struct ThreadingTrait<Supplementable<T>> { |
241 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity; | 176 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity; |
242 }; | 177 }; |
243 | 178 |
244 } // namespace blink | 179 } // namespace blink |
245 | 180 |
246 #endif // Supplementable_h | 181 #endif // Supplementable_h |
OLD | NEW |