tizen beta release
[profile/ivi/webkit-efl.git] / Source / JavaScriptCore / wtf / ThreadSpecific.h
1 /*
2  * Copyright (C) 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2009 Jian Li <jianli@chromium.org>
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(GTK)
49 #include <glib.h>
50 #elif OS(WINDOWS)
51 #include <windows.h>
52 #endif
53
54 namespace WTF {
55
56 #if 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();
60 #endif
61
62 template<typename T> class ThreadSpecific {
63     WTF_MAKE_NONCOPYABLE(ThreadSpecific);
64 public:
65     ThreadSpecific();
66     bool isSet(); // Useful as a fast check to see if this thread has set this value.
67     T* operator->();
68     operator T*();
69     T& operator*();
70
71 private:
72 #if OS(WINDOWS)
73     friend void ThreadSpecificThreadExit();
74 #endif
75
76     // Not implemented. It's technically possible to destroy a thread specific key, but one would need
77     // to make sure that all values have been destroyed already (usually, that all threads that used it
78     // have exited). It's unlikely that any user of this call will be in that situation - and having
79     // a destructor defined can be confusing, given that it has such strong pre-requisites to work correctly.
80     ~ThreadSpecific();
81     
82     T* get();
83     void set(T*);
84     void static destroy(void* ptr);
85
86 #if USE(PTHREADS) || PLATFORM(QT) || PLATFORM(GTK) || OS(WINDOWS)
87     struct Data {
88         WTF_MAKE_NONCOPYABLE(Data);
89     public:
90         Data(T* value, ThreadSpecific<T>* owner) : value(value), owner(owner) {}
91
92         T* value;
93         ThreadSpecific<T>* owner;
94 #if OS(WINDOWS)
95         void (*destructor)(void*);
96 #endif
97     };
98 #endif
99
100 #if USE(PTHREADS)
101     pthread_key_t m_key;
102 #elif PLATFORM(GTK)
103     GStaticPrivate m_key;
104 #elif OS(WINDOWS)
105     int m_index;
106 #endif
107 };
108
109 #if USE(PTHREADS)
110 template<typename T>
111 inline ThreadSpecific<T>::ThreadSpecific()
112 {
113     int error = pthread_key_create(&m_key, destroy);
114     if (error)
115         CRASH();
116 }
117
118 template<typename T>
119 inline T* ThreadSpecific<T>::get()
120 {
121     Data* data = static_cast<Data*>(pthread_getspecific(m_key));
122     return data ? data->value : 0;
123 }
124
125 template<typename T>
126 inline void ThreadSpecific<T>::set(T* ptr)
127 {
128     ASSERT(!get());
129     pthread_setspecific(m_key, new Data(ptr, this));
130 }
131
132 #elif PLATFORM(GTK)
133
134 template<typename T>
135 inline ThreadSpecific<T>::ThreadSpecific()
136 {
137     g_static_private_init(&m_key);
138 }
139
140 template<typename T>
141 inline T* ThreadSpecific<T>::get()
142 {
143     Data* data = static_cast<Data*>(g_static_private_get(&m_key));
144     return data ? data->value : 0;
145 }
146
147 template<typename T>
148 inline void ThreadSpecific<T>::set(T* ptr)
149 {
150     ASSERT(!get());
151     Data* data = new Data(ptr, this);
152     g_static_private_set(&m_key, data, destroy);
153 }
154
155 #elif OS(WINDOWS)
156
157 // TLS_OUT_OF_INDEXES is not defined on WinCE.
158 #ifndef TLS_OUT_OF_INDEXES
159 #define TLS_OUT_OF_INDEXES 0xffffffff
160 #endif
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.
165 const int kMaxTlsKeySize = 256;
166
167 long& tlsKeyCount();
168 DWORD* tlsKeys();
169
170 template<typename T>
171 inline ThreadSpecific<T>::ThreadSpecific()
172     : m_index(-1)
173 {
174     DWORD tlsKey = TlsAlloc();
175     if (tlsKey == TLS_OUT_OF_INDEXES)
176         CRASH();
177
178     m_index = InterlockedIncrement(&tlsKeyCount()) - 1;
179     if (m_index >= kMaxTlsKeySize)
180         CRASH();
181     tlsKeys()[m_index] = tlsKey;
182 }
183
184 template<typename T>
185 inline 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
191 template<typename T>
192 inline T* ThreadSpecific<T>::get()
193 {
194     Data* data = static_cast<Data*>(TlsGetValue(tlsKeys()[m_index]));
195     return data ? data->value : 0;
196 }
197
198 template<typename T>
199 inline 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
211 template<typename T>
212 inline 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 #elif PLATFORM(GTK)
221     // See comment as above
222     g_static_private_set(&data->owner->m_key, data, 0);
223 #endif
224
225     data->value->~T();
226     fastFree(data->value);
227
228 #if USE(PTHREADS)
229     pthread_setspecific(data->owner->m_key, 0);
230 #elif PLATFORM(GTK)
231     g_static_private_set(&data->owner->m_key, 0, 0);
232 #elif OS(WINDOWS)
233     TlsSetValue(tlsKeys()[data->owner->m_index], 0);
234 #else
235 #error ThreadSpecific is not implemented for this platform.
236 #endif
237
238     delete data;
239 }
240
241 template<typename T>
242 inline bool ThreadSpecific<T>::isSet()
243 {
244     return !!get();
245 }
246
247 template<typename T>
248 inline ThreadSpecific<T>::operator T*()
249 {
250     T* ptr = static_cast<T*>(get());
251     if (!ptr) {
252         // Set up thread-specific value's memory pointer before invoking constructor, in case any function it calls
253         // needs to access the value, to avoid recursion.
254         ptr = static_cast<T*>(fastZeroedMalloc(sizeof(T)));
255         set(ptr);
256         new (ptr) T;
257     }
258     return ptr;
259 }
260
261 template<typename T>
262 inline T* ThreadSpecific<T>::operator->()
263 {
264     return operator T*();
265 }
266
267 template<typename T>
268 inline T& ThreadSpecific<T>::operator*()
269 {
270     return *operator T*();
271 }
272
273 }
274
275 #endif