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/Move.h"
12 #include "mozilla/PlatformMutex.h"
13 #include "mozilla/ThreadLocal.h"
14 #include "mozilla/Vector.h"
15 
16 namespace js {
17 
18 // A MutexId secifies the name and mutex order for a mutex.
19 //
20 // The mutex order defines the allowed order of mutex acqusition on a single
21 // thread. Mutexes must be acquired in strictly increasing order. Mutexes with
22 // the same order may not be held at the same time by that thread.
23 struct MutexId {
24   const char* name;
25   uint32_t order;
26 };
27 
28 #ifndef DEBUG
29 
30 class Mutex : public mozilla::detail::MutexImpl {
31  public:
Init()32   static bool Init() { return true; }
ShutDown()33   static void ShutDown() {}
34 
Mutex(const MutexId & id)35   explicit Mutex(const MutexId& id) {}
36 
37   using MutexImpl::lock;
38   using MutexImpl::unlock;
39 };
40 
41 #else
42 
43 // In debug builds, js::Mutex is a wrapper over MutexImpl that checks correct
44 // locking order is observed.
45 //
46 // The class maintains a per-thread stack of currently-held mutexes to enable it
47 // to check this.
48 class Mutex : public mozilla::detail::MutexImpl {
49  public:
50   static bool Init();
51   static void ShutDown();
52 
Mutex(const MutexId & id)53   explicit Mutex(const MutexId& id) : id_(id) { MOZ_ASSERT(id_.order != 0); }
54 
55   void lock();
56   void unlock();
57   bool ownedByCurrentThread() const;
58 
59  private:
60   const MutexId id_;
61 
62   using MutexVector = mozilla::Vector<const Mutex*>;
63   static MOZ_THREAD_LOCAL(MutexVector*) HeldMutexStack;
64   static MutexVector& heldMutexStack();
65 };
66 
67 #endif
68 
69 }  // namespace js
70 
71 #endif  // threading_Mutex_h
72