2017-01-19 03:53: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 "SystemGroup.h"
|
|
|
|
|
2017-09-25 12:59:02 +03:00
|
|
|
#include "mozilla/AbstractThread.h"
|
2017-01-19 03:53:36 +03:00
|
|
|
#include "mozilla/Move.h"
|
2017-10-10 17:34:27 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2017-02-02 01:08:46 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2017-01-19 03:53:36 +03:00
|
|
|
#include "nsINamed.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2017-03-09 23:32:32 +03:00
|
|
|
class SystemGroupImpl final : public SchedulerGroup {
|
2017-02-02 01:08:46 +03:00
|
|
|
public:
|
|
|
|
SystemGroupImpl();
|
2017-11-06 06:37:28 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SystemGroupImpl, override)
|
2017-02-02 01:08:46 +03:00
|
|
|
|
|
|
|
static void InitStatic();
|
|
|
|
static void ShutdownStatic();
|
|
|
|
static SystemGroupImpl* Get();
|
|
|
|
|
2017-05-10 22:52:20 +03:00
|
|
|
static bool Initialized() { return !!sSingleton; }
|
|
|
|
|
2017-02-02 01:08:46 +03:00
|
|
|
private:
|
2017-10-09 20:40:12 +03:00
|
|
|
~SystemGroupImpl() = default;
|
|
|
|
static StaticRefPtr<SystemGroupImpl> sSingleton;
|
2017-02-02 01:08:46 +03:00
|
|
|
};
|
|
|
|
|
2017-10-09 20:40:12 +03:00
|
|
|
StaticRefPtr<SystemGroupImpl> SystemGroupImpl::sSingleton;
|
2017-02-02 01:08:46 +03:00
|
|
|
|
|
|
|
SystemGroupImpl::SystemGroupImpl() {
|
|
|
|
CreateEventTargets(/* aNeedValidation = */ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void SystemGroupImpl::InitStatic() {
|
|
|
|
MOZ_ASSERT(!sSingleton);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-10-09 20:40:12 +03:00
|
|
|
sSingleton = new SystemGroupImpl();
|
2017-02-02 01:08:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void SystemGroupImpl::ShutdownStatic() {
|
2017-03-11 04:55:35 +03:00
|
|
|
sSingleton->Shutdown(true);
|
2017-02-02 01:08:46 +03:00
|
|
|
sSingleton = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ SystemGroupImpl* SystemGroupImpl::Get() {
|
|
|
|
MOZ_ASSERT(sSingleton);
|
|
|
|
return sSingleton.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemGroup::InitStatic() { SystemGroupImpl::InitStatic(); }
|
|
|
|
|
|
|
|
void SystemGroup::Shutdown() { SystemGroupImpl::ShutdownStatic(); }
|
|
|
|
|
2017-05-10 22:52:20 +03:00
|
|
|
bool SystemGroup::Initialized() { return SystemGroupImpl::Initialized(); }
|
|
|
|
|
2017-07-26 11:13:35 +03:00
|
|
|
/* static */ nsresult SystemGroup::Dispatch(
|
2017-01-19 03:53:36 +03:00
|
|
|
TaskCategory aCategory, already_AddRefed<nsIRunnable>&& aRunnable) {
|
2017-07-27 21:18:54 +03:00
|
|
|
if (!SystemGroupImpl::Initialized()) {
|
2018-05-30 22:15:35 +03:00
|
|
|
return NS_DispatchToMainThread(std::move(aRunnable));
|
2017-07-27 21:18:54 +03:00
|
|
|
}
|
2018-05-30 22:15:35 +03:00
|
|
|
return SystemGroupImpl::Get()->Dispatch(aCategory, std::move(aRunnable));
|
2017-01-19 03:53:36 +03:00
|
|
|
}
|
|
|
|
|
2017-06-13 23:40:00 +03:00
|
|
|
/* static */ nsISerialEventTarget* SystemGroup::EventTargetFor(
|
2017-01-19 03:53:36 +03:00
|
|
|
TaskCategory aCategory) {
|
2017-07-27 21:18:54 +03:00
|
|
|
if (!SystemGroupImpl::Initialized()) {
|
|
|
|
return GetMainThreadSerialEventTarget();
|
|
|
|
}
|
2017-02-02 01:08:46 +03:00
|
|
|
return SystemGroupImpl::Get()->EventTargetFor(aCategory);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ AbstractThread* SystemGroup::AbstractMainThreadFor(
|
|
|
|
TaskCategory aCategory) {
|
2017-07-27 21:18:54 +03:00
|
|
|
MOZ_ASSERT(SystemGroupImpl::Initialized());
|
2017-02-02 01:08:46 +03:00
|
|
|
return SystemGroupImpl::Get()->AbstractMainThreadFor(aCategory);
|
2017-01-19 03:53:36 +03:00
|
|
|
}
|