2014-06-30 19:39:45 +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: */
|
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/. */
|
1999-04-02 13:20:44 +04:00
|
|
|
|
2013-10-23 16:01:24 +04:00
|
|
|
#include "nsThread.h"
|
|
|
|
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
|
|
|
|
// Chromium's logging can sometimes leak through...
|
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
|
|
|
|
2015-05-19 09:31:25 +03:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2013-07-09 06:15:34 +04:00
|
|
|
#include "nsMemoryPressure.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadManager.h"
|
|
|
|
#include "nsIClassInfoImpl.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2015-09-15 04:24:43 +03:00
|
|
|
#include "nsQueryObject.h"
|
2013-07-12 00:21:45 +04:00
|
|
|
#include "pratom.h"
|
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
|
|
|
#include "mozilla/CycleCollectedJSRuntime.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2011-08-06 02:10:50 +04:00
|
|
|
#include "nsIObserverService.h"
|
2011-10-12 21:52:26 +04:00
|
|
|
#include "mozilla/HangMonitor.h"
|
2014-04-09 08:57:52 +04:00
|
|
|
#include "mozilla/IOInterposer.h"
|
2014-08-06 08:43:36 +04:00
|
|
|
#include "mozilla/ipc/MessageChannel.h"
|
2015-04-03 16:52:00 +03:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
2011-08-06 02:10:50 +04:00
|
|
|
#include "mozilla/Services.h"
|
2013-07-08 00:23:43 +04:00
|
|
|
#include "nsXPCOMPrivate.h"
|
2014-03-03 09:12:27 +04:00
|
|
|
#include "mozilla/ChaosMode.h"
|
2015-04-03 16:52:00 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2016-02-03 21:59:26 +03:00
|
|
|
#include "mozilla/unused.h"
|
2015-10-06 05:00:59 +03:00
|
|
|
#include "nsThreadSyncDispatch.h"
|
2015-10-06 05:00:59 +03:00
|
|
|
#include "LeakRefPtr.h"
|
2014-03-03 09:12:27 +04:00
|
|
|
|
2014-08-30 09:21:22 +04:00
|
|
|
#ifdef MOZ_CRASHREPORTER
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsICrashReporter.h"
|
|
|
|
#endif
|
|
|
|
|
2015-08-28 12:57:44 +03:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
#include "private/pprthred.h"
|
|
|
|
#endif
|
|
|
|
|
2014-03-03 09:12:27 +04:00
|
|
|
#ifdef XP_LINUX
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#endif
|
1999-04-02 13:20:44 +04:00
|
|
|
|
2010-10-27 19:16:03 +04:00
|
|
|
#define HAVE_UALARM _BSD_SOURCE || (_XOPEN_SOURCE >= 500 || \
|
|
|
|
_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) && \
|
|
|
|
!(_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700)
|
|
|
|
|
2014-03-03 09:12:27 +04:00
|
|
|
#if defined(XP_LINUX) && !defined(ANDROID) && defined(_GNU_SOURCE)
|
|
|
|
#define HAVE_SCHED_SETAFFINITY
|
|
|
|
#endif
|
|
|
|
|
2015-12-01 06:16:40 +03:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
#include <mach/mach.h>
|
|
|
|
#include <mach/thread_policy.h>
|
|
|
|
#endif
|
|
|
|
|
2013-10-30 00:58:09 +04:00
|
|
|
#ifdef MOZ_CANARY
|
2010-10-27 19:16:03 +04:00
|
|
|
# include <unistd.h>
|
|
|
|
# include <execinfo.h>
|
|
|
|
# include <signal.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
# include "nsXULAppAPI.h"
|
|
|
|
#endif
|
|
|
|
|
2010-05-20 03:22:19 +04:00
|
|
|
#if defined(NS_FUNCTION_TIMER) && defined(_MSC_VER)
|
|
|
|
#include "nsTimerImpl.h"
|
2015-06-10 23:32:45 +03:00
|
|
|
#include "mozilla/StackWalk.h"
|
2010-05-20 03:22:19 +04:00
|
|
|
#endif
|
|
|
|
#ifdef NS_FUNCTION_TIMER
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#endif
|
|
|
|
|
2014-03-27 12:49:06 +04:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
#include "GeckoTaskTracer.h"
|
2015-06-16 05:57:19 +03:00
|
|
|
#include "TracedTaskCommon.h"
|
2014-03-27 12:49:06 +04:00
|
|
|
using namespace mozilla::tasktracer;
|
|
|
|
#endif
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2015-10-19 22:50:14 +03:00
|
|
|
static LazyLogModule sThreadLog("nsThread");
|
2013-11-19 06:34:00 +04:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
2015-10-19 22:50:14 +03:00
|
|
|
#define LOG(args) MOZ_LOG(sThreadLog, mozilla::LogLevel::Debug, args)
|
1999-04-02 13:20:44 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_DECL_CI_INTERFACE_GETTER(nsThread)
|
1999-04-02 13:20:44 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Because we do not have our own nsIFactory, we have to implement nsIClassInfo
|
|
|
|
// somewhat manually.
|
2000-11-30 08:24:53 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
class nsThreadClassInfo : public nsIClassInfo
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED // no mRefCnt
|
|
|
|
NS_DECL_NSICLASSINFO
|
2002-08-25 09:55:50 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadClassInfo()
|
|
|
|
{
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
1999-04-02 13:20:44 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsThreadClassInfo::AddRef()
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsThreadClassInfo::Release()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(nsThreadClassInfo, nsIClassInfo)
|
1999-04-06 01:02:24 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadClassInfo::GetInterfaces(uint32_t* aCount, nsIID*** aArray)
|
1999-04-06 01:02:24 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
return NS_CI_INTERFACE_GETTER_NAME(nsThread)(aCount, aArray);
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2015-03-29 17:52:54 +03:00
|
|
|
nsThreadClassInfo::GetScriptableHelper(nsIXPCScriptable** aResult)
|
1999-10-02 03:30:06 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-10-02 03:30:06 +04:00
|
|
|
}
|
|
|
|
|
1999-04-02 13:20:44 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadClassInfo::GetContractID(char** aResult)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadClassInfo::GetClassDescription(char** aResult)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadClassInfo::GetClassID(nsCID** aResult)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadClassInfo::GetFlags(uint32_t* aResult)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = THREADSAFE;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadClassInfo::GetClassIDNoAlloc(nsCID* aResult)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2013-07-19 06:31:26 +04:00
|
|
|
NS_IMPL_ADDREF(nsThread)
|
|
|
|
NS_IMPL_RELEASE(nsThread)
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsThread)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIThread)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIThreadInternal)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIEventTarget)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsPriority)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIThread)
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIClassInfo))) {
|
2013-05-31 01:20:02 +04:00
|
|
|
static nsThreadClassInfo sThreadClassInfo;
|
2007-07-08 11:08:04 +04:00
|
|
|
foundInterface = static_cast<nsIClassInfo*>(&sThreadClassInfo);
|
2006-05-10 21:30:15 +04:00
|
|
|
} else
|
|
|
|
NS_INTERFACE_MAP_END
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsThread, nsIThread, nsIThreadInternal,
|
|
|
|
nsIEventTarget, nsISupportsPriority)
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class nsThreadStartupEvent : public Runnable
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
public:
|
2013-11-20 01:27:37 +04:00
|
|
|
nsThreadStartupEvent()
|
|
|
|
: mMon("nsThreadStartupEvent.mMon")
|
2014-05-27 11:15:35 +04:00
|
|
|
, mInitialized(false)
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method does not return until the thread startup object is in the
|
|
|
|
// completion state.
|
2014-05-27 11:15:35 +04:00
|
|
|
void Wait()
|
|
|
|
{
|
|
|
|
if (mInitialized) {
|
|
|
|
// Maybe avoid locking...
|
2006-05-10 21:30:15 +04:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mMon);
|
2014-05-27 11:15:35 +04:00
|
|
|
while (!mInitialized) {
|
2006-05-10 21:30:15 +04:00
|
|
|
mon.Wait();
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 22:13:20 +04:00
|
|
|
// This method needs to be public to support older compilers (xlC_r on AIX).
|
|
|
|
// It should be called directly as this class type is reference counted.
|
2014-08-25 23:17:15 +04:00
|
|
|
virtual ~nsThreadStartupEvent() {}
|
2006-05-10 22:13:20 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
private:
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mMon);
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitialized = true;
|
2006-05-10 21:30:15 +04:00
|
|
|
mon.Notify();
|
1999-04-02 13:20:44 +04:00
|
|
|
return NS_OK;
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitor mMon;
|
2014-05-27 11:15:35 +04:00
|
|
|
bool mInitialized;
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
2016-05-13 01:15:43 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class DelayedRunnable : public Runnable,
|
|
|
|
public nsITimerCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DelayedRunnable(already_AddRefed<nsIRunnable> aRunnable,
|
|
|
|
uint32_t aDelay)
|
|
|
|
: mWrappedRunnable(aRunnable),
|
|
|
|
mDelayedFrom(TimeStamp::NowLoRes()),
|
|
|
|
mDelay(aDelay)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
nsresult Init()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mTimer);
|
|
|
|
return mTimer->InitWithCallback(this, mDelay, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult DoRun()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> r = mWrappedRunnable.forget();
|
|
|
|
return r->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override
|
|
|
|
{
|
|
|
|
// Already ran?
|
|
|
|
if (!mWrappedRunnable) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we too early?
|
|
|
|
if ((TimeStamp::NowLoRes() - mDelayedFrom).ToMilliseconds() < mDelay) {
|
|
|
|
return NS_OK; // Let the nsITimer run us.
|
|
|
|
}
|
|
|
|
|
|
|
|
mTimer->Cancel();
|
|
|
|
return DoRun();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Notify(nsITimer* aTimer) override
|
|
|
|
{
|
|
|
|
// If we already ran, the timer should have been canceled.
|
|
|
|
MOZ_ASSERT(mWrappedRunnable);
|
|
|
|
MOZ_ASSERT(aTimer == mTimer);
|
|
|
|
|
|
|
|
return DoRun();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~DelayedRunnable() {}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> mWrappedRunnable;
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
TimeStamp mDelayedFrom;
|
|
|
|
uint32_t mDelay;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(DelayedRunnable, Runnable, nsITimerCallback)
|
|
|
|
|
|
|
|
} // anonymous namespace
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
struct nsThreadShutdownContext
|
|
|
|
{
|
2016-04-20 04:49:48 +03:00
|
|
|
nsThreadShutdownContext(nsThread* aTerminatingThread,
|
|
|
|
nsThread* aJoiningThread,
|
|
|
|
bool aAwaitingShutdownAck)
|
|
|
|
: mTerminatingThread(aTerminatingThread)
|
|
|
|
, mJoiningThread(aJoiningThread)
|
|
|
|
, mAwaitingShutdownAck(aAwaitingShutdownAck)
|
2016-02-03 21:59:26 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsThreadShutdownContext);
|
|
|
|
}
|
|
|
|
~nsThreadShutdownContext()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsThreadShutdownContext);
|
|
|
|
}
|
|
|
|
|
2015-09-15 04:24:43 +03:00
|
|
|
// NB: This will be the last reference.
|
2016-04-20 04:46:35 +03:00
|
|
|
RefPtr<nsThread> mTerminatingThread;
|
|
|
|
nsThread* mJoiningThread;
|
|
|
|
bool mAwaitingShutdownAck;
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// This event is responsible for notifying nsThread::Shutdown that it is time
|
2015-09-15 04:24:43 +03:00
|
|
|
// to call PR_JoinThread. It implements nsICancelableRunnable so that it can
|
|
|
|
// run on a DOM Worker thread (where all events must implement
|
|
|
|
// nsICancelableRunnable.)
|
2016-04-26 03:23:21 +03:00
|
|
|
class nsThreadShutdownAckEvent : public CancelableRunnable
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
public:
|
2014-08-05 17:36:59 +04:00
|
|
|
explicit nsThreadShutdownAckEvent(nsThreadShutdownContext* aCtx)
|
2014-05-27 11:15:35 +04:00
|
|
|
: mShutdownContext(aCtx)
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
2015-09-15 04:24:43 +03:00
|
|
|
NS_IMETHOD Run() override
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
2016-04-20 04:46:35 +03:00
|
|
|
mShutdownContext->mTerminatingThread->ShutdownComplete(mShutdownContext);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult Cancel() override
|
2015-09-15 04:24:43 +03:00
|
|
|
{
|
|
|
|
return Run();
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
private:
|
2015-09-15 04:24:43 +03:00
|
|
|
virtual ~nsThreadShutdownAckEvent() { }
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadShutdownContext* mShutdownContext;
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// This event is responsible for setting mShutdownContext
|
2016-04-26 03:23:21 +03:00
|
|
|
class nsThreadShutdownEvent : public Runnable
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
public:
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadShutdownEvent(nsThread* aThr, nsThreadShutdownContext* aCtx)
|
|
|
|
: mThread(aThr)
|
|
|
|
, mShutdownContext(aCtx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
mThread->mShutdownContext = mShutdownContext;
|
2013-10-23 16:01:24 +04:00
|
|
|
MessageLoop::current()->Quit();
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsThread> mThread;
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadShutdownContext* mShutdownContext;
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-12-01 06:16:40 +03:00
|
|
|
static void
|
|
|
|
SetThreadAffinity(unsigned int cpu)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SCHED_SETAFFINITY
|
|
|
|
cpu_set_t cpus;
|
|
|
|
CPU_ZERO(&cpus);
|
|
|
|
CPU_SET(cpu, &cpus);
|
|
|
|
sched_setaffinity(0, sizeof(cpus), &cpus);
|
|
|
|
// Don't assert sched_setaffinity's return value because it intermittently (?)
|
|
|
|
// fails with EINVAL on Linux x64 try runs.
|
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
// OS X does not provide APIs to pin threads to specific processors, but you
|
|
|
|
// can tag threads as belonging to the same "affinity set" and the OS will try
|
|
|
|
// to run them on the same processor. To run threads on different processors,
|
|
|
|
// tag them as belonging to different affinity sets. Tag 0, the default, means
|
|
|
|
// "no affinity" so let's pretend each CPU has its own tag `cpu+1`.
|
|
|
|
thread_affinity_policy_data_t policy;
|
|
|
|
policy.affinity_tag = cpu + 1;
|
|
|
|
MOZ_ALWAYS_TRUE(thread_policy_set(mach_thread_self(), THREAD_AFFINITY_POLICY,
|
|
|
|
&policy.affinity_tag, 1) == KERN_SUCCESS);
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
MOZ_ALWAYS_TRUE(SetThreadIdealProcessor(GetCurrentThread(), cpu) != -1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-03-03 09:12:27 +04:00
|
|
|
static void
|
|
|
|
SetupCurrentThreadForChaosMode()
|
|
|
|
{
|
2015-07-15 00:29:23 +03:00
|
|
|
if (!ChaosMode::isActive(ChaosFeature::ThreadScheduling)) {
|
2014-03-03 09:12:27 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_LINUX
|
|
|
|
// PR_SetThreadPriority doesn't really work since priorities >
|
|
|
|
// PR_PRIORITY_NORMAL can't be set by non-root users. Instead we'll just use
|
|
|
|
// setpriority(2) to set random 'nice values'. In regular Linux this is only
|
|
|
|
// a dynamic adjustment so it still doesn't really do what we want, but tools
|
|
|
|
// like 'rr' can be more aggressive about honoring these values.
|
|
|
|
// Some of these calls may fail due to trying to lower the priority
|
|
|
|
// (e.g. something may have already called setpriority() for this thread).
|
|
|
|
// This makes it hard to have non-main threads with higher priority than the
|
|
|
|
// main thread, but that's hard to fix. Tools like rr can choose to honor the
|
|
|
|
// requested values anyway.
|
|
|
|
// Use just 4 priorities so there's a reasonable chance of any two threads
|
|
|
|
// having equal priority.
|
|
|
|
setpriority(PRIO_PROCESS, 0, ChaosMode::randomUint32LessThan(4));
|
|
|
|
#else
|
|
|
|
// We should set the affinity here but NSPR doesn't provide a way to expose it.
|
2014-05-27 11:15:35 +04:00
|
|
|
uint32_t priority = ChaosMode::randomUint32LessThan(PR_PRIORITY_LAST + 1);
|
|
|
|
PR_SetThreadPriority(PR_GetCurrentThread(), PRThreadPriority(priority));
|
2014-03-03 09:12:27 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Force half the threads to CPU 0 so they compete for CPU
|
|
|
|
if (ChaosMode::randomUint32LessThan(2)) {
|
2015-12-01 06:16:40 +03:00
|
|
|
SetThreadAffinity(0);
|
2014-03-03 09:12:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
/*static*/ void
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::ThreadFunc(void* aArg)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2014-11-21 01:16:36 +03:00
|
|
|
using mozilla::ipc::BackgroundChild;
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread* self = static_cast<nsThread*>(aArg); // strong reference
|
2006-05-10 21:30:15 +04:00
|
|
|
self->mThread = PR_GetCurrentThread();
|
2014-03-03 09:12:27 +04:00
|
|
|
SetupCurrentThreadForChaosMode();
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Inform the ThreadManager
|
|
|
|
nsThreadManager::get()->RegisterCurrentThread(self);
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
mozilla::IOInterposer::RegisterCurrentThread();
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// Wait for and process startup event
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
2015-09-20 11:59:56 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(self->mLock);
|
|
|
|
if (!self->mEvents->GetEvent(true, getter_AddRefs(event), lock)) {
|
|
|
|
NS_WARNING("failed waiting for thread startup event");
|
|
|
|
return;
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
event->Run(); // unblocks nsThread::Init
|
2012-07-30 18:20:58 +04:00
|
|
|
event = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
|
|
|
// Scope for MessageLoop.
|
2013-10-23 16:01:24 +04:00
|
|
|
nsAutoPtr<MessageLoop> loop(
|
2016-03-30 22:20:20 +03:00
|
|
|
new MessageLoop(MessageLoop::TYPE_MOZILLA_NONMAINTHREAD, self));
|
2013-10-23 16:01:24 +04:00
|
|
|
|
|
|
|
// Now, process incoming events...
|
|
|
|
loop->Run();
|
|
|
|
|
2014-11-21 01:16:36 +03:00
|
|
|
BackgroundChild::CloseForCurrentThread();
|
|
|
|
|
2016-01-18 20:34:38 +03:00
|
|
|
// NB: The main thread does not shut down here! It shuts down via
|
|
|
|
// nsThreadManager::Shutdown.
|
|
|
|
|
2013-10-23 16:01:24 +04:00
|
|
|
// Do NS_ProcessPendingEvents but with special handling to set
|
|
|
|
// mEventsAreDoomed atomically with the removal of the last event. The key
|
|
|
|
// invariant here is that we will never permit PutEvent to succeed if the
|
|
|
|
// event would be left in the queue after our final call to
|
2015-09-15 04:24:43 +03:00
|
|
|
// NS_ProcessPendingEvents. We also have to keep processing events as long
|
|
|
|
// as we have outstanding mRequestedShutdownContexts.
|
2013-10-23 16:01:24 +04:00
|
|
|
while (true) {
|
2015-09-15 04:24:43 +03:00
|
|
|
// Check and see if we're waiting on any threads.
|
2016-01-18 20:34:38 +03:00
|
|
|
self->WaitForAllAsynchronousShutdowns();
|
2015-09-15 04:24:43 +03:00
|
|
|
|
2013-10-23 16:01:24 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(self->mLock);
|
2015-09-20 11:59:56 +03:00
|
|
|
if (!self->mEvents->HasPendingEvent(lock)) {
|
2013-10-23 16:01:24 +04:00
|
|
|
// No events in the queue, so we will stop now. Don't let any more
|
|
|
|
// events be added, since they won't be processed. It is critical
|
|
|
|
// that no PutEvent can occur between testing that the event queue is
|
|
|
|
// empty and setting mEventsAreDoomed!
|
|
|
|
self->mEventsAreDoomed = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-12-05 05:17:15 +03:00
|
|
|
}
|
2013-10-23 16:01:24 +04:00
|
|
|
NS_ProcessPendingEvents(self);
|
2007-12-05 05:17:15 +03:00
|
|
|
}
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
mozilla::IOInterposer::UnregisterCurrentThread();
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// Inform the threadmanager that this thread is going away
|
|
|
|
nsThreadManager::get()->UnregisterCurrentThread(self);
|
|
|
|
|
|
|
|
// Dispatch shutdown ACK
|
2016-04-20 04:46:35 +03:00
|
|
|
MOZ_ASSERT(self->mShutdownContext->mTerminatingThread == self);
|
2015-09-15 04:24:43 +03:00
|
|
|
event = do_QueryObject(new nsThreadShutdownAckEvent(self->mShutdownContext));
|
2016-04-20 04:46:35 +03:00
|
|
|
self->mShutdownContext->mJoiningThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2009-06-22 17:08:04 +04:00
|
|
|
// Release any observer of the thread here.
|
2012-07-30 18:20:58 +04:00
|
|
|
self->SetObserver(nullptr);
|
2009-06-22 17:08:04 +04:00
|
|
|
|
2014-03-27 12:49:06 +04:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
FreeTraceInfo();
|
|
|
|
#endif
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_RELEASE(self);
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-08-30 09:21:22 +04:00
|
|
|
#ifdef MOZ_CRASHREPORTER
|
|
|
|
// Tell the crash reporter to save a memory report if our heuristics determine
|
|
|
|
// that an OOM failure is likely to occur soon.
|
|
|
|
static bool SaveMemoryReportNearOOM()
|
|
|
|
{
|
|
|
|
bool needMemoryReport = false;
|
|
|
|
|
|
|
|
#ifdef XP_WIN // XXX implement on other platforms as needed
|
|
|
|
const size_t LOWMEM_THRESHOLD_VIRTUAL = 200 * 1024 * 1024;
|
|
|
|
MEMORYSTATUSEX statex;
|
|
|
|
statex.dwLength = sizeof(statex);
|
|
|
|
if (GlobalMemoryStatusEx(&statex)) {
|
|
|
|
if (statex.ullAvailVirtual < LOWMEM_THRESHOLD_VIRTUAL) {
|
|
|
|
needMemoryReport = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (needMemoryReport) {
|
|
|
|
nsCOMPtr<nsICrashReporter> cr =
|
|
|
|
do_GetService("@mozilla.org/toolkit/crash-reporter;1");
|
2016-05-03 10:08:04 +03:00
|
|
|
if (cr) {
|
|
|
|
cr->SaveMemoryReport();
|
|
|
|
}
|
2014-08-30 09:21:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return needMemoryReport;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-30 00:58:09 +04:00
|
|
|
#ifdef MOZ_CANARY
|
|
|
|
int sCanaryOutputFD = -1;
|
|
|
|
#endif
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsThread::nsThread(MainThreadFlag aMainThread, uint32_t aStackSize)
|
2011-07-27 07:26:47 +04:00
|
|
|
: mLock("nsThread.mLock")
|
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
|
|
|
, mScriptObserver(nullptr)
|
2013-11-14 22:06:17 +04:00
|
|
|
, mEvents(&mEventsRoot)
|
Bug 1202497 - part 7 - make nsEventQueue use external locking; r=gerald
We want to ensure that nsThread's use of nsEventQueue uses locking done
in nsThread instead of nsEventQueue, for efficiency's sake: we only need
to lock once in nsThread, rather than the current situation of locking
in nsThread and additionally in nsEventQueue. With the current
structure of nsEventQueue, that would mean that nsThread should be using
a Monitor internally, rather than a Mutex.
Which would be well and good, except that DOM workers use nsThread's
mutex to protect their own, internal CondVar. Switching nsThread to use
a Monitor would mean that either:
- DOM workers drop their internal CondVar in favor of nsThread's
Monitor-owned CondVar. This change seems unlikely to work out well,
because now the Monitor-owned CondVar is performing double duty:
tracking availability of events in nsThread's event queue and
additionally whatever DOM workers were using a CondVar for. Having a
single CondVar track two things in such a fashion is for Experts Only.
- DOM workers grow their own Mutex to protect their own CondVar. Adding
a mutex like this would change locking in subtle ways and seems
unlikely to lead to success.
Using a Monitor in nsThread is therefore untenable, and we would like to
retain the current Mutex that lives in nsThread. Therefore, we need to
have nsEventQueue manage its own condition variable and push the
required (Mutex) locking to the client of nsEventQueue. This scheme
also seems more fitting: external clients merely need synchronized
access to the event queue; the details of managing notifications about
events in the event queue should be left up to the event queue itself.
Doing so also forces us to merge nsEventQueueBase and nsEventQueue:
there's no way to have nsEventQueueBase require an externally-defined
Mutex and then have nsEventQueue subclass nsEventQueueBase and provide
its own Mutex to the superclass. C++ initialization rules (and the way
things like CondVar are constructed) simply forbid it. But that's OK,
because we want a world where nsEventQueue is externally locked anyway,
so there's no reason to have separate classes here.
One casualty of this work is removing ChaosMode support from
nsEventQueue. nsEventQueue had support to delay placing events into the
queue, theoretically giving other threads the chance to put events there
first. Unfortunately, since the thread would have been holding a lock
(as is evident from the MutexAutoLock& parameter required), sleeping in
PutEvent accomplishes nothing but delaying the thread from getting
useful work done. We should support this, but it's complicated to
figure out how to reasonably support this right now.
A wrinkle in this overall pleasant refactoring is that nsThreadPool's
threads wait for limited amounts of time for new events to be placed in
the event queue, so that they can shut themselves down if no new events
are appearing. Setting limits on the number of threads also needs to be
able to wake up all threads, so threads can shut themselves down if
necessary.
Unfortunately, with the transition to nsEventQueue managing its own
condition variable, there's no way for nsThreadPool to perform these
functions, since there's no Monitor to wait on. Therefore, we add a
private API for accessing the condition variable and performing the
tasks nsThreadPool needs.
Prior to all the previous patches, placing items in an nsThread's event
queue required three lock/unlock pairs: one for nsThread's Mutex, one to
enter nsEventQueue's ReentrantMonitor, and one to exit nsEventQueue's
ReentrantMonitor. The upshot of all this work is that we now only
require one lock/unlock pair in nsThread itself, as things should be.
2015-09-20 12:13:09 +03:00
|
|
|
, mEventsRoot(mLock)
|
2011-07-27 07:26:47 +04:00
|
|
|
, mPriority(PRIORITY_NORMAL)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mThread(nullptr)
|
2015-04-24 23:04:50 +03:00
|
|
|
, mNestedEventLoopDepth(0)
|
2011-07-27 07:26:47 +04:00
|
|
|
, mStackSize(aStackSize)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mShutdownContext(nullptr)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mShutdownRequired(false)
|
|
|
|
, mEventsAreDoomed(false)
|
2011-10-12 21:52:26 +04:00
|
|
|
, mIsMainThread(aMainThread)
|
1999-04-06 01:02:24 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsThread::~nsThread()
|
|
|
|
{
|
2016-02-03 21:59:26 +03:00
|
|
|
NS_ASSERTION(mRequestedShutdownContexts.IsEmpty(),
|
|
|
|
"shouldn't be waiting on other threads to shutdown");
|
|
|
|
#ifdef DEBUG
|
|
|
|
// We deliberately leak these so they can be tracked by the leak checker.
|
|
|
|
// If you're having nsThreadShutdownContext leaks, you can set:
|
|
|
|
// XPCOM_MEM_LOG_CLASSES=nsThreadShutdownContext
|
|
|
|
// during a test run and that will at least tell you what thread is
|
|
|
|
// requesting shutdown on another, which can be helpful for diagnosing
|
|
|
|
// the leak.
|
|
|
|
for (size_t i = 0; i < mRequestedShutdownContexts.Length(); ++i) {
|
|
|
|
Unused << mRequestedShutdownContexts[i].forget();
|
|
|
|
}
|
|
|
|
#endif
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsThread::Init()
|
1999-10-02 03:30:06 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
// spawn thread and wait until it is fully setup
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsThreadStartupEvent> startup = new nsThreadStartupEvent();
|
2014-05-27 11:15:35 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_ADDREF_THIS();
|
2014-05-27 11:15:35 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mShutdownRequired = true;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// ThreadFunc is responsible for setting mThread
|
2014-05-27 11:15:35 +04:00
|
|
|
PRThread* thr = PR_CreateThread(PR_USER_THREAD, ThreadFunc, this,
|
2006-05-10 21:30:15 +04:00
|
|
|
PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
|
2011-07-27 07:26:47 +04:00
|
|
|
PR_JOINABLE_THREAD, mStackSize);
|
2006-05-10 21:30:15 +04:00
|
|
|
if (!thr) {
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ThreadFunc will wait for this event to be run before it tries to access
|
|
|
|
// mThread. By delaying insertion of this event into the queue, we ensure
|
|
|
|
// that mThread is set properly.
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2015-09-20 11:59:56 +03:00
|
|
|
mEventsRoot.PutEvent(startup, lock); // retain a reference
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for thread to call ThreadManager::SetupCurrentThread, which completes
|
|
|
|
// initialization of ThreadFunc.
|
|
|
|
startup->Wait();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-06-11 23:26:04 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsresult
|
|
|
|
nsThread::InitCurrentThread()
|
|
|
|
{
|
|
|
|
mThread = PR_GetCurrentThread();
|
2014-03-03 09:12:27 +04:00
|
|
|
SetupCurrentThreadForChaosMode();
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
nsThreadManager::get()->RegisterCurrentThread(this);
|
|
|
|
return NS_OK;
|
1999-10-02 03:30:06 +04:00
|
|
|
}
|
|
|
|
|
2007-12-05 05:17:15 +03:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::PutEvent(nsIRunnable* aEvent, nsNestedEventTarget* aTarget)
|
2015-07-10 06:21:46 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
|
|
|
return PutEvent(event.forget(), aTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsThread::PutEvent(already_AddRefed<nsIRunnable>&& aEvent, nsNestedEventTarget* aTarget)
|
2000-11-30 08:24:53 +03:00
|
|
|
{
|
2015-10-06 05:00:59 +03:00
|
|
|
// We want to leak the reference when we fail to dispatch it, so that
|
|
|
|
// we won't release the event in a wrong thread.
|
|
|
|
LeakRefPtr<nsIRunnable> event(Move(aEvent));
|
2014-09-27 03:21:57 +04:00
|
|
|
nsCOMPtr<nsIThreadObserver> obs;
|
|
|
|
|
2015-08-28 12:57:44 +03:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
// On debug build or when tests are enabled, assert that we are not about to
|
|
|
|
// create a deadlock in the Nuwa process.
|
|
|
|
NuwaAssertNotFrozen(PR_GetThreadID(mThread), PR_GetThreadName(mThread));
|
|
|
|
#endif
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-27 11:15:35 +04:00
|
|
|
nsChainedEventQueue* queue = aTarget ? aTarget->mQueue : &mEventsRoot;
|
2013-11-14 22:06:17 +04:00
|
|
|
if (!queue || (queue == &mEventsRoot && mEventsAreDoomed)) {
|
2007-12-05 05:17:15 +03:00
|
|
|
NS_WARNING("An event was posted to a thread that will never run it (rejected)");
|
2015-10-06 05:00:59 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-12-05 05:17:15 +03:00
|
|
|
}
|
2015-10-06 05:00:59 +03:00
|
|
|
queue->PutEvent(event.take(), lock);
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
// Make sure to grab the observer before dropping the lock, otherwise the
|
|
|
|
// event that we just placed into the queue could run and eventually delete
|
|
|
|
// this nsThread before the calling thread is scheduled again. We would then
|
|
|
|
// crash while trying to access a dead nsThread.
|
|
|
|
obs = mObserver;
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (obs) {
|
2006-05-10 21:30:15 +04:00
|
|
|
obs->OnDispatchedEvent(this);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2007-12-05 05:17:15 +03:00
|
|
|
return NS_OK;
|
2000-11-30 08:24:53 +03:00
|
|
|
}
|
|
|
|
|
2013-11-14 22:06:17 +04:00
|
|
|
nsresult
|
2015-07-10 06:21:46 +03:00
|
|
|
nsThread::DispatchInternal(already_AddRefed<nsIRunnable>&& aEvent, uint32_t aFlags,
|
2014-05-27 11:15:35 +04:00
|
|
|
nsNestedEventTarget* aTarget)
|
2000-11-30 08:24:53 +03:00
|
|
|
{
|
2015-10-06 05:00:59 +03:00
|
|
|
// We want to leak the reference when we fail to dispatch it, so that
|
|
|
|
// we won't release the event in a wrong thread.
|
|
|
|
LeakRefPtr<nsIRunnable> event(Move(aEvent));
|
2015-07-10 06:21:46 +03:00
|
|
|
if (NS_WARN_IF(!event)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (gXPCOMThreadsShutDown && MAIN_THREAD != mIsMainThread && !aTarget) {
|
2015-07-10 06:21:46 +03:00
|
|
|
NS_ASSERTION(false, "Failed Dispatch after xpcom-shutdown-threads");
|
2013-07-08 00:23:43 +04:00
|
|
|
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
|
|
|
|
}
|
|
|
|
|
2014-03-27 12:49:06 +04:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
2015-10-06 05:00:59 +03:00
|
|
|
nsCOMPtr<nsIRunnable> tracedRunnable = CreateTracedRunnable(event.take());
|
2015-06-16 05:57:19 +03:00
|
|
|
(static_cast<TracedRunnable*>(tracedRunnable.get()))->DispatchTask();
|
2015-10-06 05:00:59 +03:00
|
|
|
// XXX tracedRunnable will always leaked when we fail to disptch.
|
2015-07-10 06:21:46 +03:00
|
|
|
event = tracedRunnable.forget();
|
2014-03-27 12:49:06 +04:00
|
|
|
#endif
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aFlags & DISPATCH_SYNC) {
|
|
|
|
nsThread* thread = nsThreadManager::get()->GetCurrentThread();
|
|
|
|
if (NS_WARN_IF(!thread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// XXX we should be able to do something better here... we should
|
|
|
|
// be able to monitor the slot occupied by this event and use
|
|
|
|
// that to tell us when the event has been processed.
|
2014-03-27 12:49:06 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsThreadSyncDispatch> wrapper =
|
2015-10-06 05:00:59 +03:00
|
|
|
new nsThreadSyncDispatch(thread, event.take());
|
2015-07-10 06:21:46 +03:00
|
|
|
nsresult rv = PutEvent(wrapper, aTarget); // hold a ref
|
2007-12-05 05:17:15 +03:00
|
|
|
// Don't wait for the event to finish if we didn't dispatch it...
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-10-06 05:00:59 +03:00
|
|
|
// PutEvent leaked the wrapper runnable object on failure, so we
|
|
|
|
// explicitly release this object once for that. Note that this
|
|
|
|
// object will be released again soon because it exits the scope.
|
|
|
|
wrapper.get()->Release();
|
2007-12-05 05:17:15 +03:00
|
|
|
return rv;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-04-22 23:34:24 +04:00
|
|
|
// Allows waiting; ensure no locks are held that would deadlock us!
|
2014-05-27 11:15:35 +04:00
|
|
|
while (wrapper->IsPending()) {
|
2014-04-22 23:34:24 +04:00
|
|
|
NS_ProcessNextEvent(thread, true);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2016-01-25 06:52:34 +03:00
|
|
|
return NS_OK;
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_ASSERTION(aFlags == NS_DISPATCH_NORMAL, "unexpected dispatch flags");
|
2015-10-06 05:00:59 +03:00
|
|
|
return PutEvent(event.take(), aTarget);
|
2013-11-14 22:06:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIEventTarget
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-07-10 06:21:46 +03:00
|
|
|
nsThread::DispatchFromScript(nsIRunnable* aEvent, uint32_t aFlags)
|
2013-11-14 22:06:17 +04:00
|
|
|
{
|
2015-07-10 06:21:46 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
|
|
|
return Dispatch(event.forget(), aFlags);
|
|
|
|
}
|
2013-11-14 22:06:17 +04:00
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::Dispatch(already_AddRefed<nsIRunnable>&& aEvent, uint32_t aFlags)
|
|
|
|
{
|
|
|
|
LOG(("THRD(%p) Dispatch [%p %x]\n", this, /* XXX aEvent */nullptr, aFlags));
|
|
|
|
|
|
|
|
return DispatchInternal(Move(aEvent), aFlags, nullptr);
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
2000-11-30 08:24:53 +03:00
|
|
|
|
2016-05-13 01:15:43 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::DelayedDispatch(already_AddRefed<nsIRunnable>&& aEvent, uint32_t aDelayMs)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(!!aDelayMs, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
RefPtr<DelayedRunnable> r = new DelayedRunnable(Move(aEvent), aDelayMs);
|
|
|
|
nsresult rv = r->Init();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return DispatchInternal(r.forget(), 0, nullptr);
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::IsOnCurrentThread(bool* aResult)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = (PR_GetCurrentThread() == mThread);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
2000-11-30 08:24:53 +03:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIThread
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::GetPRThread(PRThread** aResult)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = mThread;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2015-09-15 04:24:43 +03:00
|
|
|
nsThread::AsyncShutdown()
|
1999-10-02 03:30:06 +04:00
|
|
|
{
|
2015-09-15 04:24:43 +03:00
|
|
|
LOG(("THRD(%p) async shutdown\n", this));
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// XXX If we make this warn, then we hit that warning at xpcom shutdown while
|
|
|
|
// shutting down a thread in a thread pool. That happens b/c the thread
|
|
|
|
// in the thread pool is already shutdown by the thread manager.
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!mThread) {
|
1999-10-02 03:30:06 +04:00
|
|
|
return NS_OK;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2015-09-15 04:24:43 +03:00
|
|
|
return !!ShutdownInternal(/* aSync = */ false) ? NS_OK : NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsThreadShutdownContext*
|
|
|
|
nsThread::ShutdownInternal(bool aSync)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mThread);
|
2016-04-27 08:49:48 +03:00
|
|
|
MOZ_ASSERT(mThread != PR_GetCurrentThread());
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(mThread == PR_GetCurrentThread())) {
|
2015-09-15 04:24:43 +03:00
|
|
|
return nullptr;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Prevent multiple calls to this method
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!mShutdownRequired) {
|
2015-09-15 04:24:43 +03:00
|
|
|
return nullptr;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mShutdownRequired = false;
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2015-09-15 04:24:43 +03:00
|
|
|
nsThread* currentThread = nsThreadManager::get()->GetCurrentThread();
|
|
|
|
MOZ_ASSERT(currentThread);
|
|
|
|
|
|
|
|
nsAutoPtr<nsThreadShutdownContext>& context =
|
|
|
|
*currentThread->mRequestedShutdownContexts.AppendElement();
|
2016-04-20 04:49:48 +03:00
|
|
|
context = new nsThreadShutdownContext(this, currentThread, aSync);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Set mShutdownContext and wake up the thread in case it is waiting for
|
|
|
|
// events to process.
|
2015-09-15 04:24:43 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event = new nsThreadShutdownEvent(this, context);
|
2007-12-05 05:17:15 +03:00
|
|
|
// XXXroc What if posting the event fails due to OOM?
|
2015-07-10 06:21:46 +03:00
|
|
|
PutEvent(event.forget(), nullptr);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// We could still end up with other events being added after the shutdown
|
|
|
|
// task, but that's okay because we process pending events in ThreadFunc
|
|
|
|
// after setting mShutdownContext just before exiting.
|
2015-09-15 04:24:43 +03:00
|
|
|
return context;
|
|
|
|
}
|
2014-05-27 11:15:35 +04:00
|
|
|
|
2015-09-15 04:24:43 +03:00
|
|
|
void
|
|
|
|
nsThread::ShutdownComplete(nsThreadShutdownContext* aContext)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mThread);
|
2016-04-20 04:46:35 +03:00
|
|
|
MOZ_ASSERT(aContext->mTerminatingThread == this);
|
2015-09-15 04:24:43 +03:00
|
|
|
|
2016-04-20 04:46:35 +03:00
|
|
|
if (aContext->mAwaitingShutdownAck) {
|
2015-09-15 04:24:43 +03:00
|
|
|
// We're in a synchronous shutdown, so tell whatever is up the stack that
|
|
|
|
// we're done and unwind the stack so it can call us again.
|
2016-04-20 04:46:35 +03:00
|
|
|
aContext->mAwaitingShutdownAck = false;
|
2015-09-15 04:24:43 +03:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Now, it should be safe to join without fear of dead-locking.
|
|
|
|
|
|
|
|
PR_JoinThread(mThread);
|
2012-07-30 18:20:58 +04:00
|
|
|
mThread = nullptr;
|
2009-06-22 17:08:04 +04:00
|
|
|
|
2012-01-14 22:31:13 +04:00
|
|
|
// We hold strong references to our event observers, and once the thread is
|
|
|
|
// shut down the observers can't easily unregister themselves. Do it here
|
|
|
|
// to avoid leaking.
|
|
|
|
ClearObservers();
|
|
|
|
|
2009-06-22 17:08:04 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(!mObserver, "Should have been cleared at shutdown!");
|
2009-06-22 17:08:04 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-09-15 04:24:43 +03:00
|
|
|
// Delete aContext.
|
|
|
|
MOZ_ALWAYS_TRUE(
|
2016-04-20 04:46:35 +03:00
|
|
|
aContext->mJoiningThread->mRequestedShutdownContexts.RemoveElement(aContext));
|
2015-09-15 04:24:43 +03:00
|
|
|
}
|
|
|
|
|
2016-01-18 20:34:38 +03:00
|
|
|
void
|
|
|
|
nsThread::WaitForAllAsynchronousShutdowns()
|
|
|
|
{
|
|
|
|
while (mRequestedShutdownContexts.Length()) {
|
|
|
|
NS_ProcessNextEvent(this, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 04:24:43 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::Shutdown()
|
|
|
|
{
|
|
|
|
LOG(("THRD(%p) sync shutdown\n", this));
|
|
|
|
|
|
|
|
// XXX If we make this warn, then we hit that warning at xpcom shutdown while
|
|
|
|
// shutting down a thread in a thread pool. That happens b/c the thread
|
|
|
|
// in the thread pool is already shutdown by the thread manager.
|
|
|
|
if (!mThread) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsThreadShutdownContext* context = ShutdownInternal(/* aSync = */ true);
|
|
|
|
NS_ENSURE_TRUE(context, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
// Process events on the current thread until we receive a shutdown ACK.
|
|
|
|
// Allows waiting; ensure no locks are held that would deadlock us!
|
2016-04-20 04:46:35 +03:00
|
|
|
while (context->mAwaitingShutdownAck) {
|
|
|
|
NS_ProcessNextEvent(context->mJoiningThread, true);
|
2015-09-15 04:24:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ShutdownComplete(context);
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-10-02 03:30:06 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::HasPendingEvents(bool* aResult)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
1999-04-02 13:20:44 +04:00
|
|
|
|
2015-09-20 11:47:10 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
2015-09-20 11:59:56 +03:00
|
|
|
*aResult = mEvents->HasPendingEvent(lock);
|
2015-09-20 11:47:10 +03:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-27 19:16:03 +04:00
|
|
|
#ifdef MOZ_CANARY
|
2014-05-27 11:15:35 +04:00
|
|
|
void canary_alarm_handler(int signum);
|
2010-10-27 19:16:03 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
class Canary
|
|
|
|
{
|
|
|
|
//XXX ToDo: support nested loops
|
2010-10-27 19:16:03 +04:00
|
|
|
public:
|
2014-05-27 11:15:35 +04:00
|
|
|
Canary()
|
|
|
|
{
|
2013-10-30 00:58:09 +04:00
|
|
|
if (sCanaryOutputFD > 0 && EventLatencyIsImportant()) {
|
2010-10-27 19:16:03 +04:00
|
|
|
signal(SIGALRM, canary_alarm_handler);
|
2014-05-27 11:15:35 +04:00
|
|
|
ualarm(15000, 0);
|
2010-10-27 19:16:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
~Canary()
|
|
|
|
{
|
|
|
|
if (sCanaryOutputFD != 0 && EventLatencyIsImportant()) {
|
2010-10-27 19:16:03 +04:00
|
|
|
ualarm(0, 0);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2010-10-27 19:16:03 +04:00
|
|
|
}
|
2011-01-06 01:11:56 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
static bool EventLatencyIsImportant()
|
|
|
|
{
|
2015-07-04 04:29:00 +03:00
|
|
|
return NS_IsMainThread() && XRE_IsParentProcess();
|
2011-01-06 01:11:56 +03:00
|
|
|
}
|
2010-10-27 19:16:03 +04:00
|
|
|
};
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void canary_alarm_handler(int signum)
|
2010-10-27 19:16:03 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
void* array[30];
|
2010-10-27 19:16:03 +04:00
|
|
|
const char msg[29] = "event took too long to run:\n";
|
|
|
|
// use write to be safe in the signal handler
|
2014-05-27 11:15:35 +04:00
|
|
|
write(sCanaryOutputFD, msg, sizeof(msg));
|
2013-10-30 00:58:09 +04:00
|
|
|
backtrace_symbols_fd(array, backtrace(array, 30), sCanaryOutputFD);
|
2010-10-27 19:16:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-11-19 01:19:19 +03:00
|
|
|
#define NOTIFY_EVENT_OBSERVERS(func_, params_) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if (!mEventObservers.IsEmpty()) { \
|
|
|
|
nsAutoTObserverArray<nsCOMPtr<nsIThreadObserver>, 2>::ForwardIterator \
|
|
|
|
iter_(mEventObservers); \
|
|
|
|
nsCOMPtr<nsIThreadObserver> obs_; \
|
|
|
|
while (iter_.HasMore()) { \
|
|
|
|
obs_ = iter_.GetNext(); \
|
|
|
|
obs_ -> func_ params_ ; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::ProcessNextEvent(bool aMayWait, bool* aResult)
|
1999-04-06 01:02:24 +04:00
|
|
|
{
|
2015-04-24 23:04:50 +03:00
|
|
|
LOG(("THRD(%p) ProcessNextEvent [%u %u]\n", this, aMayWait,
|
|
|
|
mNestedEventLoopDepth));
|
1999-04-06 01:02:24 +04:00
|
|
|
|
2014-08-06 08:43:36 +04:00
|
|
|
// If we're on the main thread, we shouldn't be dispatching CPOWs.
|
2015-06-24 03:52:01 +03:00
|
|
|
if (mIsMainThread == MAIN_THREAD) {
|
|
|
|
ipc::CancelCPOWs();
|
|
|
|
}
|
2014-08-06 08:43:36 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
1999-04-06 01:02:24 +04:00
|
|
|
|
2014-02-01 05:14:00 +04:00
|
|
|
// The toplevel event loop normally blocks waiting for the next event, but
|
|
|
|
// if we're trying to shut this thread down, we must exit the event loop when
|
|
|
|
// the event queue is empty.
|
|
|
|
// This only applys to the toplevel event loop! Nested event loops (e.g.
|
|
|
|
// during sync dispatch) are waiting for some state change and must be able
|
|
|
|
// to block even if something has requested shutdown of the thread. Otherwise
|
|
|
|
// we'll just busywait as we endlessly look for an event, fail to find one,
|
|
|
|
// and repeat the nested event loop since its state change hasn't happened yet.
|
2015-04-24 23:04:50 +03:00
|
|
|
bool reallyWait = aMayWait && (mNestedEventLoopDepth > 0 || !ShuttingDown());
|
2014-02-01 04:42:24 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (MAIN_THREAD == mIsMainThread && reallyWait) {
|
2011-10-12 21:52:26 +04:00
|
|
|
HangMonitor::Suspend();
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2011-10-12 21:52:26 +04:00
|
|
|
|
2011-08-06 02:10:50 +04:00
|
|
|
// Fire a memory pressure notification, if we're the main thread and one is
|
|
|
|
// pending.
|
|
|
|
if (MAIN_THREAD == mIsMainThread && !ShuttingDown()) {
|
2013-07-09 06:15:34 +04:00
|
|
|
MemoryPressureState mpPending = NS_GetPendingMemoryPressure();
|
|
|
|
if (mpPending != MemPressure_None) {
|
2011-08-06 02:10:50 +04:00
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
2013-07-09 06:15:34 +04:00
|
|
|
|
|
|
|
// 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");
|
|
|
|
|
2011-08-06 02:10:50 +04:00
|
|
|
if (os) {
|
2012-07-30 18:20:58 +04:00
|
|
|
os->NotifyObservers(nullptr, "memory-pressure",
|
2013-07-09 06:15:34 +04:00
|
|
|
mpPending == MemPressure_New ? lowMem.get() :
|
2014-05-27 11:15:35 +04:00
|
|
|
lowMemOngoing.get());
|
2013-07-09 06:15:34 +04:00
|
|
|
} else {
|
2011-08-06 02:10:50 +04:00
|
|
|
NS_WARNING("Can't get observer service!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 09:21:22 +04:00
|
|
|
#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);
|
2015-07-10 06:21:46 +03:00
|
|
|
|
2014-08-30 09:21:22 +04:00
|
|
|
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
|
|
|
|
|
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
|
|
|
++mNestedEventLoopDepth;
|
|
|
|
|
|
|
|
bool callScriptObserver = !!mScriptObserver;
|
|
|
|
if (callScriptObserver) {
|
|
|
|
mScriptObserver->BeforeProcessTask(reallyWait);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-10-24 02:26:36 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsCOMPtr<nsIThreadObserver> obs = mObserver;
|
2014-05-27 11:15:35 +04:00
|
|
|
if (obs) {
|
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
|
|
|
obs->OnProcessNextEvent(this, reallyWait);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
1999-04-06 01:02:24 +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
|
|
|
NOTIFY_EVENT_OBSERVERS(OnProcessNextEvent, (this, reallyWait));
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2010-10-27 19:16:03 +04:00
|
|
|
#ifdef MOZ_CANARY
|
|
|
|
Canary canary;
|
|
|
|
#endif
|
2006-05-10 21:30:15 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2009-06-11 21:09:35 +04:00
|
|
|
{
|
|
|
|
// Scope for |event| to make sure that its destructor fires while
|
2015-04-24 23:04:50 +03:00
|
|
|
// mNestedEventLoopDepth has been incremented, since that destructor can
|
2009-06-11 21:09:35 +04:00
|
|
|
// also do work.
|
|
|
|
|
|
|
|
// If we are shutting down, then do not wait for new events.
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
2015-09-20 11:59:56 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
mEvents->GetEvent(reallyWait, getter_AddRefs(event), lock);
|
|
|
|
}
|
2009-06-11 21:09:35 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = (event.get() != nullptr);
|
2009-06-11 21:09:35 +04:00
|
|
|
|
|
|
|
if (event) {
|
|
|
|
LOG(("THRD(%p) running [%p]\n", this, event.get()));
|
2014-05-27 11:15:35 +04:00
|
|
|
if (MAIN_THREAD == mIsMainThread) {
|
2011-10-12 21:52:26 +04:00
|
|
|
HangMonitor::NotifyActivity();
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2009-06-11 21:09:35 +04:00
|
|
|
event->Run();
|
2014-05-27 11:15:35 +04:00
|
|
|
} else if (aMayWait) {
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(ShuttingDown(),
|
|
|
|
"This should only happen when shutting down");
|
2009-06-11 21:09:35 +04:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2006-05-10 21:30: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
|
|
|
NOTIFY_EVENT_OBSERVERS(AfterProcessNextEvent, (this, *aResult));
|
2010-11-19 01:19:19 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (obs) {
|
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
|
|
|
obs->AfterProcessNextEvent(this, *aResult);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-06-07 04:06:11 +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
|
|
|
if (callScriptObserver && mScriptObserver) {
|
|
|
|
mScriptObserver->AfterProcessTask(mNestedEventLoopDepth);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-10-24 02:26:36 +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
|
|
|
--mNestedEventLoopDepth;
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
return rv;
|
1999-04-06 01:02:24 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsISupportsPriority
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::GetPriority(int32_t* aPriority)
|
2000-11-30 08:24:53 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aPriority = mPriority;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
2000-11-30 08:24:53 +03:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::SetPriority(int32_t aPriority)
|
1999-04-06 10:09:15 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!mThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// NSPR defines the following four thread priorities:
|
|
|
|
// PR_PRIORITY_LOW
|
|
|
|
// PR_PRIORITY_NORMAL
|
|
|
|
// PR_PRIORITY_HIGH
|
|
|
|
// PR_PRIORITY_URGENT
|
|
|
|
// We map the priority values defined on nsISupportsPriority to these values.
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
mPriority = aPriority;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
PRThreadPriority pri;
|
|
|
|
if (mPriority <= PRIORITY_HIGHEST) {
|
|
|
|
pri = PR_PRIORITY_URGENT;
|
|
|
|
} else if (mPriority < PRIORITY_NORMAL) {
|
|
|
|
pri = PR_PRIORITY_HIGH;
|
|
|
|
} else if (mPriority > PRIORITY_NORMAL) {
|
|
|
|
pri = PR_PRIORITY_LOW;
|
|
|
|
} else {
|
|
|
|
pri = PR_PRIORITY_NORMAL;
|
|
|
|
}
|
2014-03-03 09:12:27 +04:00
|
|
|
// If chaos mode is active, retain the randomly chosen priority
|
2015-07-15 00:29:23 +03:00
|
|
|
if (!ChaosMode::isActive(ChaosFeature::ThreadScheduling)) {
|
2014-03-03 09:12:27 +04:00
|
|
|
PR_SetThreadPriority(mThread, pri);
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
1999-04-06 10:09:15 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::AdjustPriority(int32_t aDelta)
|
1999-04-06 01:02:24 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
return SetPriority(mPriority + aDelta);
|
1999-04-06 01:02:24 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIThreadInternal
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::GetObserver(nsIThreadObserver** aObs)
|
1999-06-13 07:30:38 +04:00
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IF_ADDREF(*aObs = mObserver);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-06-13 07:30:38 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::SetObserver(nsIThreadObserver* aObs)
|
1999-06-13 07:30:38 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-27 11:15:35 +04:00
|
|
|
mObserver = aObs;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-06-13 07:30:38 +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
|
|
|
uint32_t
|
|
|
|
nsThread::RecursionDepth() const
|
2010-11-16 00:49:49 +03: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
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == mThread);
|
|
|
|
return mNestedEventLoopDepth;
|
2010-11-16 00:49:49 +03:00
|
|
|
}
|
|
|
|
|
2010-11-19 01:19:19 +03:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::AddObserver(nsIThreadObserver* aObserver)
|
2010-11-19 01:19:19 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!aObserver)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2010-11-19 01:19:19 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_WARN_IF_FALSE(!mEventObservers.Contains(aObserver),
|
2010-11-19 01:19:19 +03:00
|
|
|
"Adding an observer twice!");
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!mEventObservers.AppendElement(aObserver)) {
|
2010-11-19 01:19:19 +03:00
|
|
|
NS_WARNING("Out of memory!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::RemoveObserver(nsIThreadObserver* aObserver)
|
2010-11-19 01:19:19 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2010-11-19 01:19:19 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aObserver && !mEventObservers.RemoveElement(aObserver)) {
|
2010-11-19 01:19:19 +03:00
|
|
|
NS_WARNING("Removing an observer that was never added!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-14 22:06:17 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::PushEventQueue(nsIEventTarget** aResult)
|
2013-11-14 22:06:17 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-14 22:06:17 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2013-11-14 22:06:17 +04:00
|
|
|
|
Bug 1202497 - part 7 - make nsEventQueue use external locking; r=gerald
We want to ensure that nsThread's use of nsEventQueue uses locking done
in nsThread instead of nsEventQueue, for efficiency's sake: we only need
to lock once in nsThread, rather than the current situation of locking
in nsThread and additionally in nsEventQueue. With the current
structure of nsEventQueue, that would mean that nsThread should be using
a Monitor internally, rather than a Mutex.
Which would be well and good, except that DOM workers use nsThread's
mutex to protect their own, internal CondVar. Switching nsThread to use
a Monitor would mean that either:
- DOM workers drop their internal CondVar in favor of nsThread's
Monitor-owned CondVar. This change seems unlikely to work out well,
because now the Monitor-owned CondVar is performing double duty:
tracking availability of events in nsThread's event queue and
additionally whatever DOM workers were using a CondVar for. Having a
single CondVar track two things in such a fashion is for Experts Only.
- DOM workers grow their own Mutex to protect their own CondVar. Adding
a mutex like this would change locking in subtle ways and seems
unlikely to lead to success.
Using a Monitor in nsThread is therefore untenable, and we would like to
retain the current Mutex that lives in nsThread. Therefore, we need to
have nsEventQueue manage its own condition variable and push the
required (Mutex) locking to the client of nsEventQueue. This scheme
also seems more fitting: external clients merely need synchronized
access to the event queue; the details of managing notifications about
events in the event queue should be left up to the event queue itself.
Doing so also forces us to merge nsEventQueueBase and nsEventQueue:
there's no way to have nsEventQueueBase require an externally-defined
Mutex and then have nsEventQueue subclass nsEventQueueBase and provide
its own Mutex to the superclass. C++ initialization rules (and the way
things like CondVar are constructed) simply forbid it. But that's OK,
because we want a world where nsEventQueue is externally locked anyway,
so there's no reason to have separate classes here.
One casualty of this work is removing ChaosMode support from
nsEventQueue. nsEventQueue had support to delay placing events into the
queue, theoretically giving other threads the chance to put events there
first. Unfortunately, since the thread would have been holding a lock
(as is evident from the MutexAutoLock& parameter required), sleeping in
PutEvent accomplishes nothing but delaying the thread from getting
useful work done. We should support this, but it's complicated to
figure out how to reasonably support this right now.
A wrinkle in this overall pleasant refactoring is that nsThreadPool's
threads wait for limited amounts of time for new events to be placed in
the event queue, so that they can shut themselves down if no new events
are appearing. Setting limits on the number of threads also needs to be
able to wake up all threads, so threads can shut themselves down if
necessary.
Unfortunately, with the transition to nsEventQueue managing its own
condition variable, there's no way for nsThreadPool to perform these
functions, since there's no Monitor to wait on. Therefore, we add a
private API for accessing the condition variable and performing the
tasks nsThreadPool needs.
Prior to all the previous patches, placing items in an nsThread's event
queue required three lock/unlock pairs: one for nsThread's Mutex, one to
enter nsEventQueue's ReentrantMonitor, and one to exit nsEventQueue's
ReentrantMonitor. The upshot of all this work is that we now only
require one lock/unlock pair in nsThread itself, as things should be.
2015-09-20 12:13:09 +03:00
|
|
|
nsChainedEventQueue* queue = new nsChainedEventQueue(mLock);
|
2013-11-14 22:06:17 +04:00
|
|
|
queue->mEventTarget = new nsNestedEventTarget(this, queue);
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
queue->mNext = mEvents;
|
|
|
|
mEvents = queue;
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_ADDREF(*aResult = queue->mEventTarget);
|
2013-11-14 22:06:17 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::PopEventQueue(nsIEventTarget* aInnermostTarget)
|
2013-11-14 22:06:17 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-14 22:06:17 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2013-11-14 22:06:17 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!aInnermostTarget)) {
|
2013-11-14 22:06:17 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2013-11-14 22:06:17 +04:00
|
|
|
|
|
|
|
// Don't delete or release anything while holding the lock.
|
|
|
|
nsAutoPtr<nsChainedEventQueue> queue;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsNestedEventTarget> target;
|
2013-11-14 22:06:17 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
// Make sure we're popping the innermost event target.
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(mEvents->mEventTarget != aInnermostTarget)) {
|
2013-11-14 22:06:17 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2013-11-14 22:06:17 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mEvents != &mEventsRoot);
|
|
|
|
|
|
|
|
queue = mEvents;
|
|
|
|
mEvents = mEvents->mNext;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
2015-09-20 11:59:56 +03:00
|
|
|
while (queue->GetEvent(false, getter_AddRefs(event), lock)) {
|
|
|
|
mEvents->PutEvent(event.forget(), lock);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2013-11-14 22:06:17 +04:00
|
|
|
|
|
|
|
// Don't let the event target post any more events.
|
|
|
|
queue->mEventTarget.swap(target);
|
|
|
|
target->mQueue = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
nsThread::SetScriptObserver(mozilla::CycleCollectedJSRuntime* aScriptObserver)
|
2012-10-24 02:26:36 +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
|
|
|
if (!aScriptObserver) {
|
|
|
|
mScriptObserver = nullptr;
|
|
|
|
return;
|
2012-10-24 02:26:36 +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
|
|
|
MOZ_ASSERT(!mScriptObserver);
|
|
|
|
mScriptObserver = aScriptObserver;
|
2012-10-24 02:26:36 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsThread::nsNestedEventTarget, nsIEventTarget)
|
2013-11-14 22:06:17 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-07-10 06:21:46 +03:00
|
|
|
nsThread::nsNestedEventTarget::DispatchFromScript(nsIRunnable* aEvent, uint32_t aFlags)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
|
|
|
return Dispatch(event.forget(), aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::nsNestedEventTarget::Dispatch(already_AddRefed<nsIRunnable>&& aEvent, uint32_t aFlags)
|
2013-11-14 22:06:17 +04:00
|
|
|
{
|
2015-07-10 06:21:46 +03:00
|
|
|
LOG(("THRD(%p) Dispatch [%p %x] to nested loop %p\n", mThread.get(), /*XXX aEvent*/ nullptr,
|
2014-05-27 11:15:35 +04:00
|
|
|
aFlags, this));
|
2013-11-14 22:06:17 +04:00
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
return mThread->DispatchInternal(Move(aEvent), aFlags, this);
|
2013-11-14 22:06:17 +04:00
|
|
|
}
|
|
|
|
|
2016-05-13 01:15:43 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::nsNestedEventTarget::DelayedDispatch(already_AddRefed<nsIRunnable>&&, uint32_t)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2013-11-14 22:06:17 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::nsNestedEventTarget::IsOnCurrentThread(bool* aResult)
|
2013-11-14 22:06:17 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
return mThread->IsOnCurrentThread(aResult);
|
2013-11-14 22:06:17 +04:00
|
|
|
}
|