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 #include "mozilla/RecursiveMutex.h"
12 # define NativeHandle(m) (reinterpret_cast<CRITICAL_SECTION*>(&m))
17 RecursiveMutex::RecursiveMutex(const char* aName
)
18 : BlockingResourceBase(aName
, eRecursiveMutex
)
21 mOwningThread(nullptr),
26 // This number was adapted from NSPR.
27 static const DWORD sLockSpinCount
= 100;
29 # if defined(RELEASE_OR_BETA)
30 // Vista and later automatically allocate and subsequently leak a debug info
31 // object for each critical section that we allocate unless we tell the
32 // system not to do that.
33 DWORD flags
= CRITICAL_SECTION_NO_DEBUG_INFO
;
38 InitializeCriticalSectionEx(NativeHandle(mMutex
), sLockSpinCount
, flags
);
39 MOZ_RELEASE_ASSERT(r
);
41 pthread_mutexattr_t attr
;
43 MOZ_RELEASE_ASSERT(pthread_mutexattr_init(&attr
) == 0,
44 "pthread_mutexattr_init failed");
47 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
) == 0,
48 "pthread_mutexattr_settype failed");
50 MOZ_RELEASE_ASSERT(pthread_mutex_init(&mMutex
, &attr
) == 0,
51 "pthread_mutex_init failed");
53 MOZ_RELEASE_ASSERT(pthread_mutexattr_destroy(&attr
) == 0,
54 "pthread_mutexattr_destroy failed");
58 RecursiveMutex::~RecursiveMutex() {
60 DeleteCriticalSection(NativeHandle(mMutex
));
62 MOZ_RELEASE_ASSERT(pthread_mutex_destroy(&mMutex
) == 0,
63 "pthread_mutex_destroy failed");
67 void RecursiveMutex::LockInternal() {
69 EnterCriticalSection(NativeHandle(mMutex
));
71 MOZ_RELEASE_ASSERT(pthread_mutex_lock(&mMutex
) == 0,
72 "pthread_mutex_lock failed");
76 void RecursiveMutex::UnlockInternal() {
78 LeaveCriticalSection(NativeHandle(mMutex
));
80 MOZ_RELEASE_ASSERT(pthread_mutex_unlock(&mMutex
) == 0,
81 "pthread_mutex_unlock failed");
85 } // namespace mozilla