Backed out changeset 06f41c22f3a6 (bug 1888460) for causing linux xpcshell failures...
[gecko.git] / dom / media / CallbackThreadRegistry.h
bloba154b3a35f6674ce67e68947d847e9ce8b810a3a
1 /* -*- Mode: C++; tab-width: 2; 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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef CALLBACKTHREADREGISTRY_H
8 #define CALLBACKTHREADREGISTRY_H
10 #include <cstdint>
11 #include <mozilla/DataMutex.h>
12 #include <nsTArray.h>
13 #include <thread>
14 #include <GeckoProfiler.h>
16 namespace mozilla {
18 // This class is a singleton that tracks various callback threads and makes
19 // sure they are registered or unregistered to the profiler safely and
20 // consistently.
22 // Register and Unregister are fairly expensive and shouldn't be used in a hot
23 // path.
24 class CallbackThreadRegistry final {
25 public:
26 CallbackThreadRegistry();
28 ~CallbackThreadRegistry() {
29 // It would be nice to be able to assert that all threads have been
30 // unregistered, but we can't: it's legal to suspend an audio stream, so
31 // that the callback isn't called, and then immediately destroy it.
34 CallbackThreadRegistry(const CallbackThreadRegistry&) = delete;
35 CallbackThreadRegistry& operator=(const CallbackThreadRegistry&) = delete;
36 CallbackThreadRegistry(CallbackThreadRegistry&&) = delete;
37 CallbackThreadRegistry& operator=(CallbackThreadRegistry&&) = delete;
39 // Returns the global instance of CallbackThreadRegistry. Safe from all
40 // threads.
41 static CallbackThreadRegistry* Get();
43 // This is intended to be called in the first callback of a callback
44 // thread.
45 void Register(ProfilerThreadId aThreadId, const char* aName);
47 // This is intended to be called when an object stops an audio callback thread
48 void Unregister(ProfilerThreadId aThreadId);
50 private:
51 struct ThreadUserCount {
52 ProfilerThreadId mId; // from profiler_current_thread_id
53 int mUserCount = 0;
55 DataMutex<nsTArray<ThreadUserCount>> mThreadIds;
58 } // namespace mozilla
60 #endif // CALLBACKTHREADREGISTRY_H