author Mark Banner <>
Mon, 11 Dec 2017 13:32:51 +0000
changeset 711859 dae226e9f64cbe94c086a0f4853ebe18792fbf4b
parent 600043 aa1693a26a15eb5cba24102222687dc81eeddd7b
child 648192 c236dfe7dc98c387219d7cb45d0693560db708fd
child 694109 98e2b4ee9775a2a2b5cc1bb8a390bfe901814f38
child 710497 e6fe7420e29e6118f6668d94045b50d2233c5a09
child 754515 c32ead4e35257a9067fa2fa73a96fafca4acdacd
permissions -rw-r--r--
Bug 1425244 - Enable ESLint rule comma-spacing for accessible/tests/mochitest/. r?surkov MozReview-Commit-ID: BIgkIWmCcaM

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 */

 * DecodePool manages the threads used for decoding raster images.

#ifndef mozilla_image_DecodePool_h
#define mozilla_image_DecodePool_h

#include "mozilla/Mutex.h"
#include "mozilla/StaticPtr.h"
#include "nsCOMArray.h"
#include "nsCOMPtr.h"
#include "nsIEventTarget.h"
#include "nsIObserver.h"
#include "mozilla/RefPtr.h"
#include "nsStringFwd.h"

class nsIThread;
class nsIThreadPool;

namespace mozilla {
namespace image {

class Decoder;
class DecodePoolImpl;
class IDecodingTask;

 * DecodePool is a singleton class that manages decoding of raster images. It
 * owns a pool of image decoding threads that are used for asynchronous
 * decoding.
 * DecodePool allows callers to run a decoder, handling management of the
 * decoder's lifecycle and whether it executes on the main thread,
 * off-main-thread in the image decoding thread pool, or on some combination of
 * the two.
class DecodePool : public nsIObserver

  /// Initializes the singleton instance. Should be called from the main thread.
  static void Initialize();

  /// Returns the singleton instance.
  static DecodePool* Singleton();

  /// @return the number of processor cores we have available. This is not the
  /// same as the number of decoding threads we're actually using.
  static uint32_t NumberOfCores();

  /// Ask the DecodePool to run @aTask asynchronously and return immediately.
  void AsyncRun(IDecodingTask* aTask);

   * Run @aTask synchronously if the task would prefer it. It's up to the task
   * itself to make this decision; @see IDecodingTask::ShouldPreferSyncRun(). If
   * @aTask doesn't prefer it, just run @aTask asynchronously and return
   * immediately.
   * @return true if the task was run sync, false otherwise.
  bool SyncRunIfPreferred(IDecodingTask* aTask, const nsCString& aURI);

   * Run @aTask synchronously. This does not guarantee that @aTask will complete
   * synchronously. If, for example, @aTask doesn't yet have the data it needs to
   * run synchronously, it may recover by scheduling an async task to finish up
   * the work when the remaining data is available.
  void SyncRunIfPossible(IDecodingTask* aTask, const nsCString& aURI);

   * Returns an event target interface to the DecodePool's I/O thread. Callers
   * who want to deliver data to workers on the DecodePool can use this event
   * target.
   * @return An nsIEventTarget interface to the thread pool's I/O thread.
  already_AddRefed<nsIEventTarget> GetIOEventTarget();

  friend class DecodePoolWorker;

  virtual ~DecodePool();

  static StaticRefPtr<DecodePool> sSingleton;
  static uint32_t sNumCores;

  RefPtr<DecodePoolImpl> mImpl;

  // mMutex protects mThreads and mIOThread.
  Mutex                         mMutex;
  nsTArray<nsCOMPtr<nsIThread>> mThreads;
  nsCOMPtr<nsIThread>           mIOThread;

} // namespace image
} // namespace mozilla

#endif // mozilla_image_DecodePool_h