summaryrefslogtreecommitdiffstats
path: root/tools/profiler/public/ProfilerThreadRegistry.h
blob: 4d0fd3ef68049133f3fdf8a7e84e3eec1448ba3e (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* -*- 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/. */

#ifndef ProfilerThreadRegistry_h
#define ProfilerThreadRegistry_h

#include "mozilla/BaseProfilerDetail.h"
#include "mozilla/ProfilerThreadRegistration.h"
#include "mozilla/Vector.h"

namespace mozilla::profiler {

class ThreadRegistry {
 private:
  using RegistryMutex = baseprofiler::detail::BaseProfilerSharedMutex;
  using RegistryLockExclusive =
      baseprofiler::detail::BaseProfilerAutoLockExclusive;
  using RegistryLockShared = baseprofiler::detail::BaseProfilerAutoLockShared;

 public:
  // Aliases to data accessors (removing the ThreadRegistration prefix).

  using UnlockedConstReader = ThreadRegistrationUnlockedConstReader;
  using UnlockedConstReaderAndAtomicRW =
      ThreadRegistrationUnlockedConstReaderAndAtomicRW;
  using UnlockedRWForLockedProfiler =
      ThreadRegistrationUnlockedRWForLockedProfiler;
  using UnlockedReaderAndAtomicRWOnThread =
      ThreadRegistrationUnlockedReaderAndAtomicRWOnThread;
  using LockedRWFromAnyThread = ThreadRegistrationLockedRWFromAnyThread;
  using LockedRWOnThread = ThreadRegistrationLockedRWOnThread;

  // Off-thread access through the registry, providing the following data
  // accessors: UnlockedConstReader, UnlockedConstReaderAndAtomicRW,
  // UnlockedRWForLockedProfiler, and LockedRWFromAnyThread.
  // (See ThreadRegistration class for ON-thread access.)

  // Reference-like class pointing at a ThreadRegistration.
  // It should only exist while sRegistryMutex is locked.
  class OffThreadRef {
   public:
    // const UnlockedConstReader

    [[nodiscard]] const UnlockedConstReader& UnlockedConstReaderCRef() const {
      return mThreadRegistration->mData;
    }

    template <typename F>
    auto WithUnlockedConstReader(F&& aF) const {
      return std::forward<F>(aF)(UnlockedConstReaderCRef());
    }

    // const UnlockedConstReaderAndAtomicRW

    [[nodiscard]] const UnlockedConstReaderAndAtomicRW&
    UnlockedConstReaderAndAtomicRWCRef() const {
      return mThreadRegistration->mData;
    }

    template <typename F>
    auto WithUnlockedConstReaderAndAtomicRW(F&& aF) const {
      return std::forward<F>(aF)(UnlockedConstReaderAndAtomicRWCRef());
    }

    // UnlockedConstReaderAndAtomicRW

    [[nodiscard]] UnlockedConstReaderAndAtomicRW&
    UnlockedConstReaderAndAtomicRWRef() {
      return mThreadRegistration->mData;
    }

    template <typename F>
    auto WithUnlockedConstReaderAndAtomicRW(F&& aF) {
      return std::forward<F>(aF)(UnlockedConstReaderAndAtomicRWRef());
    }

    // const UnlockedRWForLockedProfiler

    [[nodiscard]] const UnlockedRWForLockedProfiler&
    UnlockedRWForLockedProfilerCRef() const {
      return mThreadRegistration->mData;
    }

    template <typename F>
    auto WithUnlockedRWForLockedProfiler(F&& aF) const {
      return std::forward<F>(aF)(UnlockedRWForLockedProfilerCRef());
    }

    // UnlockedRWForLockedProfiler

    [[nodiscard]] UnlockedRWForLockedProfiler&
    UnlockedRWForLockedProfilerRef() {
      return mThreadRegistration->mData;
    }

    template <typename F>
    auto WithUnlockedRWForLockedProfiler(F&& aF) {
      return std::forward<F>(aF)(UnlockedRWForLockedProfilerRef());
    }

    // const LockedRWFromAnyThread through ConstRWFromAnyThreadWithLock

    class ConstRWFromAnyThreadWithLock {
     public:
      [[nodiscard]] const LockedRWFromAnyThread& DataCRef() const {
        return mLockedRWFromAnyThread;
      }
      [[nodiscard]] const LockedRWFromAnyThread* operator->() const {
        return &mLockedRWFromAnyThread;
      }

      ConstRWFromAnyThreadWithLock(
          const LockedRWFromAnyThread& aLockedRWFromAnyThread,
          ThreadRegistration::DataMutex& aDataMutex)
          : mLockedRWFromAnyThread(aLockedRWFromAnyThread),
            mDataLock(aDataMutex) {}

     private:
      const LockedRWFromAnyThread& mLockedRWFromAnyThread;
      ThreadRegistration::DataLock mDataLock;
    };

    [[nodiscard]] ConstRWFromAnyThreadWithLock ConstLockedRWFromAnyThread()
        const {
      return ConstRWFromAnyThreadWithLock{mThreadRegistration->mData,
                                          mThreadRegistration->mDataMutex};
    }

    template <typename F>
    auto WithConstLockedRWFromAnyThread(F&& aF) const {
      ConstRWFromAnyThreadWithLock lockedData = ConstLockedRWFromAnyThread();
      return std::forward<F>(aF)(lockedData.DataCRef());
    }

    // LockedRWFromAnyThread through RWFromAnyThreadWithLock

    class RWFromAnyThreadWithLock {
     public:
      [[nodiscard]] const LockedRWFromAnyThread& DataCRef() const {
        return mLockedRWFromAnyThread;
      }
      [[nodiscard]] LockedRWFromAnyThread& DataRef() {
        return mLockedRWFromAnyThread;
      }
      [[nodiscard]] const LockedRWFromAnyThread* operator->() const {
        return &mLockedRWFromAnyThread;
      }
      [[nodiscard]] LockedRWFromAnyThread* operator->() {
        return &mLockedRWFromAnyThread;
      }

      // In some situations, it may be useful to do some on-thread operations if
      // we are indeed on this thread now. The lock is still held here; caller
      // should not use this pointer longer than this RWFromAnyThreadWithLock.
      [[nodiscard]] LockedRWOnThread* GetLockedRWOnThread() {
        if (mLockedRWFromAnyThread.Info().ThreadId() ==
            profiler_current_thread_id()) {
          // mLockedRWFromAnyThread references a subclass of the
          // ThreadRegistration's mData, so it's safe to downcast it to another
          // hierarchy level of the object.
          return &static_cast<LockedRWOnThread&>(mLockedRWFromAnyThread);
        }
        return nullptr;
      }

     private:
      friend class OffThreadRef;
      RWFromAnyThreadWithLock(LockedRWFromAnyThread& aLockedRWFromAnyThread,
                              ThreadRegistration::DataMutex& aDataMutex)
          : mLockedRWFromAnyThread(aLockedRWFromAnyThread),
            mDataLock(aDataMutex) {}

      LockedRWFromAnyThread& mLockedRWFromAnyThread;
      ThreadRegistration::DataLock mDataLock;
    };

    [[nodiscard]] RWFromAnyThreadWithLock GetLockedRWFromAnyThread() {
      return RWFromAnyThreadWithLock{mThreadRegistration->mData,
                                     mThreadRegistration->mDataMutex};
    }

    template <typename F>
    auto WithLockedRWFromAnyThread(F&& aF) {
      RWFromAnyThreadWithLock lockedData = GetLockedRWFromAnyThread();
      return std::forward<F>(aF)(lockedData.DataRef());
    }

   private:
    // Only ThreadRegistry should construct an OnThreadRef.
    friend class ThreadRegistry;
    explicit OffThreadRef(ThreadRegistration& aThreadRegistration)
        : mThreadRegistration(&aThreadRegistration) {}

    // If we have an ON-thread ref, it's safe to convert to an OFF-thread ref.
    explicit OffThreadRef(ThreadRegistration::OnThreadRef aOnThreadRef)
        : mThreadRegistration(aOnThreadRef.mThreadRegistration) {}

    [[nodiscard]] bool IsPointingAt(
        ThreadRegistration& aThreadRegistration) const {
      return mThreadRegistration == &aThreadRegistration;
    }

    // Guaranted to be non-null by construction.
    ThreadRegistration* mThreadRegistration;
  };

  // Lock the registry non-exclusively and allow iteration. E.g.:
  // `for (OffThreadRef thread : LockedRegistry{}) { ... }`
  // Do *not* export copies/references, as they could become dangling.
  // Locking order: Profiler, ThreadRegistry, ThreadRegistration.
  class LockedRegistry {
   public:
    LockedRegistry()
        : mRegistryLock([]() -> RegistryMutex& {
            MOZ_ASSERT(!IsRegistryMutexLockedOnCurrentThread(),
                       "Recursive locking detected");
            // In DEBUG builds, *before* we attempt to lock sRegistryMutex, we
            // want to check that the ThreadRegistration mutex is *not* locked
            // on this thread, to avoid inversion deadlocks.
            MOZ_ASSERT(!ThreadRegistration::IsDataMutexLockedOnCurrentThread());
            return sRegistryMutex;
          }()) {
      ThreadRegistration::WithOnThreadRef(
          [](ThreadRegistration::OnThreadRef aOnThreadRef) {
            aOnThreadRef.mThreadRegistration
                ->mIsRegistryLockedSharedOnThisThread = true;
          });
    }

    ~LockedRegistry() {
      ThreadRegistration::WithOnThreadRef(
          [](ThreadRegistration::OnThreadRef aOnThreadRef) {
            aOnThreadRef.mThreadRegistration
                ->mIsRegistryLockedSharedOnThisThread = false;
          });
    }

    [[nodiscard]] const OffThreadRef* begin() const {
      return sRegistryContainer.begin();
    }
    [[nodiscard]] OffThreadRef* begin() { return sRegistryContainer.begin(); }
    [[nodiscard]] const OffThreadRef* end() const {
      return sRegistryContainer.end();
    }
    [[nodiscard]] OffThreadRef* end() { return sRegistryContainer.end(); }

   private:
    RegistryLockShared mRegistryLock;
  };

  // Call `F(OffThreadRef)` for the given aThreadId.
  template <typename F>
  static void WithOffThreadRef(ProfilerThreadId aThreadId, F&& aF) {
    for (OffThreadRef thread : LockedRegistry{}) {
      if (thread.UnlockedConstReaderCRef().Info().ThreadId() == aThreadId) {
        std::forward<F>(aF)(thread);
        break;
      }
    }
  }

  template <typename F, typename FallbackReturn>
  [[nodiscard]] static auto WithOffThreadRefOr(ProfilerThreadId aThreadId,
                                               F&& aF,
                                               FallbackReturn&& aFallbackReturn)
      -> decltype(std::forward<F>(aF)(std::declval<OffThreadRef>())) {
    for (OffThreadRef thread : LockedRegistry{}) {
      if (thread.UnlockedConstReaderCRef().Info().ThreadId() == aThreadId) {
        return std::forward<F>(aF)(thread);
      }
    }
    return std::forward<FallbackReturn>(aFallbackReturn);
  }

  static size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) {
    LockedRegistry lockedRegistry;
    // "Ex" because we don't count static objects, but we count whatever they
    // allocated on the heap.
    size_t bytes = sRegistryContainer.sizeOfExcludingThis(aMallocSizeOf);
    for (const OffThreadRef& offThreadRef : lockedRegistry) {
      bytes +=
          offThreadRef.mThreadRegistration->SizeOfExcludingThis(aMallocSizeOf);
    }
    return bytes;
  }

  static size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) {
    return SizeOfExcludingThis(aMallocSizeOf);
  }

  [[nodiscard]] static bool IsRegistryMutexLockedOnCurrentThread() {
    return sRegistryMutex.IsLockedExclusiveOnCurrentThread() ||
           ThreadRegistration::WithOnThreadRefOr(
               [](ThreadRegistration::OnThreadRef aOnThreadRef) {
                 return aOnThreadRef.mThreadRegistration
                     ->mIsRegistryLockedSharedOnThisThread;
               },
               false);
  }

 private:
  using RegistryContainer = Vector<OffThreadRef>;

  static RegistryContainer sRegistryContainer;

  // Mutex protecting the registry.
  // Locking order: Profiler, ThreadRegistry, ThreadRegistration.
  static RegistryMutex sRegistryMutex;

  // Only allow ThreadRegistration to (un)register itself.
  friend class ThreadRegistration;
  static void Register(ThreadRegistration::OnThreadRef aOnThreadRef);
  static void Unregister(ThreadRegistration::OnThreadRef aOnThreadRef);
};

}  // namespace mozilla::profiler

#endif  // ProfilerThreadRegistry_h