1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Cross-platform lightweight thread local data wrappers. */
9 #ifndef mozilla_ThreadLocal_h
10 #define mozilla_ThreadLocal_h
16 #include "mozilla/Assertions.h"
17 #include "mozilla/Attributes.h"
18 #include "mozilla/TypeTraits.h"
25 #if defined(__has_feature)
26 #if __has_feature(cxx_thread_local)
27 #define MACOSX_HAS_THREAD_LOCAL
33 * Thread Local Storage helpers.
37 * Do not directly instantiate this class. Instead, use the
38 * MOZ_THREAD_LOCAL macro to declare or define instances. The macro
39 * takes a type name as its argument.
42 * extern MOZ_THREAD_LOCAL(int) tlsInt;
44 * MOZ_THREAD_LOCAL(int) tlsInt;
46 * static MOZ_THREAD_LOCAL(int) tlsInt;
48 * Only static-storage-duration (e.g. global variables, or static class members)
49 * objects of this class should be instantiated. This class relies on
50 * zero-initialization, which is implicit for static-storage-duration objects.
51 * It doesn't have a custom default constructor, to avoid static initializers.
55 * // Create a TLS item.
57 * // Note that init() should be invoked before the first use of set()
58 * // or get(). It is ok to call it multiple times. This must be
59 * // called in a way that avoids possible races with other threads.
60 * MOZ_THREAD_LOCAL(int) tlsKey;
61 * if (!tlsKey.init()) {
62 * // deal with the error
65 * // Set the TLS value
68 * // Get the TLS value
69 * int value = tlsKey.get();
72 // Integral types narrower than void* must be extended to avoid
73 // warnings from valgrind on some platforms. This helper type
74 // achieves that without penalizing the common case of ThreadLocals
75 // instantiated using a pointer type.
78 typedef uintptr_t Type
;
88 * ThreadLocalKeyStorage uses Thread Local APIs that are declared in
89 * processthreadsapi.h. To use this class on Windows, include that file
90 * (or windows.h) before including ThreadLocal.h.
92 * TLS_OUT_OF_INDEXES is a #define that is used to detect whether
93 * an appropriate header has been included prior to this file
95 #if defined(TLS_OUT_OF_INDEXES)
96 /* Despite not being used for MOZ_THREAD_LOCAL, we expose an implementation for
97 * Windows for cases where it's not desirable to use thread_local */
99 class ThreadLocalKeyStorage
{
101 ThreadLocalKeyStorage() : mKey(TLS_OUT_OF_INDEXES
) {}
103 inline bool initialized() const { return mKey
!= TLS_OUT_OF_INDEXES
; }
105 inline void init() { mKey
= TlsAlloc(); }
107 inline T
get() const {
108 void* h
= TlsGetValue(mKey
);
109 return static_cast<T
>(reinterpret_cast<typename Helper
<T
>::Type
>(h
));
112 inline bool set(const T aValue
) {
114 reinterpret_cast<void*>(static_cast<typename Helper
<T
>::Type
>(aValue
));
115 return TlsSetValue(mKey
, h
);
123 template <typename T
>
124 class ThreadLocalKeyStorage
{
126 constexpr ThreadLocalKeyStorage() : mKey(0), mInited(false) {}
128 inline bool initialized() const { return mInited
; }
130 inline void init() { mInited
= !pthread_key_create(&mKey
, nullptr); }
132 inline T
get() const {
133 void* h
= pthread_getspecific(mKey
);
134 return static_cast<T
>(reinterpret_cast<typename Helper
<T
>::Type
>(h
));
137 inline bool set(const T aValue
) {
139 reinterpret_cast<void*>(static_cast<typename Helper
<T
>::Type
>(aValue
));
140 return !pthread_setspecific(mKey
, h
);
149 template <typename T
>
150 class ThreadLocalNativeStorage
{
152 // __thread does not allow non-trivial constructors, but we can
153 // instead rely on zero-initialization.
154 inline bool initialized() const { return true; }
156 inline void init() {}
158 inline T
get() const { return mValue
; }
160 inline bool set(const T aValue
) {
169 template <typename T
, template <typename U
> class Storage
>
170 class ThreadLocal
: public Storage
<T
> {
172 MOZ_MUST_USE
inline bool init();
174 void infallibleInit() {
175 MOZ_RELEASE_ASSERT(init(), "Infallible TLS initialization failed");
178 inline T
get() const;
180 inline void set(const T aValue
);
183 template <typename T
, template <typename U
> class Storage
>
184 inline bool ThreadLocal
<T
, Storage
>::init() {
185 static_assert(mozilla::IsPointer
<T
>::value
|| mozilla::IsIntegral
<T
>::value
,
186 "mozilla::ThreadLocal must be used with a pointer or "
188 static_assert(sizeof(T
) <= sizeof(void*),
189 "mozilla::ThreadLocal can't be used for types larger than "
192 if (!Storage
<T
>::initialized()) {
195 return Storage
<T
>::initialized();
198 template <typename T
, template <typename U
> class Storage
>
199 inline T ThreadLocal
<T
, Storage
>::get() const {
200 MOZ_ASSERT(Storage
<T
>::initialized());
201 return Storage
<T
>::get();
204 template <typename T
, template <typename U
> class Storage
>
205 inline void ThreadLocal
<T
, Storage
>::set(const T aValue
) {
206 MOZ_ASSERT(Storage
<T
>::initialized());
207 bool succeeded
= Storage
<T
>::set(aValue
);
213 #if (defined(XP_WIN) || defined(MACOSX_HAS_THREAD_LOCAL)) && \
214 !defined(__MINGW32__)
215 #define MOZ_THREAD_LOCAL(TYPE) \
216 thread_local mozilla::detail::ThreadLocal< \
217 TYPE, mozilla::detail::ThreadLocalNativeStorage>
218 #elif defined(HAVE_THREAD_TLS_KEYWORD)
219 #define MOZ_THREAD_LOCAL(TYPE) \
220 __thread mozilla::detail::ThreadLocal< \
221 TYPE, mozilla::detail::ThreadLocalNativeStorage>
223 #define MOZ_THREAD_LOCAL(TYPE) \
224 mozilla::detail::ThreadLocal<TYPE, mozilla::detail::ThreadLocalKeyStorage>
227 } // namespace detail
228 } // namespace mozilla
230 #endif /* mozilla_ThreadLocal_h */