]> git.saurik.com Git - apple/javascriptcore.git/blob - wtf/ThreadSpecific.h
JavaScriptCore-521.tar.gz
[apple/javascriptcore.git] / 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(WIN_OS)
49 #include <windows.h>
50 #endif
51
52 namespace WTF {
53
54 #if !USE(PTHREADS) && PLATFORM(WIN_OS)
55 // ThreadSpecificThreadExit should be called each time when a thread is detached.
56 // This is done automatically for threads created with WTF::createThread.
57 void ThreadSpecificThreadExit();
58 #endif
59
60 template<typename T> class ThreadSpecific : Noncopyable {
61 public:
62 ThreadSpecific();
63 T* operator->();
64 operator T*();
65 T& operator*();
66 ~ThreadSpecific();
67
68 private:
69 #if !USE(PTHREADS) && PLATFORM(WIN_OS)
70 friend void ThreadSpecificThreadExit();
71 #endif
72
73 T* get();
74 void set(T*);
75 void static destroy(void* ptr);
76
77 #if USE(PTHREADS) || PLATFORM(WIN_OS)
78 struct Data : Noncopyable {
79 Data(T* value, ThreadSpecific<T>* owner) : value(value), owner(owner) {}
80
81 T* value;
82 ThreadSpecific<T>* owner;
83 #if !USE(PTHREADS)
84 void (*destructor)(void*);
85 #endif
86 };
87 #endif
88
89 #if USE(PTHREADS)
90 pthread_key_t m_key;
91 #elif PLATFORM(WIN_OS)
92 int m_index;
93 #endif
94 };
95
96 #if USE(PTHREADS)
97 template<typename T>
98 inline ThreadSpecific<T>::ThreadSpecific()
99 {
100 int error = pthread_key_create(&m_key, destroy);
101 if (error)
102 CRASH();
103 }
104
105 template<typename T>
106 inline ThreadSpecific<T>::~ThreadSpecific()
107 {
108 pthread_key_delete(m_key); // Does not invoke destructor functions.
109 }
110
111 template<typename T>
112 inline T* ThreadSpecific<T>::get()
113 {
114 Data* data = static_cast<Data*>(pthread_getspecific(m_key));
115 return data ? data->value : 0;
116 }
117
118 template<typename T>
119 inline void ThreadSpecific<T>::set(T* ptr)
120 {
121 ASSERT(!get());
122 pthread_setspecific(m_key, new Data(ptr, this));
123 }
124
125 #elif PLATFORM(WIN_OS)
126
127 // The maximum number of TLS keys that can be created. For simplification, we assume that:
128 // 1) Once the instance of ThreadSpecific<> is created, it will not be destructed until the program dies.
129 // 2) We do not need to hold many instances of ThreadSpecific<> data. This fixed number should be far enough.
130 const int kMaxTlsKeySize = 256;
131
132 extern long g_tls_key_count;
133 extern DWORD g_tls_keys[kMaxTlsKeySize];
134
135 template<typename T>
136 inline ThreadSpecific<T>::ThreadSpecific()
137 : m_index(-1)
138 {
139 DWORD tls_key = TlsAlloc();
140 if (tls_key == TLS_OUT_OF_INDEXES)
141 CRASH();
142
143 m_index = InterlockedIncrement(&g_tls_key_count) - 1;
144 if (m_index >= kMaxTlsKeySize)
145 CRASH();
146 g_tls_keys[m_index] = tls_key;
147 }
148
149 template<typename T>
150 inline ThreadSpecific<T>::~ThreadSpecific()
151 {
152 // Does not invoke destructor functions. They will be called from ThreadSpecificThreadExit when the thread is detached.
153 TlsFree(g_tls_keys[m_index]);
154 }
155
156 template<typename T>
157 inline T* ThreadSpecific<T>::get()
158 {
159 Data* data = static_cast<Data*>(TlsGetValue(g_tls_keys[m_index]));
160 return data ? data->value : 0;
161 }
162
163 template<typename T>
164 inline void ThreadSpecific<T>::set(T* ptr)
165 {
166 ASSERT(!get());
167 Data* data = new Data(ptr, this);
168 data->destructor = &ThreadSpecific<T>::destroy;
169 TlsSetValue(g_tls_keys[m_index], data);
170 }
171
172 #else
173 #error ThreadSpecific is not implemented for this platform.
174 #endif
175
176 template<typename T>
177 inline void ThreadSpecific<T>::destroy(void* ptr)
178 {
179 Data* data = static_cast<Data*>(ptr);
180
181 #if USE(PTHREADS)
182 // We want get() to keep working while data destructor works, because it can be called indirectly by the destructor.
183 // Some pthreads implementations zero out the pointer before calling destroy(), so we temporarily reset it.
184 pthread_setspecific(data->owner->m_key, ptr);
185 #endif
186
187 data->value->~T();
188 fastFree(data->value);
189
190 #if USE(PTHREADS)
191 pthread_setspecific(data->owner->m_key, 0);
192 #elif PLATFORM(WIN_OS)
193 TlsSetValue(g_tls_keys[data->owner->m_index], 0);
194 #else
195 #error ThreadSpecific is not implemented for this platform.
196 #endif
197
198 delete data;
199 }
200
201 template<typename T>
202 inline ThreadSpecific<T>::operator T*()
203 {
204 T* ptr = static_cast<T*>(get());
205 if (!ptr) {
206 // Set up thread-specific value's memory pointer before invoking constructor, in case any function it calls
207 // needs to access the value, to avoid recursion.
208 ptr = static_cast<T*>(fastMalloc(sizeof(T)));
209 set(ptr);
210 new (ptr) T;
211 }
212 return ptr;
213 }
214
215 template<typename T>
216 inline T* ThreadSpecific<T>::operator->()
217 {
218 return operator T*();
219 }
220
221 template<typename T>
222 inline T& ThreadSpecific<T>::operator*()
223 {
224 return *operator T*();
225 }
226
227 }
228
229 #endif