/src/mozilla-central/xpcom/threads/RecursiveMutex.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "mozilla/RecursiveMutex.h" |
8 | | |
9 | | #ifdef XP_WIN |
10 | | #include <windows.h> |
11 | | |
12 | | #define NativeHandle(m) (reinterpret_cast<CRITICAL_SECTION*>(&m)) |
13 | | #endif |
14 | | |
15 | | namespace mozilla { |
16 | | |
17 | | RecursiveMutex::RecursiveMutex(const char* aName MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL) |
18 | | : BlockingResourceBase(aName, eRecursiveMutex) |
19 | | #ifdef DEBUG |
20 | | , mOwningThread(nullptr) |
21 | | , mEntryCount(0) |
22 | | #endif |
23 | 0 | { |
24 | 0 | MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
25 | | #ifdef XP_WIN |
26 | | // This number was adapted from NSPR. |
27 | | static const DWORD sLockSpinCount = 100; |
28 | | |
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; |
34 | | #else |
35 | | DWORD flags = 0; |
36 | | #endif |
37 | | BOOL r = InitializeCriticalSectionEx(NativeHandle(mMutex), |
38 | | sLockSpinCount, flags); |
39 | | MOZ_RELEASE_ASSERT(r); |
40 | | #else |
41 | | pthread_mutexattr_t attr; |
42 | 0 |
|
43 | 0 | MOZ_RELEASE_ASSERT(pthread_mutexattr_init(&attr) == 0, |
44 | 0 | "pthread_mutexattr_init failed"); |
45 | 0 |
|
46 | 0 | MOZ_RELEASE_ASSERT(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) == 0, |
47 | 0 | "pthread_mutexattr_settype failed"); |
48 | 0 |
|
49 | 0 | MOZ_RELEASE_ASSERT(pthread_mutex_init(&mMutex, &attr) == 0, |
50 | 0 | "pthread_mutex_init failed"); |
51 | 0 |
|
52 | 0 | MOZ_RELEASE_ASSERT(pthread_mutexattr_destroy(&attr) == 0, |
53 | 0 | "pthread_mutexattr_destroy failed"); |
54 | 0 | #endif |
55 | 0 | } |
56 | | |
57 | | RecursiveMutex::~RecursiveMutex() |
58 | 0 | { |
59 | | #ifdef XP_WIN |
60 | | DeleteCriticalSection(NativeHandle(mMutex)); |
61 | | #else |
62 | 0 | MOZ_RELEASE_ASSERT(pthread_mutex_destroy(&mMutex) == 0, |
63 | 0 | "pthread_mutex_destroy failed"); |
64 | 0 | #endif |
65 | 0 | } |
66 | | |
67 | | void |
68 | | RecursiveMutex::LockInternal() |
69 | 0 | { |
70 | | #ifdef XP_WIN |
71 | | EnterCriticalSection(NativeHandle(mMutex)); |
72 | | #else |
73 | 0 | MOZ_RELEASE_ASSERT(pthread_mutex_lock(&mMutex) == 0, |
74 | 0 | "pthread_mutex_lock failed"); |
75 | 0 | #endif |
76 | 0 | } |
77 | | |
78 | | void |
79 | | RecursiveMutex::UnlockInternal() |
80 | 0 | { |
81 | | #ifdef XP_WIN |
82 | | LeaveCriticalSection(NativeHandle(mMutex)); |
83 | | #else |
84 | 0 | MOZ_RELEASE_ASSERT(pthread_mutex_unlock(&mMutex) == 0, |
85 | 0 | "pthread_mutex_unlock failed"); |
86 | 0 | #endif |
87 | 0 | } |
88 | | |
89 | | } // namespace mozilla |