content/media/SharedThreadPool.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

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.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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 #include "SharedThreadPool.h"
michael@0 8 #include "mozilla/Monitor.h"
michael@0 9 #include "mozilla/StaticPtr.h"
michael@0 10 #include "nsDataHashtable.h"
michael@0 11 #include "VideoUtils.h"
michael@0 12 #include "nsXPCOMCIDInternal.h"
michael@0 13 #include "nsComponentManagerUtils.h"
michael@0 14
michael@0 15 #ifdef XP_WIN
michael@0 16 // Required to init MSCOM by MSCOMInitThreadPoolListener.
michael@0 17 #include <objbase.h>
michael@0 18 #endif
michael@0 19
michael@0 20 namespace mozilla {
michael@0 21
michael@0 22 // Created and destroyed on the main thread.
michael@0 23 static StaticAutoPtr<ReentrantMonitor> sMonitor;
michael@0 24
michael@0 25 // Hashtable, maps thread pool name to SharedThreadPool instance.
michael@0 26 // Modified only on the main thread.
michael@0 27 static StaticAutoPtr<nsDataHashtable<nsCStringHashKey, SharedThreadPool*>> sPools;
michael@0 28
michael@0 29 static already_AddRefed<nsIThreadPool>
michael@0 30 CreateThreadPool(const nsCString& aName);
michael@0 31
michael@0 32 static void
michael@0 33 DestroySharedThreadPoolHashTable();
michael@0 34
michael@0 35 void
michael@0 36 SharedThreadPool::EnsureInitialized()
michael@0 37 {
michael@0 38 MOZ_ASSERT(NS_IsMainThread());
michael@0 39 if (sMonitor || sPools) {
michael@0 40 // Already initalized.
michael@0 41 return;
michael@0 42 }
michael@0 43 sMonitor = new ReentrantMonitor("SharedThreadPool");
michael@0 44 sPools = new nsDataHashtable<nsCStringHashKey, SharedThreadPool*>();
michael@0 45 }
michael@0 46
michael@0 47 class ShutdownPoolsEvent : public nsRunnable {
michael@0 48 public:
michael@0 49 NS_IMETHODIMP Run() {
michael@0 50 MOZ_ASSERT(NS_IsMainThread());
michael@0 51 DestroySharedThreadPoolHashTable();
michael@0 52 return NS_OK;
michael@0 53 }
michael@0 54 };
michael@0 55
michael@0 56 static void
michael@0 57 DestroySharedThreadPoolHashTable()
michael@0 58 {
michael@0 59 MOZ_ASSERT(NS_IsMainThread());
michael@0 60 MOZ_ASSERT(sMonitor && sPools);
michael@0 61 if (!sPools->Count()) {
michael@0 62 // No more SharedThreadPool singletons. Delete the hash table.
michael@0 63 // Note we don't need to lock sMonitor, since we only modify the
michael@0 64 // hash table on the main thread, and if the hash table is empty
michael@0 65 // there are no external references into its contents.
michael@0 66 sPools = nullptr;
michael@0 67 sMonitor = nullptr;
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 /* static */
michael@0 72 void
michael@0 73 SharedThreadPool::SpinUntilShutdown()
michael@0 74 {
michael@0 75 MOZ_ASSERT(NS_IsMainThread());
michael@0 76 // Wait until the ShutdownPoolsEvent has been run and shutdown the pool.
michael@0 77 while (sPools) {
michael@0 78 if (!NS_ProcessNextEvent(NS_GetCurrentThread(), true)) {
michael@0 79 break;
michael@0 80 }
michael@0 81 }
michael@0 82 MOZ_ASSERT(!sPools);
michael@0 83 MOZ_ASSERT(!sMonitor);
michael@0 84 }
michael@0 85
michael@0 86 TemporaryRef<SharedThreadPool>
michael@0 87 SharedThreadPool::Get(const nsCString& aName, uint32_t aThreadLimit)
michael@0 88 {
michael@0 89 MOZ_ASSERT(NS_IsMainThread());
michael@0 90 EnsureInitialized();
michael@0 91 MOZ_ASSERT(sMonitor);
michael@0 92 ReentrantMonitorAutoEnter mon(*sMonitor);
michael@0 93 SharedThreadPool* pool = nullptr;
michael@0 94 nsresult rv;
michael@0 95 if (!sPools->Get(aName, &pool)) {
michael@0 96 nsCOMPtr<nsIThreadPool> threadPool(CreateThreadPool(aName));
michael@0 97 NS_ENSURE_TRUE(threadPool, nullptr);
michael@0 98 pool = new SharedThreadPool(aName, threadPool);
michael@0 99
michael@0 100 // Set the thread and idle limits. Note that we don't rely on the
michael@0 101 // EnsureThreadLimitIsAtLeast() call below, as the default thread limit
michael@0 102 // is 4, and if aThreadLimit is less than 4 we'll end up with a pool
michael@0 103 // with 4 threads rather than what we expected; so we'll have unexpected
michael@0 104 // behaviour.
michael@0 105 rv = pool->SetThreadLimit(aThreadLimit);
michael@0 106 NS_ENSURE_SUCCESS(rv, nullptr);
michael@0 107
michael@0 108 rv = pool->SetIdleThreadLimit(aThreadLimit);
michael@0 109 NS_ENSURE_SUCCESS(rv, nullptr);
michael@0 110
michael@0 111 sPools->Put(aName, pool);
michael@0 112 } else if (NS_FAILED(pool->EnsureThreadLimitIsAtLeast(aThreadLimit))) {
michael@0 113 NS_WARNING("Failed to set limits on thread pool");
michael@0 114 }
michael@0 115
michael@0 116 MOZ_ASSERT(pool);
michael@0 117 RefPtr<SharedThreadPool> instance(pool);
michael@0 118 return instance.forget();
michael@0 119 }
michael@0 120
michael@0 121 NS_IMETHODIMP_(MozExternalRefCountType) SharedThreadPool::AddRef(void)
michael@0 122 {
michael@0 123 MOZ_ASSERT(sMonitor);
michael@0 124 ReentrantMonitorAutoEnter mon(*sMonitor);
michael@0 125 MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt");
michael@0 126 nsrefcnt count = ++mRefCnt;
michael@0 127 NS_LOG_ADDREF(this, count, "SharedThreadPool", sizeof(*this));
michael@0 128 return count;
michael@0 129 }
michael@0 130
michael@0 131 NS_IMETHODIMP_(MozExternalRefCountType) SharedThreadPool::Release(void)
michael@0 132 {
michael@0 133 MOZ_ASSERT(sMonitor);
michael@0 134 bool dispatchShutdownEvent;
michael@0 135 {
michael@0 136 ReentrantMonitorAutoEnter mon(*sMonitor);
michael@0 137 nsrefcnt count = --mRefCnt;
michael@0 138 NS_LOG_RELEASE(this, count, "SharedThreadPool");
michael@0 139 if (count) {
michael@0 140 return count;
michael@0 141 }
michael@0 142
michael@0 143 // Zero refcount. Must shutdown and then delete the thread pool.
michael@0 144
michael@0 145 // First, dispatch an event to the main thread to call Shutdown() on
michael@0 146 // the nsIThreadPool. The Runnable here will add a refcount to the pool,
michael@0 147 // and when the Runnable releases the nsIThreadPool it will be deleted.
michael@0 148 RefPtr<nsIRunnable> r = NS_NewRunnableMethod(mPool, &nsIThreadPool::Shutdown);
michael@0 149 NS_DispatchToMainThread(r);
michael@0 150
michael@0 151 // Remove SharedThreadPool from table of pools.
michael@0 152 sPools->Remove(mName);
michael@0 153 MOZ_ASSERT(!sPools->Get(mName));
michael@0 154
michael@0 155 // Stabilize refcount, so that if something in the dtor QIs,
michael@0 156 // it won't explode.
michael@0 157 mRefCnt = 1;
michael@0 158
michael@0 159 delete this;
michael@0 160
michael@0 161 dispatchShutdownEvent = sPools->Count() == 0;
michael@0 162 }
michael@0 163 if (dispatchShutdownEvent) {
michael@0 164 // No more SharedThreadPools alive. Destroy the hash table.
michael@0 165 // Ensure that we only run on the main thread.
michael@0 166 // Do this in an event so that if something holds the monitor we won't
michael@0 167 // be deleting the monitor while it's held.
michael@0 168 NS_DispatchToMainThread(new ShutdownPoolsEvent(), NS_DISPATCH_NORMAL);
michael@0 169 }
michael@0 170 return 0;
michael@0 171 }
michael@0 172
michael@0 173 NS_IMPL_QUERY_INTERFACE(SharedThreadPool, nsIThreadPool, nsIEventTarget)
michael@0 174
michael@0 175 SharedThreadPool::SharedThreadPool(const nsCString& aName,
michael@0 176 nsIThreadPool* aPool)
michael@0 177 : mName(aName)
michael@0 178 , mPool(aPool)
michael@0 179 , mRefCnt(0)
michael@0 180 {
michael@0 181 MOZ_COUNT_CTOR(SharedThreadPool);
michael@0 182 mEventTarget = do_QueryInterface(aPool);
michael@0 183 }
michael@0 184
michael@0 185 SharedThreadPool::~SharedThreadPool()
michael@0 186 {
michael@0 187 MOZ_COUNT_DTOR(SharedThreadPool);
michael@0 188 }
michael@0 189
michael@0 190 #ifdef XP_WIN
michael@0 191
michael@0 192 // Thread pool listener which ensures that MSCOM is initialized and
michael@0 193 // deinitialized on the thread pool thread. We may call into WMF or
michael@0 194 // DirectShow on this thread, so we need MSCOM working.
michael@0 195 class MSCOMInitThreadPoolListener MOZ_FINAL : public nsIThreadPoolListener {
michael@0 196 public:
michael@0 197 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 198 NS_DECL_NSITHREADPOOLLISTENER
michael@0 199 };
michael@0 200
michael@0 201 NS_IMPL_ISUPPORTS(MSCOMInitThreadPoolListener, nsIThreadPoolListener)
michael@0 202
michael@0 203 NS_IMETHODIMP
michael@0 204 MSCOMInitThreadPoolListener::OnThreadCreated()
michael@0 205 {
michael@0 206 HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
michael@0 207 if (FAILED(hr)) {
michael@0 208 NS_WARNING("Failed to initialize MSCOM on WMFByteStream thread.");
michael@0 209 }
michael@0 210 return NS_OK;
michael@0 211 }
michael@0 212
michael@0 213 NS_IMETHODIMP
michael@0 214 MSCOMInitThreadPoolListener::OnThreadShuttingDown()
michael@0 215 {
michael@0 216 CoUninitialize();
michael@0 217 return NS_OK;
michael@0 218 }
michael@0 219
michael@0 220 #endif // XP_WIN
michael@0 221
michael@0 222 nsresult
michael@0 223 SharedThreadPool::EnsureThreadLimitIsAtLeast(uint32_t aLimit)
michael@0 224 {
michael@0 225 // We limit the number of threads that we use for media. Note that we
michael@0 226 // set the thread limit to the same as the idle limit so that we're not
michael@0 227 // constantly creating and destroying threads (see Bug 881954). When the
michael@0 228 // thread pool threads shutdown they dispatch an event to the main thread
michael@0 229 // to call nsIThread::Shutdown(), and if we're very busy that can take a
michael@0 230 // while to run, and we end up with dozens of extra threads. Note that
michael@0 231 // threads that are idle for 60 seconds are shutdown naturally.
michael@0 232 uint32_t existingLimit = 0;
michael@0 233 nsresult rv;
michael@0 234
michael@0 235 rv = mPool->GetThreadLimit(&existingLimit);
michael@0 236 NS_ENSURE_SUCCESS(rv, rv);
michael@0 237 if (aLimit > existingLimit) {
michael@0 238 rv = mPool->SetThreadLimit(aLimit);
michael@0 239 NS_ENSURE_SUCCESS(rv, rv);
michael@0 240 }
michael@0 241
michael@0 242 rv = mPool->GetIdleThreadLimit(&existingLimit);
michael@0 243 NS_ENSURE_SUCCESS(rv, rv);
michael@0 244 if (aLimit > existingLimit) {
michael@0 245 rv = mPool->SetIdleThreadLimit(aLimit);
michael@0 246 NS_ENSURE_SUCCESS(rv, rv);
michael@0 247 }
michael@0 248
michael@0 249 return NS_OK;
michael@0 250 }
michael@0 251
michael@0 252 static already_AddRefed<nsIThreadPool>
michael@0 253 CreateThreadPool(const nsCString& aName)
michael@0 254 {
michael@0 255 MOZ_ASSERT(NS_IsMainThread());
michael@0 256
michael@0 257 nsresult rv;
michael@0 258 nsCOMPtr<nsIThreadPool> pool = do_CreateInstance(NS_THREADPOOL_CONTRACTID, &rv);
michael@0 259 NS_ENSURE_SUCCESS(rv, nullptr);
michael@0 260
michael@0 261 rv = pool->SetName(aName);
michael@0 262 NS_ENSURE_SUCCESS(rv, nullptr);
michael@0 263
michael@0 264 rv = pool->SetThreadStackSize(MEDIA_THREAD_STACK_SIZE);
michael@0 265 NS_ENSURE_SUCCESS(rv, nullptr);
michael@0 266
michael@0 267 #ifdef XP_WIN
michael@0 268 // Ensure MSCOM is initialized on the thread pools threads.
michael@0 269 nsCOMPtr<nsIThreadPoolListener> listener = new MSCOMInitThreadPoolListener();
michael@0 270 rv = pool->SetListener(listener);
michael@0 271 NS_ENSURE_SUCCESS(rv, nullptr);
michael@0 272 #endif
michael@0 273
michael@0 274 return pool.forget();
michael@0 275 }
michael@0 276
michael@0 277 } // namespace mozilla

mercurial