michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MediaTaskQueue_h_ michael@0: #define MediaTaskQueue_h_ michael@0: michael@0: #include michael@0: #include "mozilla/RefPtr.h" michael@0: #include "mozilla/Monitor.h" michael@0: #include "nsThreadUtils.h" michael@0: michael@0: class nsIRunnable; michael@0: michael@0: namespace mozilla { michael@0: michael@0: class SharedThreadPool; michael@0: michael@0: // Abstracts executing runnables in order in a thread pool. The runnables michael@0: // dispatched to the MediaTaskQueue will be executed in the order in which michael@0: // they're received, and are guaranteed to not be executed concurrently. michael@0: // They may be executed on different threads, and a memory barrier is used michael@0: // to make this threadsafe for objects that aren't already threadsafe. michael@0: class MediaTaskQueue MOZ_FINAL { michael@0: ~MediaTaskQueue(); michael@0: michael@0: public: michael@0: NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaTaskQueue) michael@0: michael@0: MediaTaskQueue(TemporaryRef aPool); michael@0: michael@0: nsresult Dispatch(TemporaryRef aRunnable); michael@0: michael@0: // Removes all pending tasks from the task queue, and blocks until michael@0: // the currently running task (if any) finishes. michael@0: void Flush(); michael@0: michael@0: // Blocks until all tasks finish executing, then shuts down the task queue michael@0: // and exits. michael@0: void Shutdown(); michael@0: michael@0: // Blocks until all task finish executing. michael@0: void AwaitIdle(); michael@0: michael@0: bool IsEmpty(); michael@0: michael@0: // Returns true if the current thread is currently running a Runnable in michael@0: // the task queue. This is for debugging/validation purposes only. michael@0: bool IsCurrentThreadIn(); michael@0: michael@0: private: michael@0: michael@0: // Blocks until all task finish executing. Called internally by methods michael@0: // that need to wait until the task queue is idle. michael@0: // mQueueMonitor must be held. michael@0: void AwaitIdleLocked(); michael@0: michael@0: RefPtr mPool; michael@0: michael@0: // Monitor that protects the queue and mIsRunning; michael@0: Monitor mQueueMonitor; michael@0: michael@0: // Queue of tasks to run. michael@0: std::queue> mTasks; michael@0: michael@0: // The thread currently running the task queue. We store a reference michael@0: // to this so that IsCurrentThreadIn() can tell if the current thread michael@0: // is the thread currently running in the task queue. michael@0: RefPtr mRunningThread; michael@0: michael@0: // True if we've dispatched an event to the pool to execute events from michael@0: // the queue. michael@0: bool mIsRunning; michael@0: michael@0: // True if we've started our shutdown process. michael@0: bool mIsShutdown; michael@0: michael@0: class Runner : public nsRunnable { michael@0: public: michael@0: Runner(MediaTaskQueue* aQueue) michael@0: : mQueue(aQueue) michael@0: { michael@0: } michael@0: NS_METHOD Run() MOZ_OVERRIDE; michael@0: private: michael@0: RefPtr mQueue; michael@0: }; michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif // MediaTaskQueue_h_