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/SchedulerGroup.h"
14 #include "mozilla/Services.h"
15 #include "mozilla/StaticPrefs_image.h"
16 #include "mozilla/TaskController.h"
17 #include "mozilla/TimeStamp.h"
19 #include "nsIObserverService.h"
20 #include "nsThreadManager.h"
21 #include "nsThreadUtils.h"
22 #include "nsXPCOMCIDInternal.h"
26 #include "GeckoProfiler.h"
27 #include "IDecodingTask.h"
28 #include "RasterImage.h"
40 ///////////////////////////////////////////////////////////////////////////////
41 // DecodePool implementation.
42 ///////////////////////////////////////////////////////////////////////////////
45 StaticRefPtr
<DecodePool
> DecodePool::sSingleton
;
47 uint32_t DecodePool::sNumCores
= 0;
49 NS_IMPL_ISUPPORTS(DecodePool
, nsIObserver
)
52 void DecodePool::Initialize() {
53 MOZ_ASSERT(NS_IsMainThread());
54 sNumCores
= max
<int32_t>(PR_GetNumberOfProcessors(), 1);
55 DecodePool::Singleton();
59 DecodePool
* DecodePool::Singleton() {
61 MOZ_ASSERT(NS_IsMainThread());
62 sSingleton
= new DecodePool();
63 ClearOnShutdown(&sSingleton
);
70 uint32_t DecodePool::NumberOfCores() { return sNumCores
; }
73 class IOThreadIniter final
: public Runnable
{
75 explicit IOThreadIniter() : Runnable("image::IOThreadIniter") {}
77 NS_IMETHOD
Run() override
{
78 MOZ_ASSERT(!NS_IsMainThread());
80 CoInitialize(nullptr);
87 DecodePool::DecodePool() : mMutex("image::IOThread") {
88 // Initialize the I/O thread.
90 // On Windows we use the io thread to get icons from the system. Any thread
91 // that makes system calls needs to call CoInitialize. And these system calls
92 // (SHGetFileInfo) should only be called from one thread at a time, in case
93 // we ever create more than on io thread.
94 nsCOMPtr
<nsIRunnable
> initer
= new IOThreadIniter();
95 nsresult rv
= NS_NewNamedThread("ImageIO", getter_AddRefs(mIOThread
), initer
);
97 nsresult rv
= NS_NewNamedThread("ImageIO", getter_AddRefs(mIOThread
));
99 MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv
) && mIOThread
,
100 "Should successfully create image I/O thread");
102 nsCOMPtr
<nsIObserverService
> obsSvc
= services::GetObserverService();
104 obsSvc
->AddObserver(this, "xpcom-shutdown-threads", false);
108 DecodePool::~DecodePool() {
109 MOZ_ASSERT(NS_IsMainThread(), "Must shut down DecodePool on main thread!");
113 DecodePool::Observe(nsISupports
*, const char* aTopic
, const char16_t
*) {
114 MOZ_ASSERT(strcmp(aTopic
, "xpcom-shutdown-threads") == 0, "Unexpected topic");
116 mShuttingDown
= true;
118 nsCOMPtr
<nsIThread
> ioThread
;
121 MutexAutoLock
lock(mMutex
);
122 ioThread
.swap(mIOThread
);
126 ioThread
->Shutdown();
132 bool DecodePool::IsShuttingDown() const { return mShuttingDown
; }
134 class DecodingTask final
: public Task
{
136 explicit DecodingTask(RefPtr
<IDecodingTask
>&& aTask
)
137 : Task(false, aTask
->Priority() == TaskPriority::eLow
138 ? EventQueuePriority::Normal
139 : EventQueuePriority::MediumHigh
),
142 bool Run() override
{
148 RefPtr
<IDecodingTask
> mTask
;
151 void DecodePool::AsyncRun(IDecodingTask
* aTask
) {
154 TaskController::Get()->AddTask(
155 MakeAndAddRef
<DecodingTask
>((RefPtr
<IDecodingTask
>(aTask
))));
158 bool DecodePool::SyncRunIfPreferred(IDecodingTask
* aTask
,
159 const nsCString
& aURI
) {
160 MOZ_ASSERT(NS_IsMainThread());
163 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("DecodePool::SyncRunIfPreferred",
166 if (aTask
->ShouldPreferSyncRun()) {
175 void DecodePool::SyncRunIfPossible(IDecodingTask
* aTask
,
176 const nsCString
& aURI
) {
177 MOZ_ASSERT(NS_IsMainThread());
180 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("DecodePool::SyncRunIfPossible",
186 already_AddRefed
<nsIEventTarget
> DecodePool::GetIOEventTarget() {
187 MutexAutoLock
threadPoolLock(mMutex
);
188 nsCOMPtr
<nsIEventTarget
> target
= mIOThread
;
189 return target
.forget();
193 } // namespace mozilla