/src/3rdparty/javascriptcore/JavaScriptCore/wtf/ThreadSpecific.h

https://bitbucket.org/ultra_iter/qt-vtl · C Header · 310 lines · 214 code · 44 blank · 52 comment · 15 complexity · 2b63dd2d32075f3195829bf645c0bd2d MD5 · raw file

  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. /* Thread local storage is implemented by using either pthread API or Windows
  30. * native API. There is subtle semantic discrepancy for the cleanup function
  31. * implementation as noted below:
  32. * @ In pthread implementation, the destructor function will be called
  33. * repeatedly if there is still non-NULL value associated with the function.
  34. * @ In Windows native implementation, the destructor function will be called
  35. * only once.
  36. * This semantic discrepancy does not impose any problem because nowhere in
  37. * WebKit the repeated call bahavior is utilized.
  38. */
  39. #ifndef WTF_ThreadSpecific_h
  40. #define WTF_ThreadSpecific_h
  41. #include <wtf/Noncopyable.h>
  42. #if USE(PTHREADS)
  43. #include <pthread.h>
  44. #elif PLATFORM(QT)
  45. #include <QThreadStorage>
  46. #elif OS(WINDOWS)
  47. #include <windows.h>
  48. #endif
  49. namespace WTF {
  50. #if !USE(PTHREADS) && !PLATFORM(QT) && OS(WINDOWS)
  51. // ThreadSpecificThreadExit should be called each time when a thread is detached.
  52. // This is done automatically for threads created with WTF::createThread.
  53. void ThreadSpecificThreadExit();
  54. #endif
  55. template<typename T> class ThreadSpecific : public Noncopyable {
  56. public:
  57. ThreadSpecific();
  58. T* operator->();
  59. operator T*();
  60. T& operator*();
  61. ~ThreadSpecific();
  62. private:
  63. #if !USE(PTHREADS) && !PLATFORM(QT) && OS(WINDOWS)
  64. friend void ThreadSpecificThreadExit();
  65. #endif
  66. T* get();
  67. void set(T*);
  68. void static destroy(void* ptr);
  69. #if USE(PTHREADS) || PLATFORM(QT) || OS(WINDOWS)
  70. struct Data : Noncopyable {
  71. Data(T* value, ThreadSpecific<T>* owner) : value(value), owner(owner) {}
  72. #if PLATFORM(QT)
  73. ~Data() { owner->destroy(this); }
  74. #endif
  75. T* value;
  76. ThreadSpecific<T>* owner;
  77. #if !USE(PTHREADS) && !PLATFORM(QT)
  78. void (*destructor)(void*);
  79. #endif
  80. };
  81. #endif
  82. #if ENABLE(SINGLE_THREADED)
  83. T* m_value;
  84. #else
  85. #if USE(PTHREADS)
  86. pthread_key_t m_key;
  87. #elif PLATFORM(QT)
  88. QThreadStorage<Data*> m_key;
  89. #elif OS(WINDOWS)
  90. int m_index;
  91. #endif
  92. #endif
  93. };
  94. #if ENABLE(SINGLE_THREADED)
  95. template<typename T>
  96. inline ThreadSpecific<T>::ThreadSpecific()
  97. : m_value(0)
  98. {
  99. }
  100. template<typename T>
  101. inline ThreadSpecific<T>::~ThreadSpecific()
  102. {
  103. }
  104. template<typename T>
  105. inline T* ThreadSpecific<T>::get()
  106. {
  107. return m_value;
  108. }
  109. template<typename T>
  110. inline void ThreadSpecific<T>::set(T* ptr)
  111. {
  112. ASSERT(!get());
  113. m_value = ptr;
  114. }
  115. #else
  116. #if USE(PTHREADS)
  117. template<typename T>
  118. inline ThreadSpecific<T>::ThreadSpecific()
  119. {
  120. int error = pthread_key_create(&m_key, destroy);
  121. if (error)
  122. CRASH();
  123. }
  124. template<typename T>
  125. inline ThreadSpecific<T>::~ThreadSpecific()
  126. {
  127. pthread_key_delete(m_key); // Does not invoke destructor functions.
  128. }
  129. template<typename T>
  130. inline T* ThreadSpecific<T>::get()
  131. {
  132. Data* data = static_cast<Data*>(pthread_getspecific(m_key));
  133. return data ? data->value : 0;
  134. }
  135. template<typename T>
  136. inline void ThreadSpecific<T>::set(T* ptr)
  137. {
  138. ASSERT(!get());
  139. pthread_setspecific(m_key, new Data(ptr, this));
  140. }
  141. #elif PLATFORM(QT)
  142. template<typename T>
  143. inline ThreadSpecific<T>::ThreadSpecific()
  144. {
  145. }
  146. template<typename T>
  147. inline ThreadSpecific<T>::~ThreadSpecific()
  148. {
  149. // Does not invoke destructor functions. QThreadStorage will do it
  150. }
  151. template<typename T>
  152. inline T* ThreadSpecific<T>::get()
  153. {
  154. Data* data = static_cast<Data*>(m_key.localData());
  155. return data ? data->value : 0;
  156. }
  157. template<typename T>
  158. inline void ThreadSpecific<T>::set(T* ptr)
  159. {
  160. ASSERT(!get());
  161. Data* data = new Data(ptr, this);
  162. m_key.setLocalData(data);
  163. }
  164. #elif OS(WINDOWS)
  165. // TLS_OUT_OF_INDEXES is not defined on WinCE.
  166. #ifndef TLS_OUT_OF_INDEXES
  167. #define TLS_OUT_OF_INDEXES 0xffffffff
  168. #endif
  169. // The maximum number of TLS keys that can be created. For simplification, we assume that:
  170. // 1) Once the instance of ThreadSpecific<> is created, it will not be destructed until the program dies.
  171. // 2) We do not need to hold many instances of ThreadSpecific<> data. This fixed number should be far enough.
  172. const int kMaxTlsKeySize = 256;
  173. long& tlsKeyCount();
  174. DWORD* tlsKeys();
  175. template<typename T>
  176. inline ThreadSpecific<T>::ThreadSpecific()
  177. : m_index(-1)
  178. {
  179. DWORD tlsKey = TlsAlloc();
  180. if (tlsKey == TLS_OUT_OF_INDEXES)
  181. CRASH();
  182. m_index = InterlockedIncrement(&tlsKeyCount()) - 1;
  183. if (m_index >= kMaxTlsKeySize)
  184. CRASH();
  185. tlsKeys()[m_index] = tlsKey;
  186. }
  187. template<typename T>
  188. inline ThreadSpecific<T>::~ThreadSpecific()
  189. {
  190. // Does not invoke destructor functions. They will be called from ThreadSpecificThreadExit when the thread is detached.
  191. TlsFree(tlsKeys()[m_index]);
  192. }
  193. template<typename T>
  194. inline T* ThreadSpecific<T>::get()
  195. {
  196. Data* data = static_cast<Data*>(TlsGetValue(tlsKeys()[m_index]));
  197. return data ? data->value : 0;
  198. }
  199. template<typename T>
  200. inline void ThreadSpecific<T>::set(T* ptr)
  201. {
  202. ASSERT(!get());
  203. Data* data = new Data(ptr, this);
  204. data->destructor = &ThreadSpecific<T>::destroy;
  205. TlsSetValue(tlsKeys()[m_index], data);
  206. }
  207. #else
  208. #error ThreadSpecific is not implemented for this platform.
  209. #endif
  210. #endif
  211. template<typename T>
  212. inline void ThreadSpecific<T>::destroy(void* ptr)
  213. {
  214. #if !ENABLE(SINGLE_THREADED)
  215. Data* data = static_cast<Data*>(ptr);
  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. #endif
  221. #if PLATFORM(QT)
  222. // See comment as above
  223. if (!data->owner->m_key.hasLocalData())
  224. data->owner->m_key.setLocalData(data);
  225. #endif
  226. data->value->~T();
  227. fastFree(data->value);
  228. #if USE(PTHREADS)
  229. pthread_setspecific(data->owner->m_key, 0);
  230. #elif PLATFORM(QT)
  231. // Do nothing here
  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. #if !PLATFORM(QT)
  238. delete data;
  239. #endif
  240. #endif
  241. }
  242. template<typename T>
  243. inline ThreadSpecific<T>::operator T*()
  244. {
  245. T* ptr = static_cast<T*>(get());
  246. if (!ptr) {
  247. // Set up thread-specific value's memory pointer before invoking constructor, in case any function it calls
  248. // needs to access the value, to avoid recursion.
  249. ptr = static_cast<T*>(fastMalloc(sizeof(T)));
  250. set(ptr);
  251. new (ptr) T;
  252. }
  253. return ptr;
  254. }
  255. template<typename T>
  256. inline T* ThreadSpecific<T>::operator->()
  257. {
  258. return operator T*();
  259. }
  260. template<typename T>
  261. inline T& ThreadSpecific<T>::operator*()
  262. {
  263. return *operator T*();
  264. }
  265. }
  266. #endif