summaryrefslogtreecommitdiffstats
path: root/mozglue/baseprofiler/public/BaseProfilerDetail.h
blob: 6ecf6e117b16caab9a55db40d6cafc57e139a715 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* -*- 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/Attributes.h"
#include "mozilla/Maybe.h"
#include "mozilla/PlatformMutex.h"
#include "mozilla/PlatformRWLock.h"
#include "mozilla/BaseProfilerUtils.h"

namespace mozilla {
namespace baseprofiler {

namespace detail {

// Thin shell around mozglue PlatformMutex, for Base Profiler internal use.
class MOZ_CAPABILITY("mutex") 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(!BaseProfilerThreadId::FromNumber(mOwningThreadId).IsSpecified(),
               "BaseProfilerMutex should have been unlocked when destroyed");
  }
#endif  // DEBUG

  [[nodiscard]] bool IsLockedOnCurrentThread() const {
    return BaseProfilerThreadId::FromNumber(mOwningThreadId) ==
           baseprofiler::profiler_current_thread_id();
  }

  void AssertCurrentThreadOwns() const MOZ_ASSERT_CAPABILITY(this) {
    MOZ_ASSERT(IsLockedOnCurrentThread());
  }

  void Lock() MOZ_CAPABILITY_ACQUIRE() {
    const BaseProfilerThreadId tid = baseprofiler::profiler_current_thread_id();
    MOZ_ASSERT(tid.IsSpecified());
    MOZ_ASSERT(!IsLockedOnCurrentThread(), "Recursive locking");
    ::mozilla::detail::MutexImpl::lock();
    MOZ_ASSERT(!BaseProfilerThreadId::FromNumber(mOwningThreadId).IsSpecified(),
               "Not unlocked properly");
    mOwningThreadId = tid.ToNumber();
  }

  [[nodiscard]] bool TryLock() MOZ_TRY_ACQUIRE(true) {
    const BaseProfilerThreadId tid = baseprofiler::profiler_current_thread_id();
    MOZ_ASSERT(tid.IsSpecified());
    MOZ_ASSERT(!IsLockedOnCurrentThread(), "Recursive locking");
    if (!::mozilla::detail::MutexImpl::tryLock()) {
      // Failed to lock, nothing more to do.
      return false;
    }
    MOZ_ASSERT(!BaseProfilerThreadId::FromNumber(mOwningThreadId).IsSpecified(),
               "Not unlocked properly");
    mOwningThreadId = tid.ToNumber();
    return true;
  }

  void Unlock() MOZ_CAPABILITY_RELEASE() {
    MOZ_ASSERT(IsLockedOnCurrentThread(), "Unlocking when not locked here");
    // We're still holding the mutex here, so it's safe to just reset
    // `mOwningThreadId`.
    mOwningThreadId = BaseProfilerThreadId{}.ToNumber();
    ::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<typename BaseProfilerThreadId::NumberType, MemoryOrdering::Relaxed>
      mOwningThreadId;

  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
  }

  MOZ_PUSH_IGNORE_THREAD_SAFETY
  void Lock() {
    if (IsActivated()) {
      mMaybeMutex->Lock();
    }
  }

  void Unlock() {
    if (IsActivated()) {
      mMaybeMutex->Unlock();
    }
  }
  MOZ_POP_THREAD_SAFETY

 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;
};

class BaseProfilerSharedMutex : public ::mozilla::detail::RWLockImpl {
 public:
#ifdef DEBUG
  ~BaseProfilerSharedMutex() {
    MOZ_ASSERT(!BaseProfilerThreadId::FromNumber(mOwningThreadId).IsSpecified(),
               "BaseProfilerMutex should have been unlocked when destroyed");
  }
#endif  // DEBUG

  [[nodiscard]] bool IsLockedExclusiveOnCurrentThread() const {
    return BaseProfilerThreadId::FromNumber(mOwningThreadId) ==
           baseprofiler::profiler_current_thread_id();
  }

  void LockExclusive() {
    const BaseProfilerThreadId tid = baseprofiler::profiler_current_thread_id();
    MOZ_ASSERT(tid.IsSpecified());
    MOZ_ASSERT(!IsLockedExclusiveOnCurrentThread(), "Recursive locking");
    ::mozilla::detail::RWLockImpl::writeLock();
    MOZ_ASSERT(!BaseProfilerThreadId::FromNumber(mOwningThreadId).IsSpecified(),
               "Not unlocked properly");
    mOwningThreadId = tid.ToNumber();
  }

  void UnlockExclusive() {
    MOZ_ASSERT(IsLockedExclusiveOnCurrentThread(),
               "Unlocking when not locked here");
    // We're still holding the mutex here, so it's safe to just reset
    // `mOwningThreadId`.
    mOwningThreadId = BaseProfilerThreadId{}.ToNumber();
    writeUnlock();
  }

  void LockShared() { readLock(); }

  void UnlockShared() { readUnlock(); }

 private:
  // Thread currently owning the exclusive 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<typename BaseProfilerThreadId::NumberType, MemoryOrdering::Relaxed>
      mOwningThreadId;
};

// RAII class to lock a shared mutex exclusively.
class MOZ_RAII BaseProfilerAutoLockExclusive {
 public:
  explicit BaseProfilerAutoLockExclusive(BaseProfilerSharedMutex& aSharedMutex)
      : mSharedMutex(aSharedMutex) {
    mSharedMutex.LockExclusive();
  }

  BaseProfilerAutoLockExclusive(const BaseProfilerAutoLockExclusive&) = delete;
  BaseProfilerAutoLockExclusive& operator=(
      const BaseProfilerAutoLockExclusive&) = delete;
  BaseProfilerAutoLockExclusive(BaseProfilerAutoLockExclusive&&) = delete;
  BaseProfilerAutoLockExclusive& operator=(BaseProfilerAutoLockExclusive&&) =
      delete;

  ~BaseProfilerAutoLockExclusive() { mSharedMutex.UnlockExclusive(); }

 private:
  BaseProfilerSharedMutex& mSharedMutex;
};

// RAII class to lock a shared mutex non-exclusively, other
// BaseProfilerAutoLockShared's may happen in other threads.
class MOZ_RAII BaseProfilerAutoLockShared {
 public:
  explicit BaseProfilerAutoLockShared(BaseProfilerSharedMutex& aSharedMutex)
      : mSharedMutex(aSharedMutex) {
    mSharedMutex.LockShared();
  }

  BaseProfilerAutoLockShared(const BaseProfilerAutoLockShared&) = delete;
  BaseProfilerAutoLockShared& operator=(const BaseProfilerAutoLockShared&) =
      delete;
  BaseProfilerAutoLockShared(BaseProfilerAutoLockShared&&) = delete;
  BaseProfilerAutoLockShared& operator=(BaseProfilerAutoLockShared&&) = delete;

  ~BaseProfilerAutoLockShared() { mSharedMutex.UnlockShared(); }

 private:
  BaseProfilerSharedMutex& mSharedMutex;
};

}  // namespace detail
}  // namespace baseprofiler
}  // namespace mozilla

#endif  // BaseProfilerDetail_h