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/. */
7 // A lock that can be acquired multiple times on the same thread.
9 #ifndef mozilla_RecursiveMutex_h
10 #define mozilla_RecursiveMutex_h
12 #include "mozilla/BlockingResourceBase.h"
20 class RecursiveMutex
: public BlockingResourceBase
{
22 explicit RecursiveMutex(const char* aName
);
29 void Lock() { LockInternal(); }
30 void Unlock() { UnlockInternal(); }
35 * AssertCurrentThreadIn
37 void AssertCurrentThreadIn();
39 * AssertNotCurrentThreadIn
41 void AssertNotCurrentThreadIn() {
42 // Not currently implemented. See bug 476536 for discussion.
45 void AssertCurrentThreadIn() {}
46 void AssertNotCurrentThreadIn() {}
50 RecursiveMutex() = delete;
51 RecursiveMutex(const RecursiveMutex
&) = delete;
52 RecursiveMutex
& operator=(const RecursiveMutex
&) = delete;
55 void UnlockInternal();
58 PRThread
* mOwningThread
;
63 pthread_mutex_t mMutex
;
65 // We eschew including windows.h and using CRITICAL_SECTION here so that files
66 // including us don't also pull in windows.h. Just use a type that's big
67 // enough for CRITICAL_SECTION, and we'll fix it up later.
72 class MOZ_RAII RecursiveMutexAutoLock
{
74 explicit RecursiveMutexAutoLock(RecursiveMutex
& aRecursiveMutex
)
75 : mRecursiveMutex(&aRecursiveMutex
) {
76 NS_ASSERTION(mRecursiveMutex
, "null mutex");
77 mRecursiveMutex
->Lock();
80 ~RecursiveMutexAutoLock(void) { mRecursiveMutex
->Unlock(); }
83 RecursiveMutexAutoLock() = delete;
84 RecursiveMutexAutoLock(const RecursiveMutexAutoLock
&) = delete;
85 RecursiveMutexAutoLock
& operator=(const RecursiveMutexAutoLock
&) = delete;
86 static void* operator new(size_t) noexcept(true);
88 mozilla::RecursiveMutex
* mRecursiveMutex
;
91 class MOZ_RAII RecursiveMutexAutoUnlock
{
93 explicit RecursiveMutexAutoUnlock(RecursiveMutex
& aRecursiveMutex
)
94 : mRecursiveMutex(&aRecursiveMutex
) {
95 NS_ASSERTION(mRecursiveMutex
, "null mutex");
96 mRecursiveMutex
->Unlock();
99 ~RecursiveMutexAutoUnlock(void) { mRecursiveMutex
->Lock(); }
102 RecursiveMutexAutoUnlock() = delete;
103 RecursiveMutexAutoUnlock(const RecursiveMutexAutoUnlock
&) = delete;
104 RecursiveMutexAutoUnlock
& operator=(const RecursiveMutexAutoUnlock
&) = delete;
105 static void* operator new(size_t) noexcept(true);
107 mozilla::RecursiveMutex
* mRecursiveMutex
;
110 } // namespace mozilla
112 #endif // mozilla_RecursiveMutex_h