2013-06-18 23:02:07 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
#ifndef mozilla_CycleCollectedJSContext_h
|
|
|
|
#define mozilla_CycleCollectedJSContext_h
|
2013-06-18 23:02:07 +04:00
|
|
|
|
2015-05-02 05:33:01 +03:00
|
|
|
#include <queue>
|
|
|
|
|
2015-03-18 21:36:03 +03:00
|
|
|
#include "mozilla/DeferredFinalize.h"
|
2017-04-25 00:15:46 +03:00
|
|
|
#include "mozilla/LinkedList.h"
|
2015-12-31 16:21:49 +03:00
|
|
|
#include "mozilla/mozalloc.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-06-18 23:02:07 +04:00
|
|
|
#include "jsapi.h"
|
2016-03-02 20:38:24 +03:00
|
|
|
#include "jsfriendapi.h"
|
2013-06-18 23:02:07 +04:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2013-06-18 23:02:15 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-07-09 18:28:15 +04:00
|
|
|
#include "nsTArray.h"
|
2013-06-18 23:02:14 +04:00
|
|
|
|
2013-06-18 23:02:15 +04:00
|
|
|
class nsCycleCollectionNoteRootCallback;
|
2013-09-09 07:28:50 +04:00
|
|
|
class nsIException;
|
2014-10-28 15:08:19 +03:00
|
|
|
class nsIRunnable;
|
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
|
|
|
class nsThread;
|
2015-12-31 16:21:49 +03:00
|
|
|
class nsWrapperCache;
|
2013-06-18 23:02:14 +04:00
|
|
|
|
2013-06-18 23:02:07 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
class CycleCollectedJSRuntime;
|
2013-07-09 18:28:15 +04:00
|
|
|
|
2013-11-21 02:35:16 +04:00
|
|
|
// Contains various stats about the cycle collection.
|
|
|
|
struct CycleCollectorResults
|
|
|
|
{
|
2014-05-07 04:25:26 +04:00
|
|
|
CycleCollectorResults()
|
|
|
|
{
|
|
|
|
// Initialize here so when we increment mNumSlices the first time we're
|
|
|
|
// not using uninitialized memory.
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2013-11-21 02:35:16 +04:00
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
mForcedGC = false;
|
|
|
|
mMergedZones = false;
|
2015-05-13 22:48:52 +03:00
|
|
|
mAnyManual = false;
|
2013-11-21 02:35:16 +04:00
|
|
|
mVisitedRefCounted = 0;
|
|
|
|
mVisitedGCed = 0;
|
|
|
|
mFreedRefCounted = 0;
|
|
|
|
mFreedGCed = 0;
|
2014-10-20 21:07:52 +04:00
|
|
|
mFreedJSZones = 0;
|
2014-05-07 04:25:26 +04:00
|
|
|
mNumSlices = 1;
|
|
|
|
// mNumSlices is initialized to one, because we call Init() after the
|
|
|
|
// per-slice increment of mNumSlices has already occurred.
|
2013-11-21 02:35:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool mForcedGC;
|
|
|
|
bool mMergedZones;
|
2015-05-13 22:48:52 +03:00
|
|
|
bool mAnyManual; // true if any slice of the CC was manually triggered, or at shutdown.
|
2013-11-21 02:35:16 +04:00
|
|
|
uint32_t mVisitedRefCounted;
|
|
|
|
uint32_t mVisitedGCed;
|
|
|
|
uint32_t mFreedRefCounted;
|
|
|
|
uint32_t mFreedGCed;
|
2014-10-20 21:07:52 +04:00
|
|
|
uint32_t mFreedJSZones;
|
2014-05-07 04:25:26 +04:00
|
|
|
uint32_t mNumSlices;
|
2013-11-21 02:35:16 +04:00
|
|
|
};
|
|
|
|
|
2016-09-14 16:47:32 +03:00
|
|
|
class CycleCollectedJSContext
|
2017-04-25 00:15:46 +03:00
|
|
|
: public LinkedListElement<CycleCollectedJSContext>
|
2013-06-18 23:02:07 +04:00
|
|
|
{
|
2017-02-24 00:23:45 +03:00
|
|
|
friend class CycleCollectedJSRuntime;
|
|
|
|
|
2013-06-18 23:02:07 +04:00
|
|
|
protected:
|
2016-09-14 16:47:32 +03:00
|
|
|
CycleCollectedJSContext();
|
|
|
|
virtual ~CycleCollectedJSContext();
|
2013-06-18 23:02:07 +04:00
|
|
|
|
2016-09-16 13:31:37 +03:00
|
|
|
MOZ_IS_CLASS_INIT
|
2017-02-11 02:47:50 +03:00
|
|
|
nsresult Initialize(JSRuntime* aParentRuntime,
|
2016-02-14 16:30:25 +03:00
|
|
|
uint32_t aMaxBytes,
|
|
|
|
uint32_t aMaxNurseryBytes);
|
|
|
|
|
2017-04-24 23:54:27 +03:00
|
|
|
// See explanation in mIsPrimaryContext.
|
|
|
|
MOZ_IS_CLASS_INIT
|
|
|
|
nsresult InitializeNonPrimary(CycleCollectedJSContext* aPrimaryContext);
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
virtual CycleCollectedJSRuntime* CreateRuntime(JSContext* aCx) = 0;
|
2013-06-18 23:02:15 +04:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2013-07-09 18:28:15 +04:00
|
|
|
|
2016-03-24 18:12:00 +03:00
|
|
|
std::queue<nsCOMPtr<nsIRunnable>> mPromiseMicroTaskQueue;
|
|
|
|
std::queue<nsCOMPtr<nsIRunnable>> mDebuggerPromiseMicroTaskQueue;
|
|
|
|
|
2013-06-18 23:02:15 +04:00
|
|
|
private:
|
2017-04-24 23:54:27 +03:00
|
|
|
MOZ_IS_CLASS_INIT
|
|
|
|
void InitializeCommon();
|
|
|
|
|
2016-07-02 03:00:47 +03:00
|
|
|
static JSObject* GetIncumbentGlobalCallback(JSContext* aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
static bool EnqueuePromiseJobCallback(JSContext* aCx,
|
|
|
|
JS::HandleObject aJob,
|
2016-03-22 18:22:23 +03:00
|
|
|
JS::HandleObject aAllocationSite,
|
2016-07-02 03:00:47 +03:00
|
|
|
JS::HandleObject aIncumbentGlobal,
|
2016-02-10 01:40:31 +03:00
|
|
|
void* aData);
|
2016-03-22 18:22:24 +03:00
|
|
|
static void PromiseRejectionTrackerCallback(JSContext* aCx,
|
|
|
|
JS::HandleObject aPromise,
|
|
|
|
PromiseRejectionHandlingState state,
|
|
|
|
void* aData);
|
2013-06-18 23:02:15 +04:00
|
|
|
|
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
|
|
|
void AfterProcessMicrotask(uint32_t aRecursionDepth);
|
2016-01-13 01:37:57 +03:00
|
|
|
public:
|
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
|
|
|
void ProcessStableStateQueue();
|
2016-01-13 01:37:57 +03:00
|
|
|
private:
|
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
|
|
|
void ProcessMetastableStateQueue(uint32_t aRecursionDepth);
|
|
|
|
|
2015-05-13 22:48:52 +03:00
|
|
|
public:
|
2013-07-09 18:28:15 +04:00
|
|
|
enum DeferredFinalizeType {
|
|
|
|
FinalizeIncrementally,
|
|
|
|
FinalizeNow,
|
|
|
|
};
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
CycleCollectedJSRuntime* Runtime() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mRuntime);
|
|
|
|
return mRuntime;
|
|
|
|
}
|
2013-06-18 23:02:14 +04:00
|
|
|
|
2013-09-09 07:28:50 +04:00
|
|
|
already_AddRefed<nsIException> GetPendingException() const;
|
|
|
|
void SetPendingException(nsIException* aException);
|
|
|
|
|
2015-05-02 05:33:01 +03:00
|
|
|
std::queue<nsCOMPtr<nsIRunnable>>& GetPromiseMicroTaskQueue();
|
2016-03-24 18:12:00 +03:00
|
|
|
std::queue<nsCOMPtr<nsIRunnable>>& GetDebuggerPromiseMicroTaskQueue();
|
2014-10-28 15:08:19 +03:00
|
|
|
|
2016-08-11 15:39:22 +03:00
|
|
|
JSContext* Context() const
|
2013-08-30 13:47:19 +04:00
|
|
|
{
|
2016-08-11 15:39:22 +03:00
|
|
|
MOZ_ASSERT(mJSContext);
|
|
|
|
return mJSContext;
|
2013-08-30 13:47:19 +04:00
|
|
|
}
|
2013-09-09 07:28:50 +04:00
|
|
|
|
2016-08-11 15:39:22 +03:00
|
|
|
JS::RootingContext* RootingCx() const
|
2016-06-24 21:19:51 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mJSContext);
|
2016-08-11 15:39:22 +03:00
|
|
|
return JS::RootingContext::get(mJSContext);
|
2016-06-24 21:19:51 +03:00
|
|
|
}
|
|
|
|
|
2016-09-05 18:54:04 +03:00
|
|
|
bool MicroTaskCheckpointDisabled() const
|
|
|
|
{
|
|
|
|
return mDisableMicroTaskCheckpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisableMicroTaskCheckpoint(bool aDisable)
|
|
|
|
{
|
|
|
|
mDisableMicroTaskCheckpoint = aDisable;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MOZ_RAII AutoDisableMicroTaskCheckpoint
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoDisableMicroTaskCheckpoint()
|
2016-09-14 16:47:32 +03:00
|
|
|
: mCCJSCX(CycleCollectedJSContext::Get())
|
2016-09-05 18:54:04 +03:00
|
|
|
{
|
2016-09-14 16:47:32 +03:00
|
|
|
mOldValue = mCCJSCX->MicroTaskCheckpointDisabled();
|
|
|
|
mCCJSCX->DisableMicroTaskCheckpoint(true);
|
2016-09-05 18:54:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoDisableMicroTaskCheckpoint()
|
|
|
|
{
|
2016-09-14 16:47:32 +03:00
|
|
|
mCCJSCX->DisableMicroTaskCheckpoint(mOldValue);
|
2016-09-05 18:54:04 +03:00
|
|
|
}
|
|
|
|
|
2016-09-14 16:47:32 +03:00
|
|
|
CycleCollectedJSContext* mCCJSCX;
|
2016-09-05 18:54:04 +03:00
|
|
|
bool mOldValue;
|
|
|
|
};
|
|
|
|
|
2016-04-19 07:04:32 +03:00
|
|
|
protected:
|
2016-08-11 15:39:22 +03:00
|
|
|
JSContext* MaybeContext() const { return mJSContext; }
|
2016-04-19 07:04:32 +03:00
|
|
|
|
|
|
|
public:
|
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
|
|
|
// nsThread entrypoints
|
|
|
|
virtual void BeforeProcessTask(bool aMightBlock) { };
|
|
|
|
virtual void AfterProcessTask(uint32_t aRecursionDepth);
|
|
|
|
|
|
|
|
// microtask processor entry point
|
|
|
|
void AfterProcessMicrotask();
|
|
|
|
|
|
|
|
uint32_t RecursionDepth();
|
|
|
|
|
|
|
|
// Run in stable state (call through nsContentUtils)
|
|
|
|
void RunInStableState(already_AddRefed<nsIRunnable>&& aRunnable);
|
|
|
|
// This isn't in the spec at all yet, but this gets the behavior we want for IDB.
|
|
|
|
// Runs after the current microtask completes.
|
|
|
|
void RunInMetastableState(already_AddRefed<nsIRunnable>&& aRunnable);
|
|
|
|
|
2016-09-14 16:47:32 +03:00
|
|
|
// Get the current thread's CycleCollectedJSContext. Returns null if there
|
2013-09-09 07:28:50 +04:00
|
|
|
// isn't one.
|
2016-09-14 16:47:32 +03:00
|
|
|
static CycleCollectedJSContext* Get();
|
2013-09-09 07:28:50 +04:00
|
|
|
|
2016-03-24 18:12:00 +03:00
|
|
|
// Queue an async microtask to the current main or worker thread.
|
2016-07-06 01:49:06 +03:00
|
|
|
virtual void DispatchToMicroTask(already_AddRefed<nsIRunnable> aRunnable);
|
2016-03-24 18:12:00 +03:00
|
|
|
|
2015-04-10 18:27:57 +03:00
|
|
|
// Storage for watching rejected promises waiting for some client to
|
|
|
|
// consume their rejection.
|
2016-03-22 18:22:24 +03:00
|
|
|
// Promises in this list have been rejected in the last turn of the
|
|
|
|
// event loop without the rejection being handled.
|
|
|
|
// Note that this can contain nullptrs in place of promises removed because
|
|
|
|
// they're consumed before it'd be reported.
|
|
|
|
JS::PersistentRooted<JS::GCVector<JSObject*, 0, js::SystemAllocPolicy>> mUncaughtRejections;
|
|
|
|
|
|
|
|
// Promises in this list have previously been reported as rejected
|
|
|
|
// (because they were in the above list), but the rejection was handled
|
|
|
|
// in the last turn of the event loop.
|
|
|
|
JS::PersistentRooted<JS::GCVector<JSObject*, 0, js::SystemAllocPolicy>> mConsumedRejections;
|
2015-04-10 18:27:57 +03:00
|
|
|
nsTArray<nsCOMPtr<nsISupports /* UncaughtRejectionObserver */ >> mUncaughtRejectionObservers;
|
|
|
|
|
2013-06-18 23:02:07 +04:00
|
|
|
private:
|
2017-04-24 23:54:27 +03:00
|
|
|
// A primary context owns the mRuntime. Non-main-thread contexts should always
|
|
|
|
// be primary. On the main thread, the primary context should be the first one
|
|
|
|
// created and the last one destroyed. Non-primary contexts are used for
|
|
|
|
// cooperatively scheduled threads.
|
|
|
|
bool mIsPrimaryContext;
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
CycleCollectedJSRuntime* mRuntime;
|
2013-06-18 23:02:15 +04:00
|
|
|
|
2016-06-24 21:19:51 +03:00
|
|
|
JSContext* mJSContext;
|
2013-06-18 23:02:14 +04:00
|
|
|
|
2013-09-09 07:28:50 +04:00
|
|
|
nsCOMPtr<nsIException> mPendingException;
|
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
|
|
|
nsThread* mOwningThread; // Manual refcounting to avoid include hell.
|
2014-05-22 17:18:02 +04:00
|
|
|
|
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
|
|
|
struct RunInMetastableStateData
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> mRunnable;
|
|
|
|
uint32_t mRecursionDepth;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsIRunnable>> mStableStateEvents;
|
|
|
|
nsTArray<RunInMetastableStateData> mMetastableStateEvents;
|
|
|
|
uint32_t mBaseRecursionDepth;
|
|
|
|
bool mDoingStableStates;
|
|
|
|
|
2016-09-05 18:54:04 +03:00
|
|
|
bool mDisableMicroTaskCheckpoint;
|
2013-06-18 23:02:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
#endif // mozilla_CycleCollectedJSContext_h
|