Bug 1796069 - Move CallbackThreadRegistry definitions to cpp file. r=padenot

Differential Revision: https://phabricator.services.mozilla.com/D161325
This commit is contained in:
Andreas Pehrson 2022-12-01 09:52:52 +00:00
Родитель b5e0b242db
Коммит 4c6b3f5c44
2 изменённых файлов: 53 добавлений и 48 удалений

Просмотреть файл

@ -8,9 +8,6 @@
#include "mozilla/ClearOnShutdown.h"
namespace mozilla {
CallbackThreadRegistry::CallbackThreadRegistry()
: mThreadIds("CallbackThreadRegistry::mThreadIds") {}
struct CallbackThreadRegistrySingleton {
CallbackThreadRegistrySingleton()
: mRegistry(MakeUnique<CallbackThreadRegistry>()) {
@ -25,10 +22,56 @@ struct CallbackThreadRegistrySingleton {
UniquePtr<CallbackThreadRegistry> mRegistry;
};
CallbackThreadRegistry::CallbackThreadRegistry()
: mThreadIds("CallbackThreadRegistry::mThreadIds") {}
/* static */
CallbackThreadRegistry* CallbackThreadRegistry::Get() {
static CallbackThreadRegistrySingleton sSingleton;
return sSingleton.mRegistry.get();
}
void CallbackThreadRegistry::Register(ProfilerThreadId aThreadId,
const char* aName) {
if (!aThreadId.IsSpecified()) {
// profiler_current_thread_id is unspecified on unsupported platforms.
return;
}
auto threadIds = mThreadIds.Lock();
for (uint32_t i = 0; i < threadIds->Length(); i++) {
if ((*threadIds)[i].mId == aThreadId) {
(*threadIds)[i].mUserCount++;
return;
}
}
ThreadUserCount tuc;
tuc.mId = aThreadId;
tuc.mUserCount = 1;
threadIds->AppendElement(tuc);
PROFILER_REGISTER_THREAD(aName);
}
void CallbackThreadRegistry::Unregister(ProfilerThreadId aThreadId) {
if (!aThreadId.IsSpecified()) {
// profiler_current_thread_id is unspedified on unsupported platforms.
return;
}
auto threadIds = mThreadIds.Lock();
for (uint32_t i = 0; i < threadIds->Length(); i++) {
if ((*threadIds)[i].mId == aThreadId) {
MOZ_ASSERT((*threadIds)[i].mUserCount > 0);
(*threadIds)[i].mUserCount--;
if ((*threadIds)[i].mUserCount == 0) {
PROFILER_UNREGISTER_THREAD();
threadIds->RemoveElementAt(i);
}
return;
}
}
MOZ_ASSERT_UNREACHABLE("Current thread was not registered");
}
} // namespace mozilla

Просмотреть файл

@ -31,59 +31,21 @@ class CallbackThreadRegistry final {
// that the callback isn't called, and then immediately destroy it.
}
CallbackThreadRegistry(const CallbackThreadRegistry&) = delete;
CallbackThreadRegistry& operator=(const CallbackThreadRegistry&) = delete;
CallbackThreadRegistry(CallbackThreadRegistry&&) = delete;
CallbackThreadRegistry& operator=(CallbackThreadRegistry&&) = delete;
// Returns the global instance of CallbackThreadRegistry. Safe from all
// threads.
static CallbackThreadRegistry* Get();
// This is intended to be called in the first callback of a callback
// thread.
void Register(ProfilerThreadId aThreadId, const char* aName) {
if (!aThreadId.IsSpecified()) {
// profiler_current_thread_id is unspecified on unsupported platforms.
return;
}
auto threadIds = mThreadIds.Lock();
for (uint32_t i = 0; i < threadIds->Length(); i++) {
if ((*threadIds)[i].mId == aThreadId) {
(*threadIds)[i].mUserCount++;
return;
}
}
ThreadUserCount tuc;
tuc.mId = aThreadId;
tuc.mUserCount = 1;
threadIds->AppendElement(tuc);
PROFILER_REGISTER_THREAD(aName);
}
void Register(ProfilerThreadId aThreadId, const char* aName);
// This is intended to be called when an object stops an audio callback thread
void Unregister(ProfilerThreadId aThreadId) {
if (!aThreadId.IsSpecified()) {
// profiler_current_thread_id is unspedified on unsupported platforms.
return;
}
auto threadIds = mThreadIds.Lock();
for (uint32_t i = 0; i < threadIds->Length(); i++) {
if ((*threadIds)[i].mId == aThreadId) {
MOZ_ASSERT((*threadIds)[i].mUserCount > 0);
(*threadIds)[i].mUserCount--;
if ((*threadIds)[i].mUserCount == 0) {
PROFILER_UNREGISTER_THREAD();
threadIds->RemoveElementAt(i);
}
return;
}
}
MOZ_ASSERT(false);
}
CallbackThreadRegistry(const CallbackThreadRegistry&) = delete;
CallbackThreadRegistry& operator=(const CallbackThreadRegistry&) = delete;
CallbackThreadRegistry(CallbackThreadRegistry&&) = delete;
CallbackThreadRegistry& operator=(CallbackThreadRegistry&&) = delete;
void Unregister(ProfilerThreadId aThreadId);
private:
struct ThreadUserCount {