2013-07-24 00:41:39 -07: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-09 13:54:33 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-07-24 00:41:39 -07: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-09 13:54:33 -07:00
|
|
|
|
2012-06-03 20:36:43 -07:00
|
|
|
/* Cross-platform lightweight thread local data wrappers. */
|
2012-05-09 13:54:33 -07:00
|
|
|
|
2013-07-24 00:41:39 -07:00
|
|
|
#ifndef mozilla_ThreadLocal_h
|
|
|
|
#define mozilla_ThreadLocal_h
|
2012-05-09 13:54:33 -07:00
|
|
|
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
// This file will get included in any file that wants to add a profiler mark.
|
|
|
|
// In order to not bring <windows.h> together we could include windef.h and
|
|
|
|
// winbase.h which are sufficient to get the prototypes for the Tls* functions.
|
|
|
|
// # include <windef.h>
|
|
|
|
// # include <winbase.h>
|
|
|
|
// Unfortunately, even including these headers causes us to add a bunch of ugly
|
|
|
|
// stuff to our namespace e.g #define CreateEvent CreateEventW
|
|
|
|
extern "C" {
|
2012-12-17 12:40:50 -08:00
|
|
|
__declspec(dllimport) void* __stdcall TlsGetValue(unsigned long);
|
|
|
|
__declspec(dllimport) int __stdcall TlsSetValue(unsigned long, void*);
|
2012-05-09 13:54:33 -07:00
|
|
|
__declspec(dllimport) unsigned long __stdcall TlsAlloc();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# include <pthread.h>
|
|
|
|
# include <signal.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
2013-07-25 16:31:48 -07:00
|
|
|
#include "mozilla/NullPtr.h"
|
2014-11-07 05:41:00 -08:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2012-05-09 13:54:33 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// sig_safe_t denotes an atomic type which can be read or stored in a single
|
|
|
|
// instruction. This means that data of this type is safe to be manipulated
|
|
|
|
// from a signal handler, or other similar asynchronous execution contexts.
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
typedef unsigned long sig_safe_t;
|
|
|
|
#else
|
|
|
|
typedef sig_atomic_t sig_safe_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Thread Local Storage helpers.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
*
|
|
|
|
* 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-11 17:51:57 -08:00
|
|
|
* // Create a TLS item.
|
|
|
|
* //
|
|
|
|
* // Note that init() should be invoked exactly once, before any usage of set()
|
|
|
|
* // or get().
|
2012-05-09 13:54:33 -07:00
|
|
|
* mozilla::ThreadLocal<int> tlsKey;
|
|
|
|
* if (!tlsKey.init()) {
|
|
|
|
* // deal with the error
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // Set the TLS value
|
|
|
|
* tlsKey.set(123);
|
|
|
|
*
|
|
|
|
* // Get the TLS value
|
|
|
|
* int value = tlsKey.get();
|
|
|
|
*/
|
2012-06-03 20:36:43 -07:00
|
|
|
template<typename T>
|
2012-05-09 13:54:33 -07:00
|
|
|
class ThreadLocal
|
|
|
|
{
|
|
|
|
#if defined(XP_WIN)
|
2014-07-10 19:10:17 -07:00
|
|
|
typedef unsigned long key_t;
|
2012-05-09 13:54:33 -07:00
|
|
|
#else
|
2014-07-10 19:10:17 -07:00
|
|
|
typedef pthread_key_t key_t;
|
2012-05-09 13:54:33 -07:00
|
|
|
#endif
|
|
|
|
|
2014-11-07 05:41:00 -08: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-10 19:10:17 -07:00
|
|
|
{
|
2014-11-07 05:41:00 -08:00
|
|
|
typedef uintptr_t Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename S>
|
|
|
|
struct Helper<S *>
|
|
|
|
{
|
|
|
|
typedef S *Type;
|
2014-07-10 19:10:17 -07:00
|
|
|
};
|
2012-05-21 23:43:55 -07:00
|
|
|
|
2014-07-10 19:10:17 -07:00
|
|
|
public:
|
|
|
|
MOZ_WARN_UNUSED_RESULT inline bool init();
|
2012-05-09 13:54:33 -07:00
|
|
|
|
2014-07-10 19:10:17 -07:00
|
|
|
inline T get() const;
|
2012-05-09 13:54:33 -07:00
|
|
|
|
2014-07-10 19:10:17 -07:00
|
|
|
inline void set(const T aValue);
|
2012-05-09 13:54:33 -07:00
|
|
|
|
2014-07-10 19:10:17 -07:00
|
|
|
bool initialized() const { return mInited; }
|
2012-05-09 13:54:33 -07:00
|
|
|
|
2014-07-10 19:10:17 -07:00
|
|
|
private:
|
|
|
|
key_t mKey;
|
|
|
|
bool mInited;
|
2012-05-09 13:54:33 -07:00
|
|
|
};
|
|
|
|
|
2012-06-03 20:36:43 -07:00
|
|
|
template<typename T>
|
2012-05-09 13:54:33 -07:00
|
|
|
inline bool
|
2012-06-03 20:36:43 -07:00
|
|
|
ThreadLocal<T>::init()
|
|
|
|
{
|
2014-11-07 05:41:00 -08: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 10:59:53 -07:00
|
|
|
static_assert(sizeof(T) <= sizeof(void*),
|
|
|
|
"mozilla::ThreadLocal can't be used for types larger than "
|
|
|
|
"a pointer");
|
2012-05-09 13:54:33 -07:00
|
|
|
MOZ_ASSERT(!initialized());
|
|
|
|
#ifdef XP_WIN
|
2014-07-10 19:10:17 -07:00
|
|
|
mKey = TlsAlloc();
|
|
|
|
mInited = mKey != 0xFFFFFFFFUL; // TLS_OUT_OF_INDEXES
|
2012-05-09 13:54:33 -07:00
|
|
|
#else
|
2014-07-10 19:10:17 -07:00
|
|
|
mInited = !pthread_key_create(&mKey, nullptr);
|
2012-05-09 13:54:33 -07:00
|
|
|
#endif
|
2014-07-10 19:10:17 -07:00
|
|
|
return mInited;
|
2012-05-09 13:54:33 -07:00
|
|
|
}
|
|
|
|
|
2012-06-03 20:36:43 -07:00
|
|
|
template<typename T>
|
2012-05-21 23:43:55 -07:00
|
|
|
inline T
|
2012-06-03 20:36:43 -07:00
|
|
|
ThreadLocal<T>::get() const
|
|
|
|
{
|
2012-05-09 13:54:33 -07:00
|
|
|
MOZ_ASSERT(initialized());
|
2014-11-07 05:41:00 -08:00
|
|
|
void* h;
|
2012-05-09 13:54:33 -07:00
|
|
|
#ifdef XP_WIN
|
2014-11-07 05:41:00 -08:00
|
|
|
h = TlsGetValue(mKey);
|
2012-05-09 13:54:33 -07:00
|
|
|
#else
|
2014-11-07 05:41:00 -08:00
|
|
|
h = pthread_getspecific(mKey);
|
2012-05-09 13:54:33 -07:00
|
|
|
#endif
|
2014-11-07 05:41:00 -08:00
|
|
|
return static_cast<T>(reinterpret_cast<typename Helper<T>::Type>(h));
|
2012-05-09 13:54:33 -07:00
|
|
|
}
|
|
|
|
|
2012-06-03 20:36:43 -07:00
|
|
|
template<typename T>
|
2012-12-17 12:40:50 -08:00
|
|
|
inline void
|
2014-07-10 19:10:17 -07:00
|
|
|
ThreadLocal<T>::set(const T aValue)
|
2012-06-03 20:36:43 -07:00
|
|
|
{
|
2012-05-09 13:54:33 -07:00
|
|
|
MOZ_ASSERT(initialized());
|
2014-11-07 05:41:00 -08:00
|
|
|
void* h = reinterpret_cast<void*>(static_cast<typename Helper<T>::Type>(aValue));
|
2012-05-09 13:54:33 -07:00
|
|
|
#ifdef XP_WIN
|
2014-11-07 05:41:00 -08:00
|
|
|
bool succeeded = TlsSetValue(mKey, h);
|
2012-05-09 13:54:33 -07:00
|
|
|
#else
|
2014-11-07 05:41:00 -08:00
|
|
|
bool succeeded = !pthread_setspecific(mKey, h);
|
2012-05-09 13:54:33 -07:00
|
|
|
#endif
|
2014-07-10 19:10:17 -07:00
|
|
|
if (!succeeded) {
|
2012-12-17 12:40:50 -08:00
|
|
|
MOZ_CRASH();
|
2014-07-10 19:10:17 -07:00
|
|
|
}
|
2012-05-09 13:54:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-07-24 00:41:39 -07:00
|
|
|
#endif /* mozilla_ThreadLocal_h */
|