Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef MediaTaskQueue_h_ |
michael@0 | 8 | #define MediaTaskQueue_h_ |
michael@0 | 9 | |
michael@0 | 10 | #include <queue> |
michael@0 | 11 | #include "mozilla/RefPtr.h" |
michael@0 | 12 | #include "mozilla/Monitor.h" |
michael@0 | 13 | #include "nsThreadUtils.h" |
michael@0 | 14 | |
michael@0 | 15 | class nsIRunnable; |
michael@0 | 16 | |
michael@0 | 17 | namespace mozilla { |
michael@0 | 18 | |
michael@0 | 19 | class SharedThreadPool; |
michael@0 | 20 | |
michael@0 | 21 | // Abstracts executing runnables in order in a thread pool. The runnables |
michael@0 | 22 | // dispatched to the MediaTaskQueue will be executed in the order in which |
michael@0 | 23 | // they're received, and are guaranteed to not be executed concurrently. |
michael@0 | 24 | // They may be executed on different threads, and a memory barrier is used |
michael@0 | 25 | // to make this threadsafe for objects that aren't already threadsafe. |
michael@0 | 26 | class MediaTaskQueue MOZ_FINAL { |
michael@0 | 27 | ~MediaTaskQueue(); |
michael@0 | 28 | |
michael@0 | 29 | public: |
michael@0 | 30 | NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaTaskQueue) |
michael@0 | 31 | |
michael@0 | 32 | MediaTaskQueue(TemporaryRef<SharedThreadPool> aPool); |
michael@0 | 33 | |
michael@0 | 34 | nsresult Dispatch(TemporaryRef<nsIRunnable> aRunnable); |
michael@0 | 35 | |
michael@0 | 36 | // Removes all pending tasks from the task queue, and blocks until |
michael@0 | 37 | // the currently running task (if any) finishes. |
michael@0 | 38 | void Flush(); |
michael@0 | 39 | |
michael@0 | 40 | // Blocks until all tasks finish executing, then shuts down the task queue |
michael@0 | 41 | // and exits. |
michael@0 | 42 | void Shutdown(); |
michael@0 | 43 | |
michael@0 | 44 | // Blocks until all task finish executing. |
michael@0 | 45 | void AwaitIdle(); |
michael@0 | 46 | |
michael@0 | 47 | bool IsEmpty(); |
michael@0 | 48 | |
michael@0 | 49 | // Returns true if the current thread is currently running a Runnable in |
michael@0 | 50 | // the task queue. This is for debugging/validation purposes only. |
michael@0 | 51 | bool IsCurrentThreadIn(); |
michael@0 | 52 | |
michael@0 | 53 | private: |
michael@0 | 54 | |
michael@0 | 55 | // Blocks until all task finish executing. Called internally by methods |
michael@0 | 56 | // that need to wait until the task queue is idle. |
michael@0 | 57 | // mQueueMonitor must be held. |
michael@0 | 58 | void AwaitIdleLocked(); |
michael@0 | 59 | |
michael@0 | 60 | RefPtr<SharedThreadPool> mPool; |
michael@0 | 61 | |
michael@0 | 62 | // Monitor that protects the queue and mIsRunning; |
michael@0 | 63 | Monitor mQueueMonitor; |
michael@0 | 64 | |
michael@0 | 65 | // Queue of tasks to run. |
michael@0 | 66 | std::queue<RefPtr<nsIRunnable>> mTasks; |
michael@0 | 67 | |
michael@0 | 68 | // The thread currently running the task queue. We store a reference |
michael@0 | 69 | // to this so that IsCurrentThreadIn() can tell if the current thread |
michael@0 | 70 | // is the thread currently running in the task queue. |
michael@0 | 71 | RefPtr<nsIThread> mRunningThread; |
michael@0 | 72 | |
michael@0 | 73 | // True if we've dispatched an event to the pool to execute events from |
michael@0 | 74 | // the queue. |
michael@0 | 75 | bool mIsRunning; |
michael@0 | 76 | |
michael@0 | 77 | // True if we've started our shutdown process. |
michael@0 | 78 | bool mIsShutdown; |
michael@0 | 79 | |
michael@0 | 80 | class Runner : public nsRunnable { |
michael@0 | 81 | public: |
michael@0 | 82 | Runner(MediaTaskQueue* aQueue) |
michael@0 | 83 | : mQueue(aQueue) |
michael@0 | 84 | { |
michael@0 | 85 | } |
michael@0 | 86 | NS_METHOD Run() MOZ_OVERRIDE; |
michael@0 | 87 | private: |
michael@0 | 88 | RefPtr<MediaTaskQueue> mQueue; |
michael@0 | 89 | }; |
michael@0 | 90 | }; |
michael@0 | 91 | |
michael@0 | 92 | } // namespace mozilla |
michael@0 | 93 | |
michael@0 | 94 | #endif // MediaTaskQueue_h_ |