diff --git a/mfbt/ThreadLocal.h b/mfbt/ThreadLocal.h index eaa187509d54b..1c0624267c70b 100644 --- a/mfbt/ThreadLocal.h +++ b/mfbt/ThreadLocal.h @@ -72,12 +72,17 @@ class ThreadLocal typedef pthread_key_t key_t; #endif + union Helper { + void *ptr; + T value; + }; + public: MOZ_WARN_UNUSED_RESULT inline bool init(); - inline T* get() const; + inline T get() const; - inline bool set(const T* value); + inline bool set(const T value); bool initialized() const { return inited; @@ -91,6 +96,7 @@ class ThreadLocal template inline bool ThreadLocal::init() { + MOZ_STATIC_ASSERT(sizeof(T) <= sizeof(void *), "mozilla::ThreadLocal can't be used for types larger than a pointer"); MOZ_ASSERT(!initialized()); #ifdef XP_WIN key = TlsAlloc(); @@ -102,24 +108,28 @@ ThreadLocal::init() { } template -inline T* +inline T ThreadLocal::get() const { MOZ_ASSERT(initialized()); + Helper h; #ifdef XP_WIN - return reinterpret_cast(TlsGetValue(key)); + h.ptr = TlsGetValue(key); #else - return reinterpret_cast(pthread_getspecific(key)); + h.ptr = pthread_getspecific(key); #endif + return h.value; } template inline bool -ThreadLocal::set(const T* value) { +ThreadLocal::set(const T value) { MOZ_ASSERT(initialized()); + Helper h; + h.value = value; #ifdef XP_WIN - return TlsSetValue(key, const_cast(value)); + return TlsSetValue(key, h.ptr); #else - return !pthread_setspecific(key, value); + return !pthread_setspecific(key, h.ptr); #endif } diff --git a/tools/profiler/TableTicker.cpp b/tools/profiler/TableTicker.cpp index 17258b5f8b04a..42761e2bac23b 100644 --- a/tools/profiler/TableTicker.cpp +++ b/tools/profiler/TableTicker.cpp @@ -69,8 +69,8 @@ using namespace mozilla; #endif -mozilla::ThreadLocal tlsStack; -mozilla::ThreadLocal tlsTicker; +mozilla::ThreadLocal tlsStack; +mozilla::ThreadLocal tlsTicker; // We need to track whether we've been initialized otherwise // we end up using tlsStack without initializing it. // Because tlsStack is totally opaque to us we can't reuse diff --git a/tools/profiler/sps_sampler.h b/tools/profiler/sps_sampler.h index cc97bc068588c..219ae41942e58 100644 --- a/tools/profiler/sps_sampler.h +++ b/tools/profiler/sps_sampler.h @@ -17,8 +17,8 @@ using mozilla::TimeDuration; struct ProfileStack; class TableTicker; -extern mozilla::ThreadLocal tlsStack; -extern mozilla::ThreadLocal tlsTicker; +extern mozilla::ThreadLocal tlsStack; +extern mozilla::ThreadLocal tlsTicker; extern bool stack_key_initialized; #ifndef SAMPLE_FUNCTION_NAME