1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Internal Base Profiler utilities.
#ifndef BaseProfilerDetail_h
#define BaseProfilerDetail_h
#include "mozilla/Atomics.h"
#include "mozilla/Maybe.h"
#include "mozilla/PlatformMutex.h"
#ifndef MOZ_GECKO_PROFILER
# error Do not #include this header when MOZ_GECKO_PROFILER is not #defined.
#endif
namespace mozilla {
namespace baseprofiler {
// Implemented in platform.cpp
MFBT_API int profiler_current_thread_id();
namespace detail {
// Thin shell around mozglue PlatformMutex, for Base Profiler internal use.
class BaseProfilerMutex : private ::mozilla::detail::MutexImpl {
public:
BaseProfilerMutex() : ::mozilla::detail::MutexImpl() {}
explicit BaseProfilerMutex(const char* aName)
: ::mozilla::detail::MutexImpl(), mName(aName) {}
BaseProfilerMutex(const BaseProfilerMutex&) = delete;
BaseProfilerMutex& operator=(const BaseProfilerMutex&) = delete;
BaseProfilerMutex(BaseProfilerMutex&&) = delete;
BaseProfilerMutex& operator=(BaseProfilerMutex&&) = delete;
#ifdef DEBUG
~BaseProfilerMutex() { MOZ_ASSERT(mOwningThreadId == 0); }
#endif // DEBUG
[[nodiscard]] bool IsLockedOnCurrentThread() const {
return mOwningThreadId == baseprofiler::profiler_current_thread_id();
}
void AssertCurrentThreadOwns() const {
MOZ_ASSERT(IsLockedOnCurrentThread());
}
void Lock() {
const int tid = baseprofiler::profiler_current_thread_id();
MOZ_ASSERT(tid != 0);
MOZ_ASSERT(!IsLockedOnCurrentThread(), "Recursive locking");
::mozilla::detail::MutexImpl::lock();
MOZ_ASSERT(mOwningThreadId == 0, "Not unlocked properly");
mOwningThreadId = tid;
}
[[nodiscard]] bool TryLock() {
const int tid = baseprofiler::profiler_current_thread_id();
MOZ_ASSERT(tid != 0);
MOZ_ASSERT(!IsLockedOnCurrentThread(), "Recursive locking");
if (!::mozilla::detail::MutexImpl::tryLock()) {
// Failed to lock, nothing more to do.
return false;
}
MOZ_ASSERT(mOwningThreadId == 0, "Not unlocked properly");
mOwningThreadId = tid;
return true;
}
void Unlock() {
MOZ_ASSERT(IsLockedOnCurrentThread(), "Unlocking when not locked here");
// We're still holding the mutex here, so it's safe to just reset
// `mOwningThreadId`.
mOwningThreadId = 0;
::mozilla::detail::MutexImpl::unlock();
}
const char* GetName() const { return mName; }
private:
// Thread currently owning the lock, or 0.
// Atomic because it may be read at any time independent of the mutex.
// Relaxed because threads only need to know if they own it already, so:
// - If it's their id, only *they* wrote that value with a locked mutex.
// - If it's different from their thread id it doesn't matter what other
// number it is (0 or another id) and that it can change again at any time.
Atomic<int, MemoryOrdering::Relaxed> mOwningThreadId{0};
const char* mName = nullptr;
};
// RAII class to lock a mutex.
class MOZ_RAII BaseProfilerAutoLock {
public:
explicit BaseProfilerAutoLock(BaseProfilerMutex& aMutex) : mMutex(aMutex) {
mMutex.Lock();
}
BaseProfilerAutoLock(const BaseProfilerAutoLock&) = delete;
BaseProfilerAutoLock& operator=(const BaseProfilerAutoLock&) = delete;
BaseProfilerAutoLock(BaseProfilerAutoLock&&) = delete;
BaseProfilerAutoLock& operator=(BaseProfilerAutoLock&&) = delete;
~BaseProfilerAutoLock() { mMutex.Unlock(); }
private:
BaseProfilerMutex& mMutex;
};
// Thin shell around mozglue PlatformMutex, for Base Profiler internal use.
// Actual mutex may be disabled at construction time.
class BaseProfilerMaybeMutex : private ::mozilla::detail::MutexImpl {
public:
explicit BaseProfilerMaybeMutex(bool aActivate) {
if (aActivate) {
mMaybeMutex.emplace();
}
}
BaseProfilerMaybeMutex(const BaseProfilerMaybeMutex&) = delete;
BaseProfilerMaybeMutex& operator=(const BaseProfilerMaybeMutex&) = delete;
BaseProfilerMaybeMutex(BaseProfilerMaybeMutex&&) = delete;
BaseProfilerMaybeMutex& operator=(BaseProfilerMaybeMutex&&) = delete;
~BaseProfilerMaybeMutex() = default;
bool IsActivated() const { return mMaybeMutex.isSome(); }
[[nodiscard]] bool IsActivatedAndLockedOnCurrentThread() const {
if (!IsActivated()) {
// Not activated, so we can never be locked.
return false;
}
return mMaybeMutex->IsLockedOnCurrentThread();
}
void AssertCurrentThreadOwns() const {
#ifdef DEBUG
if (IsActivated()) {
mMaybeMutex->AssertCurrentThreadOwns();
}
#endif // DEBUG
}
void Lock() {
if (IsActivated()) {
mMaybeMutex->Lock();
}
}
void Unlock() {
if (IsActivated()) {
mMaybeMutex->Unlock();
}
}
private:
Maybe<BaseProfilerMutex> mMaybeMutex;
};
// RAII class to lock a mutex.
class MOZ_RAII BaseProfilerMaybeAutoLock {
public:
explicit BaseProfilerMaybeAutoLock(BaseProfilerMaybeMutex& aMaybeMutex)
: mMaybeMutex(aMaybeMutex) {
mMaybeMutex.Lock();
}
BaseProfilerMaybeAutoLock(const BaseProfilerMaybeAutoLock&) = delete;
BaseProfilerMaybeAutoLock& operator=(const BaseProfilerMaybeAutoLock&) =
delete;
BaseProfilerMaybeAutoLock(BaseProfilerMaybeAutoLock&&) = delete;
BaseProfilerMaybeAutoLock& operator=(BaseProfilerMaybeAutoLock&&) = delete;
~BaseProfilerMaybeAutoLock() { mMaybeMutex.Unlock(); }
private:
BaseProfilerMaybeMutex& mMaybeMutex;
};
} // namespace detail
} // namespace baseprofiler
} // namespace mozilla
#endif // BaseProfilerDetail_h
|