xpcom/threads/nsThreadManager.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 "nsThreadManager.h"
michael@0 8 #include "nsThread.h"
michael@0 9 #include "nsThreadUtils.h"
michael@0 10 #include "nsIClassInfoImpl.h"
michael@0 11 #include "nsTArray.h"
michael@0 12 #include "nsAutoPtr.h"
michael@0 13 #ifdef MOZ_CANARY
michael@0 14 #include <fcntl.h>
michael@0 15 #include <unistd.h>
michael@0 16 #endif
michael@0 17
michael@0 18 using namespace mozilla;
michael@0 19
michael@0 20 #ifdef XP_WIN
michael@0 21 #include <windows.h>
michael@0 22 DWORD gTLSThreadIDIndex = TlsAlloc();
michael@0 23 #elif defined(NS_TLS)
michael@0 24 NS_TLS mozilla::threads::ID gTLSThreadID = mozilla::threads::Generic;
michael@0 25 #endif
michael@0 26
michael@0 27 typedef nsTArray< nsRefPtr<nsThread> > nsThreadArray;
michael@0 28
michael@0 29 //-----------------------------------------------------------------------------
michael@0 30
michael@0 31 static void
michael@0 32 ReleaseObject(void *data)
michael@0 33 {
michael@0 34 static_cast<nsISupports *>(data)->Release();
michael@0 35 }
michael@0 36
michael@0 37 static PLDHashOperator
michael@0 38 AppendAndRemoveThread(PRThread *key, nsRefPtr<nsThread> &thread, void *arg)
michael@0 39 {
michael@0 40 nsThreadArray *threads = static_cast<nsThreadArray *>(arg);
michael@0 41 threads->AppendElement(thread);
michael@0 42 return PL_DHASH_REMOVE;
michael@0 43 }
michael@0 44
michael@0 45 // statically allocated instance
michael@0 46 NS_IMETHODIMP_(MozExternalRefCountType) nsThreadManager::AddRef() { return 2; }
michael@0 47 NS_IMETHODIMP_(MozExternalRefCountType) nsThreadManager::Release() { return 1; }
michael@0 48 NS_IMPL_CLASSINFO(nsThreadManager, nullptr,
michael@0 49 nsIClassInfo::THREADSAFE | nsIClassInfo::SINGLETON,
michael@0 50 NS_THREADMANAGER_CID)
michael@0 51 NS_IMPL_QUERY_INTERFACE_CI(nsThreadManager, nsIThreadManager)
michael@0 52 NS_IMPL_CI_INTERFACE_GETTER(nsThreadManager, nsIThreadManager)
michael@0 53
michael@0 54 //-----------------------------------------------------------------------------
michael@0 55
michael@0 56 nsresult
michael@0 57 nsThreadManager::Init()
michael@0 58 {
michael@0 59 // Child processes need to initialize the thread manager before they
michael@0 60 // initialize XPCOM in order to set up the crash reporter. This leads to
michael@0 61 // situations where we get initialized twice.
michael@0 62 if (mInitialized)
michael@0 63 return NS_OK;
michael@0 64
michael@0 65 if (PR_NewThreadPrivateIndex(&mCurThreadIndex, ReleaseObject) == PR_FAILURE)
michael@0 66 return NS_ERROR_FAILURE;
michael@0 67
michael@0 68 mLock = new Mutex("nsThreadManager.mLock");
michael@0 69
michael@0 70 #ifdef MOZ_CANARY
michael@0 71 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NONBLOCK;
michael@0 72 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
michael@0 73 char* env_var_flag = getenv("MOZ_KILL_CANARIES");
michael@0 74 sCanaryOutputFD = env_var_flag ? (env_var_flag[0] ?
michael@0 75 open(env_var_flag, flags, mode) :
michael@0 76 STDERR_FILENO) : 0;
michael@0 77 #endif
michael@0 78
michael@0 79 // Setup "main" thread
michael@0 80 mMainThread = new nsThread(nsThread::MAIN_THREAD, 0);
michael@0 81
michael@0 82 nsresult rv = mMainThread->InitCurrentThread();
michael@0 83 if (NS_FAILED(rv)) {
michael@0 84 mMainThread = nullptr;
michael@0 85 return rv;
michael@0 86 }
michael@0 87
michael@0 88 // We need to keep a pointer to the current thread, so we can satisfy
michael@0 89 // GetIsMainThread calls that occur post-Shutdown.
michael@0 90 mMainThread->GetPRThread(&mMainPRThread);
michael@0 91
michael@0 92 #ifdef XP_WIN
michael@0 93 TlsSetValue(gTLSThreadIDIndex, (void*) mozilla::threads::Main);
michael@0 94 #elif defined(NS_TLS)
michael@0 95 gTLSThreadID = mozilla::threads::Main;
michael@0 96 #endif
michael@0 97
michael@0 98 mInitialized = true;
michael@0 99 return NS_OK;
michael@0 100 }
michael@0 101
michael@0 102 void
michael@0 103 nsThreadManager::Shutdown()
michael@0 104 {
michael@0 105 MOZ_ASSERT(NS_IsMainThread(), "shutdown not called from main thread");
michael@0 106
michael@0 107 // Prevent further access to the thread manager (no more new threads!)
michael@0 108 //
michael@0 109 // XXX What happens if shutdown happens before NewThread completes?
michael@0 110 // Fortunately, NewThread is only called on the main thread for now.
michael@0 111 //
michael@0 112 mInitialized = false;
michael@0 113
michael@0 114 // Empty the main thread event queue before we begin shutting down threads.
michael@0 115 NS_ProcessPendingEvents(mMainThread);
michael@0 116
michael@0 117 // We gather the threads from the hashtable into a list, so that we avoid
michael@0 118 // holding the hashtable lock while calling nsIThread::Shutdown.
michael@0 119 nsThreadArray threads;
michael@0 120 {
michael@0 121 MutexAutoLock lock(*mLock);
michael@0 122 mThreadsByPRThread.Enumerate(AppendAndRemoveThread, &threads);
michael@0 123 }
michael@0 124
michael@0 125 // It's tempting to walk the list of threads here and tell them each to stop
michael@0 126 // accepting new events, but that could lead to badness if one of those
michael@0 127 // threads is stuck waiting for a response from another thread. To do it
michael@0 128 // right, we'd need some way to interrupt the threads.
michael@0 129 //
michael@0 130 // Instead, we process events on the current thread while waiting for threads
michael@0 131 // to shutdown. This means that we have to preserve a mostly functioning
michael@0 132 // world until such time as the threads exit.
michael@0 133
michael@0 134 // Shutdown all threads that require it (join with threads that we created).
michael@0 135 for (uint32_t i = 0; i < threads.Length(); ++i) {
michael@0 136 nsThread *thread = threads[i];
michael@0 137 if (thread->ShutdownRequired())
michael@0 138 thread->Shutdown();
michael@0 139 }
michael@0 140
michael@0 141 // In case there are any more events somehow...
michael@0 142 NS_ProcessPendingEvents(mMainThread);
michael@0 143
michael@0 144 // There are no more background threads at this point.
michael@0 145
michael@0 146 // Clear the table of threads.
michael@0 147 {
michael@0 148 MutexAutoLock lock(*mLock);
michael@0 149 mThreadsByPRThread.Clear();
michael@0 150 }
michael@0 151
michael@0 152 // Normally thread shutdown clears the observer for the thread, but since the
michael@0 153 // main thread is special we do it manually here after we're sure all events
michael@0 154 // have been processed.
michael@0 155 mMainThread->SetObserver(nullptr);
michael@0 156 mMainThread->ClearObservers();
michael@0 157
michael@0 158 // Release main thread object.
michael@0 159 mMainThread = nullptr;
michael@0 160 mLock = nullptr;
michael@0 161
michael@0 162 // Remove the TLS entry for the main thread.
michael@0 163 PR_SetThreadPrivate(mCurThreadIndex, nullptr);
michael@0 164 }
michael@0 165
michael@0 166 void
michael@0 167 nsThreadManager::RegisterCurrentThread(nsThread *thread)
michael@0 168 {
michael@0 169 MOZ_ASSERT(thread->GetPRThread() == PR_GetCurrentThread(), "bad thread");
michael@0 170
michael@0 171 MutexAutoLock lock(*mLock);
michael@0 172
michael@0 173 ++mCurrentNumberOfThreads;
michael@0 174 if (mCurrentNumberOfThreads > mHighestNumberOfThreads) {
michael@0 175 mHighestNumberOfThreads = mCurrentNumberOfThreads;
michael@0 176 }
michael@0 177
michael@0 178 mThreadsByPRThread.Put(thread->GetPRThread(), thread); // XXX check OOM?
michael@0 179
michael@0 180 NS_ADDREF(thread); // for TLS entry
michael@0 181 PR_SetThreadPrivate(mCurThreadIndex, thread);
michael@0 182 }
michael@0 183
michael@0 184 void
michael@0 185 nsThreadManager::UnregisterCurrentThread(nsThread *thread)
michael@0 186 {
michael@0 187 MOZ_ASSERT(thread->GetPRThread() == PR_GetCurrentThread(), "bad thread");
michael@0 188
michael@0 189 MutexAutoLock lock(*mLock);
michael@0 190
michael@0 191 --mCurrentNumberOfThreads;
michael@0 192 mThreadsByPRThread.Remove(thread->GetPRThread());
michael@0 193
michael@0 194 PR_SetThreadPrivate(mCurThreadIndex, nullptr);
michael@0 195 // Ref-count balanced via ReleaseObject
michael@0 196 }
michael@0 197
michael@0 198 nsThread *
michael@0 199 nsThreadManager::GetCurrentThread()
michael@0 200 {
michael@0 201 // read thread local storage
michael@0 202 void *data = PR_GetThreadPrivate(mCurThreadIndex);
michael@0 203 if (data)
michael@0 204 return static_cast<nsThread *>(data);
michael@0 205
michael@0 206 if (!mInitialized) {
michael@0 207 return nullptr;
michael@0 208 }
michael@0 209
michael@0 210 // OK, that's fine. We'll dynamically create one :-)
michael@0 211 nsRefPtr<nsThread> thread = new nsThread(nsThread::NOT_MAIN_THREAD, 0);
michael@0 212 if (!thread || NS_FAILED(thread->InitCurrentThread()))
michael@0 213 return nullptr;
michael@0 214
michael@0 215 return thread.get(); // reference held in TLS
michael@0 216 }
michael@0 217
michael@0 218 NS_IMETHODIMP
michael@0 219 nsThreadManager::NewThread(uint32_t creationFlags,
michael@0 220 uint32_t stackSize,
michael@0 221 nsIThread **result)
michael@0 222 {
michael@0 223 // No new threads during Shutdown
michael@0 224 if (NS_WARN_IF(!mInitialized))
michael@0 225 return NS_ERROR_NOT_INITIALIZED;
michael@0 226
michael@0 227 nsThread *thr = new nsThread(nsThread::NOT_MAIN_THREAD, stackSize);
michael@0 228 if (!thr)
michael@0 229 return NS_ERROR_OUT_OF_MEMORY;
michael@0 230 NS_ADDREF(thr);
michael@0 231
michael@0 232 nsresult rv = thr->Init();
michael@0 233 if (NS_FAILED(rv)) {
michael@0 234 NS_RELEASE(thr);
michael@0 235 return rv;
michael@0 236 }
michael@0 237
michael@0 238 // At this point, we expect that the thread has been registered in mThread;
michael@0 239 // however, it is possible that it could have also been replaced by now, so
michael@0 240 // we cannot really assert that it was added.
michael@0 241
michael@0 242 *result = thr;
michael@0 243 return NS_OK;
michael@0 244 }
michael@0 245
michael@0 246 NS_IMETHODIMP
michael@0 247 nsThreadManager::GetThreadFromPRThread(PRThread *thread, nsIThread **result)
michael@0 248 {
michael@0 249 // Keep this functioning during Shutdown
michael@0 250 if (NS_WARN_IF(!mMainThread))
michael@0 251 return NS_ERROR_NOT_INITIALIZED;
michael@0 252 if (NS_WARN_IF(!thread))
michael@0 253 return NS_ERROR_INVALID_ARG;
michael@0 254
michael@0 255 nsRefPtr<nsThread> temp;
michael@0 256 {
michael@0 257 MutexAutoLock lock(*mLock);
michael@0 258 mThreadsByPRThread.Get(thread, getter_AddRefs(temp));
michael@0 259 }
michael@0 260
michael@0 261 NS_IF_ADDREF(*result = temp);
michael@0 262 return NS_OK;
michael@0 263 }
michael@0 264
michael@0 265 NS_IMETHODIMP
michael@0 266 nsThreadManager::GetMainThread(nsIThread **result)
michael@0 267 {
michael@0 268 // Keep this functioning during Shutdown
michael@0 269 if (NS_WARN_IF(!mMainThread))
michael@0 270 return NS_ERROR_NOT_INITIALIZED;
michael@0 271 NS_ADDREF(*result = mMainThread);
michael@0 272 return NS_OK;
michael@0 273 }
michael@0 274
michael@0 275 NS_IMETHODIMP
michael@0 276 nsThreadManager::GetCurrentThread(nsIThread **result)
michael@0 277 {
michael@0 278 // Keep this functioning during Shutdown
michael@0 279 if (NS_WARN_IF(!mMainThread))
michael@0 280 return NS_ERROR_NOT_INITIALIZED;
michael@0 281 *result = GetCurrentThread();
michael@0 282 if (!*result)
michael@0 283 return NS_ERROR_OUT_OF_MEMORY;
michael@0 284 NS_ADDREF(*result);
michael@0 285 return NS_OK;
michael@0 286 }
michael@0 287
michael@0 288 NS_IMETHODIMP
michael@0 289 nsThreadManager::GetIsMainThread(bool *result)
michael@0 290 {
michael@0 291 // This method may be called post-Shutdown
michael@0 292
michael@0 293 *result = (PR_GetCurrentThread() == mMainPRThread);
michael@0 294 return NS_OK;
michael@0 295 }
michael@0 296
michael@0 297 uint32_t
michael@0 298 nsThreadManager::GetHighestNumberOfThreads()
michael@0 299 {
michael@0 300 MutexAutoLock lock(*mLock);
michael@0 301 return mHighestNumberOfThreads;
michael@0 302 }

mercurial