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