Bug 756709 - Make ThreadLocal::set crash on failure (which really can only happen if per-thread memory for the TLS entry couldn't be allocated, which should be really rare). r=Ms2ger
authorJeff Walden <jwalden@mit.edu>
Mon, 17 Dec 2012 15:40:50 -0500
changeset 117121 6e5acaff8239634f6ccc30107768c87038e799f1
parent 117120 58edc998ee4f9de4bae86eeccec77ea5ef3ef95a
child 117122 c7da4138801e1b217d75e64bf0a41803676806e1
push id24087
push usergraememcc_firefox@graeme-online.co.uk
push dateSat, 29 Dec 2012 12:13:53 +0000
treeherdermozilla-central@b5ea211f23a9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMs2ger
bugs756709
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 756709 - Make ThreadLocal::set crash on failure (which really can only happen if per-thread memory for the TLS entry couldn't be allocated, which should be really rare). r=Ms2ger
mfbt/ThreadLocal.h
--- a/mfbt/ThreadLocal.h
+++ b/mfbt/ThreadLocal.h
@@ -1,29 +1,29 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * 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/. */
 
 /* Cross-platform lightweight thread local data wrappers. */
 
-#ifndef mozilla_TLS_h_
-#define mozilla_TLS_h_
+#ifndef mozilla_ThreadLocal_h_
+#define mozilla_ThreadLocal_h_
 
 #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" {
-__declspec(dllimport) void * __stdcall TlsGetValue(unsigned long);
-__declspec(dllimport) int __stdcall TlsSetValue(unsigned long, void *);
+__declspec(dllimport) void* __stdcall TlsGetValue(unsigned long);
+__declspec(dllimport) int __stdcall TlsSetValue(unsigned long, void*);
 __declspec(dllimport) unsigned long __stdcall TlsAlloc();
 }
 #else
 #  include <pthread.h>
 #  include <signal.h>
 #endif
 
 #include "mozilla/Assertions.h"
@@ -78,32 +78,32 @@ class ThreadLocal
       T value;
     };
 
   public:
     MOZ_WARN_UNUSED_RESULT inline bool init();
 
     inline T get() const;
 
-    inline bool set(const T value);
+    inline void set(const T value);
 
     bool initialized() const {
       return inited;
     }
 
   private:
     key_t key;
     bool inited;
 };
 
 template<typename T>
 inline bool
 ThreadLocal<T>::init()
 {
-  MOZ_STATIC_ASSERT(sizeof(T) <= sizeof(void *),
+  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();
   inited = key != 0xFFFFFFFFUL; // TLS_OUT_OF_INDEXES
 #else
   inited = !pthread_key_create(&key, NULL);
@@ -121,24 +121,27 @@ ThreadLocal<T>::get() const
   h.ptr = TlsGetValue(key);
 #else
   h.ptr = pthread_getspecific(key);
 #endif
   return h.value;
 }
 
 template<typename T>
-inline bool
+inline void
 ThreadLocal<T>::set(const T value)
 {
   MOZ_ASSERT(initialized());
   Helper h;
   h.value = value;
+  bool succeeded;
 #ifdef XP_WIN
-  return TlsSetValue(key, h.ptr);
+  succeeded = TlsSetValue(key, h.ptr);
 #else
-  return !pthread_setspecific(key, h.ptr);
+  succeeded = !pthread_setspecific(key, h.ptr);
 #endif
+  if (!succeeded)
+    MOZ_CRASH();
 }
 
 } // namespace mozilla
 
-#endif // mozilla_TLS_h_
+#endif // mozilla_ThreadLocal_h_