2013-07-24 11:41:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-10 00:54:33 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-07-24 11:41:39 +04:00
|
|
|
* 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/. */
|
2012-05-10 00:54:33 +04:00
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
/* Cross-platform lightweight thread local data wrappers. */
|
2012-05-10 00:54:33 +04:00
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#ifndef mozilla_ThreadLocal_h
|
|
|
|
#define mozilla_ThreadLocal_h
|
2012-05-10 00:54:33 +04:00
|
|
|
|
2017-03-22 21:04:21 +03:00
|
|
|
#if !defined(XP_WIN)
|
2012-05-10 00:54:33 +04:00
|
|
|
# include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
2020-03-28 16:57:14 +03:00
|
|
|
#include <type_traits>
|
|
|
|
|
2012-05-10 00:54:33 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-11-23 22:11:22 +03:00
|
|
|
namespace detail {
|
|
|
|
|
2017-04-22 03:15:23 +03:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
# if defined(__has_feature)
|
|
|
|
# if __has_feature(cxx_thread_local)
|
|
|
|
# define MACOSX_HAS_THREAD_LOCAL
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2012-05-10 00:54:33 +04:00
|
|
|
/*
|
|
|
|
* Thread Local Storage helpers.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
*
|
2015-11-23 22:11:22 +03:00
|
|
|
* 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;
|
|
|
|
*
|
2012-05-10 00:54:33 +04:00
|
|
|
* 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:
|
|
|
|
*
|
2013-12-12 05:51:57 +04:00
|
|
|
* // Create a TLS item.
|
|
|
|
* //
|
2015-11-23 22:11:22 +03:00
|
|
|
* // 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;
|
2012-05-10 00:54:33 +04:00
|
|
|
* if (!tlsKey.init()) {
|
|
|
|
* // deal with the error
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // Set the TLS value
|
|
|
|
* tlsKey.set(123);
|
|
|
|
*
|
|
|
|
* // Get the TLS value
|
|
|
|
* int value = tlsKey.get();
|
|
|
|
*/
|
2017-09-12 10:04:17 +03:00
|
|
|
|
|
|
|
// 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 <typename S>
|
|
|
|
struct Helper {
|
|
|
|
typedef uintptr_t Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename S>
|
|
|
|
struct Helper<S*> {
|
|
|
|
typedef S* Type;
|
|
|
|
};
|
|
|
|
|
2017-09-27 23:09:23 +03:00
|
|
|
#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)
|
2017-09-12 10:04:17 +03:00
|
|
|
/* 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 */
|
2012-06-04 07:36:43 +04:00
|
|
|
template <typename T>
|
2017-09-12 09:37:31 +03:00
|
|
|
class ThreadLocalKeyStorage {
|
2017-09-12 10:04:17 +03:00
|
|
|
public:
|
|
|
|
ThreadLocalKeyStorage() : mKey(TLS_OUT_OF_INDEXES) {}
|
|
|
|
|
|
|
|
inline bool initialized() const { return mKey != TLS_OUT_OF_INDEXES; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-09-12 10:04:17 +03:00
|
|
|
inline void init() { mKey = TlsAlloc(); }
|
|
|
|
|
|
|
|
inline T get() const {
|
|
|
|
void* h = TlsGetValue(mKey);
|
|
|
|
return static_cast<T>(reinterpret_cast<typename Helper<T>::Type>(h));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool set(const T aValue) {
|
2020-09-30 11:24:18 +03:00
|
|
|
void* h = const_cast<void*>(reinterpret_cast<const void*>(
|
|
|
|
static_cast<typename Helper<T>::Type>(aValue)));
|
2017-09-12 10:04:17 +03:00
|
|
|
return TlsSetValue(mKey, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned long mKey;
|
|
|
|
};
|
2017-09-27 23:09:23 +03:00
|
|
|
# endif
|
2017-09-12 10:04:17 +03:00
|
|
|
#else
|
|
|
|
template <typename T>
|
|
|
|
class ThreadLocalKeyStorage {
|
2014-07-11 06:10:17 +04:00
|
|
|
public:
|
2017-11-15 09:33:00 +03:00
|
|
|
constexpr ThreadLocalKeyStorage() : mKey(0), mInited(false) {}
|
2017-09-12 09:37:31 +03:00
|
|
|
|
|
|
|
inline bool initialized() const { return mInited; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-09-12 09:37:31 +03:00
|
|
|
inline void init() { mInited = !pthread_key_create(&mKey, nullptr); }
|
|
|
|
|
|
|
|
inline T get() const {
|
|
|
|
void* h = pthread_getspecific(mKey);
|
|
|
|
return static_cast<T>(reinterpret_cast<typename Helper<T>::Type>(h));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool set(const T aValue) {
|
2020-09-30 11:24:18 +03:00
|
|
|
const void* h = reinterpret_cast<const void*>(
|
|
|
|
static_cast<typename Helper<T>::Type>(aValue));
|
2017-09-12 09:37:31 +03:00
|
|
|
return !pthread_setspecific(mKey, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
pthread_key_t mKey;
|
|
|
|
bool mInited;
|
|
|
|
};
|
2017-07-29 00:56:49 +03:00
|
|
|
#endif
|
2017-09-12 09:37:31 +03:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
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() {}
|
2017-07-29 00:56:49 +03:00
|
|
|
|
2017-09-12 09:37:31 +03:00
|
|
|
inline T get() const { return mValue; }
|
|
|
|
|
|
|
|
inline bool set(const T aValue) {
|
|
|
|
mValue = aValue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T mValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, template <typename U> class Storage>
|
|
|
|
class ThreadLocal : public Storage<T> {
|
|
|
|
public:
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] inline bool init();
|
2017-09-12 09:37:31 +03:00
|
|
|
|
2017-08-22 01:18:58 +03:00
|
|
|
void infallibleInit() {
|
|
|
|
MOZ_RELEASE_ASSERT(init(), "Infallible TLS initialization failed");
|
|
|
|
}
|
2012-05-10 00:54:33 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
inline T get() const;
|
2012-05-10 00:54:33 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
inline void set(const T aValue);
|
2019-03-19 18:11:31 +03:00
|
|
|
|
|
|
|
using Type = T;
|
2012-05-10 00:54:33 +04:00
|
|
|
};
|
|
|
|
|
2017-09-12 09:37:31 +03:00
|
|
|
template <typename T, template <typename U> class Storage>
|
|
|
|
inline bool ThreadLocal<T, Storage>::init() {
|
2020-03-28 16:57:15 +03:00
|
|
|
static_assert(std::is_pointer_v<T> || std::is_integral_v<T>,
|
2014-11-07 16:41:00 +03:00
|
|
|
"mozilla::ThreadLocal must be used with a pointer or "
|
|
|
|
"integral type");
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(sizeof(T) <= sizeof(void*),
|
|
|
|
"mozilla::ThreadLocal can't be used for types larger than "
|
|
|
|
"a pointer");
|
2015-11-23 22:11:22 +03:00
|
|
|
|
2017-09-12 09:37:31 +03:00
|
|
|
if (!Storage<T>::initialized()) {
|
|
|
|
Storage<T>::init();
|
2015-11-23 22:11:22 +03:00
|
|
|
}
|
2017-09-12 09:37:31 +03:00
|
|
|
return Storage<T>::initialized();
|
2012-05-10 00:54:33 +04:00
|
|
|
}
|
|
|
|
|
2017-09-12 09:37:31 +03:00
|
|
|
template <typename T, template <typename U> class Storage>
|
|
|
|
inline T ThreadLocal<T, Storage>::get() const {
|
|
|
|
MOZ_ASSERT(Storage<T>::initialized());
|
|
|
|
return Storage<T>::get();
|
2012-05-10 00:54:33 +04:00
|
|
|
}
|
|
|
|
|
2017-09-12 09:37:31 +03:00
|
|
|
template <typename T, template <typename U> class Storage>
|
|
|
|
inline void ThreadLocal<T, Storage>::set(const T aValue) {
|
|
|
|
MOZ_ASSERT(Storage<T>::initialized());
|
|
|
|
bool succeeded = Storage<T>::set(aValue);
|
2014-07-11 06:10:17 +04:00
|
|
|
if (!succeeded) {
|
2012-12-18 00:40:50 +04:00
|
|
|
MOZ_CRASH();
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2012-05-10 00:54:33 +04:00
|
|
|
}
|
|
|
|
|
2018-05-10 19:11:19 +03:00
|
|
|
#if (defined(XP_WIN) || defined(MACOSX_HAS_THREAD_LOCAL)) && \
|
|
|
|
!defined(__MINGW32__)
|
2017-09-12 09:37:31 +03:00
|
|
|
# define MOZ_THREAD_LOCAL(TYPE) \
|
2019-01-31 01:26:27 +03:00
|
|
|
thread_local ::mozilla::detail::ThreadLocal< \
|
|
|
|
TYPE, ::mozilla::detail::ThreadLocalNativeStorage>
|
2017-09-12 09:37:31 +03:00
|
|
|
#elif defined(HAVE_THREAD_TLS_KEYWORD)
|
|
|
|
# define MOZ_THREAD_LOCAL(TYPE) \
|
2019-01-31 01:26:27 +03:00
|
|
|
__thread ::mozilla::detail::ThreadLocal< \
|
|
|
|
TYPE, ::mozilla::detail::ThreadLocalNativeStorage>
|
2015-11-23 22:11:22 +03:00
|
|
|
#else
|
2017-09-12 09:37:31 +03:00
|
|
|
# define MOZ_THREAD_LOCAL(TYPE) \
|
2019-01-31 01:26:27 +03:00
|
|
|
::mozilla::detail::ThreadLocal<TYPE, \
|
|
|
|
::mozilla::detail::ThreadLocalKeyStorage>
|
2015-11-23 22:11:22 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace detail
|
2012-05-10 00:54:33 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#endif /* mozilla_ThreadLocal_h */
|