Bug 1093823 - Avoid valgrind report from ThreadLocal<bool>. r=froydnj

This commit is contained in:
Tom Tromey 2014-11-07 08:41:00 -05:00
Родитель 95d665262e
Коммит b818cc4847
1 изменённых файлов: 24 добавлений и 11 удалений

Просмотреть файл

@ -30,6 +30,7 @@ __declspec(dllimport) unsigned long __stdcall TlsAlloc();
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
#include "mozilla/NullPtr.h"
#include "mozilla/TypeTraits.h"
namespace mozilla {
@ -78,10 +79,20 @@ class ThreadLocal
typedef pthread_key_t key_t;
#endif
union Helper
// 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
{
void* mPtr;
T mValue;
typedef uintptr_t Type;
};
template<typename S>
struct Helper<S *>
{
typedef S *Type;
};
public:
@ -102,6 +113,9 @@ template<typename T>
inline bool
ThreadLocal<T>::init()
{
static_assert(mozilla::IsPointer<T>::value || mozilla::IsIntegral<T>::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");
@ -120,13 +134,13 @@ inline T
ThreadLocal<T>::get() const
{
MOZ_ASSERT(initialized());
Helper h;
void* h;
#ifdef XP_WIN
h.mPtr = TlsGetValue(mKey);
h = TlsGetValue(mKey);
#else
h.mPtr = pthread_getspecific(mKey);
h = pthread_getspecific(mKey);
#endif
return h.mValue;
return static_cast<T>(reinterpret_cast<typename Helper<T>::Type>(h));
}
template<typename T>
@ -134,12 +148,11 @@ inline void
ThreadLocal<T>::set(const T aValue)
{
MOZ_ASSERT(initialized());
Helper h;
h.mValue = aValue;
void* h = reinterpret_cast<void*>(static_cast<typename Helper<T>::Type>(aValue));
#ifdef XP_WIN
bool succeeded = TlsSetValue(mKey, h.mPtr);
bool succeeded = TlsSetValue(mKey, h);
#else
bool succeeded = !pthread_setspecific(mKey, h.mPtr);
bool succeeded = !pthread_setspecific(mKey, h);
#endif
if (!succeeded) {
MOZ_CRASH();