content/media/MediaTaskQueue.h

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

mercurial