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"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
|
|
|
#include "mozilla/DebugOnly.h"
|
2016-09-06 23:39:37 +03:00
|
|
|
#include "mozilla/HangMonitor.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"
|
|
|
|
#include "private/prpriv.h" // For PR_GetThreadID
|
2016-09-23 00:07:01 +03:00
|
|
|
#include "WinUtils.h"
|
2016-07-19 22:35:36 +03:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
class MOZ_RAII SyncRunnable
|
2016-07-19 22:35:36 +03:00
|
|
|
{
|
|
|
|
public:
|
2016-10-26 00:43:40 +03:00
|
|
|
explicit SyncRunnable(already_AddRefed<nsIRunnable>&& aRunnable)
|
2017-02-10 21:27:01 +03:00
|
|
|
: mRunnable(aRunnable)
|
2016-07-19 22:35:36 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mRunnable);
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:27:01 +03:00
|
|
|
~SyncRunnable() = default;
|
2016-10-26 00:43:40 +03:00
|
|
|
|
|
|
|
void Run()
|
2016-07-19 22:35:36 +03:00
|
|
|
{
|
|
|
|
mRunnable->Run();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-02-10 21:27:01 +03:00
|
|
|
nsCOMPtr<nsIRunnable> mRunnable;
|
|
|
|
mozilla::mscom::SpinEvent mEvent;
|
2016-07-19 22:35:36 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace mscom {
|
|
|
|
|
|
|
|
HANDLE MainThreadInvoker::sMainThread = nullptr;
|
|
|
|
|
|
|
|
/* static */ bool
|
|
|
|
MainThreadInvoker::InitStatics()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-10-26 00:43:40 +03:00
|
|
|
MainThreadInvoker::Invoke(already_AddRefed<nsIRunnable>&& aRunnable)
|
2016-07-19 22:35:36 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> runnable(Move(aRunnable));
|
|
|
|
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
|
|
|
|
|
|
|
SyncRunnable syncRunnable(runnable.forget());
|
|
|
|
|
2016-07-19 22:35:36 +03:00
|
|
|
if (!::QueueUserAPC(&MainThreadAPC, sMainThread,
|
2016-10-26 00:43:40 +03:00
|
|
|
reinterpret_cast<UINT_PTR>(&syncRunnable))) {
|
2016-07-19 22:35:36 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-26 00:43:40 +03:00
|
|
|
|
2016-09-23 00:07:01 +03:00
|
|
|
// We should ensure a call to NtTestAlert() is made on the main thread so
|
|
|
|
// that the main thread will check for APCs during event processing. If we
|
|
|
|
// omit this then the main thread will not check its APC queue until it is
|
|
|
|
// idle.
|
|
|
|
widget::WinUtils::SetAPCPending();
|
2016-10-26 00:43:40 +03:00
|
|
|
|
|
|
|
return syncRunnable.WaitUntilComplete();
|
2016-07-19 22:35:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ VOID CALLBACK
|
|
|
|
MainThreadInvoker::MainThreadAPC(ULONG_PTR aParam)
|
|
|
|
{
|
2017-06-07 05:33:19 +03:00
|
|
|
AutoProfilerThreadWake wake;
|
2016-09-06 23:39:37 +03:00
|
|
|
mozilla::HangMonitor::NotifyActivity(mozilla::HangMonitor::kGeneralActivity);
|
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);
|
2016-07-19 22:35:36 +03:00
|
|
|
runnable->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mscom
|
|
|
|
} // namespace mozilla
|