From eacb809d5fcc8c35f7ec530973b29da7fed1cb75 Mon Sep 17 00:00:00 2001 From: Kyle Huey Date: Sun, 22 May 2016 08:30:03 -0400 Subject: [PATCH] Bug 1274114: Factor out main thread specific parts of event processing. r=froydnj --- xpcom/threads/nsThread.cpp | 106 ++++++++++++++++++++----------------- xpcom/threads/nsThread.h | 3 ++ 2 files changed, 59 insertions(+), 50 deletions(-) diff --git a/xpcom/threads/nsThread.cpp b/xpcom/threads/nsThread.cpp index 61f5b6607918..4a8a38634168 100644 --- a/xpcom/threads/nsThread.cpp +++ b/xpcom/threads/nsThread.cpp @@ -966,11 +966,6 @@ nsThread::ProcessNextEvent(bool aMayWait, bool* aResult) LOG(("THRD(%p) ProcessNextEvent [%u %u]\n", this, aMayWait, mNestedEventLoopDepth)); - // If we're on the main thread, we shouldn't be dispatching CPOWs. - if (mIsMainThread == MAIN_THREAD) { - ipc::CancelCPOWs(); - } - if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) { return NS_ERROR_NOT_SAME_THREAD; } @@ -985,53 +980,10 @@ nsThread::ProcessNextEvent(bool aMayWait, bool* aResult) // and repeat the nested event loop since its state change hasn't happened yet. bool reallyWait = aMayWait && (mNestedEventLoopDepth > 0 || !ShuttingDown()); - if (MAIN_THREAD == mIsMainThread && reallyWait) { - HangMonitor::Suspend(); + if (mIsMainThread == MAIN_THREAD) { + DoMainThreadSpecificProcessing(reallyWait); } - // Fire a memory pressure notification, if we're the main thread and one is - // pending. - if (MAIN_THREAD == mIsMainThread && !ShuttingDown()) { - MemoryPressureState mpPending = NS_GetPendingMemoryPressure(); - if (mpPending != MemPressure_None) { - nsCOMPtr os = services::GetObserverService(); - - // Use no-forward to prevent the notifications from being transferred to - // the children of this process. - NS_NAMED_LITERAL_STRING(lowMem, "low-memory-no-forward"); - NS_NAMED_LITERAL_STRING(lowMemOngoing, "low-memory-ongoing-no-forward"); - - if (os) { - os->NotifyObservers(nullptr, "memory-pressure", - mpPending == MemPressure_New ? lowMem.get() : - lowMemOngoing.get()); - } else { - NS_WARNING("Can't get observer service!"); - } - } - } - -#ifdef MOZ_CRASHREPORTER - if (MAIN_THREAD == mIsMainThread && !ShuttingDown()) { - // Keep an eye on memory usage (cheap, ~7ms) somewhat frequently, - // but save memory reports (expensive, ~75ms) less frequently. - const size_t LOW_MEMORY_CHECK_SECONDS = 30; - const size_t LOW_MEMORY_SAVE_SECONDS = 3 * 60; - - static TimeStamp nextCheck = TimeStamp::NowLoRes() - + TimeDuration::FromSeconds(LOW_MEMORY_CHECK_SECONDS); - - TimeStamp now = TimeStamp::NowLoRes(); - if (now >= nextCheck) { - if (SaveMemoryReportNearOOM()) { - nextCheck = now + TimeDuration::FromSeconds(LOW_MEMORY_SAVE_SECONDS); - } else { - nextCheck = now + TimeDuration::FromSeconds(LOW_MEMORY_CHECK_SECONDS); - } - } - } -#endif - ++mNestedEventLoopDepth; bool callScriptObserver = !!mScriptObserver; @@ -1281,6 +1233,60 @@ nsThread::SetScriptObserver(mozilla::CycleCollectedJSRuntime* aScriptObserver) mScriptObserver = aScriptObserver; } +void +nsThread::DoMainThreadSpecificProcessing(bool aReallyWait) +{ + MOZ_ASSERT(mIsMainThread == MAIN_THREAD); + + ipc::CancelCPOWs(); + + if (aReallyWait) { + HangMonitor::Suspend(); + } + + // Fire a memory pressure notification, if one is pending. + if (!ShuttingDown()) { + MemoryPressureState mpPending = NS_GetPendingMemoryPressure(); + if (mpPending != MemPressure_None) { + nsCOMPtr os = services::GetObserverService(); + + // Use no-forward to prevent the notifications from being transferred to + // the children of this process. + NS_NAMED_LITERAL_STRING(lowMem, "low-memory-no-forward"); + NS_NAMED_LITERAL_STRING(lowMemOngoing, "low-memory-ongoing-no-forward"); + + if (os) { + os->NotifyObservers(nullptr, "memory-pressure", + mpPending == MemPressure_New ? lowMem.get() : + lowMemOngoing.get()); + } else { + NS_WARNING("Can't get observer service!"); + } + } + } + +#ifdef MOZ_CRASHREPORTER + if (!ShuttingDown()) { + // Keep an eye on memory usage (cheap, ~7ms) somewhat frequently, + // but save memory reports (expensive, ~75ms) less frequently. + const size_t LOW_MEMORY_CHECK_SECONDS = 30; + const size_t LOW_MEMORY_SAVE_SECONDS = 3 * 60; + + static TimeStamp nextCheck = TimeStamp::NowLoRes() + + TimeDuration::FromSeconds(LOW_MEMORY_CHECK_SECONDS); + + TimeStamp now = TimeStamp::NowLoRes(); + if (now >= nextCheck) { + if (SaveMemoryReportNearOOM()) { + nextCheck = now + TimeDuration::FromSeconds(LOW_MEMORY_SAVE_SECONDS); + } else { + nextCheck = now + TimeDuration::FromSeconds(LOW_MEMORY_CHECK_SECONDS); + } + } + } +#endif +} + //----------------------------------------------------------------------------- NS_IMPL_ISUPPORTS(nsThread::nsNestedEventTarget, nsIEventTarget) diff --git a/xpcom/threads/nsThread.h b/xpcom/threads/nsThread.h index 4e152c763a15..54116006fa86 100644 --- a/xpcom/threads/nsThread.h +++ b/xpcom/threads/nsThread.h @@ -78,6 +78,9 @@ public: void WaitForAllAsynchronousShutdowns(); +private: + void DoMainThreadSpecificProcessing(bool aReallyWait); + protected: class nsChainedEventQueue;