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/. */
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
#include "nsThreadManager.h"
|
|
|
|
#include "nsThread.h"
|
2009-10-28 20:28:57 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsIClassInfoImpl.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2017-09-14 14:51:41 +03:00
|
|
|
#include "nsXULAppAPI.h"
|
2017-09-02 02:39:13 +03:00
|
|
|
#include "LabeledEventQueue.h"
|
|
|
|
#include "MainThreadQueue.h"
|
2017-04-19 08:24:09 +03:00
|
|
|
#include "mozilla/AbstractThread.h"
|
2018-01-28 10:41:36 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm)
This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows:
Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage.
Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM.
There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities.
The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together.
One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization.
MozReview-Commit-ID: 9Evtr9oIJvx
2017-06-21 05:42:13 +03:00
|
|
|
#include "mozilla/EventQueue.h"
|
2017-03-21 10:44:12 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2017-07-29 00:56:49 +03:00
|
|
|
#include "mozilla/Scheduler.h"
|
2017-07-20 02:07:39 +03:00
|
|
|
#include "mozilla/SystemGroup.h"
|
2018-01-28 10:41:36 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm)
This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows:
Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage.
Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM.
There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities.
The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together.
One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization.
MozReview-Commit-ID: 9Evtr9oIJvx
2017-06-21 05:42:13 +03:00
|
|
|
#include "mozilla/ThreadEventQueue.h"
|
2014-07-04 14:34:15 +04:00
|
|
|
#include "mozilla/ThreadLocal.h"
|
Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm)
This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows:
Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage.
Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM.
There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities.
The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together.
One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization.
MozReview-Commit-ID: 9Evtr9oIJvx
2017-06-21 05:42:13 +03:00
|
|
|
#include "PrioritizedEventQueue.h"
|
2013-10-30 00:58:09 +04:00
|
|
|
#ifdef MOZ_CANARY
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2016-08-24 17:18:06 +03:00
|
|
|
#include "MainThreadIdlePeriod.h"
|
2017-03-21 10:44:12 +03:00
|
|
|
#include "InputEventStatistics.h"
|
2016-08-24 17:18:06 +03: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
|
|
|
using namespace mozilla;
|
|
|
|
|
2015-11-23 22:11:22 +03:00
|
|
|
static MOZ_THREAD_LOCAL(bool) sTLSIsMainThread;
|
2017-07-29 00:56:49 +03:00
|
|
|
static MOZ_THREAD_LOCAL(PRThread*) gTlsCurrentVirtualThread;
|
2014-07-04 14:34:15 +04:00
|
|
|
|
2017-08-30 06:13:50 +03:00
|
|
|
bool
|
|
|
|
NS_IsMainThreadTLSInitialized()
|
|
|
|
{
|
|
|
|
return sTLSIsMainThread.initialized();
|
|
|
|
}
|
|
|
|
|
2014-07-04 14:34:15 +04:00
|
|
|
bool
|
|
|
|
NS_IsMainThread()
|
|
|
|
{
|
|
|
|
return sTLSIsMainThread.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NS_SetMainThread()
|
|
|
|
{
|
2015-11-23 22:11:22 +03:00
|
|
|
if (!sTLSIsMainThread.init()) {
|
|
|
|
MOZ_CRASH();
|
2014-07-04 14:34:15 +04:00
|
|
|
}
|
2015-11-23 22:11:22 +03:00
|
|
|
sTLSIsMainThread.set(true);
|
2014-07-04 14:34:15 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
2017-07-29 00:56:49 +03:00
|
|
|
void
|
|
|
|
NS_SetMainThread(PRThread* aVirtualThread)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(Scheduler::IsCooperativeThread());
|
|
|
|
|
|
|
|
MOZ_ASSERT(!gTlsCurrentVirtualThread.get());
|
|
|
|
gTlsCurrentVirtualThread.set(aVirtualThread);
|
|
|
|
NS_SetMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NS_UnsetMainThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(Scheduler::IsCooperativeThread());
|
|
|
|
|
|
|
|
sTLSIsMainThread.set(false);
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
gTlsCurrentVirtualThread.set(nullptr);
|
|
|
|
}
|
|
|
|
|
2018-02-05 21:55:07 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
void
|
|
|
|
AssertIsOnMainThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // mozilla namespace
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-06-10 09:04:49 +03:00
|
|
|
typedef nsTArray<NotNull<RefPtr<nsThread>>> nsThreadArray;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static void
|
2014-05-27 11:15:35 +04:00
|
|
|
ReleaseObject(void* aData)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
static_cast<nsISupports*>(aData)->Release();
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// statically allocated instance
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsThreadManager::AddRef()
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsThreadManager::Release()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2013-10-11 00:42:16 +04:00
|
|
|
NS_IMPL_CLASSINFO(nsThreadManager, nullptr,
|
2010-06-22 20:59:57 +04:00
|
|
|
nsIClassInfo::THREADSAFE | nsIClassInfo::SINGLETON,
|
|
|
|
NS_THREADMANAGER_CID)
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(nsThreadManager, nsIThreadManager)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsThreadManager, nsIThreadManager)
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2018-01-28 10:41:36 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Simple observer to monitor the beginning of the shutdown.
|
|
|
|
class ShutdownObserveHelper final : public nsIObserver
|
|
|
|
, public nsSupportsWeakReference
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
Create(ShutdownObserveHelper** aObserver)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aObserver);
|
|
|
|
|
|
|
|
RefPtr<ShutdownObserveHelper> observer = new ShutdownObserveHelper();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (NS_WARN_IF(!obs)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = obs->AddObserver(observer, NS_XPCOM_SHUTDOWN_OBSERVER_ID, true);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = obs->AddObserver(observer, "content-child-will-shutdown", true);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
observer.forget(aObserver);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) override
|
|
|
|
{
|
|
|
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) ||
|
|
|
|
!strcmp(aTopic, "content-child-will-shutdown")) {
|
|
|
|
mShuttingDown = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ShuttingDown() const
|
|
|
|
{
|
|
|
|
return mShuttingDown;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit ShutdownObserveHelper()
|
|
|
|
: mShuttingDown(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~ShutdownObserveHelper() = default;
|
|
|
|
|
|
|
|
bool mShuttingDown;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(ShutdownObserveHelper)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(ShutdownObserveHelper)
|
|
|
|
NS_IMPL_RELEASE(ShutdownObserveHelper)
|
|
|
|
|
|
|
|
StaticRefPtr<ShutdownObserveHelper> gShutdownObserveHelper;
|
|
|
|
|
|
|
|
} // anonymous
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-09-21 00:10:20 +03:00
|
|
|
/*static*/ nsThreadManager&
|
|
|
|
nsThreadManager::get()
|
|
|
|
{
|
|
|
|
static nsThreadManager sInstance;
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
2018-01-28 10:41:36 +03:00
|
|
|
/* static */ void
|
|
|
|
nsThreadManager::InitializeShutdownObserver()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!gShutdownObserveHelper);
|
|
|
|
|
|
|
|
RefPtr<ShutdownObserveHelper> observer;
|
|
|
|
nsresult rv = ShutdownObserveHelper::Create(getter_AddRefs(observer));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gShutdownObserveHelper = observer;
|
|
|
|
ClearOnShutdown(&gShutdownObserveHelper);
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsresult
|
|
|
|
nsThreadManager::Init()
|
|
|
|
{
|
2014-02-01 03:02:30 +04:00
|
|
|
// Child processes need to initialize the thread manager before they
|
|
|
|
// initialize XPCOM in order to set up the crash reporter. This leads to
|
|
|
|
// situations where we get initialized twice.
|
2014-05-27 11:15:35 +04:00
|
|
|
if (mInitialized) {
|
2014-02-01 03:02:30 +04:00
|
|
|
return NS_OK;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-02-01 03:02:30 +04:00
|
|
|
|
2017-07-29 00:56:49 +03:00
|
|
|
if (!gTlsCurrentVirtualThread.init()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scheduler::EventLoopActivation::Init();
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (PR_NewThreadPrivateIndex(&mCurThreadIndex, ReleaseObject) == PR_FAILURE) {
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
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
|
|
|
|
2013-10-30 00:58:09 +04:00
|
|
|
#ifdef MOZ_CANARY
|
|
|
|
const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NONBLOCK;
|
|
|
|
const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
|
|
|
|
char* env_var_flag = getenv("MOZ_KILL_CANARIES");
|
2014-05-27 11:15:35 +04:00
|
|
|
sCanaryOutputFD =
|
|
|
|
env_var_flag ? (env_var_flag[0] ? open(env_var_flag, flags, mode) :
|
|
|
|
STDERR_FILENO) :
|
|
|
|
0;
|
2013-10-30 00:58:09 +04:00
|
|
|
#endif
|
|
|
|
|
Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm)
This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows:
Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage.
Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM.
There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities.
The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together.
One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization.
MozReview-Commit-ID: 9Evtr9oIJvx
2017-06-21 05:42:13 +03:00
|
|
|
nsCOMPtr<nsIIdlePeriod> idlePeriod = new MainThreadIdlePeriod();
|
|
|
|
|
2017-07-29 00:56:49 +03:00
|
|
|
bool startScheduler = false;
|
|
|
|
if (XRE_IsContentProcess() && Scheduler::IsSchedulerEnabled()) {
|
|
|
|
mMainThread = Scheduler::Init(idlePeriod);
|
|
|
|
startScheduler = true;
|
|
|
|
} else {
|
2017-09-02 02:39:13 +03:00
|
|
|
if (XRE_IsContentProcess() && Scheduler::UseMultipleQueues()) {
|
|
|
|
mMainThread = CreateMainThread<ThreadEventQueue<PrioritizedEventQueue<LabeledEventQueue>>, LabeledEventQueue>(idlePeriod);
|
|
|
|
} else {
|
|
|
|
mMainThread = CreateMainThread<ThreadEventQueue<PrioritizedEventQueue<EventQueue>>, EventQueue>(idlePeriod);
|
|
|
|
}
|
2017-07-29 00:56:49 +03:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
nsresult rv = mMainThread->InitCurrentThread();
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mMainThread = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to keep a pointer to the current thread, so we can satisfy
|
|
|
|
// GetIsMainThread calls that occur post-Shutdown.
|
|
|
|
mMainThread->GetPRThread(&mMainPRThread);
|
|
|
|
|
2017-04-19 08:24:09 +03:00
|
|
|
// Init AbstractThread.
|
|
|
|
AbstractThread::InitTLS();
|
|
|
|
AbstractThread::InitMainThread();
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitialized = true;
|
2017-07-29 00:56:49 +03:00
|
|
|
|
|
|
|
if (startScheduler) {
|
|
|
|
Scheduler::Start();
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsThreadManager::Shutdown()
|
|
|
|
{
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "shutdown not called from main thread");
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Prevent further access to the thread manager (no more new threads!)
|
|
|
|
//
|
2015-03-23 23:49:09 +03:00
|
|
|
// What happens if shutdown happens before NewThread completes?
|
|
|
|
// We Shutdown() the new thread, and return error if we've started Shutdown
|
|
|
|
// between when NewThread started, and when the thread finished initializing
|
|
|
|
// and registering with ThreadManager.
|
2006-05-10 21:30:15 +04:00
|
|
|
//
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitialized = false;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2006-05-11 05:24:57 +04:00
|
|
|
// Empty the main thread event queue before we begin shutting down threads.
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_ProcessPendingEvents(mMainThread);
|
|
|
|
|
2006-05-11 05:24:57 +04:00
|
|
|
// We gather the threads from the hashtable into a list, so that we avoid
|
|
|
|
// holding the hashtable lock while calling nsIThread::Shutdown.
|
2006-05-10 21:30:15 +04:00
|
|
|
nsThreadArray threads;
|
|
|
|
{
|
2015-03-23 23:49:09 +03:00
|
|
|
OffTheBooksMutexAutoLock lock(mLock);
|
2015-07-10 02:54:59 +03:00
|
|
|
for (auto iter = mThreadsByPRThread.Iter(); !iter.Done(); iter.Next()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsThread>& thread = iter.Data();
|
2016-06-10 09:04:49 +03:00
|
|
|
threads.AppendElement(WrapNotNull(thread));
|
2015-07-10 02:54:59 +03:00
|
|
|
iter.Remove();
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// It's tempting to walk the list of threads here and tell them each to stop
|
|
|
|
// accepting new events, but that could lead to badness if one of those
|
|
|
|
// threads is stuck waiting for a response from another thread. To do it
|
|
|
|
// right, we'd need some way to interrupt the threads.
|
2014-05-27 11:15:35 +04:00
|
|
|
//
|
2006-05-11 05:24:57 +04:00
|
|
|
// Instead, we process events on the current thread while waiting for threads
|
|
|
|
// to shutdown. This means that we have to preserve a mostly functioning
|
|
|
|
// world until such time as the threads exit.
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Shutdown all threads that require it (join with threads that we created).
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < threads.Length(); ++i) {
|
2016-06-10 09:04:49 +03:00
|
|
|
NotNull<nsThread*> thread = threads[i];
|
2014-05-27 11:15:35 +04:00
|
|
|
if (thread->ShutdownRequired()) {
|
2006-05-10 21:30:15 +04:00
|
|
|
thread->Shutdown();
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2016-01-18 20:34:38 +03:00
|
|
|
// NB: It's possible that there are events in the queue that want to *start*
|
|
|
|
// an asynchronous shutdown. But we have already shutdown the threads above,
|
|
|
|
// so there's no need to worry about them. We only have to wait for all
|
|
|
|
// in-flight asynchronous thread shutdowns to complete.
|
|
|
|
mMainThread->WaitForAllAsynchronousShutdowns();
|
|
|
|
|
2006-05-11 05:24:57 +04:00
|
|
|
// In case there are any more events somehow...
|
|
|
|
NS_ProcessPendingEvents(mMainThread);
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// There are no more background threads at this point.
|
|
|
|
|
2006-05-11 05:24:57 +04:00
|
|
|
// Clear the table of threads.
|
|
|
|
{
|
2015-03-23 23:49:09 +03:00
|
|
|
OffTheBooksMutexAutoLock lock(mLock);
|
2006-05-11 05:24:57 +04:00
|
|
|
mThreadsByPRThread.Clear();
|
|
|
|
}
|
|
|
|
|
2009-06-22 17:08:04 +04:00
|
|
|
// Normally thread shutdown clears the observer for the thread, but since the
|
|
|
|
// main thread is special we do it manually here after we're sure all events
|
|
|
|
// have been processed.
|
2012-07-30 18:20:58 +04:00
|
|
|
mMainThread->SetObserver(nullptr);
|
2012-01-14 22:31:13 +04:00
|
|
|
mMainThread->ClearObservers();
|
2009-06-22 17:08:04 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// Release main thread object.
|
2012-07-30 18:20:58 +04:00
|
|
|
mMainThread = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Remove the TLS entry for the main thread.
|
2012-07-30 18:20:58 +04:00
|
|
|
PR_SetThreadPrivate(mCurThreadIndex, nullptr);
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-10 09:04:49 +03:00
|
|
|
nsThreadManager::RegisterCurrentThread(nsThread& aThread)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2016-06-10 09:04:49 +03:00
|
|
|
MOZ_ASSERT(aThread.GetPRThread() == PR_GetCurrentThread(), "bad aThread");
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2015-03-23 23:49:09 +03:00
|
|
|
OffTheBooksMutexAutoLock lock(mLock);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2013-04-19 15:54:18 +04:00
|
|
|
++mCurrentNumberOfThreads;
|
|
|
|
if (mCurrentNumberOfThreads > mHighestNumberOfThreads) {
|
|
|
|
mHighestNumberOfThreads = mCurrentNumberOfThreads;
|
|
|
|
}
|
|
|
|
|
2016-06-10 09:04:49 +03:00
|
|
|
mThreadsByPRThread.Put(aThread.GetPRThread(), &aThread); // XXX check OOM?
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2016-06-10 09:04:49 +03:00
|
|
|
aThread.AddRef(); // for TLS entry
|
|
|
|
PR_SetThreadPrivate(mCurThreadIndex, &aThread);
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-10 09:04:49 +03:00
|
|
|
nsThreadManager::UnregisterCurrentThread(nsThread& aThread)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2016-06-10 09:04:49 +03:00
|
|
|
MOZ_ASSERT(aThread.GetPRThread() == PR_GetCurrentThread(), "bad aThread");
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2015-03-23 23:49:09 +03:00
|
|
|
OffTheBooksMutexAutoLock lock(mLock);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2013-04-19 15:54:18 +04:00
|
|
|
--mCurrentNumberOfThreads;
|
2016-06-10 09:04:49 +03:00
|
|
|
mThreadsByPRThread.Remove(aThread.GetPRThread());
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
PR_SetThreadPrivate(mCurThreadIndex, nullptr);
|
2006-05-10 21:30:15 +04:00
|
|
|
// Ref-count balanced via ReleaseObject
|
|
|
|
}
|
|
|
|
|
2017-07-29 00:56:49 +03:00
|
|
|
nsThread*
|
|
|
|
nsThreadManager::CreateCurrentThread(SynchronizedEventQueue* aQueue,
|
|
|
|
nsThread::MainThreadFlag aMainThread)
|
|
|
|
{
|
|
|
|
// Make sure we don't have an nsThread yet.
|
|
|
|
MOZ_ASSERT(!PR_GetThreadPrivate(mCurThreadIndex));
|
|
|
|
|
|
|
|
if (!mInitialized) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK, that's fine. We'll dynamically create one :-)
|
|
|
|
RefPtr<nsThread> thread = new nsThread(WrapNotNull(aQueue), aMainThread, 0);
|
|
|
|
if (!thread || NS_FAILED(thread->InitCurrentThread())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return thread.get(); // reference held in TLS
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread*
|
2006-05-10 21:30:15 +04:00
|
|
|
nsThreadManager::GetCurrentThread()
|
|
|
|
{
|
|
|
|
// read thread local storage
|
2014-05-27 11:15:35 +04:00
|
|
|
void* data = PR_GetThreadPrivate(mCurThreadIndex);
|
|
|
|
if (data) {
|
|
|
|
return static_cast<nsThread*>(data);
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2008-02-21 15:47:26 +03:00
|
|
|
if (!mInitialized) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2008-02-21 15:47:26 +03:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// OK, that's fine. We'll dynamically create one :-)
|
Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm)
This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows:
Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage.
Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM.
There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities.
The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together.
One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization.
MozReview-Commit-ID: 9Evtr9oIJvx
2017-06-21 05:42:13 +03:00
|
|
|
RefPtr<ThreadEventQueue<EventQueue>> queue =
|
|
|
|
new ThreadEventQueue<EventQueue>(MakeUnique<EventQueue>());
|
|
|
|
RefPtr<nsThread> thread = new nsThread(WrapNotNull(queue), nsThread::NOT_MAIN_THREAD, 0);
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!thread || NS_FAILED(thread->InitCurrentThread())) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
return thread.get(); // reference held in TLS
|
|
|
|
}
|
|
|
|
|
2018-01-19 18:41:22 +03:00
|
|
|
bool
|
|
|
|
nsThreadManager::IsNSThread() const
|
|
|
|
{
|
|
|
|
return mInitialized && !!PR_GetThreadPrivate(mCurThreadIndex);
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadManager::NewThread(uint32_t aCreationFlags,
|
|
|
|
uint32_t aStackSize,
|
|
|
|
nsIThread** aResult)
|
2016-12-20 17:10:20 +03:00
|
|
|
{
|
|
|
|
return NewNamedThread(NS_LITERAL_CSTRING(""), aStackSize, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThreadManager::NewNamedThread(const nsACString& aName,
|
|
|
|
uint32_t aStackSize,
|
|
|
|
nsIThread** aResult)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2015-03-23 23:49:09 +03:00
|
|
|
// Note: can be called from arbitrary threads
|
2017-06-14 11:05:53 +03:00
|
|
|
|
2006-05-11 05:24:57 +04:00
|
|
|
// No new threads during Shutdown
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!mInitialized)) {
|
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
|
|
|
|
Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm)
This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows:
Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage.
Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM.
There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities.
The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together.
One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization.
MozReview-Commit-ID: 9Evtr9oIJvx
2017-06-21 05:42:13 +03:00
|
|
|
RefPtr<ThreadEventQueue<EventQueue>> queue =
|
|
|
|
new ThreadEventQueue<EventQueue>(MakeUnique<EventQueue>());
|
|
|
|
RefPtr<nsThread> thr = new nsThread(WrapNotNull(queue), nsThread::NOT_MAIN_THREAD, aStackSize);
|
2016-12-20 17:10:20 +03:00
|
|
|
nsresult rv = thr->Init(aName); // Note: blocks until the new thread has been set up
|
2006-05-10 21:30:15 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:49:09 +03:00
|
|
|
// At this point, we expect that the thread has been registered in mThreadByPRThread;
|
2006-05-10 21:30:15 +04:00
|
|
|
// however, it is possible that it could have also been replaced by now, so
|
2015-03-23 23:49:09 +03:00
|
|
|
// we cannot really assert that it was added. Instead, kill it if we entered
|
|
|
|
// Shutdown() during/before Init()
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mInitialized)) {
|
|
|
|
if (thr->ShutdownRequired()) {
|
|
|
|
thr->Shutdown(); // ok if it happens multiple times
|
|
|
|
}
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2015-03-23 23:49:09 +03:00
|
|
|
thr.forget(aResult);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadManager::GetThreadFromPRThread(PRThread* aThread, nsIThread** aResult)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2006-05-11 05:24:57 +04:00
|
|
|
// Keep this functioning during Shutdown
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!mMainThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!aThread)) {
|
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
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsThread> temp;
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2015-03-23 23:49:09 +03:00
|
|
|
OffTheBooksMutexAutoLock lock(mLock);
|
2014-05-27 11:15:35 +04:00
|
|
|
mThreadsByPRThread.Get(aThread, getter_AddRefs(temp));
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IF_ADDREF(*aResult = temp);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadManager::GetMainThread(nsIThread** aResult)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2006-05-11 05:24:57 +04:00
|
|
|
// Keep this functioning during Shutdown
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!mMainThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
NS_ADDREF(*aResult = mMainThread);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadManager::GetCurrentThread(nsIThread** aResult)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2006-05-11 05:24:57 +04:00
|
|
|
// Keep this functioning during Shutdown
|
2017-06-20 19:51:29 +03:00
|
|
|
if (!mMainThread) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
*aResult = GetCurrentThread();
|
|
|
|
if (!*aResult) {
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
NS_ADDREF(*aResult);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-06-21 19:59:28 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThreadManager::SpinEventLoopUntil(nsINestedEventLoopCondition* aCondition)
|
2018-01-28 10:41:36 +03:00
|
|
|
{
|
|
|
|
return SpinEventLoopUntilInternal(aCondition, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThreadManager::SpinEventLoopUntilOrShutdown(nsINestedEventLoopCondition* aCondition)
|
|
|
|
{
|
|
|
|
return SpinEventLoopUntilInternal(aCondition, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsThreadManager::SpinEventLoopUntilInternal(nsINestedEventLoopCondition* aCondition,
|
|
|
|
bool aCheckingShutdown)
|
2017-06-21 19:59:28 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsINestedEventLoopCondition> condition(aCondition);
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2018-01-28 10:41:36 +03:00
|
|
|
// Nothing to do if already shutting down. Note that gShutdownObserveHelper is
|
|
|
|
// nullified on shutdown.
|
|
|
|
if (aCheckingShutdown &&
|
|
|
|
(!gShutdownObserveHelper || gShutdownObserveHelper->ShuttingDown())) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-06-21 19:59:28 +03:00
|
|
|
if (!mozilla::SpinEventLoopUntil([&]() -> bool {
|
2018-01-28 10:41:36 +03:00
|
|
|
// Shutting down is started.
|
|
|
|
if (aCheckingShutdown &&
|
|
|
|
(!gShutdownObserveHelper ||
|
|
|
|
gShutdownObserveHelper->ShuttingDown())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-21 19:59:28 +03:00
|
|
|
bool isDone = false;
|
|
|
|
rv = condition->IsDone(&isDone);
|
|
|
|
// JS failure should be unusual, but we need to stop and propagate
|
|
|
|
// the error back to the caller.
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isDone;
|
|
|
|
})) {
|
|
|
|
// We stopped early for some reason, which is unexpected.
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we exited when the condition told us to, we need to return whether
|
|
|
|
// the condition encountered failure when executing.
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-06-21 19:59:28 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThreadManager::SpinEventLoopUntilEmpty()
|
|
|
|
{
|
|
|
|
nsIThread* thread = NS_GetCurrentThread();
|
|
|
|
|
|
|
|
while (NS_HasPendingEvents(thread)) {
|
|
|
|
(void)NS_ProcessNextEvent(thread, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-20 02:07:39 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThreadManager::GetSystemGroupEventTarget(nsIEventTarget** aTarget)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEventTarget> target = SystemGroup::EventTargetFor(TaskCategory::Other);
|
|
|
|
target.forget(aTarget);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-19 15:54:18 +04:00
|
|
|
uint32_t
|
|
|
|
nsThreadManager::GetHighestNumberOfThreads()
|
|
|
|
{
|
2015-03-23 23:49:09 +03:00
|
|
|
OffTheBooksMutexAutoLock lock(mLock);
|
2013-04-19 15:54:18 +04:00
|
|
|
return mHighestNumberOfThreads;
|
|
|
|
}
|
2017-04-14 19:27:32 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-05-19 10:41:24 +03:00
|
|
|
nsThreadManager::DispatchToMainThread(nsIRunnable *aEvent, uint32_t aPriority)
|
2017-04-14 19:27:32 +03:00
|
|
|
{
|
|
|
|
// Note: C++ callers should instead use NS_DispatchToMainThread.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// Keep this functioning during Shutdown
|
|
|
|
if (NS_WARN_IF(!mMainThread)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2017-05-19 10:41:24 +03:00
|
|
|
if (aPriority != nsIRunnablePriority::PRIORITY_NORMAL) {
|
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
|
|
|
return mMainThread->DispatchFromScript(
|
|
|
|
new PrioritizableRunnable(event.forget(), aPriority), 0);
|
|
|
|
}
|
2017-04-14 19:27:32 +03:00
|
|
|
return mMainThread->DispatchFromScript(aEvent, 0);
|
|
|
|
}
|
2017-06-14 11:05:53 +03:00
|
|
|
|
2017-03-21 10:44:12 +03:00
|
|
|
void
|
|
|
|
nsThreadManager::EnableMainThreadEventPrioritization()
|
|
|
|
{
|
2017-07-28 10:14:54 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-03-21 10:44:12 +03:00
|
|
|
InputEventStatistics::Get().SetEnable(true);
|
Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm)
This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows:
Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage.
Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM.
There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities.
The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together.
One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization.
MozReview-Commit-ID: 9Evtr9oIJvx
2017-06-21 05:42:13 +03:00
|
|
|
mMainThread->EnableInputEventPrioritization();
|
2017-03-21 10:44:12 +03:00
|
|
|
}
|
|
|
|
|
2017-07-28 10:14:54 +03:00
|
|
|
void
|
|
|
|
nsThreadManager::FlushInputEventPrioritization()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mMainThread->FlushInputEventPrioritization();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsThreadManager::SuspendInputEventPrioritization()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mMainThread->SuspendInputEventPrioritization();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsThreadManager::ResumeInputEventPrioritization()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mMainThread->ResumeInputEventPrioritization();
|
|
|
|
}
|
|
|
|
|
2017-06-14 11:05:53 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThreadManager::IdleDispatchToMainThread(nsIRunnable *aEvent, uint32_t aTimeout)
|
|
|
|
{
|
|
|
|
// Note: C++ callers should instead use NS_IdleDispatchToThread or
|
|
|
|
// NS_IdleDispatchToCurrentThread.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
|
|
|
if (aTimeout) {
|
|
|
|
return NS_IdleDispatchToThread(event.forget(), aTimeout, mMainThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_IdleDispatchToThread(event.forget(), mMainThread);
|
|
|
|
}
|
2017-07-29 00:56:49 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
PRThread*
|
|
|
|
GetCurrentVirtualThread()
|
|
|
|
{
|
|
|
|
// We call GetCurrentVirtualThread very early in startup, before the TLS is
|
|
|
|
// initialized. Make sure we don't assert in that case.
|
|
|
|
if (gTlsCurrentVirtualThread.initialized()) {
|
|
|
|
if (gTlsCurrentVirtualThread.get()) {
|
|
|
|
return gTlsCurrentVirtualThread.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PR_GetCurrentThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
PRThread*
|
|
|
|
GetCurrentPhysicalThread()
|
|
|
|
{
|
|
|
|
return PR_GetCurrentThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|