diff --git a/dom/workers/RuntimeService.cpp b/dom/workers/RuntimeService.cpp index 22f92d893599..631667671195 100644 --- a/dom/workers/RuntimeService.cpp +++ b/dom/workers/RuntimeService.cpp @@ -2877,10 +2877,7 @@ WorkerThreadPrimaryRunnable::Run() { #ifdef MOZ_GECKO_PROFILER - PseudoStack* stack = profiler_get_pseudo_stack(); - if (stack) { - stack->sampleContext(cx); - } + profiler_set_js_context(cx); #endif { @@ -2896,9 +2893,7 @@ WorkerThreadPrimaryRunnable::Run() BackgroundChild::CloseForCurrentThread(); #ifdef MOZ_GECKO_PROFILER - if (stack) { - stack->sampleContext(nullptr); - } + profiler_clear_js_context(); #endif } diff --git a/js/xpconnect/src/XPCJSContext.cpp b/js/xpconnect/src/XPCJSContext.cpp index eaa8cb08c42c..9115f89bde6a 100644 --- a/js/xpconnect/src/XPCJSContext.cpp +++ b/js/xpconnect/src/XPCJSContext.cpp @@ -1620,8 +1620,7 @@ XPCJSContext::~XPCJSContext() #ifdef MOZ_GECKO_PROFILER // Tell the profiler that the context is gone - if (PseudoStack* stack = profiler_get_pseudo_stack()) - stack->sampleContext(nullptr); + profiler_clear_js_context(); #endif Preferences::UnregisterCallback(ReloadPrefsCallback, JS_OPTIONS_DOT_STR, this); @@ -3495,8 +3494,7 @@ XPCJSContext::Initialize() JS_SetWrapObjectCallbacks(cx, &WrapObjectCallbacks); js::SetPreserveWrapperCallback(cx, PreserveWrapper); #ifdef MOZ_GECKO_PROFILER - if (PseudoStack* stack = profiler_get_pseudo_stack()) - stack->sampleContext(cx); + profiler_set_js_context(cx); #endif JS_SetAccumulateTelemetryCallback(cx, AccumulateTelemetryCallback); js::SetActivityCallback(cx, ActivityCallback, this); diff --git a/tools/profiler/core/platform.cpp b/tools/profiler/core/platform.cpp index e859d3e169f2..bbeeb9fe5a32 100644 --- a/tools/profiler/core/platform.cpp +++ b/tools/profiler/core/platform.cpp @@ -2949,6 +2949,36 @@ profiler_tracing(const char* aCategory, const char* aInfo, locked_profiler_add_marker(lock, aInfo, marker); } +void +profiler_set_js_context(JSContext* aCx) +{ + // This function runs both on and off the main thread. + + MOZ_ASSERT(aCx); + + PseudoStack* stack = tlsPseudoStack.get(); + if (!stack) { + return; + } + + stack->sampleContext(aCx); +} + +void +profiler_clear_js_context() +{ + // This function runs both on and off the main thread. + + MOZ_RELEASE_ASSERT(gPS); + + PseudoStack* stack = tlsPseudoStack.get(); + if (!stack) { + return; + } + + stack->sampleContext(nullptr); +} + // END externally visible functions //////////////////////////////////////////////////////////////////////// diff --git a/tools/profiler/public/GeckoProfiler.h b/tools/profiler/public/GeckoProfiler.h index 5a98107603a0..58464847bc3a 100644 --- a/tools/profiler/public/GeckoProfiler.h +++ b/tools/profiler/public/GeckoProfiler.h @@ -527,6 +527,9 @@ profiler_get_pseudo_stack(void) return tlsPseudoStack.get(); } +void profiler_set_js_context(JSContext* aCx); +void profiler_clear_js_context(); + class GeckoProfilerReporter final : public nsIMemoryReporter { public: diff --git a/xpcom/build/XPCOMInit.cpp b/xpcom/build/XPCOMInit.cpp index 507738734a53..c147e56e6f30 100644 --- a/xpcom/build/XPCOMInit.cpp +++ b/xpcom/build/XPCOMInit.cpp @@ -1022,9 +1022,7 @@ ShutdownXPCOM(nsIServiceManager* aServMgr) // JS pseudo-stack's internal reference to the main thread JSContext, // duplicating the call in XPCJSContext::~XPCJSContext() in case that // never fired. - if (PseudoStack* stack = profiler_get_pseudo_stack()) { - stack->sampleContext(nullptr); - } + profiler_clear_js_context(); #endif if (sInitializedJS) {