CallbackThreadRegistry.h (2029B)
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/. */ 6 7 #ifndef CALLBACKTHREADREGISTRY_H 8 #define CALLBACKTHREADREGISTRY_H 9 10 #include <GeckoProfiler.h> 11 #include <mozilla/DataMutex.h> 12 #include <nsTArray.h> 13 14 #include <thread> 15 16 namespace mozilla { 17 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. 21 // 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(); 27 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. 32 } 33 34 CallbackThreadRegistry(const CallbackThreadRegistry&) = delete; 35 CallbackThreadRegistry& operator=(const CallbackThreadRegistry&) = delete; 36 CallbackThreadRegistry(CallbackThreadRegistry&&) = delete; 37 CallbackThreadRegistry& operator=(CallbackThreadRegistry&&) = delete; 38 39 // Returns the global instance of CallbackThreadRegistry. Safe from all 40 // threads. 41 static CallbackThreadRegistry* Get(); 42 43 // This is intended to be called in the first callback of a callback 44 // thread. 45 void Register(ProfilerThreadId aThreadId, const char* aName); 46 47 // This is intended to be called when an object stops an audio callback thread 48 void Unregister(ProfilerThreadId aThreadId); 49 50 private: 51 struct ThreadUserCount { 52 ProfilerThreadId mId; // from profiler_current_thread_id 53 int mUserCount = 0; 54 }; 55 DataMutex<nsTArray<ThreadUserCount>> mThreadIds; 56 }; 57 58 } // namespace mozilla 59 60 #endif // CALLBACKTHREADREGISTRY_H