gecko-dev/xpcom/threads
Kyle Huey 76e3009ab8 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 06:10:46 -07:00
..
AbstractThread.cpp Bug 1146086: use promise to Init() in PlatformDecoderModule. r=jya,r=cpearce 2015-08-11 14:09:12 +10:00
AbstractThread.h Bug 1146086: use promise to Init() in PlatformDecoderModule. r=jya,r=cpearce 2015-08-11 14:09:12 +10:00
BackgroundHangMonitor.cpp Bug 1134279 - Change Telemetry data producers to use the correct recording flags. r=vladan 2015-03-23 04:39:00 +01:00
BackgroundHangMonitor.h Bug 1182996 - Fix and add missing namespace comments. rs=ehsan 2015-07-13 08:25:42 -07:00
HangAnnotations.cpp Bug 1145631 - Part 1: Replace MOZ_OVERRIDE and MOZ_FINAL with override and final in the tree; r=froydnj 2015-03-21 12:28:04 -04:00
HangAnnotations.h Bug 1128768: Part 2 - Refactor hang annotation code; r=vladan 2015-03-09 19:41:24 -06:00
HangMonitor.cpp Bug 1172216 - Move nsStackwalk to mozglue. r=glandium 2015-06-10 16:32:45 -04:00
HangMonitor.h Bug 1128768: Part 2 - Refactor hang annotation code; r=vladan 2015-03-09 19:41:24 -06:00
LazyIdleThread.cpp Bug 1179909: Refactor stable state handling. r=smaug 2015-08-11 06:10:46 -07:00
LazyIdleThread.h Bug 1155059: Patch 1&2 - Convert Dispatch() and friends to already_AddRefed<> r=froydnj 2015-07-09 23:21:46 -04:00
SharedThreadPool.cpp Bug 1190496 - Hoist SharedThreadPool into xpcom. r=froydnj 2015-08-04 14:00:58 -07:00
SharedThreadPool.h Bug 1191063 - Followup comments. r=me DONTBUILD 2015-08-04 17:36:06 -07:00
SyncRunnable.h Bug 1060973 - Fix more bad implicit constructors in XPCOM; r=froydnj 2014-09-02 09:50:07 -04:00
TaskDispatcher.h Bug 1190492 - Hoist AbstractThread and TaskDispatcher to xpcom. r=froydnj 2015-08-07 16:38:35 -07:00
TaskQueue.cpp Bug 1190495 - Hoist TaskQueue into xpcom. r=froydnj 2015-08-11 08:55:22 -04:00
TaskQueue.h Bug 1190495 - Hoist TaskQueue into xpcom. r=froydnj 2015-08-11 08:55:22 -04:00
ThreadStackHelper.cpp Bug 1182996 - Fix and add missing namespace comments. rs=ehsan 2015-07-13 08:25:42 -07:00
ThreadStackHelper.h Bug 1045176 - Unbreak build on non-SPS platforms after bug 1016629. r=nchen 2014-07-28 12:29:00 +02:00
TimerThread.cpp Bug 1190735 - Remove nsITimer.TYPE_REPEATING_PRECISE. r=froydnj. 2015-08-04 17:30:53 -07:00
TimerThread.h Bug 1059572 - Part 1: Move PostTimerEvent to TimerThread to allow TimerThread's monitor to protect it. r=nfroyd 2015-07-22 12:39:34 -05:00
moz.build Bug 1190495 - Hoist TaskQueue into xpcom. r=froydnj 2015-08-11 08:55:22 -04:00
nsEnvironment.cpp Bug 1150197 - Remove useless null checks after allocating memory with new from xpcom/threads/; r=froydnj 2015-04-02 14:57:46 -04:00
nsEnvironment.h Bug 1145631 - Part 1: Replace MOZ_OVERRIDE and MOZ_FINAL with override and final in the tree; r=froydnj 2015-03-21 12:28:04 -04:00
nsEventQueue.cpp Bug 1182516 - Add Chaos Mode environment variable MOZ_CHAOSMODE. r=roc 2015-07-14 17:29:23 -04:00
nsEventQueue.h Bug 1155059: Patch 1&2 - Convert Dispatch() and friends to already_AddRefed<> r=froydnj 2015-07-09 23:21:46 -04:00
nsICancelableRunnable.idl
nsIEnvironment.idl
nsIEventTarget.idl Bug 1155059: Patch 1&2 - Convert Dispatch() and friends to already_AddRefed<> r=froydnj 2015-07-09 23:21:46 -04:00
nsIProcess.idl
nsIRunnable.idl
nsISupportsPriority.idl
nsIThread.idl
nsIThreadInternal.idl Bug 1179909: Refactor stable state handling. r=smaug 2015-08-11 06:10:46 -07:00
nsIThreadManager.idl Backout change b371db089894 (bug 1138620) due to backout of bug 970307. 2015-05-19 11:26:24 +08:00
nsIThreadPool.idl Bug 1155059: Patch 1&2 - Convert Dispatch() and friends to already_AddRefed<> r=froydnj 2015-07-09 23:21:46 -04:00
nsITimer.idl Bug 1190735 - Remove nsITimer.TYPE_REPEATING_PRECISE. r=froydnj. 2015-08-04 17:30:53 -07:00
nsMemoryPressure.cpp
nsMemoryPressure.h
nsProcess.h Bug 1145631 - Part 1: Replace MOZ_OVERRIDE and MOZ_FINAL with override and final in the tree; r=froydnj 2015-03-21 12:28:04 -04:00
nsProcessCommon.cpp Bug 1134923 - Remove NS_Alloc/NS_Realloc/NS_Free. r=nfroyd 2015-05-01 09:40:30 +09:00
nsThread.cpp Bug 1179909: Refactor stable state handling. r=smaug 2015-08-11 06:10:46 -07:00
nsThread.h Bug 1179909: Refactor stable state handling. r=smaug 2015-08-11 06:10:46 -07:00
nsThreadManager.cpp Bug 1185470: Remove 'Get' prefixes from hashtable iterator methods. r=froydnj 2015-07-20 20:21:28 +08:00
nsThreadManager.h Backout change 39e167bbd14c, a80140872ea5, adae9be2294d and b71ccef9c674 (bug 970307). 2015-05-19 14:31:25 +08:00
nsThreadPool.cpp Bug 1155059: Patch 1&2 - Convert Dispatch() and friends to already_AddRefed<> r=froydnj 2015-07-09 23:21:46 -04:00
nsThreadPool.h Bug 1155059: Patch 1&2 - Convert Dispatch() and friends to already_AddRefed<> r=froydnj 2015-07-09 23:21:46 -04:00
nsTimerImpl.cpp Bug 1190735 - Remove nsITimer.TYPE_REPEATING_PRECISE. r=froydnj. 2015-08-04 17:30:53 -07:00
nsTimerImpl.h Bug 1059572 - Part 1: Move PostTimerEvent to TimerThread to allow TimerThread's monitor to protect it. r=nfroyd 2015-07-22 12:39:34 -05:00