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/. */
7 * DecodePool manages the threads used for decoding raster images.
10 #ifndef mozilla_image_DecodePool_h
11 #define mozilla_image_DecodePool_h
13 #include "mozilla/Mutex.h"
14 #include "mozilla/StaticPtr.h"
15 #include "nsCOMArray.h"
17 #include "nsIEventTarget.h"
18 #include "nsIObserver.h"
19 #include "mozilla/RefPtr.h"
20 #include "nsStringFwd.h"
33 * DecodePool is a singleton class that manages decoding of raster images. It
34 * owns a pool of image decoding threads that are used for asynchronous
37 * DecodePool allows callers to run a decoder, handling management of the
38 * decoder's lifecycle and whether it executes on the main thread,
39 * off-main-thread in the image decoding thread pool, or on some combination of
42 class DecodePool final
: public nsIObserver
{
44 NS_DECL_THREADSAFE_ISUPPORTS
47 /// Initializes the singleton instance. Should be called from the main thread.
48 static void Initialize();
50 /// Returns the singleton instance.
51 static DecodePool
* Singleton();
53 /// @return the number of processor cores we have available. This is not the
54 /// same as the number of decoding threads we're actually using.
55 static uint32_t NumberOfCores();
57 /// True if the DecodePool is being shutdown. This may only be called by
58 /// threads from the pool to check if they should keep working or not.
59 bool IsShuttingDown() const;
61 /// Ask the DecodePool to run @aTask asynchronously and return immediately.
62 void AsyncRun(IDecodingTask
* aTask
);
65 * Run @aTask synchronously if the task would prefer it. It's up to the task
66 * itself to make this decision; @see IDecodingTask::ShouldPreferSyncRun(). If
67 * @aTask doesn't prefer it, just run @aTask asynchronously and return
69 * @return true if the task was run sync, false otherwise.
71 bool SyncRunIfPreferred(IDecodingTask
* aTask
, const nsCString
& aURI
);
74 * Run @aTask synchronously. This does not guarantee that @aTask will complete
75 * synchronously. If, for example, @aTask doesn't yet have the data it needs
76 * to run synchronously, it may recover by scheduling an async task to finish
77 * up the work when the remaining data is available.
79 void SyncRunIfPossible(IDecodingTask
* aTask
, const nsCString
& aURI
);
82 * Returns an event target interface to the DecodePool's I/O thread. Callers
83 * who want to deliver data to workers on the DecodePool can use this event
86 * @return An nsISerialEventTarget interface to the thread pool's I/O thread.
88 already_AddRefed
<nsISerialEventTarget
> GetIOEventTarget();
91 friend class DecodePoolWorker
;
94 virtual ~DecodePool();
96 static StaticRefPtr
<DecodePool
> sSingleton
;
97 static uint32_t sNumCores
;
98 bool mShuttingDown
= false;
100 // mMutex protects mIOThread.
102 nsCOMPtr
<nsIThread
> mIOThread
MOZ_GUARDED_BY(mMutex
);
106 } // namespace mozilla
108 #endif // mozilla_image_DecodePool_h