2006-05-10 21:30:15 +04:00
|
|
|
/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2011-10-05 17:11:17 +04:00
|
|
|
#include "base/message_loop.h"
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsBaseAppShell.h"
|
2014-02-13 21:54:10 +04:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
#include "nsExceptionHandler.h"
|
|
|
|
#endif
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2006-05-17 04:25:35 +04:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
#include "mozilla/Services.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// When processing the next thread event, the appshell may process native
|
|
|
|
// events (if not in performance mode), which can result in suppressing the
|
|
|
|
// next thread event for at most this many ticks:
|
2015-08-01 08:14:06 +03:00
|
|
|
#define THREAD_EVENT_STARVATION_LIMIT PR_MillisecondsToInterval(10)
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsBaseAppShell, nsIAppShell, nsIThreadObserver, nsIObserver)
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
nsBaseAppShell::nsBaseAppShell()
|
2007-03-23 02:04:51 +03:00
|
|
|
: mSuspendNativeCount(0)
|
2011-03-29 01:18:44 +04:00
|
|
|
, mEventloopNestingLevel(0)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mBlockedWait(nullptr)
|
2015-04-13 19:02:27 +03:00
|
|
|
, mFavorPerf(0)
|
2014-02-07 10:17:07 +04:00
|
|
|
, mNativeEventPending(false)
|
2015-04-13 19:02:27 +03:00
|
|
|
, mStarvationDelay(0)
|
|
|
|
, mSwitchTime(0)
|
|
|
|
, mLastNativeEventTime(0)
|
2008-02-26 16:36:40 +03:00
|
|
|
, mEventloopNestingState(eEventloopNone)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mRunning(false)
|
|
|
|
, mExiting(false)
|
|
|
|
, mBlockNativeEvent(false)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-03 04:03:03 +04:00
|
|
|
nsBaseAppShell::~nsBaseAppShell()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsresult
|
|
|
|
nsBaseAppShell::Init()
|
|
|
|
{
|
|
|
|
// Configure ourselves as an observer for the current thread:
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThreadInternal> threadInt =
|
|
|
|
do_QueryInterface(NS_GetCurrentThread());
|
|
|
|
NS_ENSURE_STATE(threadInt);
|
|
|
|
|
|
|
|
threadInt->SetObserver(this);
|
2006-05-17 04:25:35 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obsSvc =
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
mozilla::services::GetObserverService();
|
2006-05-17 04:25:35 +04:00
|
|
|
if (obsSvc)
|
2011-10-17 18:59:28 +04:00
|
|
|
obsSvc->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-01-28 02:27:39 +03:00
|
|
|
// Called by nsAppShell's native event callback
|
2006-05-10 21:30:15 +04:00
|
|
|
void
|
2011-01-28 02:27:39 +03:00
|
|
|
nsBaseAppShell::NativeEventCallback()
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2014-02-07 10:17:07 +04:00
|
|
|
if (!mNativeEventPending.exchange(false))
|
2006-05-10 21:30:15 +04:00
|
|
|
return;
|
2006-05-17 04:25:35 +04:00
|
|
|
|
|
|
|
// If DoProcessNextNativeEvent is on the stack, then we assume that we can
|
|
|
|
// just unwind and let nsThread::ProcessNextEvent process the next event.
|
|
|
|
// However, if we are called from a nested native event loop (maybe via some
|
2008-02-26 16:36:40 +03:00
|
|
|
// plug-in or library function), then go ahead and process Gecko events now.
|
|
|
|
if (mEventloopNestingState == eEventloopXPCOM) {
|
|
|
|
mEventloopNestingState = eEventloopOther;
|
|
|
|
// XXX there is a tiny risk we will never get a new NativeEventCallback,
|
|
|
|
// XXX see discussion in bug 389931.
|
2006-05-10 21:30:15 +04:00
|
|
|
return;
|
2006-05-17 04:25:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// nsBaseAppShell::Run is not being used to pump events, so this may be
|
|
|
|
// our only opportunity to process pending gecko events.
|
|
|
|
|
|
|
|
nsIThread *thread = NS_GetCurrentThread();
|
2011-09-29 10:19:26 +04:00
|
|
|
bool prevBlockNativeEvent = mBlockNativeEvent;
|
2011-01-28 02:27:39 +03:00
|
|
|
if (mEventloopNestingState == eEventloopOther) {
|
2008-03-15 04:12:13 +03:00
|
|
|
if (!NS_HasPendingEvents(thread))
|
|
|
|
return;
|
|
|
|
// We're in a nested native event loop and have some gecko events to
|
|
|
|
// process. While doing that we block processing native events from the
|
|
|
|
// appshell - instead, we want to get back to the nested native event
|
|
|
|
// loop ASAP (bug 420148).
|
2011-10-17 18:59:28 +04:00
|
|
|
mBlockNativeEvent = true;
|
2008-03-15 04:12:13 +03:00
|
|
|
}
|
|
|
|
|
2014-02-13 21:54:10 +04:00
|
|
|
IncrementEventloopNestingLevel();
|
2008-03-15 04:12:13 +03:00
|
|
|
EventloopNestingState prevVal = mEventloopNestingState;
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_ProcessPendingEvents(thread, THREAD_EVENT_STARVATION_LIMIT);
|
2011-10-17 18:59:28 +04:00
|
|
|
mProcessedGeckoEvents = true;
|
2008-02-26 16:36:40 +03:00
|
|
|
mEventloopNestingState = prevVal;
|
2008-03-15 04:12:13 +03:00
|
|
|
mBlockNativeEvent = prevBlockNativeEvent;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Continue processing pending events later (we don't want to starve the
|
|
|
|
// embedders event loop).
|
|
|
|
if (NS_HasPendingEvents(thread))
|
2010-11-29 19:25:16 +03:00
|
|
|
DoProcessMoreGeckoEvents();
|
2008-03-25 19:56:04 +03:00
|
|
|
|
2014-02-13 21:54:10 +04:00
|
|
|
DecrementEventloopNestingLevel();
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2010-11-29 19:25:16 +03:00
|
|
|
// Note, this is currently overidden on windows, see comments in nsAppShell for
|
2014-02-07 10:17:07 +04:00
|
|
|
// details.
|
2010-11-29 19:25:16 +03:00
|
|
|
void
|
|
|
|
nsBaseAppShell::DoProcessMoreGeckoEvents()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
OnDispatchedEvent(nullptr);
|
2010-11-29 19:25:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Main thread via OnProcessNextEvent below
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
nsBaseAppShell::DoProcessNextNativeEvent(bool mayWait)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
|
|
|
// The next native event to be processed may trigger our NativeEventCallback,
|
|
|
|
// in which case we do not want it to process any thread events since we'll
|
|
|
|
// do that when this function returns.
|
2006-05-17 04:25:35 +04:00
|
|
|
//
|
|
|
|
// If the next native event is not our NativeEventCallback, then we may end
|
|
|
|
// up recursing into this function.
|
|
|
|
//
|
|
|
|
// However, if the next native event is not our NativeEventCallback, but it
|
|
|
|
// results in another native event loop, then our NativeEventCallback could
|
2008-02-26 16:36:40 +03:00
|
|
|
// fire and it will see mEventloopNestingState as eEventloopOther.
|
2006-05-17 04:25:35 +04:00
|
|
|
//
|
2008-02-26 16:36:40 +03:00
|
|
|
EventloopNestingState prevVal = mEventloopNestingState;
|
|
|
|
mEventloopNestingState = eEventloopXPCOM;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-02-13 21:54:10 +04:00
|
|
|
IncrementEventloopNestingLevel();
|
2011-09-29 10:19:26 +04:00
|
|
|
bool result = ProcessNextNativeEvent(mayWait);
|
2014-02-13 21:54:10 +04:00
|
|
|
DecrementEventloopNestingLevel();
|
2008-02-26 16:36:40 +03:00
|
|
|
|
|
|
|
mEventloopNestingState = prevVal;
|
2006-05-10 21:30:15 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// nsIAppShell methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseAppShell::Run(void)
|
|
|
|
{
|
2009-06-30 20:02:37 +04:00
|
|
|
NS_ENSURE_STATE(!mRunning); // should not call Run twice
|
2011-10-17 18:59:28 +04:00
|
|
|
mRunning = true;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2009-07-20 23:04:52 +04:00
|
|
|
nsIThread *thread = NS_GetCurrentThread();
|
|
|
|
|
2009-06-30 20:02:37 +04:00
|
|
|
MessageLoop::current()->Run();
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2009-07-20 23:04:52 +04:00
|
|
|
NS_ProcessPendingEvents(thread);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mRunning = false;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseAppShell::Exit(void)
|
|
|
|
{
|
2009-06-30 20:02:37 +04:00
|
|
|
if (mRunning && !mExiting) {
|
|
|
|
MessageLoop::current()->Quit();
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mExiting = true;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-04-13 19:02:27 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseAppShell::FavorPerformanceHint(bool favorPerfOverStarvation,
|
|
|
|
uint32_t starvationDelay)
|
|
|
|
{
|
|
|
|
mStarvationDelay = PR_MillisecondsToInterval(starvationDelay);
|
|
|
|
if (favorPerfOverStarvation) {
|
|
|
|
++mFavorPerf;
|
|
|
|
} else {
|
|
|
|
--mFavorPerf;
|
|
|
|
mSwitchTime = PR_IntervalNow();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-23 02:04:51 +03:00
|
|
|
NS_IMETHODIMP
|
2008-03-25 19:56:04 +03:00
|
|
|
nsBaseAppShell::SuspendNative()
|
2007-03-23 02:04:51 +03:00
|
|
|
{
|
|
|
|
++mSuspendNativeCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-03-25 19:56:04 +03:00
|
|
|
nsBaseAppShell::ResumeNative()
|
2007-03-23 02:04:51 +03:00
|
|
|
{
|
|
|
|
--mSuspendNativeCount;
|
|
|
|
NS_ASSERTION(mSuspendNativeCount >= 0, "Unbalanced call to nsBaseAppShell::ResumeNative!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-03-25 19:56:04 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBaseAppShell::GetEventloopNestingLevel(uint32_t* aNestingLevelResult)
|
2008-03-25 19:56:04 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aNestingLevelResult);
|
|
|
|
|
|
|
|
*aNestingLevelResult = mEventloopNestingLevel;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// nsIThreadObserver methods:
|
|
|
|
|
|
|
|
// Called from any thread
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseAppShell::OnDispatchedEvent(nsIThreadInternal *thr)
|
|
|
|
{
|
2008-03-15 04:12:13 +03:00
|
|
|
if (mBlockNativeEvent)
|
|
|
|
return NS_OK;
|
|
|
|
|
2014-02-07 10:17:07 +04:00
|
|
|
if (mNativeEventPending.exchange(true))
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
2008-02-26 16:36:40 +03:00
|
|
|
|
2010-11-29 19:25:16 +03:00
|
|
|
// Returns on the main thread in NativeEventCallback above
|
2006-05-10 21:30:15 +04:00
|
|
|
ScheduleNativeEventCallback();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called from the main thread
|
|
|
|
NS_IMETHODIMP
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
nsBaseAppShell::OnProcessNextEvent(nsIThreadInternal *thr, bool mayWait)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2008-03-15 04:12:13 +03:00
|
|
|
if (mBlockNativeEvent) {
|
|
|
|
if (!mayWait)
|
|
|
|
return NS_OK;
|
|
|
|
// Hmm, we're in a nested native event loop and would like to get
|
|
|
|
// back to it ASAP, but it seems a gecko event has caused us to
|
|
|
|
// spin up a nested XPCOM event loop (eg. modal window), so we
|
|
|
|
// really must start processing native events here again.
|
2011-10-17 18:59:28 +04:00
|
|
|
mBlockNativeEvent = false;
|
2008-03-15 04:12:13 +03:00
|
|
|
if (NS_HasPendingEvents(thr))
|
|
|
|
OnDispatchedEvent(thr); // in case we blocked it earlier
|
|
|
|
}
|
|
|
|
|
2015-04-13 19:02:27 +03:00
|
|
|
PRIntervalTime start = PR_IntervalNow();
|
|
|
|
PRIntervalTime limit = THREAD_EVENT_STARVATION_LIMIT;
|
|
|
|
|
2008-02-26 16:36:40 +03:00
|
|
|
// Unblock outer nested wait loop (below).
|
|
|
|
if (mBlockedWait)
|
2011-10-17 18:59:28 +04:00
|
|
|
*mBlockedWait = false;
|
2008-02-26 16:36:40 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *oldBlockedWait = mBlockedWait;
|
2008-02-26 16:36:40 +03:00
|
|
|
mBlockedWait = &mayWait;
|
|
|
|
|
|
|
|
// When mayWait is true, we need to make sure that there is an event in the
|
|
|
|
// thread's event queue before we return. Otherwise, the thread will block
|
|
|
|
// on its event queue waiting for an event.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool needEvent = mayWait;
|
2010-10-26 03:34:13 +04:00
|
|
|
// Reset prior to invoking DoProcessNextNativeEvent which might cause
|
|
|
|
// NativeEventCallback to process gecko events.
|
2011-10-17 18:59:28 +04:00
|
|
|
mProcessedGeckoEvents = false;
|
2008-02-26 16:36:40 +03:00
|
|
|
|
2015-04-13 19:02:27 +03:00
|
|
|
if (mFavorPerf <= 0 && start > mSwitchTime + mStarvationDelay) {
|
|
|
|
// Favor pending native events
|
|
|
|
PRIntervalTime now = start;
|
|
|
|
bool keepGoing;
|
|
|
|
do {
|
|
|
|
mLastNativeEventTime = now;
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
keepGoing = DoProcessNextNativeEvent(false);
|
2015-04-13 19:02:27 +03:00
|
|
|
} while (keepGoing && ((now = PR_IntervalNow()) - start) < limit);
|
|
|
|
} else {
|
|
|
|
// Avoid starving native events completely when in performance mode
|
|
|
|
if (start - mLastNativeEventTime > limit) {
|
|
|
|
mLastNativeEventTime = start;
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
DoProcessNextNativeEvent(false);
|
2015-04-13 19:02:27 +03:00
|
|
|
}
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2010-10-26 03:34:13 +04:00
|
|
|
while (!NS_HasPendingEvents(thr) && !mProcessedGeckoEvents) {
|
2006-05-10 21:30:15 +04:00
|
|
|
// If we have been asked to exit from Run, then we should not wait for
|
2008-02-26 16:36:40 +03:00
|
|
|
// events to process. Note that an inner nested event loop causes
|
|
|
|
// 'mayWait' to become false too, through 'mBlockedWait'.
|
|
|
|
if (mExiting)
|
2011-10-17 18:59:28 +04:00
|
|
|
mayWait = false;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2015-04-13 19:02:27 +03:00
|
|
|
mLastNativeEventTime = PR_IntervalNow();
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
if (!DoProcessNextNativeEvent(mayWait) || !mayWait)
|
2006-05-10 21:30:15 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-02-26 16:36:40 +03:00
|
|
|
mBlockedWait = oldBlockedWait;
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// Make sure that the thread event queue does not block on its monitor, as
|
|
|
|
// it normally would do if it did not have any pending events. To avoid
|
|
|
|
// that, we simply insert a dummy event into its queue during shutdown.
|
2012-04-07 00:40:10 +04:00
|
|
|
if (needEvent && !mExiting && !NS_HasPendingEvents(thr)) {
|
|
|
|
DispatchDummyEvent(thr);
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-05-17 04:25:35 +04:00
|
|
|
|
2012-04-07 00:40:10 +04:00
|
|
|
bool
|
|
|
|
nsBaseAppShell::DispatchDummyEvent(nsIThread* aTarget)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (!mDummyEvent)
|
2017-01-18 03:50:34 +03:00
|
|
|
mDummyEvent = new mozilla::Runnable("DummyEvent");
|
2012-04-07 00:40:10 +04:00
|
|
|
|
|
|
|
return NS_SUCCEEDED(aTarget->Dispatch(mDummyEvent, NS_DISPATCH_NORMAL));
|
|
|
|
}
|
|
|
|
|
2014-02-13 21:54:10 +04:00
|
|
|
void
|
|
|
|
nsBaseAppShell::IncrementEventloopNestingLevel()
|
|
|
|
{
|
|
|
|
++mEventloopNestingLevel;
|
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
CrashReporter::SetEventloopNestingLevel(mEventloopNestingLevel);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsBaseAppShell::DecrementEventloopNestingLevel()
|
|
|
|
{
|
|
|
|
--mEventloopNestingLevel;
|
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
CrashReporter::SetEventloopNestingLevel(mEventloopNestingLevel);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-06-07 04:06:11 +04:00
|
|
|
// Called from the main thread
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseAppShell::AfterProcessNextEvent(nsIThreadInternal *thr,
|
2013-10-23 16:01:20 +04:00
|
|
|
bool eventWasProcessed)
|
2006-06-07 04:06:11 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-05-17 04:25:35 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseAppShell::Observe(nsISupports *subject, const char *topic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *data)
|
2006-05-17 04:25:35 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!strcmp(topic, NS_XPCOM_SHUTDOWN_OBSERVER_ID), "oops");
|
|
|
|
Exit();
|
|
|
|
return NS_OK;
|
|
|
|
}
|