From 43a8113072b96affb2b7a3a7a4e965547d3d0c41 Mon Sep 17 00:00:00 2001 From: Gaming4JC Date: Wed, 22 Jan 2020 19:28:33 -0500 Subject: Bug 1405821 - Move microtask handling to CycleCollectedJSContext Tag UXP Issue #1344 --- xpcom/base/CycleCollectedJSContext.cpp | 12 +++++++- xpcom/base/CycleCollectedJSContext.h | 51 ++++++++++++++++++++++++++++++++++ xpcom/base/moz.build | 1 + 3 files changed, 63 insertions(+), 1 deletion(-) (limited to 'xpcom') diff --git a/xpcom/base/CycleCollectedJSContext.cpp b/xpcom/base/CycleCollectedJSContext.cpp index e16c15455..0a85ae6ac 100644 --- a/xpcom/base/CycleCollectedJSContext.cpp +++ b/xpcom/base/CycleCollectedJSContext.cpp @@ -79,6 +79,7 @@ #include "nsCycleCollectionParticipant.h" #include "nsCycleCollector.h" #include "nsDOMJSUtils.h" +#include "nsDOMMutationObserver.h" #include "nsJSUtils.h" #include "nsWrapperCache.h" @@ -438,6 +439,7 @@ CycleCollectedJSContext::CycleCollectedJSContext() , mJSHolders(256) , mDoingStableStates(false) , mDisableMicroTaskCheckpoint(false) + , mMicroTaskLevel(0) , mOutOfMemoryState(OOMState::OK) , mLargeAllocationFailureState(OOMState::OK) { @@ -1379,7 +1381,7 @@ CycleCollectedJSContext::AfterProcessTask(uint32_t aRecursionDepth) // Step 4.1: Execute microtasks. if (!mDisableMicroTaskCheckpoint) { if (NS_IsMainThread()) { - nsContentUtils::PerformMainThreadMicroTaskCheckpoint(); + PerformMainThreadMicroTaskCheckpoint(); Promise::PerformMicroTaskCheckpoint(); } else { Promise::PerformWorkerMicroTaskCheckpoint(); @@ -1659,6 +1661,14 @@ CycleCollectedJSContext::DispatchToMicroTask(already_AddRefed aRunn mPromiseMicroTaskQueue.push(runnable.forget()); } +void +CycleCollectedJSContext::PerformMainThreadMicroTaskCheckpoint() +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsDOMMutationObserver::HandleMutations(); +} + void CycleCollectedJSContext::EnvironmentPreparer::invoke(JS::HandleObject scope, js::ScriptEnvironmentPreparer::Closure& closure) diff --git a/xpcom/base/CycleCollectedJSContext.h b/xpcom/base/CycleCollectedJSContext.h index ac4cf4361..2197eae92 100644 --- a/xpcom/base/CycleCollectedJSContext.h +++ b/xpcom/base/CycleCollectedJSContext.h @@ -402,6 +402,37 @@ public: // Queue an async microtask to the current main or worker thread. virtual void DispatchToMicroTask(already_AddRefed aRunnable); + // Call EnterMicroTask when you're entering JS execution. + // Usually the best way to do this is to use nsAutoMicroTask. + void EnterMicroTask() + { + ++mMicroTaskLevel; + } + + void LeaveMicroTask() + { + if (--mMicroTaskLevel == 0) { + PerformMainThreadMicroTaskCheckpoint(); + } + } + + bool IsInMicroTask() + { + return mMicroTaskLevel != 0; + } + + uint32_t MicroTaskLevel() + { + return mMicroTaskLevel; + } + + void SetMicroTaskLevel(uint32_t aLevel) + { + mMicroTaskLevel = aLevel; + } + + void PerformMainThreadMicroTaskCheckpoint(); + // Storage for watching rejected promises waiting for some client to // consume their rejection. @@ -452,6 +483,7 @@ private: bool mDisableMicroTaskCheckpoint; + uint32_t mMicroTaskLevel; OOMState mOutOfMemoryState; OOMState mLargeAllocationFailureState; @@ -470,6 +502,25 @@ private: EnvironmentPreparer mEnvironmentPreparer; }; +class MOZ_STACK_CLASS nsAutoMicroTask +{ +public: + nsAutoMicroTask() + { + CycleCollectedJSContext* ccjs = CycleCollectedJSContext::Get(); + if (ccjs) { + ccjs->EnterMicroTask(); + } + } + ~nsAutoMicroTask() + { + CycleCollectedJSContext* ccjs = CycleCollectedJSContext::Get(); + if (ccjs) { + ccjs->LeaveMicroTask(); + } + } +}; + void TraceScriptHolder(nsISupports* aHolder, JSTracer* aTracer); // Returns true if the JS::TraceKind is one the cycle collector cares about. diff --git a/xpcom/base/moz.build b/xpcom/base/moz.build index d6a336b40..0fdf47d7d 100644 --- a/xpcom/base/moz.build +++ b/xpcom/base/moz.build @@ -152,6 +152,7 @@ FINAL_LIBRARY = 'xul' LOCAL_INCLUDES += [ '../build', + '/dom/base', '/xpcom/ds', ] -- cgit v1.2.3