]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/ThreadSpecific.h
7e5679f9649ffcef42ebaa7e9779c8383f329fc8
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Jian Li <jianli@chromium.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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
37 * This semantic discrepancy does not impose any problem because nowhere in
38 * WebKit the repeated call bahavior is utilized.
41 #ifndef WTF_ThreadSpecific_h
42 #define WTF_ThreadSpecific_h
44 #include <wtf/Noncopyable.h>
49 #include <QThreadStorage>
56 #if !USE(PTHREADS) && !PLATFORM(QT) && OS(WINDOWS)
57 // ThreadSpecificThreadExit should be called each time when a thread is detached.
58 // This is done automatically for threads created with WTF::createThread.
59 void ThreadSpecificThreadExit();
62 template<typename T
> class ThreadSpecific
: public Noncopyable
{
71 #if !USE(PTHREADS) && !PLATFORM(QT) && OS(WINDOWS)
72 friend void ThreadSpecificThreadExit();
77 void static destroy(void* ptr
);
79 #if USE(PTHREADS) || PLATFORM(QT) || OS(WINDOWS)
80 struct Data
: Noncopyable
{
81 Data(T
* value
, ThreadSpecific
<T
>* owner
) : value(value
), owner(owner
) {}
83 ~Data() { owner
->destroy(this); }
87 ThreadSpecific
<T
>* owner
;
88 #if !USE(PTHREADS) && !PLATFORM(QT)
89 void (*destructor
)(void*);
94 #if ENABLE(SINGLE_THREADED)
100 QThreadStorage
<Data
*> m_key
;
107 #if ENABLE(SINGLE_THREADED)
109 inline ThreadSpecific
<T
>::ThreadSpecific()
115 inline ThreadSpecific
<T
>::~ThreadSpecific()
120 inline T
* ThreadSpecific
<T
>::get()
126 inline void ThreadSpecific
<T
>::set(T
* ptr
)
134 inline ThreadSpecific
<T
>::ThreadSpecific()
136 int error
= pthread_key_create(&m_key
, destroy
);
142 inline ThreadSpecific
<T
>::~ThreadSpecific()
144 pthread_key_delete(m_key
); // Does not invoke destructor functions.
148 inline T
* ThreadSpecific
<T
>::get()
150 Data
* data
= static_cast<Data
*>(pthread_getspecific(m_key
));
151 return data
? data
->value
: 0;
155 inline void ThreadSpecific
<T
>::set(T
* ptr
)
158 pthread_setspecific(m_key
, new Data(ptr
, this));
164 inline ThreadSpecific
<T
>::ThreadSpecific()
169 inline ThreadSpecific
<T
>::~ThreadSpecific()
171 // Does not invoke destructor functions. QThreadStorage will do it
175 inline T
* ThreadSpecific
<T
>::get()
177 Data
* data
= static_cast<Data
*>(m_key
.localData());
178 return data
? data
->value
: 0;
182 inline void ThreadSpecific
<T
>::set(T
* ptr
)
185 Data
* data
= new Data(ptr
, this);
186 m_key
.setLocalData(data
);
191 // TLS_OUT_OF_INDEXES is not defined on WinCE.
192 #ifndef TLS_OUT_OF_INDEXES
193 #define TLS_OUT_OF_INDEXES 0xffffffff
196 // The maximum number of TLS keys that can be created. For simplification, we assume that:
197 // 1) Once the instance of ThreadSpecific<> is created, it will not be destructed until the program dies.
198 // 2) We do not need to hold many instances of ThreadSpecific<> data. This fixed number should be far enough.
199 const int kMaxTlsKeySize
= 256;
205 inline ThreadSpecific
<T
>::ThreadSpecific()
208 DWORD tlsKey
= TlsAlloc();
209 if (tlsKey
== TLS_OUT_OF_INDEXES
)
212 m_index
= InterlockedIncrement(&tlsKeyCount()) - 1;
213 if (m_index
>= kMaxTlsKeySize
)
215 tlsKeys()[m_index
] = tlsKey
;
219 inline ThreadSpecific
<T
>::~ThreadSpecific()
221 // Does not invoke destructor functions. They will be called from ThreadSpecificThreadExit when the thread is detached.
222 TlsFree(tlsKeys()[m_index
]);
226 inline T
* ThreadSpecific
<T
>::get()
228 Data
* data
= static_cast<Data
*>(TlsGetValue(tlsKeys()[m_index
]));
229 return data
? data
->value
: 0;
233 inline void ThreadSpecific
<T
>::set(T
* ptr
)
236 Data
* data
= new Data(ptr
, this);
237 data
->destructor
= &ThreadSpecific
<T
>::destroy
;
238 TlsSetValue(tlsKeys()[m_index
], data
);
242 #error ThreadSpecific is not implemented for this platform.
247 inline void ThreadSpecific
<T
>::destroy(void* ptr
)
249 #if !ENABLE(SINGLE_THREADED)
250 Data
* data
= static_cast<Data
*>(ptr
);
253 // We want get() to keep working while data destructor works, because it can be called indirectly by the destructor.
254 // Some pthreads implementations zero out the pointer before calling destroy(), so we temporarily reset it.
255 pthread_setspecific(data
->owner
->m_key
, ptr
);
258 // See comment as above
259 data
->owner
->m_key
.setLocalData(data
);
263 fastFree(data
->value
);
266 pthread_setspecific(data
->owner
->m_key
, 0);
270 TlsSetValue(tlsKeys()[data
->owner
->m_index
], 0);
272 #error ThreadSpecific is not implemented for this platform.
282 inline ThreadSpecific
<T
>::operator T
*()
284 T
* ptr
= static_cast<T
*>(get());
286 // Set up thread-specific value's memory pointer before invoking constructor, in case any function it calls
287 // needs to access the value, to avoid recursion.
288 ptr
= static_cast<T
*>(fastMalloc(sizeof(T
)));
296 inline T
* ThreadSpecific
<T
>::operator->()
298 return operator T
*();
302 inline T
& ThreadSpecific
<T
>::operator*()
304 return *operator T
*();