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.
|
2015-04-02 23:21:53 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2015-08-18 20:31:16 +03:00
|
|
|
#include "mozilla/StateWatching.h" // We initialize the StateWatching logging in this file.
|
2015-07-16 21:52:43 +03:00
|
|
|
#include "mozilla/TaskQueue.h"
|
|
|
|
#include "mozilla/TaskDispatcher.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-04-02 23:21:53 +03:00
|
|
|
|
2015-07-16 21:52:43 +03:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-11-29 08:01:18 +03:00
|
|
|
class EventTargetWrapper : public AbstractThread
|
2015-03-14 05:41:12 +03:00
|
|
|
{
|
2015-04-14 08:08:47 +03:00
|
|
|
public:
|
2016-11-29 08:01:18 +03:00
|
|
|
explicit EventTargetWrapper(nsIEventTarget* aTarget, bool aRequireTailDispatch)
|
2015-04-14 21:58:30 +03:00
|
|
|
: AbstractThread(aRequireTailDispatch)
|
2015-04-14 09:53:07 +03:00
|
|
|
, mTarget(aTarget)
|
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, NS_IsMainThread() && aTarget->IsOnCurrentThread());
|
2015-04-14 21:58:30 +03:00
|
|
|
}
|
2015-03-14 05:41:12 +03:00
|
|
|
|
2015-04-15 03:25:14 +03:00
|
|
|
virtual void Dispatch(already_AddRefed<nsIRunnable> aRunnable,
|
2015-04-14 20:58:49 +03:00
|
|
|
DispatchFailureHandling aFailureHandling = AssertDispatchSuccess,
|
|
|
|
DispatchReason aReason = NormalDispatch) override
|
2015-04-14 08:08:47 +03:00
|
|
|
{
|
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)) {
|
2016-11-29 08:01:18 +03:00
|
|
|
currentThread->TailDispatcher().AddTask(this, Move(aRunnable), aFailureHandling);
|
2015-04-15 20:54:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-29 08:01:18 +03:00
|
|
|
RefPtr<nsIRunnable> runner(new Runner(this, Move(aRunnable), false /* already drained by TaskGroupRunnable */));
|
|
|
|
nsresult rv = mTarget->Dispatch(runner.forget(), NS_DISPATCH_NORMAL);
|
2015-04-15 03:25:14 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aFailureHandling == DontAssertDispatchSuccess || NS_SUCCEEDED(rv));
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << rv;
|
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;
|
|
|
|
|
2015-04-14 08:08:47 +03:00
|
|
|
virtual bool IsCurrentThreadIn() override
|
|
|
|
{
|
2017-08-03 02:56:30 +03:00
|
|
|
return mTarget->IsOnCurrentThread();
|
2015-04-14 08:08:47 +03:00
|
|
|
}
|
|
|
|
|
2015-04-16 22:24:54 +03:00
|
|
|
void FireTailDispatcher()
|
|
|
|
{
|
2017-08-01 00:29:07 +03:00
|
|
|
AutoEnter context(this);
|
|
|
|
|
2015-04-16 22:24:54 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mTailDispatcher.isSome());
|
|
|
|
mTailDispatcher.ref().DrainDirectTasks();
|
|
|
|
mTailDispatcher.reset();
|
|
|
|
}
|
2015-04-14 21:58:30 +03:00
|
|
|
|
|
|
|
virtual TaskDispatcher& TailDispatcher() override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsCurrentThreadIn());
|
|
|
|
if (!mTailDispatcher.isSome()) {
|
|
|
|
mTailDispatcher.emplace(/* aIsTailDispatcher = */ true);
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NewRunnableMethod("EventTargetWrapper::FireTailDispatcher",
|
|
|
|
this,
|
|
|
|
&EventTargetWrapper::FireTailDispatcher);
|
2015-06-11 05:36:12 +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
|
|
|
|
2016-09-21 12:24:43 +03:00
|
|
|
virtual bool MightHaveTailTasks() override
|
|
|
|
{
|
|
|
|
return mTailDispatcher.isSome();
|
|
|
|
}
|
|
|
|
|
2016-11-29 08:01:18 +03:00
|
|
|
virtual nsIEventTarget* AsEventTarget() override { return mTarget; }
|
2015-04-20 15:29:35 +03:00
|
|
|
|
2015-04-14 08:08:47 +03:00
|
|
|
private:
|
2016-11-29 08:01:18 +03:00
|
|
|
nsCOMPtr<nsIThread> mRunningThread;
|
|
|
|
RefPtr<nsIEventTarget> mTarget;
|
2015-04-14 21:58:30 +03:00
|
|
|
Maybe<AutoTaskDispatcher> mTailDispatcher;
|
2016-11-29 08:01:18 +03:00
|
|
|
|
|
|
|
virtual already_AddRefed<nsIRunnable>
|
|
|
|
CreateDirectTaskDrainer(already_AddRefed<nsIRunnable> aRunnable) override
|
|
|
|
{
|
|
|
|
RefPtr<Runner> runner =
|
|
|
|
new Runner(this, Move(aRunnable), /* aDrainDirectTasks */ true);
|
|
|
|
return runner.forget();
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:45:07 +03:00
|
|
|
class Runner : public CancelableRunnable {
|
|
|
|
class MOZ_STACK_CLASS AutoTaskGuard final {
|
|
|
|
public:
|
|
|
|
explicit AutoTaskGuard(EventTargetWrapper* aThread)
|
|
|
|
: mLastCurrentThread(nullptr)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aThread);
|
|
|
|
mLastCurrentThread = sCurrentThreadTLS.get();
|
|
|
|
sCurrentThreadTLS.set(aThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoTaskGuard()
|
|
|
|
{
|
|
|
|
sCurrentThreadTLS.set(mLastCurrentThread);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
AbstractThread* mLastCurrentThread;
|
|
|
|
};
|
|
|
|
|
2016-11-29 08:01:18 +03:00
|
|
|
public:
|
|
|
|
explicit Runner(EventTargetWrapper* aThread,
|
|
|
|
already_AddRefed<nsIRunnable> aRunnable,
|
|
|
|
bool aDrainDirectTasks)
|
2017-06-12 22:34:10 +03:00
|
|
|
: CancelableRunnable("EventTargetWrapper::Runner")
|
|
|
|
, mThread(aThread)
|
2016-11-29 08:01:18 +03:00
|
|
|
, mRunnable(aRunnable)
|
|
|
|
, mDrainDirectTasks(aDrainDirectTasks)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override
|
|
|
|
{
|
2017-03-08 19:45:07 +03:00
|
|
|
AutoTaskGuard taskGuard(mThread);
|
2016-11-29 08:01:18 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(mThread == AbstractThread::GetCurrent());
|
|
|
|
MOZ_ASSERT(mThread->IsCurrentThreadIn());
|
|
|
|
nsresult rv = mRunnable->Run();
|
|
|
|
|
|
|
|
if (mDrainDirectTasks) {
|
|
|
|
mThread->TailDispatcher().DrainDirectTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:45:07 +03:00
|
|
|
nsresult Cancel() override
|
|
|
|
{
|
|
|
|
// Set the TLS during Cancel() just in case it calls Run().
|
|
|
|
AutoTaskGuard taskGuard(mThread);
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// Try to cancel the runnable if it implements the right interface.
|
|
|
|
// Otherwise just skip the runnable.
|
|
|
|
nsCOMPtr<nsICancelableRunnable> cr = do_QueryInterface(mRunnable);
|
|
|
|
if (cr) {
|
|
|
|
rv = cr->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<EventTargetWrapper> mThread;
|
|
|
|
RefPtr<nsIRunnable> mRunnable;
|
|
|
|
bool mDrainDirectTasks;
|
|
|
|
};
|
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)
|
|
|
|
{
|
|
|
|
Dispatch(Move(aEvent), DontAssertDispatchSuccess, NormalDispatch);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AbstractThread::DelayedDispatch(already_AddRefed<nsIRunnable> aEvent,
|
|
|
|
uint32_t aDelayMs)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:24:43 +03:00
|
|
|
void
|
|
|
|
AbstractThread::TailDispatchTasksFor(AbstractThread* aThread)
|
|
|
|
{
|
|
|
|
if (MightHaveTailTasks()) {
|
|
|
|
TailDispatcher().DispatchTasksFor(aThread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-04-19 08:24:09 +03:00
|
|
|
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);
|
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
NS_GetMainThread(getter_AddRefs(mainThread));
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mainThread);
|
2016-11-29 08:01:18 +03:00
|
|
|
sMainThread = new EventTargetWrapper(mainThread.get(), /* 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 01:14:09 +03:00
|
|
|
void
|
|
|
|
AbstractThread::DispatchStateChange(already_AddRefed<nsIRunnable> aRunnable)
|
|
|
|
{
|
|
|
|
GetCurrent()->TailDispatcher().AddStateChangeTask(this, Move(aRunnable));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
AbstractThread::DispatchDirectTask(already_AddRefed<nsIRunnable> aRunnable)
|
|
|
|
{
|
|
|
|
GetCurrent()->TailDispatcher().AddDirectTask(Move(aRunnable));
|
|
|
|
}
|
|
|
|
|
2016-04-12 07:12:22 +03:00
|
|
|
/* static */
|
2015-08-11 06:50:07 +03:00
|
|
|
already_AddRefed<AbstractThread>
|
2016-04-12 07:12:22 +03:00
|
|
|
AbstractThread::CreateXPCOMThreadWrapper(nsIThread* aThread, bool aRequireTailDispatch)
|
2015-08-11 06:50:07 +03:00
|
|
|
{
|
2016-11-29 08:01:18 +03:00
|
|
|
RefPtr<EventTargetWrapper> wrapper = new EventTargetWrapper(aThread, aRequireTailDispatch);
|
2017-03-08 19:45:07 +03:00
|
|
|
|
|
|
|
bool onCurrentThread = false;
|
|
|
|
Unused << aThread->IsOnCurrentThread(&onCurrentThread);
|
|
|
|
|
|
|
|
if (onCurrentThread) {
|
|
|
|
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.
|
|
|
|
nsCOMPtr<nsIRunnable> r =
|
2017-06-12 22:34:10 +03:00
|
|
|
NS_NewRunnableFunction("AbstractThread::CreateXPCOMThreadWrapper",
|
|
|
|
[wrapper]() { 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();
|
|
|
|
}
|
|
|
|
|
2016-11-29 08:01:18 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<AbstractThread>
|
|
|
|
AbstractThread::CreateEventTargetWrapper(nsIEventTarget* aEventTarget,
|
|
|
|
bool aRequireTailDispatch)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aEventTarget);
|
|
|
|
nsCOMPtr<nsIThread> thread(do_QueryInterface(aEventTarget));
|
|
|
|
Unused << thread; // simpler than DebugOnly<nsCOMPtr<nsIThread>>
|
|
|
|
MOZ_ASSERT(!thread, "nsIThread should be wrapped by CreateXPCOMThreadWrapper!");
|
|
|
|
|
|
|
|
RefPtr<EventTargetWrapper> wrapper =
|
|
|
|
new EventTargetWrapper(aEventTarget, aRequireTailDispatch);
|
|
|
|
|
|
|
|
return wrapper.forget();
|
|
|
|
}
|
|
|
|
|
2015-03-14 05:41:12 +03:00
|
|
|
} // namespace mozilla
|