Bug 1336344 - use sizeof(pthread_mutex_t) to size MutexImpl's platformData_; r=fitzgen
authorNathan Froyd <froydnj@mozilla.com>
Wed, 22 Feb 2017 16:41:35 -0500
changeset 373434 999757e9e5a576c884201746546a3420a92f7447
parent 373433 ee8c9e627623d293c646c60830090deaf29fa44a
child 373435 2bd07604e754e005219ca0b1e1395e0c96abbaf6
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfitzgen
bugs1336344
milestone54.0a1
Bug 1336344 - use sizeof(pthread_mutex_t) to size MutexImpl's platformData_; r=fitzgen For pthreads platforms, we have a terribly large condition for the size of a MutexImpl that attempts to hardcode the number of words that pthread_mutex_t takes. This hardcoding isn't always correct. We originally did this to try and keep <pthread.h> includes out of the headers, but the PlatformConditionVariable.h header already includes <pthread.h> anyway, and <pthread.h> isn't so namespace-invasive as <windows.h>. So go ahead and include <pthread.h> and use the actual definition of pthread_mutex_t to size the platformData_ member.
mozglue/misc/PlatformMutex.h
--- a/mozglue/misc/PlatformMutex.h
+++ b/mozglue/misc/PlatformMutex.h
@@ -5,16 +5,20 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_PlatformMutex_h
 #define mozilla_PlatformMutex_h
 
 #include "mozilla/Attributes.h"
 #include "mozilla/Move.h"
 
+#if !defined(XP_WIN)
+# include <pthread.h>
+#endif
+
 namespace mozilla {
 
 namespace detail {
 
 class ConditionVariableImpl;
 
 class MutexImpl
 {
@@ -35,29 +39,23 @@ protected:
 private:
   MutexImpl(const MutexImpl&) = delete;
   void operator=(const MutexImpl&) = delete;
   MutexImpl(MutexImpl&&) = delete;
   void operator=(MutexImpl&&) = delete;
 
   PlatformData* platformData();
 
-// Linux and maybe other platforms define the storage size of pthread_mutex_t in
-// bytes. However, we must define it as an array of void pointers to ensure
-// proper alignment.
-#if defined(__APPLE__) && defined(__MACH__) && defined(__i386__)
-  void* platformData_[11];
-#elif defined(__APPLE__) && defined(__MACH__) && defined(__amd64__)
-  void* platformData_[8];
-#elif defined(__linux__)
-  void* platformData_[40 / sizeof(void*)];
-#elif defined(_WIN32)
+#if !defined(XP_WIN)
+  void* platformData_[sizeof(pthread_mutex_t) / sizeof(void*)];
+  static_assert(sizeof(pthread_mutex_t) / sizeof(void*) != 0 &&
+                sizeof(pthread_mutex_t) % sizeof(void*) == 0,
+                "pthread_mutex_t must have pointer alignment");
+#else
   void* platformData_[6];
-#else
-  void* platformData_[64 / sizeof(void*)];
 #endif
 
   friend class mozilla::detail::ConditionVariableImpl;
 };
 
 } // namespace detail
 
 } // namespace mozilla