Merge "Fix build break by removing TIZEN_RECORDING_SURFACE_SET" into tizen_2.1
[framework/web/webkit-efl.git] / Source / WTF / 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 #include <wtf/StdLibExtras.h>
46
47 #if USE(PTHREADS)
48 #include <pthread.h>
49 #elif OS(WINDOWS)
50 #include <windows.h>
51 #endif
52
53 namespace WTF {
54
55 #if OS(WINDOWS)
56 // ThreadSpecificThreadExit should be called each time when a thread is detached.
57 // This is done automatically for threads created with WTF::createThread.
58 void ThreadSpecificThreadExit();
59 #endif
60
61 template<typename T> class ThreadSpecific {
62     WTF_MAKE_NONCOPYABLE(ThreadSpecific);
63 public:
64     ThreadSpecific();
65     bool isSet(); // Useful as a fast check to see if this thread has set this value.
66     T* operator->();
67     operator T*();
68     T& operator*();
69
70 private:
71 #if OS(WINDOWS)
72     friend void ThreadSpecificThreadExit();
73 #endif
74
75     // Not implemented. It's technically possible to destroy a thread specific key, but one would need
76     // to make sure that all values have been destroyed already (usually, that all threads that used it
77     // have exited). It's unlikely that any user of this call will be in that situation - and having
78     // a destructor defined can be confusing, given that it has such strong pre-requisites to work correctly.
79     ~ThreadSpecific();
80
81     T* get();
82     void set(T*);
83     void static destroy(void* ptr);
84
85     struct Data {
86         WTF_MAKE_NONCOPYABLE(Data);
87     public:
88         Data(T* value, ThreadSpecific<T>* owner) : value(value), owner(owner) {}
89
90         T* value;
91         ThreadSpecific<T>* owner;
92 #if OS(WINDOWS)
93         void (*destructor)(void*);
94 #endif
95     };
96
97 #if USE(PTHREADS)
98     pthread_key_t m_key;
99 #elif OS(WINDOWS)
100     int m_index;
101 #endif
102 };
103
104 #if USE(PTHREADS)
105 template<typename T>
106 inline ThreadSpecific<T>::ThreadSpecific()
107 {
108     int error = pthread_key_create(&m_key, destroy);
109     if (error)
110         CRASH();
111 }
112
113 template<typename T>
114 inline T* ThreadSpecific<T>::get()
115 {
116     Data* data = static_cast<Data*>(pthread_getspecific(m_key));
117     return data ? data->value : 0;
118 }
119
120 template<typename T>
121 inline void ThreadSpecific<T>::set(T* ptr)
122 {
123     ASSERT(!get());
124     pthread_setspecific(m_key, new Data(ptr, this));
125 }
126
127 #elif OS(WINDOWS)
128
129 // TLS_OUT_OF_INDEXES is not defined on WinCE.
130 #ifndef TLS_OUT_OF_INDEXES
131 #define TLS_OUT_OF_INDEXES 0xffffffff
132 #endif
133
134 // The maximum number of TLS keys that can be created. For simplification, we assume that:
135 // 1) Once the instance of ThreadSpecific<> is created, it will not be destructed until the program dies.
136 // 2) We do not need to hold many instances of ThreadSpecific<> data. This fixed number should be far enough.
137 const int kMaxTlsKeySize = 256;
138
139 WTF_EXPORT_PRIVATE long& tlsKeyCount();
140 WTF_EXPORT_PRIVATE DWORD* tlsKeys();
141
142 template<typename T>
143 inline ThreadSpecific<T>::ThreadSpecific()
144     : m_index(-1)
145 {
146     DWORD tlsKey = TlsAlloc();
147     if (tlsKey == TLS_OUT_OF_INDEXES)
148         CRASH();
149
150     m_index = InterlockedIncrement(&tlsKeyCount()) - 1;
151     if (m_index >= kMaxTlsKeySize)
152         CRASH();
153     tlsKeys()[m_index] = tlsKey;
154 }
155
156 template<typename T>
157 inline ThreadSpecific<T>::~ThreadSpecific()
158 {
159     // Does not invoke destructor functions. They will be called from ThreadSpecificThreadExit when the thread is detached.
160     TlsFree(tlsKeys()[m_index]);
161 }
162
163 template<typename T>
164 inline T* ThreadSpecific<T>::get()
165 {
166     Data* data = static_cast<Data*>(TlsGetValue(tlsKeys()[m_index]));
167     return data ? data->value : 0;
168 }
169
170 template<typename T>
171 inline void ThreadSpecific<T>::set(T* ptr)
172 {
173     ASSERT(!get());
174     Data* data = new Data(ptr, this);
175     data->destructor = &ThreadSpecific<T>::destroy;
176     TlsSetValue(tlsKeys()[m_index], data);
177 }
178
179 #else
180 #error ThreadSpecific is not implemented for this platform.
181 #endif
182
183 template<typename T>
184 inline void ThreadSpecific<T>::destroy(void* ptr)
185 {
186     Data* data = static_cast<Data*>(ptr);
187
188 #if USE(PTHREADS)
189     // We want get() to keep working while data destructor works, because it can be called indirectly by the destructor.
190     // Some pthreads implementations zero out the pointer before calling destroy(), so we temporarily reset it.
191     pthread_setspecific(data->owner->m_key, ptr);
192 #endif
193
194     data->value->~T();
195     fastFree(data->value);
196
197 #if USE(PTHREADS)
198     pthread_setspecific(data->owner->m_key, 0);
199 #elif OS(WINDOWS)
200     TlsSetValue(tlsKeys()[data->owner->m_index], 0);
201 #else
202 #error ThreadSpecific is not implemented for this platform.
203 #endif
204
205     delete data;
206 }
207
208 template<typename T>
209 inline bool ThreadSpecific<T>::isSet()
210 {
211     return !!get();
212 }
213
214 template<typename T>
215 inline ThreadSpecific<T>::operator T*()
216 {
217     T* ptr = static_cast<T*>(get());
218     if (!ptr) {
219         // Set up thread-specific value's memory pointer before invoking constructor, in case any function it calls
220         // needs to access the value, to avoid recursion.
221         ptr = static_cast<T*>(fastZeroedMalloc(sizeof(T)));
222         set(ptr);
223         new (NotNull, ptr) T;
224     }
225     return ptr;
226 }
227
228 template<typename T>
229 inline T* ThreadSpecific<T>::operator->()
230 {
231     return operator T*();
232 }
233
234 template<typename T>
235 inline T& ThreadSpecific<T>::operator*()
236 {
237     return *operator T*();
238 }
239
240 } // namespace WTF
241
242 #endif // WTF_ThreadSpecific_h