Commit 1f76b475 authored by Gerald Squelart's avatar Gerald Squelart
Browse files

Bug 1721569 - Move main-thread id functions to cpp files - r=florian

This hides the scProfilerMainThreadId detail, and makes for a safer API.
Also, ::profiler_init_main_thread_id() calls ::mozilla::baseprofiler::profiler_init_main_thread_id().
And in non-MOZ_GECKO_PROFILER builds, AUTO_PROFILER_INIT calls profiler_init_main_thread_id(), which makes other main-thread functions usable there (assuming profiler_current_thread_id works).

Differential Revision: https://phabricator.services.mozilla.com/D121695
parent 84d1ba42
Loading
Loading
Loading
Loading
+19 −5
Original line number Diff line number Diff line
@@ -131,8 +131,22 @@ BaseProfilerThreadId profiler_current_thread_id() {

// --------------------------------------------- Platform-agnostic definitions

namespace mozilla::baseprofiler::detail {
// Statically initialized to 0, then set once from profiler_init(), which should
// be called from the main thread before any other use of the profiler.
BaseProfilerThreadId scProfilerMainThreadId;
}  // namespace mozilla::baseprofiler::detail
namespace mozilla::baseprofiler {

static BaseProfilerThreadId scBaseProfilerMainThreadId{};

void profiler_init_main_thread_id() {
  if (!scBaseProfilerMainThreadId.IsSpecified()) {
    scBaseProfilerMainThreadId = profiler_current_thread_id();
  }
}

BaseProfilerThreadId profiler_main_thread_id() {
  return scBaseProfilerMainThreadId;
}

bool profiler_is_main_thread() {
  return profiler_current_thread_id() == scBaseProfilerMainThreadId;
}

}  // namespace mozilla::baseprofiler
+1 −1
Original line number Diff line number Diff line
@@ -2566,7 +2566,7 @@ static Vector<const char*> SplitAtCommas(const char* aString,
void profiler_init(void* aStackTop) {
  LOG("profiler_init");

  detail::scProfilerMainThreadId = profiler_current_thread_id();
  profiler_init_main_thread_id();

  VTUNE_INIT();

+2 −1
Original line number Diff line number Diff line
@@ -38,7 +38,8 @@
// following macros and functions, which encapsulate the most common operations
// and thus avoid the need for many #ifdefs.

#  define AUTO_BASE_PROFILER_INIT
#  define AUTO_BASE_PROFILER_INIT \
    ::mozilla::baseprofiler::profiler_init_main_thread_id()

#  define BASE_PROFILER_REGISTER_THREAD(name)
#  define BASE_PROFILER_UNREGISTER_THREAD()
+7 −13
Original line number Diff line number Diff line
@@ -118,19 +118,13 @@ namespace mozilla::baseprofiler {
// Get the current thread's ID.
[[nodiscard]] MFBT_API BaseProfilerThreadId profiler_current_thread_id();

namespace detail {
// Statically initialized to 0, then set once from profiler_init(), which should
// be called from the main thread before any other use of the profiler.
extern MFBT_DATA BaseProfilerThreadId scProfilerMainThreadId;
}  // namespace detail

[[nodiscard]] inline BaseProfilerThreadId profiler_main_thread_id() {
  return detail::scProfilerMainThreadId;
}
// Must be called at least once from the main thread, before any other main-
// thread id function.
MFBT_API void profiler_init_main_thread_id();

[[nodiscard]] inline bool profiler_is_main_thread() {
  return profiler_current_thread_id() == profiler_main_thread_id();
}
[[nodiscard]] MFBT_API BaseProfilerThreadId profiler_main_thread_id();

[[nodiscard]] MFBT_API bool profiler_is_main_thread();

}  // namespace mozilla::baseprofiler

+1 −6
Original line number Diff line number Diff line
@@ -119,12 +119,7 @@ void TestProfilerUtils() {
  }

  {
    if (!mozilla::baseprofiler::detail::scProfilerMainThreadId.IsSpecified()) {
      // Special case: This may happen if the profiler has not yet been
      // initialized. We only need to set scProfilerMainThreadId.
      mozilla::baseprofiler::detail::scProfilerMainThreadId =
          mozilla::baseprofiler::profiler_current_thread_id();
    }
    mozilla::baseprofiler::profiler_init_main_thread_id();

    using mozilla::baseprofiler::BaseProfilerThreadId;
    using Number = BaseProfilerThreadId::NumberType;
Loading