2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
#include "nsThreadManager.h"
|
|
|
|
#include "nsThread.h"
|
2009-10-28 20:28:57 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsIClassInfoImpl.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2014-07-04 14:34:15 +04:00
|
|
|
#include "mozilla/ThreadLocal.h"
|
2013-10-30 00:58:09 +04:00
|
|
|
#ifdef MOZ_CANARY
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2006-05-10 21:30:15 +04:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2009-11-09 22:30:01 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <windows.h>
|
2010-11-12 01:52:30 +03:00
|
|
|
DWORD gTLSThreadIDIndex = TlsAlloc();
|
2009-11-09 22:30:01 +03:00
|
|
|
#elif defined(NS_TLS)
|
2010-11-12 01:52:30 +03:00
|
|
|
NS_TLS mozilla::threads::ID gTLSThreadID = mozilla::threads::Generic;
|
2009-10-28 20:28:57 +03:00
|
|
|
#endif
|
|
|
|
|
2014-07-04 14:34:15 +04:00
|
|
|
static mozilla::ThreadLocal<bool> sTLSIsMainThread;
|
|
|
|
|
|
|
|
bool
|
|
|
|
NS_IsMainThread()
|
|
|
|
{
|
|
|
|
return sTLSIsMainThread.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NS_SetMainThread()
|
|
|
|
{
|
|
|
|
if (!sTLSIsMainThread.initialized()) {
|
|
|
|
if (!sTLSIsMainThread.init()) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
sTLSIsMainThread.set(true);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
typedef nsTArray<nsRefPtr<nsThread>> nsThreadArray;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static void
|
2014-05-27 11:15:35 +04:00
|
|
|
ReleaseObject(void* aData)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
static_cast<nsISupports*>(aData)->Release();
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static PLDHashOperator
|
2014-05-27 11:15:35 +04:00
|
|
|
AppendAndRemoveThread(PRThread* aKey, nsRefPtr<nsThread>& aThread, void* aArg)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadArray* threads = static_cast<nsThreadArray*>(aArg);
|
|
|
|
threads->AppendElement(aThread);
|
2006-05-10 21:30:15 +04:00
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// statically allocated instance
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsThreadManager::AddRef()
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsThreadManager::Release()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2013-10-11 00:42:16 +04:00
|
|
|
NS_IMPL_CLASSINFO(nsThreadManager, nullptr,
|
2010-06-22 20:59:57 +04:00
|
|
|
nsIClassInfo::THREADSAFE | nsIClassInfo::SINGLETON,
|
|
|
|
NS_THREADMANAGER_CID)
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(nsThreadManager, nsIThreadManager)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsThreadManager, nsIThreadManager)
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsThreadManager::Init()
|
|
|
|
{
|
2014-02-01 03:02:30 +04:00
|
|
|
// Child processes need to initialize the thread manager before they
|
|
|
|
// initialize XPCOM in order to set up the crash reporter. This leads to
|
|
|
|
// situations where we get initialized twice.
|
2014-05-27 11:15:35 +04:00
|
|
|
if (mInitialized) {
|
2014-02-01 03:02:30 +04:00
|
|
|
return NS_OK;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-02-01 03:02:30 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (PR_NewThreadPrivateIndex(&mCurThreadIndex, ReleaseObject) == PR_FAILURE) {
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
mLock = new Mutex("nsThreadManager.mLock");
|
|
|
|
|
2013-10-30 00:58:09 +04:00
|
|
|
#ifdef MOZ_CANARY
|
|
|
|
const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NONBLOCK;
|
|
|
|
const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
|
|
|
|
char* env_var_flag = getenv("MOZ_KILL_CANARIES");
|
2014-05-27 11:15:35 +04:00
|
|
|
sCanaryOutputFD =
|
|
|
|
env_var_flag ? (env_var_flag[0] ? open(env_var_flag, flags, mode) :
|
|
|
|
STDERR_FILENO) :
|
|
|
|
0;
|
2013-10-30 00:58:09 +04:00
|
|
|
#endif
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// Setup "main" thread
|
2011-10-12 21:52:26 +04:00
|
|
|
mMainThread = new nsThread(nsThread::MAIN_THREAD, 0);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
nsresult rv = mMainThread->InitCurrentThread();
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mMainThread = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to keep a pointer to the current thread, so we can satisfy
|
|
|
|
// GetIsMainThread calls that occur post-Shutdown.
|
|
|
|
mMainThread->GetPRThread(&mMainPRThread);
|
|
|
|
|
2009-11-09 22:30:01 +03:00
|
|
|
#ifdef XP_WIN
|
2014-05-27 11:15:35 +04:00
|
|
|
TlsSetValue(gTLSThreadIDIndex, (void*)mozilla::threads::Main);
|
2009-11-09 22:30:01 +03:00
|
|
|
#elif defined(NS_TLS)
|
2010-11-12 01:52:30 +03:00
|
|
|
gTLSThreadID = mozilla::threads::Main;
|
2009-10-28 20:28:57 +03:00
|
|
|
#endif
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitialized = true;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsThreadManager::Shutdown()
|
|
|
|
{
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "shutdown not called from main thread");
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Prevent further access to the thread manager (no more new threads!)
|
|
|
|
//
|
|
|
|
// XXX What happens if shutdown happens before NewThread completes?
|
|
|
|
// Fortunately, NewThread is only called on the main thread for now.
|
|
|
|
//
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitialized = false;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2006-05-11 05:24:57 +04:00
|
|
|
// Empty the main thread event queue before we begin shutting down threads.
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_ProcessPendingEvents(mMainThread);
|
|
|
|
|
2006-05-11 05:24:57 +04:00
|
|
|
// We gather the threads from the hashtable into a list, so that we avoid
|
|
|
|
// holding the hashtable lock while calling nsIThread::Shutdown.
|
2006-05-10 21:30:15 +04:00
|
|
|
nsThreadArray threads;
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(*mLock);
|
2006-05-10 21:30:15 +04:00
|
|
|
mThreadsByPRThread.Enumerate(AppendAndRemoveThread, &threads);
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's tempting to walk the list of threads here and tell them each to stop
|
|
|
|
// accepting new events, but that could lead to badness if one of those
|
|
|
|
// threads is stuck waiting for a response from another thread. To do it
|
|
|
|
// right, we'd need some way to interrupt the threads.
|
2014-05-27 11:15:35 +04:00
|
|
|
//
|
2006-05-11 05:24:57 +04:00
|
|
|
// Instead, we process events on the current thread while waiting for threads
|
|
|
|
// to shutdown. This means that we have to preserve a mostly functioning
|
|
|
|
// world until such time as the threads exit.
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Shutdown all threads that require it (join with threads that we created).
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < threads.Length(); ++i) {
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread* thread = threads[i];
|
|
|
|
if (thread->ShutdownRequired()) {
|
2006-05-10 21:30:15 +04:00
|
|
|
thread->Shutdown();
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2006-05-11 05:24:57 +04:00
|
|
|
// In case there are any more events somehow...
|
|
|
|
NS_ProcessPendingEvents(mMainThread);
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// There are no more background threads at this point.
|
|
|
|
|
2006-05-11 05:24:57 +04:00
|
|
|
// Clear the table of threads.
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(*mLock);
|
2006-05-11 05:24:57 +04:00
|
|
|
mThreadsByPRThread.Clear();
|
|
|
|
}
|
|
|
|
|
2009-06-22 17:08:04 +04:00
|
|
|
// Normally thread shutdown clears the observer for the thread, but since the
|
|
|
|
// main thread is special we do it manually here after we're sure all events
|
|
|
|
// have been processed.
|
2012-07-30 18:20:58 +04:00
|
|
|
mMainThread->SetObserver(nullptr);
|
2012-01-14 22:31:13 +04:00
|
|
|
mMainThread->ClearObservers();
|
2009-06-22 17:08:04 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// Release main thread object.
|
2012-07-30 18:20:58 +04:00
|
|
|
mMainThread = nullptr;
|
|
|
|
mLock = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Remove the TLS entry for the main thread.
|
2012-07-30 18:20:58 +04:00
|
|
|
PR_SetThreadPrivate(mCurThreadIndex, nullptr);
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadManager::RegisterCurrentThread(nsThread* aThread)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
MOZ_ASSERT(aThread->GetPRThread() == PR_GetCurrentThread(), "bad aThread");
|
2006-05-10 21:30:15 +04:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(*mLock);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2013-04-19 15:54:18 +04:00
|
|
|
++mCurrentNumberOfThreads;
|
|
|
|
if (mCurrentNumberOfThreads > mHighestNumberOfThreads) {
|
|
|
|
mHighestNumberOfThreads = mCurrentNumberOfThreads;
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
mThreadsByPRThread.Put(aThread->GetPRThread(), aThread); // XXX check OOM?
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_ADDREF(aThread); // for TLS entry
|
|
|
|
PR_SetThreadPrivate(mCurThreadIndex, aThread);
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadManager::UnregisterCurrentThread(nsThread* aThread)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
MOZ_ASSERT(aThread->GetPRThread() == PR_GetCurrentThread(), "bad aThread");
|
2006-05-10 21:30:15 +04:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(*mLock);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2013-04-19 15:54:18 +04:00
|
|
|
--mCurrentNumberOfThreads;
|
2014-05-27 11:15:35 +04:00
|
|
|
mThreadsByPRThread.Remove(aThread->GetPRThread());
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
PR_SetThreadPrivate(mCurThreadIndex, nullptr);
|
2006-05-10 21:30:15 +04:00
|
|
|
// Ref-count balanced via ReleaseObject
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread*
|
2006-05-10 21:30:15 +04:00
|
|
|
nsThreadManager::GetCurrentThread()
|
|
|
|
{
|
|
|
|
// read thread local storage
|
2014-05-27 11:15:35 +04:00
|
|
|
void* data = PR_GetThreadPrivate(mCurThreadIndex);
|
|
|
|
if (data) {
|
|
|
|
return static_cast<nsThread*>(data);
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2008-02-21 15:47:26 +03:00
|
|
|
if (!mInitialized) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2008-02-21 15:47:26 +03:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// OK, that's fine. We'll dynamically create one :-)
|
2011-10-12 21:52:26 +04:00
|
|
|
nsRefPtr<nsThread> thread = new nsThread(nsThread::NOT_MAIN_THREAD, 0);
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!thread || NS_FAILED(thread->InitCurrentThread())) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
return thread.get(); // reference held in TLS
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadManager::NewThread(uint32_t aCreationFlags,
|
|
|
|
uint32_t aStackSize,
|
|
|
|
nsIThread** aResult)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2006-05-11 05:24:57 +04:00
|
|
|
// No new threads during Shutdown
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!mInitialized)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread* thr = new nsThread(nsThread::NOT_MAIN_THREAD, aStackSize);
|
|
|
|
if (!thr) {
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_ADDREF(thr);
|
|
|
|
|
|
|
|
nsresult rv = thr->Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(thr);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we expect that the thread has been registered in mThread;
|
|
|
|
// however, it is possible that it could have also been replaced by now, so
|
|
|
|
// we cannot really assert that it was added.
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = thr;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadManager::GetThreadFromPRThread(PRThread* aThread, nsIThread** aResult)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2006-05-11 05:24:57 +04:00
|
|
|
// Keep this functioning during Shutdown
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!mMainThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!aThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
nsRefPtr<nsThread> temp;
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(*mLock);
|
2014-05-27 11:15:35 +04:00
|
|
|
mThreadsByPRThread.Get(aThread, getter_AddRefs(temp));
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IF_ADDREF(*aResult = temp);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadManager::GetMainThread(nsIThread** aResult)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2006-05-11 05:24:57 +04:00
|
|
|
// Keep this functioning during Shutdown
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!mMainThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
NS_ADDREF(*aResult = mMainThread);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadManager::GetCurrentThread(nsIThread** aResult)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2006-05-11 05:24:57 +04:00
|
|
|
// Keep this functioning during Shutdown
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!mMainThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
*aResult = GetCurrentThread();
|
|
|
|
if (!*aResult) {
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
NS_ADDREF(*aResult);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadManager::GetIsMainThread(bool* aResult)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
|
|
|
// This method may be called post-Shutdown
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = (PR_GetCurrentThread() == mMainPRThread);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-11-04 04:41:06 +03:00
|
|
|
|
2013-04-19 15:54:18 +04:00
|
|
|
uint32_t
|
|
|
|
nsThreadManager::GetHighestNumberOfThreads()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(*mLock);
|
|
|
|
return mHighestNumberOfThreads;
|
|
|
|
}
|