2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Jian Li <jianli@chromium.org>
4 * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
16 * its contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 /* Thread local storage is implemented by using either pthread API or Windows
32 * native API. There is subtle semantic discrepancy for the cleanup function
33 * implementation as noted below:
34 * @ In pthread implementation, the destructor function will be called
35 * repeatedly if there is still non-NULL value associated with the function.
36 * @ In Windows native implementation, the destructor function will be called
38 * This semantic discrepancy does not impose any problem because nowhere in
39 * WebKit the repeated call bahavior is utilized.
42 #ifndef WTF_ThreadSpecific_h
43 #define WTF_ThreadSpecific_h
45 #include "wtf/Noncopyable.h"
46 #include "wtf/StdLibExtras.h"
48 #include "wtf/WTFExport.h"
59 // ThreadSpecificThreadExit should be called each time when a thread is detached.
60 // This is done automatically for threads created with WTF::createThread.
61 WTF_EXPORT
void ThreadSpecificThreadExit();
64 template<typename T
> class ThreadSpecific
{
65 WTF_MAKE_NONCOPYABLE(ThreadSpecific
);
68 bool isSet(); // Useful as a fast check to see if this thread has set this value.
75 WTF_EXPORT
friend void ThreadSpecificThreadExit();
78 // Not implemented. It's technically possible to destroy a thread specific key, but one would need
79 // to make sure that all values have been destroyed already (usually, that all threads that used it
80 // have exited). It's unlikely that any user of this call will be in that situation - and having
81 // a destructor defined can be confusing, given that it has such strong pre-requisites to work correctly.
86 void static destroy(void* ptr
);
89 WTF_MAKE_NONCOPYABLE(Data
);
91 Data(T
* value
, ThreadSpecific
<T
>* owner
) : value(value
), owner(owner
) {}
94 ThreadSpecific
<T
>* owner
;
96 void (*destructor
)(void*);
109 typedef pthread_key_t ThreadSpecificKey
;
111 inline void threadSpecificKeyCreate(ThreadSpecificKey
* key
, void (*destructor
)(void *))
113 int error
= pthread_key_create(key
, destructor
);
118 inline void threadSpecificKeyDelete(ThreadSpecificKey key
)
120 int error
= pthread_key_delete(key
);
125 inline void threadSpecificSet(ThreadSpecificKey key
, void* value
)
127 pthread_setspecific(key
, value
);
130 inline void* threadSpecificGet(ThreadSpecificKey key
)
132 return pthread_getspecific(key
);
136 inline ThreadSpecific
<T
>::ThreadSpecific()
138 int error
= pthread_key_create(&m_key
, destroy
);
144 inline T
* ThreadSpecific
<T
>::get()
146 Data
* data
= static_cast<Data
*>(pthread_getspecific(m_key
));
147 return data
? data
->value
: 0;
151 inline void ThreadSpecific
<T
>::set(T
* ptr
)
154 pthread_setspecific(m_key
, new Data(ptr
, this));
159 // TLS_OUT_OF_INDEXES is not defined on WinCE.
160 #ifndef TLS_OUT_OF_INDEXES
161 #define TLS_OUT_OF_INDEXES 0xffffffff
164 // The maximum number of TLS keys that can be created. For simplification, we assume that:
165 // 1) Once the instance of ThreadSpecific<> is created, it will not be destructed until the program dies.
166 // 2) We do not need to hold many instances of ThreadSpecific<> data. This fixed number should be far enough.
167 const int kMaxTlsKeySize
= 256;
169 WTF_EXPORT
long& tlsKeyCount();
170 WTF_EXPORT DWORD
* tlsKeys();
172 class PlatformThreadSpecificKey
;
173 typedef PlatformThreadSpecificKey
* ThreadSpecificKey
;
175 WTF_EXPORT
void threadSpecificKeyCreate(ThreadSpecificKey
*, void (*)(void *));
176 WTF_EXPORT
void threadSpecificKeyDelete(ThreadSpecificKey
);
177 WTF_EXPORT
void threadSpecificSet(ThreadSpecificKey
, void*);
178 WTF_EXPORT
void* threadSpecificGet(ThreadSpecificKey
);
181 inline ThreadSpecific
<T
>::ThreadSpecific()
184 DWORD tlsKey
= TlsAlloc();
185 if (tlsKey
== TLS_OUT_OF_INDEXES
)
188 m_index
= InterlockedIncrement(&tlsKeyCount()) - 1;
189 if (m_index
>= kMaxTlsKeySize
)
191 tlsKeys()[m_index
] = tlsKey
;
195 inline ThreadSpecific
<T
>::~ThreadSpecific()
197 // Does not invoke destructor functions. They will be called from ThreadSpecificThreadExit when the thread is detached.
198 TlsFree(tlsKeys()[m_index
]);
202 inline T
* ThreadSpecific
<T
>::get()
204 Data
* data
= static_cast<Data
*>(TlsGetValue(tlsKeys()[m_index
]));
205 return data
? data
->value
: 0;
209 inline void ThreadSpecific
<T
>::set(T
* ptr
)
212 Data
* data
= new Data(ptr
, this);
213 data
->destructor
= &ThreadSpecific
<T
>::destroy
;
214 TlsSetValue(tlsKeys()[m_index
], data
);
218 #error ThreadSpecific is not implemented for this platform.
222 inline void ThreadSpecific
<T
>::destroy(void* ptr
)
227 Data
* data
= static_cast<Data
*>(ptr
);
230 // We want get() to keep working while data destructor works, because it can be called indirectly by the destructor.
231 // Some pthreads implementations zero out the pointer before calling destroy(), so we temporarily reset it.
232 pthread_setspecific(data
->owner
->m_key
, ptr
);
236 fastFree(data
->value
);
239 pthread_setspecific(data
->owner
->m_key
, 0);
241 TlsSetValue(tlsKeys()[data
->owner
->m_index
], 0);
243 #error ThreadSpecific is not implemented for this platform.
250 inline bool ThreadSpecific
<T
>::isSet()
256 inline ThreadSpecific
<T
>::operator T
*()
258 T
* ptr
= static_cast<T
*>(get());
260 // Set up thread-specific value's memory pointer before invoking constructor, in case any function it calls
261 // needs to access the value, to avoid recursion.
262 ptr
= static_cast<T
*>(fastZeroedMalloc(sizeof(T
)));
264 new (NotNull
, ptr
) T
;
270 inline T
* ThreadSpecific
<T
>::operator->()
272 return operator T
*();
276 inline T
& ThreadSpecific
<T
>::operator*()
278 return *operator T
*();
283 using WTF::ThreadSpecific
;
285 #endif // WTF_ThreadSpecific_h