Bug 1337537 - Make a SystemGroup singleton (r=ehsan)

MozReview-Commit-ID: Jnwrr49daeM
This commit is contained in:
Bill McCloskey 2017-02-01 14:08:46 -08:00
Родитель 9b20275aa5
Коммит e1b2f2ef09
3 изменённых файлов: 93 добавлений и 4 удалений

Просмотреть файл

@ -130,6 +130,7 @@ extern nsresult nsStringInputStreamConstructor(nsISupports*, REFNSIID, void**);
#include "mozilla/Services.h"
#include "mozilla/Omnijar.h"
#include "mozilla/HangMonitor.h"
#include "mozilla/SystemGroup.h"
#include "mozilla/Telemetry.h"
#include "mozilla/BackgroundHangMonitor.h"
@ -553,6 +554,9 @@ NS_InitXPCOM2(nsIServiceManager** aResult,
return rv;
}
// Init the SystemGroup for dispatching main thread runnables.
SystemGroup::InitStatic();
// Set up the timer globals/timer thread
rv = nsTimerImpl::Startup();
if (NS_WARN_IF(NS_FAILED(rv))) {
@ -786,6 +790,9 @@ NS_InitMinimalXPCOM()
return rv;
}
// Init the SystemGroup for dispatching main thread runnables.
SystemGroup::InitStatic();
// Set up the timer globals/timer thread.
rv = nsTimerImpl::Startup();
if (NS_WARN_IF(NS_FAILED(rv))) {
@ -1065,6 +1072,9 @@ ShutdownXPCOM(nsIServiceManager* aServMgr)
nsComponentManagerImpl::gComponentManager = nullptr;
nsCategoryManager::Destroy();
// Shut down SystemGroup for main thread dispatching.
SystemGroup::Shutdown();
NS_ShutdownAtomTable();
NS_IF_RELEASE(gDebug);

Просмотреть файл

@ -7,21 +7,91 @@
#include "SystemGroup.h"
#include "mozilla/Move.h"
#include "mozilla/UniquePtr.h"
#include "nsINamed.h"
using namespace mozilla;
class SystemGroupImpl final : public ValidatingDispatcher
{
public:
SystemGroupImpl();
~SystemGroupImpl() {}
static void InitStatic();
static void ShutdownStatic();
static SystemGroupImpl* Get();
NS_METHOD_(MozExternalRefCountType) AddRef(void)
{
return 2;
}
NS_METHOD_(MozExternalRefCountType) Release(void)
{
return 1;
}
private:
static UniquePtr<SystemGroupImpl> sSingleton;
};
UniquePtr<SystemGroupImpl> SystemGroupImpl::sSingleton;
SystemGroupImpl::SystemGroupImpl()
{
CreateEventTargets(/* aNeedValidation = */ true);
}
/* static */ void
SystemGroupImpl::InitStatic()
{
MOZ_ASSERT(!sSingleton);
MOZ_ASSERT(NS_IsMainThread());
sSingleton = MakeUnique<SystemGroupImpl>();
}
/* static */ void
SystemGroupImpl::ShutdownStatic()
{
sSingleton->Shutdown();
sSingleton = nullptr;
}
/* static */ SystemGroupImpl*
SystemGroupImpl::Get()
{
MOZ_ASSERT(sSingleton);
return sSingleton.get();
}
void
SystemGroup::InitStatic()
{
SystemGroupImpl::InitStatic();
}
void
SystemGroup::Shutdown()
{
SystemGroupImpl::ShutdownStatic();
}
/* static */ nsresult
SystemGroup::Dispatch(const char* aName,
TaskCategory aCategory,
already_AddRefed<nsIRunnable>&& aRunnable)
{
return Dispatcher::UnlabeledDispatch(aName, aCategory, Move(aRunnable));
return SystemGroupImpl::Get()->Dispatch(aName, aCategory, Move(aRunnable));
}
/* static */ nsIEventTarget*
SystemGroup::EventTargetFor(TaskCategory aCategory)
{
nsCOMPtr<nsIEventTarget> main = do_GetMainThread();
return main;
return SystemGroupImpl::Get()->EventTargetFor(aCategory);
}
/* static */ AbstractThread*
SystemGroup::AbstractMainThreadFor(TaskCategory aCategory)
{
return SystemGroupImpl::Get()->AbstractMainThreadFor(aCategory);
}

Просмотреть файл

@ -8,6 +8,7 @@
#define mozilla_SystemGroup_h
#include "mozilla/TaskCategory.h"
#include "mozilla/Dispatcher.h"
// The SystemGroup should be used for dispatching runnables that don't need to
// touch web content. Runnables dispatched to the SystemGroup are run in order
@ -16,7 +17,8 @@
namespace mozilla {
class SystemGroup {
class SystemGroup
{
public:
// This method is safe to use from any thread.
static nsresult Dispatch(const char* aName,
@ -25,6 +27,13 @@ class SystemGroup {
// This method is safe to use from any thread.
static nsIEventTarget* EventTargetFor(TaskCategory aCategory);
// Must be called on the main thread. The AbstractThread can always be used
// off the main thread.
static AbstractThread* AbstractMainThreadFor(TaskCategory aCategory);
static void InitStatic();
static void Shutdown();
};
} // namespace mozilla