1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef threading_Mutex_h 8 #define threading_Mutex_h 9 10 #include "mozilla/Assertions.h" 11 #include "mozilla/Attributes.h" 12 #include "mozilla/Move.h" 13 #include "mozilla/ThreadLocal.h" 14 #include "mozilla/Vector.h" 15 16 #include <new> 17 #include <string.h> 18 19 namespace js { 20 21 class ConditionVariable; 22 23 namespace detail { 24 25 class MutexImpl 26 { 27 public: 28 struct PlatformData; 29 30 MutexImpl(); 31 ~MutexImpl(); 32 MutexImpl(MutexImpl && rhs)33 MutexImpl(MutexImpl&& rhs) 34 : platformData_(rhs.platformData_) 35 { 36 MOZ_ASSERT(this != &rhs, "self move disallowed!"); 37 rhs.platformData_ = nullptr; 38 } 39 40 MutexImpl& operator=(MutexImpl&& rhs) { 41 this->~MutexImpl(); 42 new (this) MutexImpl(mozilla::Move(rhs)); 43 return *this; 44 } 45 46 bool operator==(const MutexImpl& rhs) { 47 return platformData_ == rhs.platformData_; 48 } 49 50 protected: 51 void lock(); 52 void unlock(); 53 54 private: 55 MutexImpl(const MutexImpl&) = delete; 56 void operator=(const MutexImpl&) = delete; 57 58 friend class js::ConditionVariable; platformData()59 PlatformData* platformData() { 60 MOZ_ASSERT(platformData_); 61 return platformData_; 62 }; 63 64 PlatformData* platformData_; 65 }; 66 67 } // namespace detail 68 69 // A MutexId secifies the name and mutex order for a mutex. 70 // 71 // The mutex order defines the allowed order of mutex acqusition on a single 72 // thread. Mutexes must be acquired in strictly increasing order. Mutexes with 73 // the same order may not be held at the same time by that thread. 74 struct MutexId 75 { 76 const char* name; 77 uint32_t order; 78 }; 79 80 #ifndef DEBUG 81 82 class Mutex : public detail::MutexImpl 83 { 84 public: Init()85 static bool Init() { return true; } ShutDown()86 static void ShutDown() {} 87 Mutex(const MutexId & id)88 explicit Mutex(const MutexId& id) {} 89 90 using MutexImpl::lock; 91 using MutexImpl::unlock; 92 }; 93 94 #else 95 96 // In debug builds, js::Mutex is a wrapper over MutexImpl that checks correct 97 // locking order is observed. 98 // 99 // The class maintains a per-thread stack of currently-held mutexes to enable it 100 // to check this. 101 class Mutex : public detail::MutexImpl 102 { 103 public: 104 static bool Init(); 105 static void ShutDown(); 106 Mutex(const MutexId & id)107 explicit Mutex(const MutexId& id) 108 : id_(id) 109 { 110 MOZ_ASSERT(id_.order != 0); 111 } 112 113 void lock(); 114 void unlock(); 115 116 private: 117 const MutexId id_; 118 119 using MutexVector = mozilla::Vector<const Mutex*>; 120 static MOZ_THREAD_LOCAL(MutexVector*) HeldMutexStack; 121 static MutexVector& heldMutexStack(); 122 }; 123 124 #endif 125 126 } // namespace js 127 128 #endif // threading_Mutex_h 129