content/media/MediaTaskQueue.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/MediaTaskQueue.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,94 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef MediaTaskQueue_h_
    1.11 +#define MediaTaskQueue_h_
    1.12 +
    1.13 +#include <queue>
    1.14 +#include "mozilla/RefPtr.h"
    1.15 +#include "mozilla/Monitor.h"
    1.16 +#include "nsThreadUtils.h"
    1.17 +
    1.18 +class nsIRunnable;
    1.19 +
    1.20 +namespace mozilla {
    1.21 +
    1.22 +class SharedThreadPool;
    1.23 +
    1.24 +// Abstracts executing runnables in order in a thread pool. The runnables
    1.25 +// dispatched to the MediaTaskQueue will be executed in the order in which
    1.26 +// they're received, and are guaranteed to not be executed concurrently.
    1.27 +// They may be executed on different threads, and a memory barrier is used
    1.28 +// to make this threadsafe for objects that aren't already threadsafe.
    1.29 +class MediaTaskQueue MOZ_FINAL {
    1.30 +  ~MediaTaskQueue();
    1.31 +
    1.32 +public:
    1.33 +  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaTaskQueue)
    1.34 +
    1.35 +  MediaTaskQueue(TemporaryRef<SharedThreadPool> aPool);
    1.36 +
    1.37 +  nsresult Dispatch(TemporaryRef<nsIRunnable> aRunnable);
    1.38 +
    1.39 +  // Removes all pending tasks from the task queue, and blocks until
    1.40 +  // the currently running task (if any) finishes.
    1.41 +  void Flush();
    1.42 +
    1.43 +  // Blocks until all tasks finish executing, then shuts down the task queue
    1.44 +  // and exits.
    1.45 +  void Shutdown();
    1.46 +
    1.47 +  // Blocks until all task finish executing.
    1.48 +  void AwaitIdle();
    1.49 +
    1.50 +  bool IsEmpty();
    1.51 +
    1.52 +  // Returns true if the current thread is currently running a Runnable in
    1.53 +  // the task queue. This is for debugging/validation purposes only.
    1.54 +  bool IsCurrentThreadIn();
    1.55 +
    1.56 +private:
    1.57 +
    1.58 +  // Blocks until all task finish executing. Called internally by methods
    1.59 +  // that need to wait until the task queue is idle.
    1.60 +  // mQueueMonitor must be held.
    1.61 +  void AwaitIdleLocked();
    1.62 +
    1.63 +  RefPtr<SharedThreadPool> mPool;
    1.64 +
    1.65 +  // Monitor that protects the queue and mIsRunning;
    1.66 +  Monitor mQueueMonitor;
    1.67 +
    1.68 +  // Queue of tasks to run.
    1.69 +  std::queue<RefPtr<nsIRunnable>> mTasks;
    1.70 +
    1.71 +  // The thread currently running the task queue. We store a reference
    1.72 +  // to this so that IsCurrentThreadIn() can tell if the current thread
    1.73 +  // is the thread currently running in the task queue.
    1.74 +  RefPtr<nsIThread> mRunningThread;
    1.75 +
    1.76 +  // True if we've dispatched an event to the pool to execute events from
    1.77 +  // the queue.
    1.78 +  bool mIsRunning;
    1.79 +
    1.80 +  // True if we've started our shutdown process.
    1.81 +  bool mIsShutdown;
    1.82 +
    1.83 +  class Runner : public nsRunnable {
    1.84 +  public:
    1.85 +    Runner(MediaTaskQueue* aQueue)
    1.86 +      : mQueue(aQueue)
    1.87 +    {
    1.88 +    }
    1.89 +    NS_METHOD Run() MOZ_OVERRIDE;
    1.90 +  private:
    1.91 +    RefPtr<MediaTaskQueue> mQueue;
    1.92 +  };
    1.93 +};
    1.94 +
    1.95 +} // namespace mozilla
    1.96 +
    1.97 +#endif // MediaTaskQueue_h_

mercurial