diff --git a/js/src/jsapi-tests/testThreadingMutex.cpp b/js/src/jsapi-tests/testThreadingMutex.cpp index f0d3d408e418..ed5e8833fa32 100644 --- a/js/src/jsapi-tests/testThreadingMutex.cpp +++ b/js/src/jsapi-tests/testThreadingMutex.cpp @@ -34,17 +34,3 @@ BEGIN_TEST(testThreadingUnlockGuard) return true; } END_TEST(testThreadingUnlockGuard) - -BEGIN_TEST(testThreadingMoveMutex) -{ - js::Mutex mutex; - mutex.lock(); - mutex.unlock(); - - js::Mutex another(mozilla::Move(mutex)); - another.lock(); - another.unlock(); - - return true; -} -END_TEST(testThreadingMoveMutex) diff --git a/js/src/threading/Mutex.h b/js/src/threading/Mutex.h index fae91c3145bf..2640bfc05492 100644 --- a/js/src/threading/Mutex.h +++ b/js/src/threading/Mutex.h @@ -7,12 +7,7 @@ #ifndef threading_Mutex_h #define threading_Mutex_h -#include -#include - -#include "mozilla/Assertions.h" #include "mozilla/Attributes.h" -#include "mozilla/Move.h" namespace js { @@ -27,29 +22,28 @@ public: void lock(); void unlock(); - Mutex(Mutex&& rhs) - : platformData_(rhs.platformData_) - { - MOZ_ASSERT(this != &rhs, "self move disallowed!"); - rhs.platformData_ = nullptr; - } - - Mutex& operator=(Mutex&& rhs) { - this->~Mutex(); - new (this) Mutex(mozilla::Move(rhs)); - return *this; - } - private: Mutex(const Mutex&) = delete; void operator=(const Mutex&) = delete; + Mutex(Mutex&&) = delete; + void operator=(Mutex&&) = delete; - PlatformData* platformData() { - MOZ_ASSERT(platformData_); - return platformData_; - }; + PlatformData* platformData(); - 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) + void* platformData_[6]; +#else + void* platformData_[64 / sizeof(void*)]; +#endif }; } // namespace js diff --git a/js/src/threading/posix/Mutex.cpp b/js/src/threading/posix/Mutex.cpp index bef5b2102aab..3e26178edc19 100644 --- a/js/src/threading/posix/Mutex.cpp +++ b/js/src/threading/posix/Mutex.cpp @@ -4,32 +4,23 @@ * 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/. */ -#include +#include "mozilla/Assertions.h" -#include "js/Utility.h" +#include #include "threading/Mutex.h" #include "threading/posix/MutexPlatformData.h" js::Mutex::Mutex() { - AutoEnterOOMUnsafeRegion oom; - platformData_ = js_new(); - if (!platformData_) - oom.crash("js::Mutex::Mutex"); - int r = pthread_mutex_init(&platformData()->ptMutex, NULL); MOZ_RELEASE_ASSERT(r == 0); } js::Mutex::~Mutex() { - if (!platformData_) - return; - int r = pthread_mutex_destroy(&platformData()->ptMutex); MOZ_RELEASE_ASSERT(r == 0); - js_delete(platformData()); } void @@ -45,3 +36,11 @@ js::Mutex::unlock() int r = pthread_mutex_unlock(&platformData()->ptMutex); MOZ_RELEASE_ASSERT(r == 0); } + +js::Mutex::PlatformData* +js::Mutex::platformData() +{ + static_assert(sizeof(platformData_) >= sizeof(PlatformData), + "platformData_ is too small"); + return reinterpret_cast(platformData_); +} diff --git a/js/src/threading/windows/Mutex.cpp b/js/src/threading/windows/Mutex.cpp index e3277c314930..ec638100e29c 100644 --- a/js/src/threading/windows/Mutex.cpp +++ b/js/src/threading/windows/Mutex.cpp @@ -4,12 +4,11 @@ * 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/. */ +#include "mozilla/Assertions.h" #include "mozilla/DebugOnly.h" #include "jswin.h" -#include "js/Utility.h" - #include "threading/Mutex.h" #include "threading/windows/MutexPlatformData.h" @@ -44,11 +43,6 @@ static MutexNativeImports NativeImports; js::Mutex::Mutex() { - AutoEnterOOMUnsafeRegion oom; - platformData_ = js_new(); - if (!platformData_) - oom.crash("js::Mutex::Mutex"); - // This number was adopted from NSPR. const static DWORD LockSpinCount = 1500; BOOL r; @@ -65,11 +59,7 @@ js::Mutex::Mutex() js::Mutex::~Mutex() { - if (!platformData_) - return; - DeleteCriticalSection(&platformData()->criticalSection); - js_delete(platformData()); } void @@ -83,3 +73,11 @@ js::Mutex::unlock() { LeaveCriticalSection(&platformData()->criticalSection); } + +js::Mutex::PlatformData* +js::Mutex::platformData() +{ + static_assert(sizeof(platformData_) >= sizeof(PlatformData), + "platformData_ is too small"); + return reinterpret_cast(platformData_); +}