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 "heap/Handle.h" |
29 #include "wtf/Assertions.h" | 30 #include "wtf/Assertions.h" |
30 #include "wtf/HashMap.h" | 31 #include "wtf/HashMap.h" |
31 #include "wtf/OwnPtr.h" | 32 #include "wtf/OwnPtr.h" |
32 #include "wtf/PassOwnPtr.h" | 33 #include "wtf/PassOwnPtr.h" |
33 | 34 |
34 #if !ASSERT_DISABLED | 35 #if !ASSERT_DISABLED |
35 #include "wtf/Threading.h" | 36 #include "wtf/Threading.h" |
36 #endif | 37 #endif |
37 | 38 |
38 namespace WebCore { | 39 namespace WebCore { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 // since in most case you'd provide the value while worker preparation is | 81 // since in most case you'd provide the value while worker preparation is |
81 // being done on the main thread, even before the worker thread is started. | 82 // being done on the main thread, even before the worker thread is started. |
82 // If that's the case you can explicitly call reattachThread() when the | 83 // If that's the case you can explicitly call reattachThread() when the |
83 // Supplementable object is passed to the final destination thread (i.e. | 84 // Supplementable object is passed to the final destination thread (i.e. |
84 // worker thread). Please be extremely careful to use the method though, | 85 // worker thread). Please be extremely careful to use the method though, |
85 // as randomly calling the method could easily cause racy condition. | 86 // as randomly calling the method could easily cause racy condition. |
86 // | 87 // |
87 // Note that reattachThread() does nothing if assertion is not enabled. | 88 // Note that reattachThread() does nothing if assertion is not enabled. |
88 // | 89 // |
89 | 90 |
90 template<typename T> | 91 template<typename T, bool isGarbageCollected> |
91 class Supplementable; | 92 class SupplementBase; |
| 93 |
| 94 template<typename T, bool isGarbageCollected> |
| 95 class SupplementableBase; |
| 96 |
| 97 template<typename T, bool isGarbageCollected> |
| 98 struct SupplementableTraits; |
92 | 99 |
93 template<typename T> | 100 template<typename T> |
94 class Supplement { | 101 struct SupplementableTraits<T, true> { |
| 102 typedef PassOwnPtrWillBeRawPtr<SupplementBase<T, true> > SupplementArgumentT
ype; |
| 103 typedef WillBeHeapHashMap<const char*, OwnPtrWillBeMember<SupplementBase<T,
true> >, PtrHash<const char*> > SupplementMap; |
| 104 }; |
| 105 |
| 106 template<typename T> |
| 107 struct SupplementableTraits<T, false> { |
| 108 typedef PassOwnPtr<SupplementBase<T, false> > SupplementArgumentType; |
| 109 typedef HashMap<const char*, OwnPtr<SupplementBase<T, false> >, PtrHash<cons
t char*> > SupplementMap; |
| 110 }; |
| 111 |
| 112 template<bool> |
| 113 class SupplementTracing; |
| 114 |
| 115 template<> |
| 116 class SupplementTracing<true> : public GarbageCollectedMixin { |
95 public: | 117 public: |
96 virtual ~Supplement() { } | 118 virtual void trace(Visitor*) = 0; |
| 119 }; |
| 120 |
| 121 template<> |
| 122 class SupplementTracing<false> { |
| 123 public: |
| 124 virtual void trace(Visitor*) { } |
| 125 }; |
| 126 |
| 127 template<typename T, bool isGarbageCollected = false> |
| 128 class SupplementBase : public SupplementTracing<isGarbageCollected> { |
| 129 public: |
| 130 virtual ~SupplementBase() { } |
97 #if SECURITY_ASSERT_ENABLED | 131 #if SECURITY_ASSERT_ENABLED |
98 virtual bool isRefCountedWrapper() const { return false; } | 132 virtual bool isRefCountedWrapper() const { return false; } |
99 #endif | 133 #endif |
100 | 134 |
101 static void provideTo(Supplementable<T>& host, const char* key, PassOwnPtr<S
upplement<T> > supplement) | 135 static void provideTo(SupplementableBase<T, isGarbageCollected>& host, const
char* key, typename SupplementableTraits<T, isGarbageCollected>::SupplementArgu
mentType supplement) |
102 { | 136 { |
103 host.provideSupplement(key, supplement); | 137 host.provideSupplement(key, supplement); |
104 } | 138 } |
105 | 139 |
106 static Supplement<T>* from(Supplementable<T>& host, const char* key) | 140 static SupplementBase<T, isGarbageCollected>* from(SupplementableBase<T, isG
arbageCollected>& host, const char* key) |
107 { | 141 { |
108 return host.requireSupplement(key); | 142 return host.requireSupplement(key); |
109 } | 143 } |
110 | 144 |
111 static Supplement<T>* from(Supplementable<T>* host, const char* key) | 145 static SupplementBase<T, isGarbageCollected>* from(SupplementableBase<T, isG
arbageCollected>* host, const char* key) |
112 { | 146 { |
113 return host ? host->requireSupplement(key) : 0; | 147 return host ? host->requireSupplement(key) : 0; |
114 } | 148 } |
115 }; | 149 }; |
116 | 150 |
117 template<typename T> | 151 template<typename T, bool isGarbageCollected = false> |
118 class Supplementable { | 152 class SupplementableBase { |
119 public: | 153 public: |
120 void provideSupplement(const char* key, PassOwnPtr<Supplement<T> > supplemen
t) | 154 void provideSupplement(const char* key, typename SupplementableTraits<T, isG
arbageCollected>::SupplementArgumentType supplement) |
121 { | 155 { |
122 ASSERT(m_threadId == currentThread()); | 156 ASSERT(m_threadId == currentThread()); |
123 ASSERT(!m_supplements.get(key)); | 157 ASSERT(!m_supplements.get(key)); |
124 m_supplements.set(key, supplement); | 158 m_supplements.set(key, supplement); |
125 } | 159 } |
126 | 160 |
127 void removeSupplement(const char* key) | 161 void removeSupplement(const char* key) |
128 { | 162 { |
129 ASSERT(m_threadId == currentThread()); | 163 ASSERT(m_threadId == currentThread()); |
130 m_supplements.remove(key); | 164 m_supplements.remove(key); |
131 } | 165 } |
132 | 166 |
133 Supplement<T>* requireSupplement(const char* key) | 167 SupplementBase<T, isGarbageCollected>* requireSupplement(const char* key) |
134 { | 168 { |
135 ASSERT(m_threadId == currentThread()); | 169 ASSERT(m_threadId == currentThread()); |
136 return m_supplements.get(key); | 170 return m_supplements.get(key); |
137 } | 171 } |
138 | 172 |
139 void reattachThread() | 173 void reattachThread() |
140 { | 174 { |
141 #if !ASSERT_DISABLED | 175 #if !ASSERT_DISABLED |
142 m_threadId = currentThread(); | 176 m_threadId = currentThread(); |
143 #endif | 177 #endif |
144 } | 178 } |
145 | 179 |
| 180 void trace(Visitor* visitor) |
| 181 { |
| 182 visitor->trace(m_supplements); |
| 183 } |
| 184 |
146 #if !ASSERT_DISABLED | 185 #if !ASSERT_DISABLED |
147 protected: | 186 protected: |
148 Supplementable() : m_threadId(currentThread()) { } | 187 SupplementableBase() : m_threadId(currentThread()) { } |
149 #endif | 188 #endif |
150 | 189 |
151 private: | 190 private: |
152 typedef HashMap<const char*, OwnPtr<Supplement<T> >, PtrHash<const char*> >
SupplementMap; | 191 typename SupplementableTraits<T, isGarbageCollected>::SupplementMap m_supple
ments; |
153 SupplementMap m_supplements; | |
154 #if !ASSERT_DISABLED | 192 #if !ASSERT_DISABLED |
155 ThreadIdentifier m_threadId; | 193 ThreadIdentifier m_threadId; |
156 #endif | 194 #endif |
157 }; | 195 }; |
158 | 196 |
| 197 template<typename T> |
| 198 class HeapSupplement : public SupplementBase<T, true> { }; |
| 199 |
| 200 template<typename T> |
| 201 class HeapSupplementable : public SupplementableBase<T, true> { }; |
| 202 |
| 203 template<typename T> |
| 204 class Supplement : public SupplementBase<T, false> { }; |
| 205 |
| 206 template<typename T> |
| 207 class Supplementable : public SupplementableBase<T, false> { }; |
| 208 |
| 209 template<typename T> |
| 210 struct ThreadingTrait<WebCore::SupplementBase<T, true> > { |
| 211 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity; |
| 212 }; |
| 213 |
| 214 template<typename T> |
| 215 struct ThreadingTrait<WebCore::SupplementableBase<T, true> > { |
| 216 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity; |
| 217 }; |
| 218 |
159 } // namespace WebCore | 219 } // namespace WebCore |
160 | 220 |
161 #endif // Supplementable_h | 221 #endif // Supplementable_h |
OLD | NEW |