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
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
2014-11-07 16:41:00 +03:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2012-05-10 00:54:33 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
#if defined(HAVE_THREAD_TLS_KEYWORD) || defined(XP_WIN) || defined(MACOSX_HAS_THREAD_LOCAL)
|
2015-11-23 22:11:22 +03:00
|
|
|
#define MOZ_HAS_THREAD_LOCAL
|
|
|
|
#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();
|
|
|
|
*/
|
2012-06-04 07:36:43 +04:00
|
|
|
template<typename T>
|
2012-05-10 00:54:33 +04:00
|
|
|
class ThreadLocal
|
|
|
|
{
|
2015-11-23 22:11:22 +03:00
|
|
|
#ifndef MOZ_HAS_THREAD_LOCAL
|
2014-07-11 06:10:17 +04:00
|
|
|
typedef pthread_key_t key_t;
|
2012-05-10 00:54:33 +04:00
|
|
|
|
2014-11-07 16:41:00 +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
|
2014-07-11 06:10:17 +04:00
|
|
|
{
|
2014-11-07 16:41:00 +03:00
|
|
|
typedef uintptr_t Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename S>
|
|
|
|
struct Helper<S *>
|
|
|
|
{
|
|
|
|
typedef S *Type;
|
2014-07-11 06:10:17 +04:00
|
|
|
};
|
2015-11-23 22:11:22 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
bool initialized() const {
|
|
|
|
#ifdef MOZ_HAS_THREAD_LOCAL
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return mInited;
|
|
|
|
#endif
|
|
|
|
}
|
2012-05-22 10:43:55 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
public:
|
2015-11-23 22:11:22 +03:00
|
|
|
// __thread does not allow non-trivial constructors, but we can
|
|
|
|
// instead rely on zero-initialization.
|
|
|
|
#ifndef MOZ_HAS_THREAD_LOCAL
|
2016-01-12 21:16:59 +03:00
|
|
|
ThreadLocal()
|
|
|
|
: mKey(0), mInited(false)
|
|
|
|
{}
|
2015-11-23 22:11:22 +03:00
|
|
|
#endif
|
2016-01-12 21:16:59 +03:00
|
|
|
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE inline bool init();
|
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);
|
2012-05-10 00:54:33 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
private:
|
2015-11-23 22:11:22 +03:00
|
|
|
#ifdef MOZ_HAS_THREAD_LOCAL
|
|
|
|
T mValue;
|
|
|
|
#else
|
2014-07-11 06:10:17 +04:00
|
|
|
key_t mKey;
|
|
|
|
bool mInited;
|
2015-11-23 22:11:22 +03:00
|
|
|
#endif
|
2012-05-10 00:54:33 +04:00
|
|
|
};
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
template<typename T>
|
2012-05-10 00:54:33 +04:00
|
|
|
inline bool
|
2012-06-04 07:36:43 +04:00
|
|
|
ThreadLocal<T>::init()
|
|
|
|
{
|
2014-11-07 16:41:00 +03:00
|
|
|
static_assert(mozilla::IsPointer<T>::value || mozilla::IsIntegral<T>::value,
|
|
|
|
"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
|
|
|
|
|
|
|
#ifdef MOZ_HAS_THREAD_LOCAL
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
if (!initialized()) {
|
|
|
|
mInited = !pthread_key_create(&mKey, nullptr);
|
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
return mInited;
|
2015-11-23 22:11:22 +03:00
|
|
|
#endif
|
2012-05-10 00:54:33 +04:00
|
|
|
}
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
template<typename T>
|
2012-05-22 10:43:55 +04:00
|
|
|
inline T
|
2012-06-04 07:36:43 +04:00
|
|
|
ThreadLocal<T>::get() const
|
|
|
|
{
|
2015-11-23 22:11:22 +03:00
|
|
|
#ifdef MOZ_HAS_THREAD_LOCAL
|
|
|
|
return mValue;
|
|
|
|
#else
|
2012-05-10 00:54:33 +04:00
|
|
|
MOZ_ASSERT(initialized());
|
2014-11-07 16:41:00 +03:00
|
|
|
void* h;
|
|
|
|
h = pthread_getspecific(mKey);
|
|
|
|
return static_cast<T>(reinterpret_cast<typename Helper<T>::Type>(h));
|
2015-11-23 22:11:22 +03:00
|
|
|
#endif
|
2012-05-10 00:54:33 +04:00
|
|
|
}
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
template<typename T>
|
2012-12-18 00:40:50 +04:00
|
|
|
inline void
|
2014-07-11 06:10:17 +04:00
|
|
|
ThreadLocal<T>::set(const T aValue)
|
2012-06-04 07:36:43 +04:00
|
|
|
{
|
2015-11-23 22:11:22 +03:00
|
|
|
#ifdef MOZ_HAS_THREAD_LOCAL
|
|
|
|
mValue = aValue;
|
|
|
|
#else
|
2012-05-10 00:54:33 +04:00
|
|
|
MOZ_ASSERT(initialized());
|
2014-11-07 16:41:00 +03:00
|
|
|
void* h = reinterpret_cast<void*>(static_cast<typename Helper<T>::Type>(aValue));
|
|
|
|
bool succeeded = !pthread_setspecific(mKey, h);
|
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
|
|
|
}
|
2015-11-23 22:11:22 +03:00
|
|
|
#endif
|
2012-05-10 00:54:33 +04:00
|
|
|
}
|
|
|
|
|
2015-11-23 22:11:22 +03:00
|
|
|
#ifdef MOZ_HAS_THREAD_LOCAL
|
2017-04-22 03:15:23 +03:00
|
|
|
#if defined(XP_WIN) || defined(MACOSX_HAS_THREAD_LOCAL)
|
2017-03-22 21:04:21 +03:00
|
|
|
#define MOZ_THREAD_LOCAL(TYPE) thread_local mozilla::detail::ThreadLocal<TYPE>
|
|
|
|
#else
|
2015-11-23 22:11:22 +03:00
|
|
|
#define MOZ_THREAD_LOCAL(TYPE) __thread mozilla::detail::ThreadLocal<TYPE>
|
2017-03-22 21:04:21 +03:00
|
|
|
#endif
|
2015-11-23 22:11:22 +03:00
|
|
|
#else
|
|
|
|
#define MOZ_THREAD_LOCAL(TYPE) mozilla::detail::ThreadLocal<TYPE>
|
|
|
|
#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 */
|