/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /* Cross-platform lightweight thread local data wrappers. */ #ifndef mozilla_ThreadLocal_h #define mozilla_ThreadLocal_h #if !defined(XP_WIN) # include #endif #include "mozilla/Assertions.h" #include "mozilla/Attributes.h" #include "mozilla/TypeTraits.h" namespace mozilla { namespace detail { #ifdef XP_MACOSX # if defined(__has_feature) # if __has_feature(cxx_thread_local) # define MACOSX_HAS_THREAD_LOCAL # endif # endif #endif /* * Thread Local Storage helpers. * * Usage: * * Do not directly instantiate this class. Instead, use the * MOZ_THREAD_LOCAL macro to declare or define instances. The macro * takes a type name as its argument. * * Declare like this: * extern MOZ_THREAD_LOCAL(int) tlsInt; * Define like this: * MOZ_THREAD_LOCAL(int) tlsInt; * or: * static MOZ_THREAD_LOCAL(int) tlsInt; * * Only static-storage-duration (e.g. global variables, or static class members) * objects of this class should be instantiated. This class relies on * zero-initialization, which is implicit for static-storage-duration objects. * It doesn't have a custom default constructor, to avoid static initializers. * * API usage: * * // Create a TLS item. * // * // Note that init() should be invoked before the first use of set() * // or get(). It is ok to call it multiple times. This must be * // called in a way that avoids possible races with other threads. * MOZ_THREAD_LOCAL(int) tlsKey; * if (!tlsKey.init()) { * // deal with the error * } * * // Set the TLS value * tlsKey.set(123); * * // Get the TLS value * int value = tlsKey.get(); */ // Integral types narrower than void* must be extended to avoid // warnings from valgrind on some platforms. This helper type // achieves that without penalizing the common case of ThreadLocals // instantiated using a pointer type. template struct Helper { typedef uintptr_t Type; }; template struct Helper { typedef S *Type; }; #if defined(XP_WIN) /* * ThreadLocalKeyStorage uses Thread Local APIs that are declared in * processthreadsapi.h. To use this class on Windows, include that file * (or windows.h) before including ThreadLocal.h. * * TLS_OUT_OF_INDEXES is a #define that is used to detect whether * an appropriate header has been included prior to this file */ #if defined(TLS_OUT_OF_INDEXES) /* Despite not being used for MOZ_THREAD_LOCAL, we expose an implementation for * Windows for cases where it's not desirable to use thread_local */ template class ThreadLocalKeyStorage { public: ThreadLocalKeyStorage() : mKey(TLS_OUT_OF_INDEXES) {} inline bool initialized() const { return mKey != TLS_OUT_OF_INDEXES; } inline void init() { mKey = TlsAlloc(); } inline T get() const { void* h = TlsGetValue(mKey); return static_cast(reinterpret_cast::Type>(h)); } inline bool set(const T aValue) { void* h = reinterpret_cast(static_cast::Type>(aValue)); return TlsSetValue(mKey, h); } private: unsigned long mKey; }; #endif #else template class ThreadLocalKeyStorage { public: ThreadLocalKeyStorage() : mKey(0), mInited(false) {} inline bool initialized() const { return mInited; } inline void init() { mInited = !pthread_key_create(&mKey, nullptr); } inline T get() const { void* h = pthread_getspecific(mKey); return static_cast(reinterpret_cast::Type>(h)); } inline bool set(const T aValue) { void* h = reinterpret_cast(static_cast::Type>(aValue)); return !pthread_setspecific(mKey, h); } private: pthread_key_t mKey; bool mInited; }; #endif template class ThreadLocalNativeStorage { public: // __thread does not allow non-trivial constructors, but we can // instead rely on zero-initialization. inline bool initialized() const { return true; } inline void init() { } inline T get() const { return mValue; } inline bool set(const T aValue) { mValue = aValue; return true; } private: T mValue; }; template class Storage> class ThreadLocal: public Storage { public: MOZ_MUST_USE inline bool init(); void infallibleInit() { MOZ_RELEASE_ASSERT(init(), "Infallible TLS initialization failed"); } inline T get() const; inline void set(const T aValue); }; template class Storage> inline bool ThreadLocal::init() { static_assert(mozilla::IsPointer::value || mozilla::IsIntegral::value, "mozilla::ThreadLocal must be used with a pointer or " "integral type"); static_assert(sizeof(T) <= sizeof(void*), "mozilla::ThreadLocal can't be used for types larger than " "a pointer"); if (!Storage::initialized()) { Storage::init(); } return Storage::initialized(); } template class Storage> inline T ThreadLocal::get() const { MOZ_ASSERT(Storage::initialized()); return Storage::get(); } template class Storage> inline void ThreadLocal::set(const T aValue) { MOZ_ASSERT(Storage::initialized()); bool succeeded = Storage::set(aValue); if (!succeeded) { MOZ_CRASH(); } } #if defined(XP_WIN) || defined(MACOSX_HAS_THREAD_LOCAL) #define MOZ_THREAD_LOCAL(TYPE) thread_local mozilla::detail::ThreadLocal #elif defined(HAVE_THREAD_TLS_KEYWORD) #define MOZ_THREAD_LOCAL(TYPE) __thread mozilla::detail::ThreadLocal #else #define MOZ_THREAD_LOCAL(TYPE) mozilla::detail::ThreadLocal #endif } // namespace detail } // namespace mozilla #endif /* mozilla_ThreadLocal_h */