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"
|
2017-08-28 09:11:38 +03:00
|
|
|
#include "mozilla/SystemGroup.h"
|
2016-07-19 22:35:36 +03:00
|
|
|
#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.
|
|
|
|
*/
|
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)
|
|
|
|
{}
|
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
|
2016-07-19 22:35:36 +03:00
|
|
|
{
|
2017-07-25 20:56:13 +03:00
|
|
|
if (mHasRun) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
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();
|
2017-07-25 20:56:13 +03:00
|
|
|
return NS_OK;
|
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;
|
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
|
|
|
|
2017-07-25 20:56:13 +03:00
|
|
|
RefPtr<SyncRunnable> syncRunnable = new SyncRunnable(runnable.forget());
|
2016-10-26 00:43:40 +03:00
|
|
|
|
2017-08-28 09:11:38 +03:00
|
|
|
if (NS_FAILED(SystemGroup::Dispatch(
|
|
|
|
TaskCategory::Other, do_AddRef(syncRunnable)))) {
|
2016-07-19 22:35:36 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-26 00:43:40 +03:00
|
|
|
|
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
|
|
|
|
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;
|
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();
|
2017-07-25 20:56:13 +03:00
|
|
|
NS_RELEASE(runnable);
|
2016-07-19 22:35:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mscom
|
|
|
|
} // namespace mozilla
|