/work/obj-fuzz/dist/include/mozilla/StaticMutex.h
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 | | #ifndef mozilla_StaticMutex_h |
8 | | #define mozilla_StaticMutex_h |
9 | | |
10 | | #include "mozilla/Atomics.h" |
11 | | #include "mozilla/Mutex.h" |
12 | | |
13 | | namespace mozilla { |
14 | | |
15 | | /** |
16 | | * StaticMutex is a Mutex that can (and in fact, must) be used as a |
17 | | * global/static variable. |
18 | | * |
19 | | * The main reason to use StaticMutex as opposed to |
20 | | * StaticAutoPtr<OffTheBooksMutex> is that we instantiate the StaticMutex in a |
21 | | * thread-safe manner the first time it's used. |
22 | | * |
23 | | * The same caveats that apply to StaticAutoPtr apply to StaticMutex. In |
24 | | * particular, do not use StaticMutex as a stack variable or a class instance |
25 | | * variable, because this class relies on the fact that global variablies are |
26 | | * initialized to 0 in order to initialize mMutex. It is only safe to use |
27 | | * StaticMutex as a global or static variable. |
28 | | */ |
29 | | template <recordreplay::Behavior Recording> |
30 | | class MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS BaseStaticMutex |
31 | | { |
32 | | public: |
33 | | // In debug builds, check that mMutex is initialized for us as we expect by |
34 | | // the compiler. In non-debug builds, don't declare a constructor so that |
35 | | // the compiler can see that the constructor is trivial. |
36 | | #ifdef DEBUG |
37 | | BaseStaticMutex() |
38 | | { |
39 | | MOZ_ASSERT(!mMutex); |
40 | | } |
41 | | #endif |
42 | | |
43 | | void Lock() |
44 | 1.53k | { |
45 | 1.53k | Mutex()->Lock(); |
46 | 1.53k | } mozilla::BaseStaticMutex<(mozilla::recordreplay::Behavior)1>::Lock() Line | Count | Source | 44 | 272 | { | 45 | 272 | Mutex()->Lock(); | 46 | 272 | } |
mozilla::BaseStaticMutex<(mozilla::recordreplay::Behavior)0>::Lock() Line | Count | Source | 44 | 1.26k | { | 45 | 1.26k | Mutex()->Lock(); | 46 | 1.26k | } |
|
47 | | |
48 | | void Unlock() |
49 | 1.53k | { |
50 | 1.53k | Mutex()->Unlock(); |
51 | 1.53k | } mozilla::BaseStaticMutex<(mozilla::recordreplay::Behavior)1>::Unlock() Line | Count | Source | 49 | 272 | { | 50 | 272 | Mutex()->Unlock(); | 51 | 272 | } |
mozilla::BaseStaticMutex<(mozilla::recordreplay::Behavior)0>::Unlock() Line | Count | Source | 49 | 1.26k | { | 50 | 1.26k | Mutex()->Unlock(); | 51 | 1.26k | } |
|
52 | | |
53 | | void AssertCurrentThreadOwns() |
54 | 0 | { |
55 | | #ifdef DEBUG |
56 | | Mutex()->AssertCurrentThreadOwns(); |
57 | | #endif |
58 | | } |
59 | | |
60 | | private: |
61 | | OffTheBooksMutex* Mutex() |
62 | 3.07k | { |
63 | 3.07k | if (mMutex) { |
64 | 3.04k | return mMutex; |
65 | 3.04k | } |
66 | 22 | |
67 | 22 | OffTheBooksMutex* mutex = new OffTheBooksMutex("StaticMutex", Recording); |
68 | 22 | if (!mMutex.compareExchange(nullptr, mutex)) { |
69 | 0 | delete mutex; |
70 | 0 | } |
71 | 22 | |
72 | 22 | return mMutex; |
73 | 22 | } mozilla::BaseStaticMutex<(mozilla::recordreplay::Behavior)1>::Mutex() Line | Count | Source | 62 | 544 | { | 63 | 544 | if (mMutex) { | 64 | 528 | return mMutex; | 65 | 528 | } | 66 | 16 | | 67 | 16 | OffTheBooksMutex* mutex = new OffTheBooksMutex("StaticMutex", Recording); | 68 | 16 | if (!mMutex.compareExchange(nullptr, mutex)) { | 69 | 0 | delete mutex; | 70 | 0 | } | 71 | 16 | | 72 | 16 | return mMutex; | 73 | 16 | } |
mozilla::BaseStaticMutex<(mozilla::recordreplay::Behavior)0>::Mutex() Line | Count | Source | 62 | 2.52k | { | 63 | 2.52k | if (mMutex) { | 64 | 2.52k | return mMutex; | 65 | 2.52k | } | 66 | 6 | | 67 | 6 | OffTheBooksMutex* mutex = new OffTheBooksMutex("StaticMutex", Recording); | 68 | 6 | if (!mMutex.compareExchange(nullptr, mutex)) { | 69 | 0 | delete mutex; | 70 | 0 | } | 71 | 6 | | 72 | 6 | return mMutex; | 73 | 6 | } |
|
74 | | |
75 | | Atomic<OffTheBooksMutex*, SequentiallyConsistent, Recording> mMutex; |
76 | | |
77 | | |
78 | | // Disallow copy constructor, but only in debug mode. We only define |
79 | | // a default constructor in debug mode (see above); if we declared |
80 | | // this constructor always, the compiler wouldn't generate a trivial |
81 | | // default constructor for us in non-debug mode. |
82 | | #ifdef DEBUG |
83 | | BaseStaticMutex(BaseStaticMutex& aOther); |
84 | | #endif |
85 | | |
86 | | // Disallow these operators. |
87 | | BaseStaticMutex& operator=(BaseStaticMutex* aRhs); |
88 | | static void* operator new(size_t) CPP_THROW_NEW; |
89 | | static void operator delete(void*); |
90 | | }; |
91 | | |
92 | | typedef BaseStaticMutex<recordreplay::Behavior::Preserve> StaticMutex; |
93 | | typedef BaseStaticMutex<recordreplay::Behavior::DontPreserve> StaticMutexNotRecorded; |
94 | | |
95 | | // Helper for StaticMutexAutoLock/Unlock. |
96 | | class MOZ_STACK_CLASS AnyStaticMutex |
97 | | { |
98 | | public: |
99 | | MOZ_IMPLICIT AnyStaticMutex(StaticMutex& aMutex) |
100 | | : mStaticMutex(&aMutex), mStaticMutexNotRecorded(nullptr) |
101 | 214 | {} |
102 | | |
103 | | MOZ_IMPLICIT AnyStaticMutex(StaticMutexNotRecorded& aMutex) |
104 | | : mStaticMutex(nullptr), mStaticMutexNotRecorded(&aMutex) |
105 | 1.26k | {} |
106 | | |
107 | | void Lock() |
108 | 1.47k | { |
109 | 1.47k | if (mStaticMutex) { |
110 | 214 | mStaticMutex->Lock(); |
111 | 1.26k | } else { |
112 | 1.26k | mStaticMutexNotRecorded->Lock(); |
113 | 1.26k | } |
114 | 1.47k | } |
115 | | |
116 | | void Unlock() |
117 | 1.47k | { |
118 | 1.47k | if (mStaticMutex) { |
119 | 214 | mStaticMutex->Unlock(); |
120 | 1.26k | } else { |
121 | 1.26k | mStaticMutexNotRecorded->Unlock(); |
122 | 1.26k | } |
123 | 1.47k | } |
124 | | |
125 | | private: |
126 | | StaticMutex* mStaticMutex; |
127 | | StaticMutexNotRecorded* mStaticMutexNotRecorded; |
128 | | }; |
129 | | |
130 | | typedef BaseAutoLock<AnyStaticMutex> StaticMutexAutoLock; |
131 | | typedef BaseAutoUnlock<AnyStaticMutex> StaticMutexAutoUnlock; |
132 | | |
133 | | } // namespace mozilla |
134 | | |
135 | | #endif |