Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
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/. */
7 #ifndef MediaTaskQueue_h_
8 #define MediaTaskQueue_h_
10 #include <queue>
11 #include "mozilla/RefPtr.h"
12 #include "mozilla/Monitor.h"
13 #include "nsThreadUtils.h"
15 class nsIRunnable;
17 namespace mozilla {
19 class SharedThreadPool;
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();
29 public:
30 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaTaskQueue)
32 MediaTaskQueue(TemporaryRef<SharedThreadPool> aPool);
34 nsresult Dispatch(TemporaryRef<nsIRunnable> aRunnable);
36 // Removes all pending tasks from the task queue, and blocks until
37 // the currently running task (if any) finishes.
38 void Flush();
40 // Blocks until all tasks finish executing, then shuts down the task queue
41 // and exits.
42 void Shutdown();
44 // Blocks until all task finish executing.
45 void AwaitIdle();
47 bool IsEmpty();
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();
53 private:
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();
60 RefPtr<SharedThreadPool> mPool;
62 // Monitor that protects the queue and mIsRunning;
63 Monitor mQueueMonitor;
65 // Queue of tasks to run.
66 std::queue<RefPtr<nsIRunnable>> mTasks;
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;
73 // True if we've dispatched an event to the pool to execute events from
74 // the queue.
75 bool mIsRunning;
77 // True if we've started our shutdown process.
78 bool mIsShutdown;
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 };
92 } // namespace mozilla
94 #endif // MediaTaskQueue_h_