Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: Source/platform/Supplementable.h

Issue 178663004: Oilpan: move WorkerGlobalScope to oilpan's heap. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Let WorkerThread hold a global scope persistent. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 SupplementableTraitsImpl;
92 99
93 template<typename T> 100 template<typename T>
94 class Supplement { 101 struct SupplementableTraitsImpl<T, true> {
102 typedef RawPtr<SupplementBase<T, true> > SupplementArgumentType;
Mads Ager (chromium) 2014/02/26 08:00:36 Shouldn't this be PassOwnPtrWillBeRawPtr?
sof 2014/02/26 09:11:21 Yes, that would be tidier.
103 typedef WillBeHeapHashMap<const char*, RefPtrWillBeMember<SupplementBase<T, true> >, PtrHash<const char*> > SupplementMap;
Mads Ager (chromium) 2014/02/26 08:00:36 Shouldn't this be OwnPtrWillBeMember?
sof 2014/02/26 09:11:21 Done.
104 };
105
106 template<typename T>
107 struct SupplementableTraitsImpl<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 // FIXME: oilpan: this doesn't work as wanted when used in e.g.,
113 //
114 // class C : public GarbageCollected<C>, Supplementable<C> { .. };
115 //
116 // as deriving that C is a subclass of GarbageCollected while declaring C doesn' t
117 // appear possible (returns 'false'.)
118 //
119 // This is why we have a bool template argument instead. This has some merit whi le
120 // in transition, as it allows the Supplementable to remain off-heap while conve rting
121 // the object itself.
122 template<typename T>
123 struct SupplementableTraits : public SupplementableTraitsImpl<T, WTF::IsSubclass OfTemplate<T, WebCore::GarbageCollected>::value> {
Mads Ager (chromium) 2014/02/26 08:00:36 Let's remove SupplementableTraits. As you state it
sof 2014/02/26 09:11:21 yes, i'm currently out of ideas of how to derive a
124 };
125
126 template<bool>
127 class SupplementTracing;
128
129 template<>
130 class SupplementTracing<true> {
95 public: 131 public:
96 virtual ~Supplement() { } 132 virtual void trace(Visitor*) = 0;
133 };
134
135 template<>
136 class SupplementTracing<false> {
137 public:
138 virtual void trace(Visitor*) { }
139 };
140
141 template<typename T, bool isGarbageCollected = false>
142 class SupplementBase : public SupplementTracing<isGarbageCollected> {
143 public:
144 virtual ~SupplementBase() { }
97 #if SECURITY_ASSERT_ENABLED 145 #if SECURITY_ASSERT_ENABLED
98 virtual bool isRefCountedWrapper() const { return false; } 146 virtual bool isRefCountedWrapper() const { return false; }
99 #endif 147 #endif
100 148
101 static void provideTo(Supplementable<T>& host, const char* key, PassOwnPtr<S upplement<T> > supplement) 149 static void provideTo(SupplementableBase<T, isGarbageCollected>& host, const char* key, typename SupplementableTraitsImpl<T, isGarbageCollected>::Supplement ArgumentType supplement)
102 { 150 {
103 host.provideSupplement(key, supplement); 151 host.provideSupplement(key, supplement);
104 } 152 }
105 153
106 static Supplement<T>* from(Supplementable<T>& host, const char* key) 154 static SupplementBase<T, isGarbageCollected>* from(SupplementableBase<T, isG arbageCollected>& host, const char* key)
107 { 155 {
108 return host.requireSupplement(key); 156 return host.requireSupplement(key);
109 } 157 }
110 158
111 static Supplement<T>* from(Supplementable<T>* host, const char* key) 159 static SupplementBase<T, isGarbageCollected>* from(SupplementableBase<T, isG arbageCollected>* host, const char* key)
112 { 160 {
113 return host ? host->requireSupplement(key) : 0; 161 return host ? host->requireSupplement(key) : 0;
114 } 162 }
115 }; 163 };
116 164
117 template<typename T> 165 template<typename T, bool isGarbageCollected = false>
118 class Supplementable { 166 class SupplementableBase {
119 public: 167 public:
120 void provideSupplement(const char* key, PassOwnPtr<Supplement<T> > supplemen t) 168 void provideSupplement(const char* key, typename SupplementableTraitsImpl<T, isGarbageCollected>::SupplementArgumentType supplement)
121 { 169 {
122 ASSERT(m_threadId == currentThread()); 170 ASSERT(m_threadId == currentThread());
123 ASSERT(!m_supplements.get(key)); 171 ASSERT(!m_supplements.get(key));
124 m_supplements.set(key, supplement); 172 m_supplements.set(key, supplement);
125 } 173 }
126 174
127 void removeSupplement(const char* key) 175 void removeSupplement(const char* key)
128 { 176 {
129 ASSERT(m_threadId == currentThread()); 177 ASSERT(m_threadId == currentThread());
130 m_supplements.remove(key); 178 m_supplements.remove(key);
131 } 179 }
132 180
133 Supplement<T>* requireSupplement(const char* key) 181 SupplementBase<T, isGarbageCollected>* requireSupplement(const char* key)
134 { 182 {
135 ASSERT(m_threadId == currentThread()); 183 ASSERT(m_threadId == currentThread());
136 return m_supplements.get(key); 184 return m_supplements.get(key);
137 } 185 }
138 186
139 void reattachThread() 187 void reattachThread()
140 { 188 {
141 #if !ASSERT_DISABLED 189 #if !ASSERT_DISABLED
142 m_threadId = currentThread(); 190 m_threadId = currentThread();
143 #endif 191 #endif
144 } 192 }
145 193
194 void trace(Visitor* visitor)
195 {
196 visitor->trace(m_supplements);
197 }
198
146 #if !ASSERT_DISABLED 199 #if !ASSERT_DISABLED
147 protected: 200 protected:
148 Supplementable() : m_threadId(currentThread()) { } 201 SupplementableBase() : m_threadId(currentThread()) { }
149 #endif 202 #endif
150 203
151 private: 204 private:
152 typedef HashMap<const char*, OwnPtr<Supplement<T> >, PtrHash<const char*> > SupplementMap; 205 typename SupplementableTraitsImpl<T, isGarbageCollected>::SupplementMap m_su pplements;
153 SupplementMap m_supplements;
154 #if !ASSERT_DISABLED 206 #if !ASSERT_DISABLED
155 ThreadIdentifier m_threadId; 207 ThreadIdentifier m_threadId;
156 #endif 208 #endif
157 }; 209 };
158 210
211 template<typename T>
212 class HeapSupplement : public SupplementBase<T, true> { };
213
214 template<typename T>
215 class HeapSupplementable : public SupplementableBase<T, true> { };
216
217 template<typename T>
218 class Supplement : public SupplementBase<T, false> { };
219
220 template<typename T>
221 class Supplementable : public SupplementableBase<T, false> { };
222
223 template<typename T>
224 struct ThreadingTrait<WebCore::SupplementBase<T, true> > {
225 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity;
226 };
227
228 template<typename T>
229 struct ThreadingTrait<WebCore::SupplementableBase<T, true> > {
230 static const ThreadAffinity Affinity = ThreadingTrait<T>::Affinity;
231 };
232
159 } // namespace WebCore 233 } // namespace WebCore
160 234
161 #endif // Supplementable_h 235 #endif // Supplementable_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698