1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "DecodePool.h"
10 #include "mozilla/ClearOnShutdown.h"
11 #include "mozilla/DebugOnly.h"
12 #include "mozilla/Monitor.h"
13 #include "mozilla/ProfilerLabels.h"
14 #include "mozilla/SchedulerGroup.h"
15 #include "mozilla/Services.h"
16 #include "mozilla/StaticPrefs_image.h"
17 #include "mozilla/TaskController.h"
18 #include "mozilla/TimeStamp.h"
19 #include "mozilla/AppShutdown.h"
21 #include "nsIObserverService.h"
22 #include "nsThreadManager.h"
23 #include "nsThreadUtils.h"
24 #include "nsXPCOMCIDInternal.h"
28 #include "IDecodingTask.h"
29 #include "RasterImage.h"
33 # include "mozilla/WindowsProcessMitigations.h"
42 ///////////////////////////////////////////////////////////////////////////////
43 // DecodePool implementation.
44 ///////////////////////////////////////////////////////////////////////////////
47 StaticRefPtr
<DecodePool
> DecodePool::sSingleton
;
49 uint32_t DecodePool::sNumCores
= 0;
51 NS_IMPL_ISUPPORTS(DecodePool
, nsIObserver
)
54 void DecodePool::Initialize() {
55 MOZ_ASSERT(NS_IsMainThread());
56 sNumCores
= max
<int32_t>(PR_GetNumberOfProcessors(), 1);
57 DecodePool::Singleton();
61 DecodePool
* DecodePool::Singleton() {
63 MOZ_ASSERT(NS_IsMainThread());
64 sSingleton
= new DecodePool();
65 ClearOnShutdown(&sSingleton
);
72 uint32_t DecodePool::NumberOfCores() { return sNumCores
; }
75 class IOThreadIniter final
: public Runnable
{
77 explicit IOThreadIniter() : Runnable("image::IOThreadIniter") {}
79 NS_IMETHOD
Run() override
{
80 MOZ_ASSERT(!NS_IsMainThread());
82 CoInitialize(nullptr);
89 DecodePool::DecodePool() : mMutex("image::IOThread") {
90 // Initialize the I/O thread.
92 // On Windows we use the io thread to get icons from the system. Any thread
93 // that makes system calls needs to call CoInitialize. And these system calls
94 // (SHGetFileInfo) should only be called from one thread at a time, in case
95 // we ever create more than one io thread. If win32k is locked down, we can't
96 // call SHGetFileInfo anyway, so we don't need the initializer.
97 nsCOMPtr
<nsIRunnable
> initer
=
98 IsWin32kLockedDown() ? nullptr : new IOThreadIniter();
99 nsresult rv
= NS_NewNamedThread("ImageIO", getter_AddRefs(mIOThread
), initer
);
101 nsresult rv
= NS_NewNamedThread("ImageIO", getter_AddRefs(mIOThread
));
103 MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv
) && mIOThread
,
104 "Should successfully create image I/O thread");
106 nsCOMPtr
<nsIObserverService
> obsSvc
= services::GetObserverService();
108 obsSvc
->AddObserver(this, "xpcom-shutdown-threads", false);
112 DecodePool::~DecodePool() {
113 MOZ_ASSERT(NS_IsMainThread(), "Must shut down DecodePool on main thread!");
117 DecodePool::Observe(nsISupports
*, const char* aTopic
, const char16_t
*) {
118 MOZ_ASSERT(strcmp(aTopic
, "xpcom-shutdown-threads") == 0, "Unexpected topic");
120 mShuttingDown
= true;
122 nsCOMPtr
<nsIThread
> ioThread
;
125 MutexAutoLock
lock(mMutex
);
126 ioThread
.swap(mIOThread
);
130 ioThread
->Shutdown();
136 /* static */ bool DecodePool::IsShuttingDown() {
137 if (MOZ_UNLIKELY(!sSingleton
)) {
138 return AppShutdown::IsInOrBeyond(ShutdownPhase::XPCOMShutdownThreads
);
141 return sSingleton
->mShuttingDown
;
144 class DecodingTask final
: public Task
{
146 explicit DecodingTask(RefPtr
<IDecodingTask
>&& aTask
)
147 : Task(Kind::OffMainThreadOnly
, aTask
->Priority() == TaskPriority::eLow
148 ? EventQueuePriority::Normal
149 : EventQueuePriority::RenderBlocking
),
152 TaskResult
Run() override
{
154 return TaskResult::Complete
;
157 #ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
158 bool GetName(nsACString
& aName
) override
{
159 aName
.AssignLiteral("ImageDecodingTask");
165 RefPtr
<IDecodingTask
> mTask
;
168 void DecodePool::AsyncRun(IDecodingTask
* aTask
) {
171 TaskController::Get()->AddTask(
172 MakeAndAddRef
<DecodingTask
>((RefPtr
<IDecodingTask
>(aTask
))));
175 bool DecodePool::SyncRunIfPreferred(IDecodingTask
* aTask
,
176 const nsCString
& aURI
) {
177 MOZ_ASSERT(NS_IsMainThread());
180 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("DecodePool::SyncRunIfPreferred",
183 if (aTask
->ShouldPreferSyncRun()) {
192 void DecodePool::SyncRunIfPossible(IDecodingTask
* aTask
,
193 const nsCString
& aURI
) {
194 MOZ_ASSERT(NS_IsMainThread());
197 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("DecodePool::SyncRunIfPossible",
203 already_AddRefed
<nsISerialEventTarget
> DecodePool::GetIOEventTarget() {
204 MutexAutoLock
threadPoolLock(mMutex
);
205 nsCOMPtr
<nsISerialEventTarget
> target
= mIOThread
;
206 return target
.forget();
210 } // namespace mozilla