source: webkit/trunk/JavaScriptCore/wtf/ThreadSpecific.h@ 51199

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

2009-09-16 Zoltan Herczeg <[email protected]>

Reviewed by Simon Hausmann.

[Qt] Fix wtf/ThreadSpecific.h under Qt to free thread local objects.
https://bugs.webkit.org/show_bug.cgi?id=29295

This is an important fix when JavaScript workers are in use, since
unfreed ThreadGlobalDatas leak a big amount of memory (50-100k each).
QThreadStorage calls the destructor of a given object, which is the
ThreadSpecific::Data. Unlike pthread, Qt is object oriented, and does
not support the calling of a static utility function when the thread
is about to close. In this patch we call the ThreadSpecific::destroy()
utility function from the destructor of ThreadSpecific::Data. Moreover,
since Qt resets all thread local values to 0 before the calling of the
appropriate destructors, we set back the pointer to its original value.
This is necessary because the get() method of the ThreadSpecific
object may be called during the exuction of the destructor.

  • wtf/ThreadSpecific.h: (WTF::ThreadSpecific::Data::~Data): (WTF::::~ThreadSpecific): (WTF::::set): (WTF::::destroy):
  • Property svn:eol-style set to native
File size: 7.5 KB
Line 
1/*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Jian Li <[email protected]>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/* Thread local storage is implemented by using either pthread API or Windows
31 * native API. There is subtle semantic discrepancy for the cleanup function
32 * implementation as noted below:
33 * @ In pthread implementation, the destructor function will be called
34 * repeatedly if there is still non-NULL value associated with the function.
35 * @ In Windows native implementation, the destructor function will be called
36 * only once.
37 * This semantic discrepancy does not impose any problem because nowhere in
38 * WebKit the repeated call bahavior is utilized.
39 */
40
41#ifndef WTF_ThreadSpecific_h
42#define WTF_ThreadSpecific_h
43
44#include <wtf/Noncopyable.h>
45
46#if USE(PTHREADS)
47#include <pthread.h>
48#elif PLATFORM(QT)
49#include <QThreadStorage>
50#elif PLATFORM(WIN_OS)
51#include <windows.h>
52#endif
53
54namespace WTF {
55
56#if !USE(PTHREADS) && !PLATFORM(QT) && PLATFORM(WIN_OS)
57// ThreadSpecificThreadExit should be called each time when a thread is detached.
58// This is done automatically for threads created with WTF::createThread.
59void ThreadSpecificThreadExit();
60#endif
61
62template<typename T> class ThreadSpecific : public Noncopyable {
63public:
64 ThreadSpecific();
65 T* operator->();
66 operator T*();
67 T& operator*();
68 ~ThreadSpecific();
69
70private:
71#if !USE(PTHREADS) && !PLATFORM(QT) && PLATFORM(WIN_OS)
72 friend void ThreadSpecificThreadExit();
73#endif
74
75 T* get();
76 void set(T*);
77 void static destroy(void* ptr);
78
79#if USE(PTHREADS) || PLATFORM(QT) || PLATFORM(WIN_OS)
80 struct Data : Noncopyable {
81 Data(T* value, ThreadSpecific<T>* owner) : value(value), owner(owner) {}
82#if PLATFORM(QT)
83 ~Data() { owner->destroy(this); }
84#endif
85
86 T* value;
87 ThreadSpecific<T>* owner;
88#if !USE(PTHREADS) && !PLATFORM(QT)
89 void (*destructor)(void*);
90#endif
91 };
92#endif
93
94#if USE(PTHREADS)
95 pthread_key_t m_key;
96#elif PLATFORM(QT)
97 QThreadStorage<Data*> m_key;
98#elif PLATFORM(WIN_OS)
99 int m_index;
100#endif
101};
102
103#if USE(PTHREADS)
104template<typename T>
105inline ThreadSpecific<T>::ThreadSpecific()
106{
107 int error = pthread_key_create(&m_key, destroy);
108 if (error)
109 CRASH();
110}
111
112template<typename T>
113inline ThreadSpecific<T>::~ThreadSpecific()
114{
115 pthread_key_delete(m_key); // Does not invoke destructor functions.
116}
117
118template<typename T>
119inline T* ThreadSpecific<T>::get()
120{
121 Data* data = static_cast<Data*>(pthread_getspecific(m_key));
122 return data ? data->value : 0;
123}
124
125template<typename T>
126inline void ThreadSpecific<T>::set(T* ptr)
127{
128 ASSERT(!get());
129 pthread_setspecific(m_key, new Data(ptr, this));
130}
131
132#elif PLATFORM(QT)
133
134template<typename T>
135inline ThreadSpecific<T>::ThreadSpecific()
136{
137}
138
139template<typename T>
140inline ThreadSpecific<T>::~ThreadSpecific()
141{
142 // Does not invoke destructor functions. QThreadStorage will do it
143}
144
145template<typename T>
146inline T* ThreadSpecific<T>::get()
147{
148 Data* data = static_cast<Data*>(m_key.localData());
149 return data ? data->value : 0;
150}
151
152template<typename T>
153inline void ThreadSpecific<T>::set(T* ptr)
154{
155 ASSERT(!get());
156 Data* data = new Data(ptr, this);
157 m_key.setLocalData(data);
158}
159
160#elif PLATFORM(WIN_OS)
161
162// The maximum number of TLS keys that can be created. For simplification, we assume that:
163// 1) Once the instance of ThreadSpecific<> is created, it will not be destructed until the program dies.
164// 2) We do not need to hold many instances of ThreadSpecific<> data. This fixed number should be far enough.
165const int kMaxTlsKeySize = 256;
166
167long& tlsKeyCount();
168DWORD* tlsKeys();
169
170template<typename T>
171inline ThreadSpecific<T>::ThreadSpecific()
172 : m_index(-1)
173{
174 DWORD tls_key = TlsAlloc();
175 if (tls_key == TLS_OUT_OF_INDEXES)
176 CRASH();
177
178 m_index = InterlockedIncrement(&tlsKeyCount()) - 1;
179 if (m_index >= kMaxTlsKeySize)
180 CRASH();
181 tlsKeys()[m_index] = tls_key;
182}
183
184template<typename T>
185inline ThreadSpecific<T>::~ThreadSpecific()
186{
187 // Does not invoke destructor functions. They will be called from ThreadSpecificThreadExit when the thread is detached.
188 TlsFree(tlsKeys()[m_index]);
189}
190
191template<typename T>
192inline T* ThreadSpecific<T>::get()
193{
194 Data* data = static_cast<Data*>(TlsGetValue(tlsKeys()[m_index]));
195 return data ? data->value : 0;
196}
197
198template<typename T>
199inline void ThreadSpecific<T>::set(T* ptr)
200{
201 ASSERT(!get());
202 Data* data = new Data(ptr, this);
203 data->destructor = &ThreadSpecific<T>::destroy;
204 TlsSetValue(tlsKeys()[m_index], data);
205}
206
207#else
208#error ThreadSpecific is not implemented for this platform.
209#endif
210
211template<typename T>
212inline void ThreadSpecific<T>::destroy(void* ptr)
213{
214 Data* data = static_cast<Data*>(ptr);
215
216#if USE(PTHREADS)
217 // We want get() to keep working while data destructor works, because it can be called indirectly by the destructor.
218 // Some pthreads implementations zero out the pointer before calling destroy(), so we temporarily reset it.
219 pthread_setspecific(data->owner->m_key, ptr);
220#endif
221#if PLATFORM(QT)
222 // See comment as above
223 data->owner->m_key.setLocalData(data);
224#endif
225
226 data->value->~T();
227 fastFree(data->value);
228
229#if USE(PTHREADS)
230 pthread_setspecific(data->owner->m_key, 0);
231#elif PLATFORM(QT)
232 // Do nothing here
233#elif PLATFORM(WIN_OS)
234 TlsSetValue(tlsKeys()[data->owner->m_index], 0);
235#else
236#error ThreadSpecific is not implemented for this platform.
237#endif
238
239#if !PLATFORM(QT)
240 delete data;
241#endif
242}
243
244template<typename T>
245inline ThreadSpecific<T>::operator T*()
246{
247 T* ptr = static_cast<T*>(get());
248 if (!ptr) {
249 // Set up thread-specific value's memory pointer before invoking constructor, in case any function it calls
250 // needs to access the value, to avoid recursion.
251 ptr = static_cast<T*>(fastMalloc(sizeof(T)));
252 set(ptr);
253 new (ptr) T;
254 }
255 return ptr;
256}
257
258template<typename T>
259inline T* ThreadSpecific<T>::operator->()
260{
261 return operator T*();
262}
263
264template<typename T>
265inline T& ThreadSpecific<T>::operator*()
266{
267 return *operator T*();
268}
269
270}
271
272#endif
Note: See TracBrowser for help on using the repository browser.