2016-07-19 22:35:36 +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/. */
|
|
|
|
|
|
|
|
#include "mozilla/mscom/MainThreadInvoker.h"
|
|
|
|
|
2016-09-06 23:39:37 +03:00
|
|
|
#include "GeckoProfiler.h"
|
2016-07-19 22:35:36 +03:00
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
2018-04-30 04:21:20 +03:00
|
|
|
#include "mozilla/BackgroundHangMonitor.h"
|
2016-07-19 22:35:36 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2020-04-07 18:16:33 +03:00
|
|
|
#include "mozilla/SchedulerGroup.h"
|
2017-02-10 21:27:01 +03:00
|
|
|
#include "mozilla/mscom/SpinEvent.h"
|
2016-07-19 22:35:36 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2018-10-10 07:31:35 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2016-07-19 22:35:36 +03:00
|
|
|
#include "private/prpriv.h" // For PR_GetThreadID
|
2018-02-23 03:44:07 +03:00
|
|
|
#include <winternl.h> // For NTSTATUS and NTAPI
|
2016-07-19 22:35:36 +03:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-02-23 03:44:07 +03:00
|
|
|
typedef NTSTATUS(NTAPI* NtTestAlertPtr)(VOID);
|
|
|
|
|
2016-10-26 00:43:40 +03:00
|
|
|
/**
|
|
|
|
* SyncRunnable implements different code paths depending on whether or not
|
|
|
|
* we are running on a multiprocessor system. In the multiprocessor case, we
|
|
|
|
* leave the thread in a spin loop while waiting for the main thread to execute
|
|
|
|
* our runnable. Since spinning is pointless in the uniprocessor case, we block
|
|
|
|
* on an event that is set by the main thread once it has finished the runnable.
|
|
|
|
*/
|
2017-07-25 20:56:13 +03:00
|
|
|
class SyncRunnable : public mozilla::Runnable {
|
2016-07-19 22:35:36 +03:00
|
|
|
public:
|
2017-07-25 20:56:13 +03:00
|
|
|
explicit SyncRunnable(already_AddRefed<nsIRunnable> aRunnable)
|
|
|
|
: mozilla::Runnable("MainThreadInvoker"), mRunnable(aRunnable) {
|
2018-02-23 03:44:07 +03:00
|
|
|
static const bool gotStatics = InitStatics();
|
|
|
|
MOZ_ASSERT(gotStatics);
|
2018-10-10 07:31:35 +03:00
|
|
|
Unused << gotStatics;
|
2018-02-23 03:44:07 +03:00
|
|
|
}
|
2016-07-19 22:35:36 +03:00
|
|
|
|
2017-02-10 21:27:01 +03:00
|
|
|
~SyncRunnable() = default;
|
2016-10-26 00:43:40 +03:00
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2017-07-25 20:56:13 +03:00
|
|
|
if (mHasRun) {
|
2018-02-23 03:44:07 +03:00
|
|
|
// The APC already ran, so we have nothing to do.
|
2017-07-25 20:56:13 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-02-23 03:44:07 +03:00
|
|
|
|
|
|
|
// Run the pending APC in the queue.
|
|
|
|
MOZ_ASSERT(sNtTestAlert);
|
|
|
|
sNtTestAlert();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is called by MainThreadInvoker::MainThreadAPC.
|
|
|
|
void APCRun() {
|
2017-07-25 20:56:13 +03:00
|
|
|
mHasRun = true;
|
|
|
|
|
2017-07-26 00:57:18 +03:00
|
|
|
TimeStamp runStart(TimeStamp::Now());
|
2016-07-19 22:35:36 +03:00
|
|
|
mRunnable->Run();
|
2017-07-26 00:57:18 +03:00
|
|
|
TimeStamp runEnd(TimeStamp::Now());
|
|
|
|
|
|
|
|
mDuration = runEnd - runStart;
|
2016-10-26 00:43:40 +03:00
|
|
|
|
2017-02-10 21:27:01 +03:00
|
|
|
mEvent.Signal();
|
2016-10-26 00:43:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WaitUntilComplete() {
|
2017-02-10 21:27:01 +03:00
|
|
|
return mEvent.Wait(mozilla::mscom::MainThreadInvoker::GetTargetThread());
|
2016-07-19 22:35:36 +03:00
|
|
|
}
|
|
|
|
|
2017-07-26 00:57:18 +03:00
|
|
|
const mozilla::TimeDuration& GetDuration() const { return mDuration; }
|
|
|
|
|
2016-07-19 22:35:36 +03:00
|
|
|
private:
|
2017-07-25 20:56:13 +03:00
|
|
|
bool mHasRun = false;
|
2017-02-10 21:27:01 +03:00
|
|
|
nsCOMPtr<nsIRunnable> mRunnable;
|
|
|
|
mozilla::mscom::SpinEvent mEvent;
|
2017-07-26 00:57:18 +03:00
|
|
|
mozilla::TimeDuration mDuration;
|
2018-02-23 03:44:07 +03:00
|
|
|
|
|
|
|
static NtTestAlertPtr sNtTestAlert;
|
|
|
|
|
|
|
|
static bool InitStatics() {
|
|
|
|
sNtTestAlert = reinterpret_cast<NtTestAlertPtr>(
|
|
|
|
::GetProcAddress(::GetModuleHandleW(L"ntdll.dll"), "NtTestAlert"));
|
|
|
|
MOZ_ASSERT(sNtTestAlert);
|
|
|
|
return sNtTestAlert;
|
|
|
|
}
|
2016-07-19 22:35:36 +03:00
|
|
|
};
|
|
|
|
|
2018-02-23 03:44:07 +03:00
|
|
|
NtTestAlertPtr SyncRunnable::sNtTestAlert = nullptr;
|
|
|
|
|
2016-07-19 22:35:36 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace mscom {
|
|
|
|
|
|
|
|
HANDLE MainThreadInvoker::sMainThread = nullptr;
|
|
|
|
|
2019-02-26 01:08:36 +03:00
|
|
|
/* static */
|
|
|
|
bool MainThreadInvoker::InitStatics() {
|
2016-07-19 22:35:36 +03:00
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
nsresult rv = ::NS_GetMainThread(getter_AddRefs(mainThread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-26 00:43:40 +03:00
|
|
|
|
2016-07-19 22:35:36 +03:00
|
|
|
PRThread* mainPrThread = nullptr;
|
|
|
|
rv = mainThread->GetPRThread(&mainPrThread);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-26 00:43:40 +03:00
|
|
|
|
2016-07-19 22:35:36 +03:00
|
|
|
PRUint32 tid = ::PR_GetThreadID(mainPrThread);
|
|
|
|
sMainThread = ::OpenThread(SYNCHRONIZE | THREAD_SET_CONTEXT, FALSE, tid);
|
2016-10-26 00:43:40 +03:00
|
|
|
|
2016-09-23 00:07:01 +03:00
|
|
|
return !!sMainThread;
|
2016-07-19 22:35:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MainThreadInvoker::MainThreadInvoker() {
|
|
|
|
static const bool gotStatics = InitStatics();
|
|
|
|
MOZ_ASSERT(gotStatics);
|
2018-10-10 07:31:35 +03:00
|
|
|
Unused << gotStatics;
|
2016-07-19 22:35:36 +03:00
|
|
|
}
|
|
|
|
|
2016-10-26 00:43:40 +03:00
|
|
|
bool MainThreadInvoker::Invoke(already_AddRefed<nsIRunnable>&& aRunnable) {
|
2018-05-30 22:15:35 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(std::move(aRunnable));
|
2016-07-19 22:35:36 +03:00
|
|
|
if (!runnable) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-26 00:43:40 +03:00
|
|
|
|
2016-07-19 22:35:36 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
runnable->Run();
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-26 00:43:40 +03:00
|
|
|
|
2017-07-25 20:56:13 +03:00
|
|
|
RefPtr<SyncRunnable> syncRunnable = new SyncRunnable(runnable.forget());
|
2016-10-26 00:43:40 +03:00
|
|
|
|
2018-02-23 03:44:07 +03:00
|
|
|
// The main thread could be either blocked on a condition variable waiting
|
|
|
|
// for a Gecko event, or it could be blocked waiting on a Windows HANDLE in
|
|
|
|
// IPC code (doing a sync message send). In the former case, we wake it by
|
|
|
|
// posting a Gecko runnable to the main thread. In the latter case, we wake
|
|
|
|
// it using an APC. However, the latter case doesn't happen very often now
|
|
|
|
// and APCs aren't otherwise run by the main thread. To ensure the
|
|
|
|
// SyncRunnable is cleaned up, we need both to run consistently.
|
|
|
|
// To do this, we:
|
|
|
|
// 1. Queue an APC which does the actual work.
|
2017-07-25 20:56:13 +03:00
|
|
|
// This ref gets released in MainThreadAPC when it runs.
|
|
|
|
SyncRunnable* syncRunnableRef = syncRunnable.get();
|
|
|
|
NS_ADDREF(syncRunnableRef);
|
|
|
|
if (!::QueueUserAPC(&MainThreadAPC, sMainThread,
|
|
|
|
reinterpret_cast<UINT_PTR>(syncRunnableRef))) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-26 00:43:40 +03:00
|
|
|
|
2018-02-23 03:44:07 +03:00
|
|
|
// 2. Post a Gecko runnable (which always runs). If the APC hasn't run, the
|
|
|
|
// Gecko runnable runs it. Otherwise, it does nothing.
|
2020-04-07 18:16:33 +03:00
|
|
|
if (NS_FAILED(SchedulerGroup::Dispatch(TaskCategory::Other,
|
|
|
|
do_AddRef(syncRunnable)))) {
|
2018-02-23 03:44:07 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-26 00:57:18 +03:00
|
|
|
bool result = syncRunnable->WaitUntilComplete();
|
|
|
|
mDuration = syncRunnable->GetDuration();
|
|
|
|
return result;
|
2016-07-19 22:35:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ VOID CALLBACK MainThreadInvoker::MainThreadAPC(ULONG_PTR aParam) {
|
2017-10-04 01:11:18 +03:00
|
|
|
AUTO_PROFILER_THREAD_WAKE;
|
2018-04-30 04:21:20 +03:00
|
|
|
mozilla::BackgroundHangMonitor().NotifyActivity();
|
2016-07-19 22:35:36 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-10-26 00:43:40 +03:00
|
|
|
auto runnable = reinterpret_cast<SyncRunnable*>(aParam);
|
2018-02-23 03:44:07 +03:00
|
|
|
runnable->APCRun();
|
2017-07-25 20:56:13 +03:00
|
|
|
NS_RELEASE(runnable);
|
2016-07-19 22:35:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mscom
|
|
|
|
} // namespace mozilla
|