source: webkit/trunk/JavaScriptCore/wtf/CrossThreadRefCounted.h@ 43311

Last change on this file since 43311 was 41536, checked in by [email protected], 16 years ago

2009-03-09 David Levin <[email protected]>

Reviewed by Darin Adler.

Bug 23175: String and UString should be able to share a UChar* buffer.
<https://bugs.webkit.org/show_bug.cgi?id=23175>

Add CrossThreadRefCounted.

  • wtf/CrossThreadRefCounted.h: Added. (WTF::CrossThreadRefCounted::create): (WTF::CrossThreadRefCounted::isShared): (WTF::CrossThreadRefCounted::dataAccessMustBeThreadSafe): (WTF::CrossThreadRefCounted::mayBePassedToAnotherThread): (WTF::CrossThreadRefCounted::CrossThreadRefCounted): (WTF::CrossThreadRefCounted::~CrossThreadRefCounted): (WTF::CrossThreadRefCounted::ref): (WTF::CrossThreadRefCounted::deref): (WTF::CrossThreadRefCounted::release): (WTF::CrossThreadRefCounted::copy): (WTF::CrossThreadRefCounted::threadSafeDeref):
  • wtf/RefCounted.h:
  • wtf/Threading.h: (WTF::ThreadSafeSharedBase::ThreadSafeSharedBase): (WTF::ThreadSafeSharedBase::derefBase): (WTF::ThreadSafeShared::ThreadSafeShared): (WTF::ThreadSafeShared::deref):
File size: 5.5 KB
Line 
1/*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31#ifndef CrossThreadRefCounted_h
32#define CrossThreadRefCounted_h
33
34#include <wtf/Noncopyable.h>
35#include <wtf/PassRefPtr.h>
36#include <wtf/RefCounted.h>
37#include <wtf/Threading.h>
38#include <wtf/TypeTraits.h>
39
40namespace WTF {
41
42 // Used to allowing sharing data across classes and threads (like ThreadedSafeShared).
43 //
44 // Why not just use ThreadSafeShared?
45 // ThreadSafeShared can have a significant perf impact when used in low level classes
46 // (like UString) that get ref/deref'ed a lot. This class has the benefit of doing fast ref
47 // counts like RefPtr whenever possible, but it has the downside that you need to copy it
48 // to use it on another thread.
49 //
50 // Is this class threadsafe?
51 // While each instance of the class is not threadsafe, the copied instance is threadsafe
52 // with respect to the original and any other copies. The underlying m_data is jointly
53 // owned by the original instance and all copies.
54 template<class T>
55 class CrossThreadRefCounted : Noncopyable {
56 public:
57 static PassRefPtr<CrossThreadRefCounted<T> > create(T* data)
58 {
59 return adoptRef(new CrossThreadRefCounted<T>(data, 0));
60 }
61
62 // Used to make an instance that can be used on another thread.
63 PassRefPtr<CrossThreadRefCounted<T> > crossThreadCopy();
64
65 void ref();
66 void deref();
67 T* release();
68
69#ifndef NDEBUG
70 bool mayBePassedToAnotherThread() const { ASSERT(!m_threadId); return m_refCounter.hasOneRef(); }
71#endif
72
73 private:
74 CrossThreadRefCounted(T* data, ThreadSafeSharedBase* threadedCounter)
75 : m_threadSafeRefCounter(threadedCounter)
76 , m_data(data)
77#ifndef NDEBUG
78 , m_threadId(0)
79#endif
80 {
81 }
82
83 ~CrossThreadRefCounted()
84 {
85 if (!m_threadSafeRefCounter)
86 delete m_data;
87 }
88
89 void threadSafeDeref();
90
91 RefCountedBase m_refCounter;
92 ThreadSafeSharedBase* m_threadSafeRefCounter;
93 T* m_data;
94#ifndef NDEBUG
95 ThreadIdentifier m_threadId;
96#endif
97 };
98
99 template<class T>
100 void CrossThreadRefCounted<T>::ref()
101 {
102 ASSERT(!m_threadId || m_threadId == currentThread());
103 m_refCounter.ref();
104#ifndef NDEBUG
105 // Store the threadId as soon as the ref count gets to 2.
106 // The class gets created with a ref count of 1 and then passed
107 // to another thread where to ref count get increased. This
108 // is a heuristic but it seems to always work and has helped
109 // find some bugs.
110 if (!m_threadId && m_refCounter.refCount() == 2)
111 m_threadId = currentThread();
112#endif
113 }
114
115 template<class T>
116 void CrossThreadRefCounted<T>::deref()
117 {
118 ASSERT(!m_threadId || m_threadId == currentThread());
119 if (m_refCounter.derefBase()) {
120 threadSafeDeref();
121 delete this;
122 } else {
123#ifndef NDEBUG
124 // Clear the threadId when the ref goes to 1 because it
125 // is safe to be passed to another thread at this point.
126 if (m_threadId && m_refCounter.refCount() == 1)
127 m_threadId = 0;
128#endif
129 }
130 }
131
132 template<class T>
133 T* CrossThreadRefCounted<T>::release()
134 {
135 ASSERT(!isShared());
136
137 T* data = m_data;
138 m_data = 0;
139 return data;
140 }
141
142 template<class T>
143 PassRefPtr<CrossThreadRefCounted<T> > CrossThreadRefCounted<T>::crossThreadCopy()
144 {
145 if (m_threadSafeRefCounter)
146 m_threadSafeRefCounter->ref();
147 else
148 m_threadSafeRefCounter = new ThreadSafeSharedBase(2);
149 return adoptRef(new CrossThreadRefCounted<T>(m_data, m_threadSafeRefCounter));
150 }
151
152
153 template<class T>
154 void CrossThreadRefCounted<T>::threadSafeDeref()
155 {
156 if (m_threadSafeRefCounter && m_threadSafeRefCounter->derefBase()) {
157 delete m_threadSafeRefCounter;
158 m_threadSafeRefCounter = 0;
159 }
160 }
161} // namespace WTF
162
163using WTF::CrossThreadRefCounted;
164
165#endif // CrossThreadRefCounted_h
Note: See TracBrowser for help on using the repository browser.