2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CrossThreadRefCounted_h
32 #define CrossThreadRefCounted_h
34 #include <wtf/Noncopyable.h>
35 #include <wtf/PassRefPtr.h>
36 #include <wtf/RefCounted.h>
37 #include <wtf/Threading.h>
41 // Used to allowing sharing data across classes and threads (like ThreadedSafeShared).
43 // Why not just use ThreadSafeShared?
44 // ThreadSafeShared can have a significant perf impact when used in low level classes
45 // (like UString) that get ref/deref'ed a lot. This class has the benefit of doing fast ref
46 // counts like RefPtr whenever possible, but it has the downside that you need to copy it
47 // to use it on another thread.
49 // Is this class threadsafe?
50 // While each instance of the class is not threadsafe, the copied instance is threadsafe
51 // with respect to the original and any other copies. The underlying m_data is jointly
52 // owned by the original instance and all copies.
54 class CrossThreadRefCounted
: Noncopyable
{
56 static PassRefPtr
<CrossThreadRefCounted
<T
> > create(T
* data
)
58 return adoptRef(new CrossThreadRefCounted
<T
>(data
, 0));
61 // Used to make an instance that can be used on another thread.
62 PassRefPtr
<CrossThreadRefCounted
<T
> > crossThreadCopy();
70 return !m_refCounter
.hasOneRef() || (m_threadSafeRefCounter
&& !m_threadSafeRefCounter
->hasOneRef());
74 bool mayBePassedToAnotherThread() const { ASSERT(!m_threadId
); return m_refCounter
.hasOneRef(); }
78 CrossThreadRefCounted(T
* data
, ThreadSafeSharedBase
* threadedCounter
)
79 : m_threadSafeRefCounter(threadedCounter
)
87 ~CrossThreadRefCounted()
89 if (!m_threadSafeRefCounter
)
93 void threadSafeDeref();
95 RefCountedBase m_refCounter
;
96 ThreadSafeSharedBase
* m_threadSafeRefCounter
;
99 ThreadIdentifier m_threadId
;
104 void CrossThreadRefCounted
<T
>::ref()
107 // MobileSafari allocates content on the UI thread then
108 // frees it on the Web thread. This is ok. This occurs in a
109 // couple of places. In particular, http://maps.google.com
110 // which uses JS to add an iFrame with an about:blank URL and
111 // then executes some JS. Much of this work happens on the
113 // Ideally we want to have this assert here:
114 // ASSERT(WebThreadIsLockedOrDisabled())
115 // but then we would need to include code from
116 // WebCore. So just don't do any ASSERTs here.
119 // Store the threadId as soon as the ref count gets to 2.
120 // The class gets created with a ref count of 1 and then passed
121 // to another thread where to ref count get increased. This
122 // is a heuristic but it seems to always work and has helped
124 if (!m_threadId
&& m_refCounter
.refCount() == 2)
125 m_threadId
= currentThread();
130 void CrossThreadRefCounted
<T
>::deref()
132 // MobileSafari allocates content on the UI thread then
133 // frees it on the Web thread. This is ok. This occurs in a
134 // couple of places. In particular, http://maps.google.com
135 // which uses JS to add an iFrame with an about:blank URL and
136 // then executes some JS. Much of this work happens on the
138 // Ideally we want to have this assert here:
139 // ASSERT(WebThreadIsLockedOrDisabled())
140 // but then we would need to include code from
141 // WebCore. So just don't do any ASSERTs here.
142 if (m_refCounter
.derefBase()) {
147 // Clear the threadId when the ref goes to 1 because it
148 // is safe to be passed to another thread at this point.
149 if (m_threadId
&& m_refCounter
.refCount() == 1)
156 T
* CrossThreadRefCounted
<T
>::release()
166 PassRefPtr
<CrossThreadRefCounted
<T
> > CrossThreadRefCounted
<T
>::crossThreadCopy()
168 if (m_threadSafeRefCounter
)
169 m_threadSafeRefCounter
->ref();
171 m_threadSafeRefCounter
= new ThreadSafeSharedBase(2);
172 return adoptRef(new CrossThreadRefCounted
<T
>(m_data
, m_threadSafeRefCounter
));
177 void CrossThreadRefCounted
<T
>::threadSafeDeref()
179 if (m_threadSafeRefCounter
&& m_threadSafeRefCounter
->derefBase()) {
180 delete m_threadSafeRefCounter
;
181 m_threadSafeRefCounter
= 0;
186 using WTF::CrossThreadRefCounted
;
188 #endif // CrossThreadRefCounted_h