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
12 #if !defined(XP_WIN) && !defined(__wasi__)
16 #include <type_traits>
18 #include "mozilla/Assertions.h"
19 #include "mozilla/Attributes.h"
26 # if defined(__has_feature)
27 # if __has_feature(cxx_thread_local)
28 # define MACOSX_HAS_THREAD_LOCAL
34 * Thread Local Storage helpers.
38 * Do not directly instantiate this class. Instead, use the
39 * MOZ_THREAD_LOCAL macro to declare or define instances. The macro
40 * takes a type name as its argument.
43 * extern MOZ_THREAD_LOCAL(int) tlsInt;
45 * MOZ_THREAD_LOCAL(int) tlsInt;
47 * static MOZ_THREAD_LOCAL(int) tlsInt;
49 * Only static-storage-duration (e.g. global variables, or static class members)
50 * objects of this class should be instantiated. This class relies on
51 * zero-initialization, which is implicit for static-storage-duration objects.
52 * It doesn't have a custom default constructor, to avoid static initializers.
56 * // Create a TLS item.
58 * // Note that init() should be invoked before the first use of set()
59 * // or get(). It is ok to call it multiple times. This must be
60 * // called in a way that avoids possible races with other threads.
61 * MOZ_THREAD_LOCAL(int) tlsKey;
62 * if (!tlsKey.init()) {
63 * // deal with the error
66 * // Set the TLS value
69 * // Get the TLS value
70 * int value = tlsKey.get();
73 // Integral types narrower than void* must be extended to avoid
74 // warnings from valgrind on some platforms. This helper type
75 // achieves that without penalizing the common case of ThreadLocals
76 // instantiated using a pointer type.
79 typedef uintptr_t Type
;
89 * ThreadLocalKeyStorage uses Thread Local APIs that are declared in
90 * processthreadsapi.h. To use this class on Windows, include that file
91 * (or windows.h) before including ThreadLocal.h.
93 * TLS_OUT_OF_INDEXES is a #define that is used to detect whether
94 * an appropriate header has been included prior to this file
96 # if defined(TLS_OUT_OF_INDEXES)
97 /* Despite not being used for MOZ_THREAD_LOCAL, we expose an implementation for
98 * Windows for cases where it's not desirable to use thread_local */
100 class ThreadLocalKeyStorage
{
102 ThreadLocalKeyStorage() : mKey(TLS_OUT_OF_INDEXES
) {}
104 inline bool initialized() const { return mKey
!= TLS_OUT_OF_INDEXES
; }
106 inline void init() { mKey
= TlsAlloc(); }
108 inline T
get() const {
109 void* h
= TlsGetValue(mKey
);
110 return static_cast<T
>(reinterpret_cast<typename Helper
<T
>::Type
>(h
));
113 inline bool set(const T aValue
) {
114 void* h
= const_cast<void*>(reinterpret_cast<const void*>(
115 static_cast<typename Helper
<T
>::Type
>(aValue
)));
116 return TlsSetValue(mKey
, h
);
123 #elif defined(__wasi__)
124 // There are no threads on WASI, so we just use a global variable.
125 template <typename T
>
126 class ThreadLocalKeyStorage
{
128 constexpr ThreadLocalKeyStorage() : mInited(false) {}
130 inline bool initialized() const { return mInited
; }
132 inline void init() { mInited
= true; }
134 inline T
get() const { return mVal
; }
136 inline bool set(const T aValue
) {
146 template <typename T
>
147 class ThreadLocalKeyStorage
{
149 constexpr ThreadLocalKeyStorage() : mKey(0), mInited(false) {}
151 inline bool initialized() const { return mInited
; }
153 inline void init() { mInited
= !pthread_key_create(&mKey
, nullptr); }
155 inline T
get() const {
156 void* h
= pthread_getspecific(mKey
);
157 return static_cast<T
>(reinterpret_cast<typename Helper
<T
>::Type
>(h
));
160 inline bool set(const T aValue
) {
161 const void* h
= reinterpret_cast<const void*>(
162 static_cast<typename Helper
<T
>::Type
>(aValue
));
163 return !pthread_setspecific(mKey
, h
);
172 template <typename T
>
173 class ThreadLocalNativeStorage
{
175 // __thread does not allow non-trivial constructors, but we can
176 // instead rely on zero-initialization.
177 inline bool initialized() const { return true; }
179 inline void init() {}
181 inline T
get() const { return mValue
; }
183 inline bool set(const T aValue
) {
192 template <typename T
, template <typename U
> class Storage
>
193 class ThreadLocal
: public Storage
<T
> {
195 [[nodiscard
]] inline bool init();
197 void infallibleInit() {
198 MOZ_RELEASE_ASSERT(init(), "Infallible TLS initialization failed");
201 inline T
get() const;
203 inline void set(const T aValue
);
208 template <typename T
, template <typename U
> class Storage
>
209 inline bool ThreadLocal
<T
, Storage
>::init() {
210 static_assert(std::is_pointer_v
<T
> || std::is_integral_v
<T
>,
211 "mozilla::ThreadLocal must be used with a pointer or "
213 static_assert(sizeof(T
) <= sizeof(void*),
214 "mozilla::ThreadLocal can't be used for types larger than "
217 if (!Storage
<T
>::initialized()) {
220 return Storage
<T
>::initialized();
223 template <typename T
, template <typename U
> class Storage
>
224 inline T ThreadLocal
<T
, Storage
>::get() const {
225 MOZ_ASSERT(Storage
<T
>::initialized());
226 return Storage
<T
>::get();
229 template <typename T
, template <typename U
> class Storage
>
230 inline void ThreadLocal
<T
, Storage
>::set(const T aValue
) {
231 MOZ_ASSERT(Storage
<T
>::initialized());
232 bool succeeded
= Storage
<T
>::set(aValue
);
238 #if (defined(XP_WIN) || defined(MACOSX_HAS_THREAD_LOCAL)) && \
239 !defined(__MINGW32__)
240 # define MOZ_THREAD_LOCAL(TYPE) \
241 thread_local ::mozilla::detail::ThreadLocal< \
242 TYPE, ::mozilla::detail::ThreadLocalNativeStorage>
243 #elif defined(HAVE_THREAD_TLS_KEYWORD)
244 # define MOZ_THREAD_LOCAL(TYPE) \
245 __thread ::mozilla::detail::ThreadLocal< \
246 TYPE, ::mozilla::detail::ThreadLocalNativeStorage>
248 # define MOZ_THREAD_LOCAL(TYPE) \
249 ::mozilla::detail::ThreadLocal<TYPE, \
250 ::mozilla::detail::ThreadLocalKeyStorage>
253 } // namespace detail
254 } // namespace mozilla
256 #endif /* mozilla_ThreadLocal_h */