2015-03-14 05:41:12 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2015-07-16 21:52:43 +03:00
|
|
|
#include "mozilla/AbstractThread.h"
|
2015-03-14 05:41:12 +03:00
|
|
|
|
2015-04-02 23:21:53 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2015-04-14 21:58:30 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2015-08-18 20:31:16 +03:00
|
|
|
#include "mozilla/MozPromise.h" // We initialize the MozPromise logging in this file.
|
|
|
|
#include "mozilla/StateWatching.h" // We initialize the StateWatching logging in this file.
|
2020-04-20 05:07:10 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
#include "mozilla/TaskDispatcher.h"
|
2020-04-20 05:07:10 +03:00
|
|
|
#include "mozilla/TaskQueue.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
#include "nsContentUtils.h"
|
2020-04-29 00:18:17 +03:00
|
|
|
#include "nsIThreadInternal.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2020-04-29 00:18:17 +03:00
|
|
|
#include "nsThreadManager.h"
|
2020-04-20 05:07:10 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
|
2015-03-14 05:41:12 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2015-10-19 22:50:14 +03:00
|
|
|
LazyLogModule gMozPromiseLog("MozPromise");
|
|
|
|
LazyLogModule gStateWatchingLog("StateWatching");
|
|
|
|
|
2015-04-02 23:21:53 +03:00
|
|
|
StaticRefPtr<AbstractThread> sMainThread;
|
2015-11-23 22:11:22 +03:00
|
|
|
MOZ_THREAD_LOCAL(AbstractThread*) AbstractThread::sCurrentThreadTLS;
|
2015-04-02 23:21:53 +03:00
|
|
|
|
2020-04-29 00:18:17 +03:00
|
|
|
class XPCOMThreadWrapper : public AbstractThread {
|
2015-04-14 08:08:47 +03:00
|
|
|
public:
|
2020-04-29 00:18:17 +03:00
|
|
|
explicit XPCOMThreadWrapper(nsIThreadInternal* aThread,
|
2016-11-29 08:01:18 +03:00
|
|
|
bool aRequireTailDispatch)
|
2020-04-29 00:18:17 +03:00
|
|
|
: AbstractThread(aRequireTailDispatch), mThread(aThread) {
|
2015-04-14 21:58:30 +03:00
|
|
|
// Our current mechanism of implementing tail dispatch is appshell-specific.
|
|
|
|
// This is because a very similar mechanism already exists on the main
|
|
|
|
// thread, and we want to avoid making event dispatch on the main thread
|
|
|
|
// more complicated than it already is.
|
|
|
|
//
|
|
|
|
// If you need to use tail dispatch on other XPCOM threads, you'll need to
|
|
|
|
// implement an nsIThreadObserver to fire the tail dispatcher at the
|
2017-08-03 02:56:30 +03:00
|
|
|
// appropriate times. You will also need to modify this assertion.
|
|
|
|
MOZ_ASSERT_IF(aRequireTailDispatch,
|
2020-04-29 00:18:17 +03:00
|
|
|
NS_IsMainThread() && aThread->IsOnCurrentThread());
|
2015-04-14 21:58:30 +03:00
|
|
|
}
|
2015-03-14 05:41:12 +03:00
|
|
|
|
2020-04-20 05:06:51 +03:00
|
|
|
nsresult Dispatch(already_AddRefed<nsIRunnable> aRunnable,
|
|
|
|
DispatchReason aReason = NormalDispatch) override {
|
2015-04-15 20:54:25 +03:00
|
|
|
AbstractThread* currentThread;
|
2015-05-14 04:47:42 +03:00
|
|
|
if (aReason != TailDispatch && (currentThread = GetCurrent()) &&
|
|
|
|
RequiresTailDispatch(currentThread)) {
|
2018-05-30 22:15:35 +03:00
|
|
|
return currentThread->TailDispatcher().AddTask(this,
|
|
|
|
std::move(aRunnable));
|
2015-04-15 20:54:25 +03:00
|
|
|
}
|
|
|
|
|
2019-02-25 13:50:02 +03:00
|
|
|
RefPtr<nsIRunnable> runner = new Runner(this, std::move(aRunnable));
|
2020-04-29 00:18:17 +03:00
|
|
|
return mThread->Dispatch(runner.forget(), NS_DISPATCH_NORMAL);
|
2015-04-14 08:08:47 +03:00
|
|
|
}
|
|
|
|
|
2017-06-09 23:19:13 +03:00
|
|
|
// Prevent a GCC warning about the other overload of Dispatch being hidden.
|
|
|
|
using AbstractThread::Dispatch;
|
|
|
|
|
2020-04-20 05:06:51 +03:00
|
|
|
bool IsCurrentThreadIn() const override {
|
2020-04-29 00:18:17 +03:00
|
|
|
return mThread->IsOnCurrentThread();
|
2015-04-14 08:08:47 +03:00
|
|
|
}
|
|
|
|
|
2015-04-16 22:24:54 +03:00
|
|
|
void FireTailDispatcher() {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mTailDispatcher.isSome());
|
|
|
|
mTailDispatcher.ref().DrainDirectTasks();
|
|
|
|
mTailDispatcher.reset();
|
|
|
|
}
|
2015-04-14 21:58:30 +03:00
|
|
|
|
2020-04-20 05:06:51 +03:00
|
|
|
TaskDispatcher& TailDispatcher() override {
|
2015-04-14 21:58:30 +03:00
|
|
|
MOZ_ASSERT(IsCurrentThreadIn());
|
|
|
|
if (!mTailDispatcher.isSome()) {
|
|
|
|
mTailDispatcher.emplace(/* aIsTailDispatcher = */ true);
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2020-04-29 00:18:17 +03:00
|
|
|
NewRunnableMethod("XPCOMThreadWrapper::FireTailDispatcher", this,
|
|
|
|
&XPCOMThreadWrapper::FireTailDispatcher);
|
2020-04-21 04:25:48 +03:00
|
|
|
nsContentUtils::RunInStableState(event.forget());
|
2015-04-14 21:58:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return mTailDispatcher.ref();
|
|
|
|
}
|
2015-04-14 09:53:07 +03:00
|
|
|
|
2020-04-20 05:06:51 +03:00
|
|
|
bool MightHaveTailTasks() override { return mTailDispatcher.isSome(); }
|
2016-09-21 12:24:43 +03:00
|
|
|
|
2020-04-29 00:18:17 +03:00
|
|
|
nsIEventTarget* AsEventTarget() override { return mThread; }
|
2015-04-20 15:29:35 +03:00
|
|
|
|
2015-04-14 08:08:47 +03:00
|
|
|
private:
|
2020-04-29 00:18:17 +03:00
|
|
|
const RefPtr<nsIThreadInternal> mThread;
|
2015-04-14 21:58:30 +03:00
|
|
|
Maybe<AutoTaskDispatcher> mTailDispatcher;
|
2016-11-29 08:01:18 +03:00
|
|
|
|
2020-04-20 05:08:23 +03:00
|
|
|
class Runner : public Runnable {
|
2016-11-29 08:01:18 +03:00
|
|
|
public:
|
2020-04-29 00:18:17 +03:00
|
|
|
explicit Runner(XPCOMThreadWrapper* aThread,
|
2019-02-25 13:50:02 +03:00
|
|
|
already_AddRefed<nsIRunnable> aRunnable)
|
2020-04-29 00:18:17 +03:00
|
|
|
: Runnable("XPCOMThreadWrapper::Runner"),
|
2017-06-12 22:34:10 +03:00
|
|
|
mThread(aThread),
|
2019-02-25 13:50:02 +03:00
|
|
|
mRunnable(aRunnable) {}
|
2016-11-29 08:01:18 +03:00
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
MOZ_ASSERT(mThread == AbstractThread::GetCurrent());
|
|
|
|
MOZ_ASSERT(mThread->IsCurrentThreadIn());
|
2019-02-25 13:50:02 +03:00
|
|
|
return mRunnable->Run();
|
2016-11-29 08:01:18 +03:00
|
|
|
}
|
|
|
|
|
2018-03-23 21:53:55 +03:00
|
|
|
#ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
|
2016-11-29 08:01:18 +03:00
|
|
|
NS_IMETHOD GetName(nsACString& aName) override {
|
|
|
|
aName.AssignLiteral("AbstractThread::Runner");
|
|
|
|
if (nsCOMPtr<nsINamed> named = do_QueryInterface(mRunnable)) {
|
|
|
|
nsAutoCString name;
|
|
|
|
named->GetName(name);
|
|
|
|
if (!name.IsEmpty()) {
|
|
|
|
aName.AppendLiteral(" for ");
|
|
|
|
aName.Append(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-03-23 21:53:55 +03:00
|
|
|
#endif
|
2016-11-29 08:01:18 +03:00
|
|
|
|
|
|
|
private:
|
2020-04-29 00:18:17 +03:00
|
|
|
const RefPtr<XPCOMThreadWrapper> mThread;
|
2019-02-25 13:49:01 +03:00
|
|
|
const RefPtr<nsIRunnable> mRunnable;
|
2016-11-29 08:01:18 +03:00
|
|
|
};
|
2015-04-14 08:08:47 +03:00
|
|
|
};
|
2015-03-14 05:41:12 +03:00
|
|
|
|
2017-06-09 23:19:13 +03:00
|
|
|
NS_IMPL_ISUPPORTS(AbstractThread, nsIEventTarget, nsISerialEventTarget)
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(bool)
|
|
|
|
AbstractThread::IsOnCurrentThreadInfallible() { return IsCurrentThreadIn(); }
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AbstractThread::IsOnCurrentThread(bool* aResult) {
|
|
|
|
*aResult = IsCurrentThreadIn();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AbstractThread::DispatchFromScript(nsIRunnable* aEvent, uint32_t aFlags) {
|
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
|
|
|
return Dispatch(event.forget(), aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AbstractThread::Dispatch(already_AddRefed<nsIRunnable> aEvent,
|
|
|
|
uint32_t aFlags) {
|
2018-05-30 22:15:35 +03:00
|
|
|
return Dispatch(std::move(aEvent), NormalDispatch);
|
2017-06-09 23:19:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AbstractThread::DelayedDispatch(already_AddRefed<nsIRunnable> aEvent,
|
|
|
|
uint32_t aDelayMs) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:24:43 +03:00
|
|
|
nsresult AbstractThread::TailDispatchTasksFor(AbstractThread* aThread) {
|
|
|
|
if (MightHaveTailTasks()) {
|
2017-11-15 09:58:02 +03:00
|
|
|
return TailDispatcher().DispatchTasksFor(aThread);
|
2016-09-21 12:24:43 +03:00
|
|
|
}
|
2017-11-15 09:58:02 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2016-09-21 12:24:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AbstractThread::HasTailTasksFor(AbstractThread* aThread) {
|
|
|
|
if (!MightHaveTailTasks()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return TailDispatcher().HasTasksFor(aThread);
|
|
|
|
}
|
|
|
|
|
2015-05-14 04:47:42 +03:00
|
|
|
bool AbstractThread::RequiresTailDispatch(AbstractThread* aThread) const {
|
2016-05-04 11:24:25 +03:00
|
|
|
MOZ_ASSERT(aThread);
|
2015-05-14 04:47:42 +03:00
|
|
|
// We require tail dispatch if both the source and destination
|
|
|
|
// threads support it.
|
|
|
|
return SupportsTailDispatch() && aThread->SupportsTailDispatch();
|
|
|
|
}
|
|
|
|
|
2016-05-04 11:24:25 +03:00
|
|
|
bool AbstractThread::RequiresTailDispatchFromCurrentThread() const {
|
|
|
|
AbstractThread* current = GetCurrent();
|
|
|
|
return current && RequiresTailDispatch(current);
|
|
|
|
}
|
|
|
|
|
2015-04-02 23:21:53 +03:00
|
|
|
AbstractThread* AbstractThread::MainThread() {
|
|
|
|
MOZ_ASSERT(sMainThread);
|
|
|
|
return sMainThread;
|
|
|
|
}
|
|
|
|
|
2017-04-19 08:24:09 +03:00
|
|
|
void AbstractThread::InitTLS() {
|
|
|
|
if (!sCurrentThreadTLS.init()) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractThread::InitMainThread() {
|
2015-04-02 23:21:53 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-04-07 20:59:44 +03:00
|
|
|
MOZ_ASSERT(!sMainThread);
|
2020-04-29 00:18:17 +03:00
|
|
|
nsCOMPtr<nsIThreadInternal> mainThread =
|
|
|
|
do_QueryInterface(nsThreadManager::get().GetMainThreadWeak());
|
2015-04-07 20:59:44 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mainThread);
|
2020-04-29 00:18:17 +03:00
|
|
|
sMainThread = new XPCOMThreadWrapper(mainThread.get(),
|
2016-11-29 08:01:18 +03:00
|
|
|
/* aRequireTailDispatch = */ true);
|
2015-04-07 20:59:44 +03:00
|
|
|
ClearOnShutdown(&sMainThread);
|
2015-04-14 09:53:07 +03:00
|
|
|
|
|
|
|
if (!sCurrentThreadTLS.init()) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
2020-04-20 05:07:10 +03:00
|
|
|
// Set the default current main thread so that GetCurrent() never returns
|
|
|
|
// nullptr.
|
|
|
|
sCurrentThreadTLS.set(sMainThread);
|
2015-04-14 09:53:07 +03:00
|
|
|
}
|
|
|
|
|
2015-06-10 01:14:09 +03:00
|
|
|
void AbstractThread::DispatchStateChange(
|
|
|
|
already_AddRefed<nsIRunnable> aRunnable) {
|
2018-05-30 22:15:35 +03:00
|
|
|
GetCurrent()->TailDispatcher().AddStateChangeTask(this, std::move(aRunnable));
|
2015-06-10 01:14:09 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* static */
|
|
|
|
void AbstractThread::DispatchDirectTask(
|
2015-06-10 01:14:09 +03:00
|
|
|
already_AddRefed<nsIRunnable> aRunnable) {
|
2018-05-30 22:15:35 +03:00
|
|
|
GetCurrent()->TailDispatcher().AddDirectTask(std::move(aRunnable));
|
2015-06-10 01:14:09 +03:00
|
|
|
}
|
|
|
|
|
2016-04-12 07:12:22 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<AbstractThread> AbstractThread::CreateXPCOMThreadWrapper(
|
|
|
|
nsIThread* aThread, bool aRequireTailDispatch) {
|
2020-04-29 00:18:17 +03:00
|
|
|
nsCOMPtr<nsIThreadInternal> internalThread = do_QueryInterface(aThread);
|
|
|
|
MOZ_ASSERT(internalThread, "Need an nsThread for AbstractThread");
|
|
|
|
RefPtr<XPCOMThreadWrapper> wrapper =
|
|
|
|
new XPCOMThreadWrapper(internalThread, aRequireTailDispatch);
|
2017-03-08 19:45:07 +03:00
|
|
|
|
|
|
|
bool onCurrentThread = false;
|
|
|
|
Unused << aThread->IsOnCurrentThread(&onCurrentThread);
|
|
|
|
|
|
|
|
if (onCurrentThread) {
|
2020-04-21 06:02:39 +03:00
|
|
|
MOZ_ASSERT(
|
|
|
|
!sCurrentThreadTLS.get(),
|
2020-04-29 00:18:17 +03:00
|
|
|
"There can only be a single XPCOMThreadWrapper available on a thread");
|
2017-03-08 19:45:07 +03:00
|
|
|
sCurrentThreadTLS.set(wrapper);
|
|
|
|
return wrapper.forget();
|
|
|
|
}
|
|
|
|
|
2016-04-12 07:12:22 +03:00
|
|
|
// Set the thread-local sCurrentThreadTLS to point to the wrapper on the
|
|
|
|
// target thread. This ensures that sCurrentThreadTLS is as expected by
|
|
|
|
// AbstractThread::GetCurrent() on the target thread.
|
2020-04-21 06:02:39 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"AbstractThread::CreateXPCOMThreadWrapper", [wrapper]() {
|
|
|
|
MOZ_ASSERT(!sCurrentThreadTLS.get(),
|
2020-04-29 00:18:17 +03:00
|
|
|
"There can only be a single XPCOMThreadWrapper available on "
|
2020-04-21 06:02:39 +03:00
|
|
|
"a thread");
|
|
|
|
sCurrentThreadTLS.set(wrapper);
|
|
|
|
});
|
2016-04-12 07:12:22 +03:00
|
|
|
aThread->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
|
2015-08-11 06:50:07 +03:00
|
|
|
return wrapper.forget();
|
|
|
|
}
|
2015-03-14 05:41:12 +03:00
|
|
|
} // namespace mozilla
|