2014-06-30 15:39:45 +00: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 11:12:37 +00: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/. */
|
1999-04-02 09:20:44 +00:00
|
|
|
|
2013-10-23 12:01:24 +00:00
|
|
|
#include "nsThread.h"
|
|
|
|
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
|
|
|
|
// Chromium's logging can sometimes leak through...
|
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
|
|
|
|
2013-07-09 02:15:34 +00:00
|
|
|
#include "nsMemoryPressure.h"
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsThreadManager.h"
|
|
|
|
#include "nsIClassInfoImpl.h"
|
|
|
|
#include "nsIProgrammingLanguage.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2013-07-11 20:21:45 +00:00
|
|
|
#include "pratom.h"
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "prlog.h"
|
2011-08-05 22:10:50 +00:00
|
|
|
#include "nsIObserverService.h"
|
2011-10-12 17:52:26 +00:00
|
|
|
#include "mozilla/HangMonitor.h"
|
2014-04-09 04:57:52 +00:00
|
|
|
#include "mozilla/IOInterposer.h"
|
2014-08-06 04:43:36 +00:00
|
|
|
#include "mozilla/ipc/MessageChannel.h"
|
2011-08-05 22:10:50 +00:00
|
|
|
#include "mozilla/Services.h"
|
2013-07-07 20:23:43 +00:00
|
|
|
#include "nsXPCOMPrivate.h"
|
2014-03-03 05:12:27 +00:00
|
|
|
#include "mozilla/ChaosMode.h"
|
2014-11-20 22:16:36 +00:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
2014-03-03 05:12:27 +00:00
|
|
|
|
2014-08-30 05:21:22 +00:00
|
|
|
#ifdef MOZ_CRASHREPORTER
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsICrashReporter.h"
|
|
|
|
#endif
|
|
|
|
|
2014-03-03 05:12:27 +00:00
|
|
|
#ifdef XP_LINUX
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#endif
|
1999-04-02 09:20:44 +00:00
|
|
|
|
2010-10-27 15:16:03 +00:00
|
|
|
#define HAVE_UALARM _BSD_SOURCE || (_XOPEN_SOURCE >= 500 || \
|
|
|
|
_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) && \
|
|
|
|
!(_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700)
|
|
|
|
|
2014-03-03 05:12:27 +00:00
|
|
|
#if defined(XP_LINUX) && !defined(ANDROID) && defined(_GNU_SOURCE)
|
|
|
|
#define HAVE_SCHED_SETAFFINITY
|
|
|
|
#endif
|
|
|
|
|
2013-10-29 20:58:09 +00:00
|
|
|
#ifdef MOZ_CANARY
|
2010-10-27 15:16:03 +00:00
|
|
|
# include <unistd.h>
|
|
|
|
# include <execinfo.h>
|
|
|
|
# include <signal.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
# include "nsXULAppAPI.h"
|
|
|
|
#endif
|
|
|
|
|
2010-05-19 23:22:19 +00:00
|
|
|
#if defined(NS_FUNCTION_TIMER) && defined(_MSC_VER)
|
|
|
|
#include "nsTimerImpl.h"
|
|
|
|
#include "nsStackWalk.h"
|
|
|
|
#endif
|
|
|
|
#ifdef NS_FUNCTION_TIMER
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#endif
|
|
|
|
|
2014-03-27 08:49:06 +00:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
#include "GeckoTaskTracer.h"
|
|
|
|
using namespace mozilla::tasktracer;
|
|
|
|
#endif
|
|
|
|
|
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 04:29:02 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
#ifdef PR_LOGGING
|
2014-05-27 07:15:35 +00:00
|
|
|
static PRLogModuleInfo*
|
2012-10-29 23:32:10 +00:00
|
|
|
GetThreadLog()
|
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
static PRLogModuleInfo* sLog;
|
|
|
|
if (!sLog) {
|
2012-10-29 23:32:10 +00:00
|
|
|
sLog = PR_NewLogModule("nsThread");
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2012-10-29 23:32:10 +00:00
|
|
|
return sLog;
|
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
#endif
|
2013-11-19 02:34:00 +00:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
2012-10-29 23:32:10 +00:00
|
|
|
#define LOG(args) PR_LOG(GetThreadLog(), PR_LOG_DEBUG, args)
|
1999-04-02 09:20:44 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_DECL_CI_INTERFACE_GETTER(nsThread)
|
1999-04-02 09:20:44 +00:00
|
|
|
|
2012-10-23 22:26:36 +00:00
|
|
|
nsIThreadObserver* nsThread::sMainThreadObserver = nullptr;
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Because we do not have our own nsIFactory, we have to implement nsIClassInfo
|
|
|
|
// somewhat manually.
|
2000-11-30 05:24:53 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
class nsThreadClassInfo : public nsIClassInfo
|
|
|
|
{
|
2006-05-10 17:30:15 +00:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED // no mRefCnt
|
|
|
|
NS_DECL_NSICLASSINFO
|
2002-08-25 05:55:50 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThreadClassInfo()
|
|
|
|
{
|
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
};
|
1999-04-02 09:20:44 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsThreadClassInfo::AddRef()
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsThreadClassInfo::Release()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_QUERY_INTERFACE(nsThreadClassInfo, nsIClassInfo)
|
1999-04-05 21:02:24 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThreadClassInfo::GetInterfaces(uint32_t* aCount, nsIID*** aArray)
|
1999-04-05 21:02:24 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
return NS_CI_INTERFACE_GETTER_NAME(nsThread)(aCount, aArray);
|
1999-04-02 09:20:44 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThreadClassInfo::GetHelperForLanguage(uint32_t aLang, nsISupports** aResult)
|
1999-10-01 23:30:06 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
*aResult = nullptr;
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
1999-10-01 23:30:06 +00:00
|
|
|
}
|
|
|
|
|
1999-04-02 09:20:44 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThreadClassInfo::GetContractID(char** aResult)
|
1999-04-02 09:20:44 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
*aResult = nullptr;
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
1999-04-02 09:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThreadClassInfo::GetClassDescription(char** aResult)
|
1999-04-02 09:20:44 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
*aResult = nullptr;
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
1999-04-02 09:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThreadClassInfo::GetClassID(nsCID** aResult)
|
1999-04-02 09:20:44 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
*aResult = nullptr;
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
1999-04-02 09:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThreadClassInfo::GetImplementationLanguage(uint32_t* aResult)
|
1999-04-02 09:20:44 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
*aResult = nsIProgrammingLanguage::CPLUSPLUS;
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
1999-04-02 09:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThreadClassInfo::GetFlags(uint32_t* aResult)
|
1999-04-02 09:20:44 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
*aResult = THREADSAFE;
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
1999-04-02 09:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThreadClassInfo::GetClassIDNoAlloc(nsCID* aResult)
|
1999-04-02 09:20:44 +00:00
|
|
|
{
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2013-07-19 02:31:26 +00:00
|
|
|
NS_IMPL_ADDREF(nsThread)
|
|
|
|
NS_IMPL_RELEASE(nsThread)
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsThread)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIThread)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIThreadInternal)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIEventTarget)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsPriority)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIThread)
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIClassInfo))) {
|
2013-05-30 21:20:02 +00:00
|
|
|
static nsThreadClassInfo sThreadClassInfo;
|
2007-07-08 07:08:04 +00:00
|
|
|
foundInterface = static_cast<nsIClassInfo*>(&sThreadClassInfo);
|
2006-05-10 17:30:15 +00:00
|
|
|
} else
|
|
|
|
NS_INTERFACE_MAP_END
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsThread, nsIThread, nsIThreadInternal,
|
|
|
|
nsIEventTarget, nsISupportsPriority)
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
class nsThreadStartupEvent : public nsRunnable
|
|
|
|
{
|
2006-05-10 17:30:15 +00:00
|
|
|
public:
|
2013-11-19 21:27:37 +00:00
|
|
|
nsThreadStartupEvent()
|
|
|
|
: mMon("nsThreadStartupEvent.mMon")
|
2014-05-27 07:15:35 +00:00
|
|
|
, mInitialized(false)
|
|
|
|
{
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method does not return until the thread startup object is in the
|
|
|
|
// completion state.
|
2014-05-27 07:15:35 +00:00
|
|
|
void Wait()
|
|
|
|
{
|
|
|
|
if (mInitialized) {
|
|
|
|
// Maybe avoid locking...
|
2006-05-10 17:30:15 +00:00
|
|
|
return;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mMon);
|
2014-05-27 07:15:35 +00:00
|
|
|
while (!mInitialized) {
|
2006-05-10 17:30:15 +00:00
|
|
|
mon.Wait();
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 18:13:20 +00:00
|
|
|
// This method needs to be public to support older compilers (xlC_r on AIX).
|
|
|
|
// It should be called directly as this class type is reference counted.
|
2014-08-25 19:17:15 +00:00
|
|
|
virtual ~nsThreadStartupEvent() {}
|
2006-05-10 18:13:20 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
private:
|
2014-05-27 07:15:35 +00:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mMon);
|
2011-10-17 14:59:28 +00:00
|
|
|
mInitialized = true;
|
2006-05-10 17:30:15 +00:00
|
|
|
mon.Notify();
|
1999-04-02 09:20:44 +00:00
|
|
|
return NS_OK;
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitor mMon;
|
2014-05-27 07:15:35 +00:00
|
|
|
bool mInitialized;
|
2006-05-10 17:30:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
struct nsThreadShutdownContext
|
|
|
|
{
|
|
|
|
nsThread* joiningThread;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool shutdownAck;
|
2006-05-10 17:30:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This event is responsible for notifying nsThread::Shutdown that it is time
|
|
|
|
// to call PR_JoinThread.
|
2014-05-27 07:15:35 +00:00
|
|
|
class nsThreadShutdownAckEvent : public nsRunnable
|
|
|
|
{
|
2006-05-10 17:30:15 +00:00
|
|
|
public:
|
2014-08-05 13:36:59 +00:00
|
|
|
explicit nsThreadShutdownAckEvent(nsThreadShutdownContext* aCtx)
|
2014-05-27 07:15:35 +00:00
|
|
|
: mShutdownContext(aCtx)
|
|
|
|
{
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
2014-05-27 07:15:35 +00:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mShutdownContext->shutdownAck = true;
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThreadShutdownContext* mShutdownContext;
|
2006-05-10 17:30:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This event is responsible for setting mShutdownContext
|
2014-05-27 07:15:35 +00:00
|
|
|
class nsThreadShutdownEvent : public nsRunnable
|
|
|
|
{
|
2006-05-10 17:30:15 +00:00
|
|
|
public:
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThreadShutdownEvent(nsThread* aThr, nsThreadShutdownContext* aCtx)
|
|
|
|
: mThread(aThr)
|
|
|
|
, mShutdownContext(aCtx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2006-05-10 17:30:15 +00:00
|
|
|
mThread->mShutdownContext = mShutdownContext;
|
2013-10-23 12:01:24 +00:00
|
|
|
MessageLoop::current()->Quit();
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsThread> mThread;
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThreadShutdownContext* mShutdownContext;
|
2006-05-10 17:30:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-03-03 05:12:27 +00:00
|
|
|
static void
|
|
|
|
SetupCurrentThreadForChaosMode()
|
|
|
|
{
|
2014-11-14 18:12:51 +00:00
|
|
|
if (!ChaosMode::isActive(ChaosMode::ThreadScheduling)) {
|
2014-03-03 05:12:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_LINUX
|
|
|
|
// PR_SetThreadPriority doesn't really work since priorities >
|
|
|
|
// PR_PRIORITY_NORMAL can't be set by non-root users. Instead we'll just use
|
|
|
|
// setpriority(2) to set random 'nice values'. In regular Linux this is only
|
|
|
|
// a dynamic adjustment so it still doesn't really do what we want, but tools
|
|
|
|
// like 'rr' can be more aggressive about honoring these values.
|
|
|
|
// Some of these calls may fail due to trying to lower the priority
|
|
|
|
// (e.g. something may have already called setpriority() for this thread).
|
|
|
|
// This makes it hard to have non-main threads with higher priority than the
|
|
|
|
// main thread, but that's hard to fix. Tools like rr can choose to honor the
|
|
|
|
// requested values anyway.
|
|
|
|
// Use just 4 priorities so there's a reasonable chance of any two threads
|
|
|
|
// having equal priority.
|
|
|
|
setpriority(PRIO_PROCESS, 0, ChaosMode::randomUint32LessThan(4));
|
|
|
|
#else
|
|
|
|
// We should set the affinity here but NSPR doesn't provide a way to expose it.
|
2014-05-27 07:15:35 +00:00
|
|
|
uint32_t priority = ChaosMode::randomUint32LessThan(PR_PRIORITY_LAST + 1);
|
|
|
|
PR_SetThreadPriority(PR_GetCurrentThread(), PRThreadPriority(priority));
|
2014-03-03 05:12:27 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SCHED_SETAFFINITY
|
|
|
|
// Force half the threads to CPU 0 so they compete for CPU
|
|
|
|
if (ChaosMode::randomUint32LessThan(2)) {
|
|
|
|
cpu_set_t cpus;
|
|
|
|
CPU_ZERO(&cpus);
|
|
|
|
CPU_SET(0, &cpus);
|
|
|
|
sched_setaffinity(0, sizeof(cpus), &cpus);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
/*static*/ void
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::ThreadFunc(void* aArg)
|
2006-05-10 17:30:15 +00:00
|
|
|
{
|
2014-11-20 22:16:36 +00:00
|
|
|
using mozilla::ipc::BackgroundChild;
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread* self = static_cast<nsThread*>(aArg); // strong reference
|
2006-05-10 17:30:15 +00:00
|
|
|
self->mThread = PR_GetCurrentThread();
|
2014-03-03 05:12:27 +00:00
|
|
|
SetupCurrentThreadForChaosMode();
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
// Inform the ThreadManager
|
|
|
|
nsThreadManager::get()->RegisterCurrentThread(self);
|
2014-12-18 09:00:39 +00:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
self->mThreadStatusInfo =
|
|
|
|
static_cast<void*>(nsThreadManager::get()->GetCurrentThreadStatusInfo());
|
|
|
|
#endif
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2014-04-09 04:57:52 +00:00
|
|
|
mozilla::IOInterposer::RegisterCurrentThread();
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
// Wait for and process startup event
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
2011-10-17 14:59:28 +00:00
|
|
|
if (!self->GetEvent(true, getter_AddRefs(event))) {
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_WARNING("failed waiting for thread startup event");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event->Run(); // unblocks nsThread::Init
|
2012-07-30 14:20:58 +00:00
|
|
|
event = nullptr;
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
{
|
|
|
|
// Scope for MessageLoop.
|
2013-10-23 12:01:24 +00:00
|
|
|
nsAutoPtr<MessageLoop> loop(
|
|
|
|
new MessageLoop(MessageLoop::TYPE_MOZILLA_NONMAINTHREAD));
|
|
|
|
|
|
|
|
// Now, process incoming events...
|
|
|
|
loop->Run();
|
|
|
|
|
2014-11-20 22:16:36 +00:00
|
|
|
BackgroundChild::CloseForCurrentThread();
|
|
|
|
|
2013-10-23 12:01:24 +00:00
|
|
|
// Do NS_ProcessPendingEvents but with special handling to set
|
|
|
|
// mEventsAreDoomed atomically with the removal of the last event. The key
|
|
|
|
// invariant here is that we will never permit PutEvent to succeed if the
|
|
|
|
// event would be left in the queue after our final call to
|
|
|
|
// NS_ProcessPendingEvents.
|
|
|
|
while (true) {
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(self->mLock);
|
2013-11-14 18:06:17 +00:00
|
|
|
if (!self->mEvents->HasPendingEvent()) {
|
2013-10-23 12:01:24 +00:00
|
|
|
// No events in the queue, so we will stop now. Don't let any more
|
|
|
|
// events be added, since they won't be processed. It is critical
|
|
|
|
// that no PutEvent can occur between testing that the event queue is
|
|
|
|
// empty and setting mEventsAreDoomed!
|
|
|
|
self->mEventsAreDoomed = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-12-05 02:17:15 +00:00
|
|
|
}
|
2013-10-23 12:01:24 +00:00
|
|
|
NS_ProcessPendingEvents(self);
|
2007-12-05 02:17:15 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2014-04-09 04:57:52 +00:00
|
|
|
mozilla::IOInterposer::UnregisterCurrentThread();
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
// Inform the threadmanager that this thread is going away
|
|
|
|
nsThreadManager::get()->UnregisterCurrentThread(self);
|
|
|
|
|
|
|
|
// Dispatch shutdown ACK
|
|
|
|
event = new nsThreadShutdownAckEvent(self->mShutdownContext);
|
|
|
|
self->mShutdownContext->joiningThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
|
2009-06-22 13:08:04 +00:00
|
|
|
// Release any observer of the thread here.
|
2012-07-30 14:20:58 +00:00
|
|
|
self->SetObserver(nullptr);
|
2009-06-22 13:08:04 +00:00
|
|
|
|
2014-03-27 08:49:06 +00:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
FreeTraceInfo();
|
|
|
|
#endif
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_RELEASE(self);
|
1999-04-02 09:20:44 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-08-30 05:21:22 +00:00
|
|
|
#ifdef MOZ_CRASHREPORTER
|
|
|
|
// Tell the crash reporter to save a memory report if our heuristics determine
|
|
|
|
// that an OOM failure is likely to occur soon.
|
|
|
|
static bool SaveMemoryReportNearOOM()
|
|
|
|
{
|
|
|
|
bool needMemoryReport = false;
|
|
|
|
|
|
|
|
#ifdef XP_WIN // XXX implement on other platforms as needed
|
|
|
|
const size_t LOWMEM_THRESHOLD_VIRTUAL = 200 * 1024 * 1024;
|
|
|
|
MEMORYSTATUSEX statex;
|
|
|
|
statex.dwLength = sizeof(statex);
|
|
|
|
if (GlobalMemoryStatusEx(&statex)) {
|
|
|
|
if (statex.ullAvailVirtual < LOWMEM_THRESHOLD_VIRTUAL) {
|
|
|
|
needMemoryReport = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (needMemoryReport) {
|
|
|
|
nsCOMPtr<nsICrashReporter> cr =
|
|
|
|
do_GetService("@mozilla.org/toolkit/crash-reporter;1");
|
|
|
|
cr->SaveMemoryReport();
|
|
|
|
}
|
|
|
|
|
|
|
|
return needMemoryReport;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-29 20:58:09 +00:00
|
|
|
#ifdef MOZ_CANARY
|
|
|
|
int sCanaryOutputFD = -1;
|
|
|
|
#endif
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
nsThread::nsThread(MainThreadFlag aMainThread, uint32_t aStackSize)
|
2011-07-27 03:26:47 +00:00
|
|
|
: mLock("nsThread.mLock")
|
2013-11-14 18:06:17 +00:00
|
|
|
, mEvents(&mEventsRoot)
|
2011-07-27 03:26:47 +00:00
|
|
|
, mPriority(PRIORITY_NORMAL)
|
2012-07-30 14:20:58 +00:00
|
|
|
, mThread(nullptr)
|
2011-07-27 03:26:47 +00:00
|
|
|
, mRunningEvent(0)
|
|
|
|
, mStackSize(aStackSize)
|
2012-07-30 14:20:58 +00:00
|
|
|
, mShutdownContext(nullptr)
|
2011-10-17 14:59:28 +00:00
|
|
|
, mShutdownRequired(false)
|
|
|
|
, mEventsAreDoomed(false)
|
2011-10-12 17:52:26 +00:00
|
|
|
, mIsMainThread(aMainThread)
|
2014-12-18 09:00:39 +00:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
, mThreadStatusMonitor("nsThread.mThreadStatusLock")
|
|
|
|
, mThreadStatusInfo(nullptr)
|
|
|
|
#endif
|
1999-04-05 21:02:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
nsThread::~nsThread()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsThread::Init()
|
1999-10-01 23:30:06 +00:00
|
|
|
{
|
2006-05-10 17:30:15 +00:00
|
|
|
// spawn thread and wait until it is fully setup
|
2013-11-19 21:27:37 +00:00
|
|
|
nsRefPtr<nsThreadStartupEvent> startup = new nsThreadStartupEvent();
|
2014-05-27 07:15:35 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_ADDREF_THIS();
|
2014-05-27 07:15:35 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mShutdownRequired = true;
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
// ThreadFunc is responsible for setting mThread
|
2014-05-27 07:15:35 +00:00
|
|
|
PRThread* thr = PR_CreateThread(PR_USER_THREAD, ThreadFunc, this,
|
2006-05-10 17:30:15 +00:00
|
|
|
PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
|
2011-07-27 03:26:47 +00:00
|
|
|
PR_JOINABLE_THREAD, mStackSize);
|
2006-05-10 17:30:15 +00:00
|
|
|
if (!thr) {
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ThreadFunc will wait for this event to be run before it tries to access
|
|
|
|
// mThread. By delaying insertion of this event into the queue, we ensure
|
|
|
|
// that mThread is set properly.
|
|
|
|
{
|
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 04:29:02 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2013-11-14 18:06:17 +00:00
|
|
|
mEventsRoot.PutEvent(startup);
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for thread to call ThreadManager::SetupCurrentThread, which completes
|
|
|
|
// initialization of ThreadFunc.
|
|
|
|
startup->Wait();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-06-11 19:26:04 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
nsresult
|
|
|
|
nsThread::InitCurrentThread()
|
|
|
|
{
|
|
|
|
mThread = PR_GetCurrentThread();
|
2014-03-03 05:12:27 +00:00
|
|
|
SetupCurrentThreadForChaosMode();
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
nsThreadManager::get()->RegisterCurrentThread(this);
|
2014-12-18 09:00:39 +00:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
mThreadStatusInfo =
|
|
|
|
static_cast<void*>(nsThreadManager::get()->GetCurrentThreadStatusInfo());
|
|
|
|
#endif
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
1999-10-01 23:30:06 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 02:17:15 +00:00
|
|
|
nsresult
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::PutEvent(nsIRunnable* aEvent, nsNestedEventTarget* aTarget)
|
2000-11-30 05:24:53 +00:00
|
|
|
{
|
2014-09-26 23:21:57 +00:00
|
|
|
nsCOMPtr<nsIThreadObserver> obs;
|
|
|
|
|
2006-05-10 17:30:15 +00: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 04:29:02 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-27 07:15:35 +00:00
|
|
|
nsChainedEventQueue* queue = aTarget ? aTarget->mQueue : &mEventsRoot;
|
2013-11-14 18:06:17 +00:00
|
|
|
if (!queue || (queue == &mEventsRoot && mEventsAreDoomed)) {
|
2007-12-05 02:17:15 +00:00
|
|
|
NS_WARNING("An event was posted to a thread that will never run it (rejected)");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2014-12-18 09:00:39 +00:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mThreadStatusMonitor);
|
|
|
|
SetWorking();
|
|
|
|
#endif // MOZ_NUWA_PROCESS
|
|
|
|
queue->PutEvent(aEvent);
|
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
}
|
|
|
|
#endif // MOZ_NUWA_PROCESS
|
2014-09-26 23:21:57 +00:00
|
|
|
|
|
|
|
// Make sure to grab the observer before dropping the lock, otherwise the
|
|
|
|
// event that we just placed into the queue could run and eventually delete
|
|
|
|
// this nsThread before the calling thread is scheduled again. We would then
|
|
|
|
// crash while trying to access a dead nsThread.
|
|
|
|
obs = mObserver;
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
if (obs) {
|
2006-05-10 17:30:15 +00:00
|
|
|
obs->OnDispatchedEvent(this);
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2007-12-05 02:17:15 +00:00
|
|
|
return NS_OK;
|
2000-11-30 05:24:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-14 18:06:17 +00:00
|
|
|
nsresult
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::DispatchInternal(nsIRunnable* aEvent, uint32_t aFlags,
|
|
|
|
nsNestedEventTarget* aTarget)
|
2000-11-30 05:24:53 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
if (NS_WARN_IF(!aEvent)) {
|
2013-11-19 21:27:37 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
if (gXPCOMThreadsShutDown && MAIN_THREAD != mIsMainThread && !aTarget) {
|
2013-07-07 20:23:43 +00:00
|
|
|
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
|
|
|
|
}
|
|
|
|
|
2014-03-27 08:49:06 +00:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
2015-03-17 16:29:17 +00:00
|
|
|
nsCOMPtr<nsIRunnable> tracedRunnable = CreateTracedRunnable(aEvent);
|
2014-05-27 07:15:35 +00:00
|
|
|
aEvent = tracedRunnable;
|
2014-03-27 08:49:06 +00:00
|
|
|
#endif
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
if (aFlags & DISPATCH_SYNC) {
|
|
|
|
nsThread* thread = nsThreadManager::get()->GetCurrentThread();
|
|
|
|
if (NS_WARN_IF(!thread)) {
|
2013-11-19 21:27:37 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
// XXX we should be able to do something better here... we should
|
|
|
|
// be able to monitor the slot occupied by this event and use
|
|
|
|
// that to tell us when the event has been processed.
|
2014-03-27 08:49:06 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
nsRefPtr<nsThreadSyncDispatch> wrapper =
|
2014-05-27 07:15:35 +00:00
|
|
|
new nsThreadSyncDispatch(thread, aEvent);
|
|
|
|
if (!wrapper) {
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
|
|
|
nsresult rv = PutEvent(wrapper, aTarget);
|
2007-12-05 02:17:15 +00:00
|
|
|
// Don't wait for the event to finish if we didn't dispatch it...
|
2014-05-27 07:15:35 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2007-12-05 02:17:15 +00:00
|
|
|
return rv;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2014-04-22 19:34:24 +00:00
|
|
|
// Allows waiting; ensure no locks are held that would deadlock us!
|
2014-05-27 07:15:35 +00:00
|
|
|
while (wrapper->IsPending()) {
|
2014-04-22 19:34:24 +00:00
|
|
|
NS_ProcessNextEvent(thread, true);
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2010-11-17 20:58:48 +00:00
|
|
|
return wrapper->Result();
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
NS_ASSERTION(aFlags == NS_DISPATCH_NORMAL, "unexpected dispatch flags");
|
|
|
|
return PutEvent(aEvent, aTarget);
|
2013-11-14 18:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIEventTarget
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::Dispatch(nsIRunnable* aEvent, uint32_t aFlags)
|
2013-11-14 18:06:17 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
LOG(("THRD(%p) Dispatch [%p %x]\n", this, aEvent, aFlags));
|
2013-11-14 18:06:17 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
return DispatchInternal(aEvent, aFlags, nullptr);
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
2000-11-30 05:24:53 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::IsOnCurrentThread(bool* aResult)
|
2006-05-10 17:30:15 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
*aResult = (PR_GetCurrentThread() == mThread);
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
2000-11-30 05:24:53 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIThread
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::GetPRThread(PRThread** aResult)
|
1999-04-02 09:20:44 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
*aResult = mThread;
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
1999-04-02 09:20:44 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::Shutdown()
|
1999-10-01 23:30:06 +00:00
|
|
|
{
|
2006-05-10 17:30:15 +00:00
|
|
|
LOG(("THRD(%p) shutdown\n", this));
|
|
|
|
|
|
|
|
// XXX If we make this warn, then we hit that warning at xpcom shutdown while
|
|
|
|
// shutting down a thread in a thread pool. That happens b/c the thread
|
|
|
|
// in the thread pool is already shutdown by the thread manager.
|
2014-05-27 07:15:35 +00:00
|
|
|
if (!mThread) {
|
1999-10-01 23:30:06 +00:00
|
|
|
return NS_OK;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
if (NS_WARN_IF(mThread == PR_GetCurrentThread())) {
|
2013-11-19 21:27:37 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
// Prevent multiple calls to this method
|
|
|
|
{
|
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 04:29:02 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-27 07:15:35 +00:00
|
|
|
if (!mShutdownRequired) {
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
mShutdownRequired = false;
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsThreadShutdownContext context;
|
|
|
|
context.joiningThread = nsThreadManager::get()->GetCurrentThread();
|
2011-10-17 14:59:28 +00:00
|
|
|
context.shutdownAck = false;
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
// Set mShutdownContext and wake up the thread in case it is waiting for
|
|
|
|
// events to process.
|
|
|
|
nsCOMPtr<nsIRunnable> event = new nsThreadShutdownEvent(this, &context);
|
2014-05-27 07:15:35 +00:00
|
|
|
if (!event) {
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2007-12-05 02:17:15 +00:00
|
|
|
// XXXroc What if posting the event fails due to OOM?
|
2013-11-14 18:06:17 +00:00
|
|
|
PutEvent(event, nullptr);
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
// We could still end up with other events being added after the shutdown
|
|
|
|
// task, but that's okay because we process pending events in ThreadFunc
|
|
|
|
// after setting mShutdownContext just before exiting.
|
2014-05-27 07:15:35 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
// Process events on the current thread until we receive a shutdown ACK.
|
2014-04-22 19:34:24 +00:00
|
|
|
// Allows waiting; ensure no locks are held that would deadlock us!
|
2014-05-27 07:15:35 +00:00
|
|
|
while (!context.shutdownAck) {
|
2014-04-22 19:34:24 +00:00
|
|
|
NS_ProcessNextEvent(context.joiningThread, true);
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
// Now, it should be safe to join without fear of dead-locking.
|
|
|
|
|
|
|
|
PR_JoinThread(mThread);
|
2012-07-30 14:20:58 +00:00
|
|
|
mThread = nullptr;
|
2009-06-22 13:08:04 +00:00
|
|
|
|
2012-01-14 18:31:13 +00:00
|
|
|
// We hold strong references to our event observers, and once the thread is
|
|
|
|
// shut down the observers can't easily unregister themselves. Do it here
|
|
|
|
// to avoid leaking.
|
|
|
|
ClearObservers();
|
|
|
|
|
2009-06-22 13:08:04 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
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 04:29:02 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2013-02-08 05:54:20 +00:00
|
|
|
MOZ_ASSERT(!mObserver, "Should have been cleared at shutdown!");
|
2009-06-22 13:08:04 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
1999-10-01 23:30:06 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::HasPendingEvents(bool* aResult)
|
2006-05-10 17:30:15 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-19 21:27:37 +00:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
1999-04-02 09:20:44 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
*aResult = mEvents->GetEvent(false, nullptr);
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-27 15:16:03 +00:00
|
|
|
#ifdef MOZ_CANARY
|
2014-05-27 07:15:35 +00:00
|
|
|
void canary_alarm_handler(int signum);
|
2010-10-27 15:16:03 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
class Canary
|
|
|
|
{
|
|
|
|
//XXX ToDo: support nested loops
|
2010-10-27 15:16:03 +00:00
|
|
|
public:
|
2014-05-27 07:15:35 +00:00
|
|
|
Canary()
|
|
|
|
{
|
2013-10-29 20:58:09 +00:00
|
|
|
if (sCanaryOutputFD > 0 && EventLatencyIsImportant()) {
|
2010-10-27 15:16:03 +00:00
|
|
|
signal(SIGALRM, canary_alarm_handler);
|
2014-05-27 07:15:35 +00:00
|
|
|
ualarm(15000, 0);
|
2010-10-27 15:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
~Canary()
|
|
|
|
{
|
|
|
|
if (sCanaryOutputFD != 0 && EventLatencyIsImportant()) {
|
2010-10-27 15:16:03 +00:00
|
|
|
ualarm(0, 0);
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2010-10-27 15:16:03 +00:00
|
|
|
}
|
2011-01-05 22:11:56 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
static bool EventLatencyIsImportant()
|
|
|
|
{
|
2011-04-03 02:14:00 +00:00
|
|
|
return NS_IsMainThread() && XRE_GetProcessType() == GeckoProcessType_Default;
|
2011-01-05 22:11:56 +00:00
|
|
|
}
|
2010-10-27 15:16:03 +00:00
|
|
|
};
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
void canary_alarm_handler(int signum)
|
2010-10-27 15:16:03 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
void* array[30];
|
2010-10-27 15:16:03 +00:00
|
|
|
const char msg[29] = "event took too long to run:\n";
|
|
|
|
// use write to be safe in the signal handler
|
2014-05-27 07:15:35 +00:00
|
|
|
write(sCanaryOutputFD, msg, sizeof(msg));
|
2013-10-29 20:58:09 +00:00
|
|
|
backtrace_symbols_fd(array, backtrace(array, 30), sCanaryOutputFD);
|
2010-10-27 15:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-11-18 22:19:19 +00:00
|
|
|
#define NOTIFY_EVENT_OBSERVERS(func_, params_) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if (!mEventObservers.IsEmpty()) { \
|
|
|
|
nsAutoTObserverArray<nsCOMPtr<nsIThreadObserver>, 2>::ForwardIterator \
|
|
|
|
iter_(mEventObservers); \
|
|
|
|
nsCOMPtr<nsIThreadObserver> obs_; \
|
|
|
|
while (iter_.HasMore()) { \
|
|
|
|
obs_ = iter_.GetNext(); \
|
|
|
|
obs_ -> func_ params_ ; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::ProcessNextEvent(bool aMayWait, bool* aResult)
|
1999-04-05 21:02:24 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
LOG(("THRD(%p) ProcessNextEvent [%u %u]\n", this, aMayWait, mRunningEvent));
|
1999-04-05 21:02:24 +00:00
|
|
|
|
2014-08-06 04:43:36 +00:00
|
|
|
// If we're on the main thread, we shouldn't be dispatching CPOWs.
|
2014-08-25 19:17:15 +00:00
|
|
|
MOZ_RELEASE_ASSERT(mIsMainThread != MAIN_THREAD ||
|
2014-10-08 04:32:45 +00:00
|
|
|
!ipc::ParentProcessIsBlocked());
|
2014-08-06 04:43:36 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-19 21:27:37 +00:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
1999-04-05 21:02:24 +00:00
|
|
|
|
2014-02-01 01:14:00 +00:00
|
|
|
// The toplevel event loop normally blocks waiting for the next event, but
|
|
|
|
// if we're trying to shut this thread down, we must exit the event loop when
|
|
|
|
// the event queue is empty.
|
|
|
|
// This only applys to the toplevel event loop! Nested event loops (e.g.
|
|
|
|
// during sync dispatch) are waiting for some state change and must be able
|
|
|
|
// to block even if something has requested shutdown of the thread. Otherwise
|
|
|
|
// we'll just busywait as we endlessly look for an event, fail to find one,
|
|
|
|
// and repeat the nested event loop since its state change hasn't happened yet.
|
2014-05-27 07:15:35 +00:00
|
|
|
bool reallyWait = aMayWait && (mRunningEvent > 0 || !ShuttingDown());
|
2014-02-01 00:42:24 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
if (MAIN_THREAD == mIsMainThread && reallyWait) {
|
2011-10-12 17:52:26 +00:00
|
|
|
HangMonitor::Suspend();
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2011-10-12 17:52:26 +00:00
|
|
|
|
2011-08-05 22:10:50 +00:00
|
|
|
// Fire a memory pressure notification, if we're the main thread and one is
|
|
|
|
// pending.
|
|
|
|
if (MAIN_THREAD == mIsMainThread && !ShuttingDown()) {
|
2013-07-09 02:15:34 +00:00
|
|
|
MemoryPressureState mpPending = NS_GetPendingMemoryPressure();
|
|
|
|
if (mpPending != MemPressure_None) {
|
2011-08-05 22:10:50 +00:00
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
2013-07-09 02:15:34 +00:00
|
|
|
|
|
|
|
// Use no-forward to prevent the notifications from being transferred to
|
|
|
|
// the children of this process.
|
|
|
|
NS_NAMED_LITERAL_STRING(lowMem, "low-memory-no-forward");
|
|
|
|
NS_NAMED_LITERAL_STRING(lowMemOngoing, "low-memory-ongoing-no-forward");
|
|
|
|
|
2011-08-05 22:10:50 +00:00
|
|
|
if (os) {
|
2012-07-30 14:20:58 +00:00
|
|
|
os->NotifyObservers(nullptr, "memory-pressure",
|
2013-07-09 02:15:34 +00:00
|
|
|
mpPending == MemPressure_New ? lowMem.get() :
|
2014-05-27 07:15:35 +00:00
|
|
|
lowMemOngoing.get());
|
2013-07-09 02:15:34 +00:00
|
|
|
} else {
|
2011-08-05 22:10:50 +00:00
|
|
|
NS_WARNING("Can't get observer service!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 05:21:22 +00:00
|
|
|
#ifdef MOZ_CRASHREPORTER
|
|
|
|
if (MAIN_THREAD == mIsMainThread && !ShuttingDown()) {
|
|
|
|
// Keep an eye on memory usage (cheap, ~7ms) somewhat frequently,
|
|
|
|
// but save memory reports (expensive, ~75ms) less frequently.
|
|
|
|
const size_t LOW_MEMORY_CHECK_SECONDS = 30;
|
|
|
|
const size_t LOW_MEMORY_SAVE_SECONDS = 3 * 60;
|
|
|
|
|
|
|
|
static TimeStamp nextCheck = TimeStamp::NowLoRes()
|
|
|
|
+ TimeDuration::FromSeconds(LOW_MEMORY_CHECK_SECONDS);
|
|
|
|
|
|
|
|
TimeStamp now = TimeStamp::NowLoRes();
|
|
|
|
if (now >= nextCheck) {
|
|
|
|
if (SaveMemoryReportNearOOM()) {
|
|
|
|
nextCheck = now + TimeDuration::FromSeconds(LOW_MEMORY_SAVE_SECONDS);
|
|
|
|
} else {
|
|
|
|
nextCheck = now + TimeDuration::FromSeconds(LOW_MEMORY_CHECK_SECONDS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-23 22:26:36 +00:00
|
|
|
bool notifyMainThreadObserver =
|
|
|
|
(MAIN_THREAD == mIsMainThread) && sMainThreadObserver;
|
2014-05-27 07:15:35 +00:00
|
|
|
if (notifyMainThreadObserver) {
|
|
|
|
sMainThreadObserver->OnProcessNextEvent(this, reallyWait, mRunningEvent);
|
|
|
|
}
|
2012-10-23 22:26:36 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
nsCOMPtr<nsIThreadObserver> obs = mObserver;
|
2014-05-27 07:15:35 +00:00
|
|
|
if (obs) {
|
2014-02-01 00:42:24 +00:00
|
|
|
obs->OnProcessNextEvent(this, reallyWait, mRunningEvent);
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
1999-04-05 21:02:24 +00:00
|
|
|
|
2010-11-18 22:19:19 +00:00
|
|
|
NOTIFY_EVENT_OBSERVERS(OnProcessNextEvent,
|
2014-02-01 00:42:24 +00:00
|
|
|
(this, reallyWait, mRunningEvent));
|
2010-11-18 22:19:19 +00:00
|
|
|
|
2009-06-11 17:09:35 +00:00
|
|
|
++mRunningEvent;
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2010-10-27 15:16:03 +00:00
|
|
|
#ifdef MOZ_CANARY
|
|
|
|
Canary canary;
|
|
|
|
#endif
|
2006-05-10 17:30:15 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2009-06-11 17:09:35 +00:00
|
|
|
{
|
|
|
|
// Scope for |event| to make sure that its destructor fires while
|
|
|
|
// mRunningEvent has been incremented, since that destructor can
|
|
|
|
// also do work.
|
|
|
|
|
|
|
|
// If we are shutting down, then do not wait for new events.
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
2014-02-01 00:42:24 +00:00
|
|
|
mEvents->GetEvent(reallyWait, getter_AddRefs(event));
|
2009-06-11 17:09:35 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
*aResult = (event.get() != nullptr);
|
2009-06-11 17:09:35 +00:00
|
|
|
|
|
|
|
if (event) {
|
|
|
|
LOG(("THRD(%p) running [%p]\n", this, event.get()));
|
2014-05-27 07:15:35 +00:00
|
|
|
if (MAIN_THREAD == mIsMainThread) {
|
2011-10-12 17:52:26 +00:00
|
|
|
HangMonitor::NotifyActivity();
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2009-06-11 17:09:35 +00:00
|
|
|
event->Run();
|
2014-05-27 07:15:35 +00:00
|
|
|
} else if (aMayWait) {
|
2013-02-08 05:54:20 +00:00
|
|
|
MOZ_ASSERT(ShuttingDown(),
|
|
|
|
"This should only happen when shutting down");
|
2009-06-11 17:09:35 +00:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
2009-06-11 17:09:35 +00:00
|
|
|
--mRunningEvent;
|
2010-11-18 22:19:19 +00:00
|
|
|
|
2014-12-18 09:00:39 +00:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
nsCOMPtr<nsIRunnable> notifyAllIdleRunnable;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mThreadStatusMonitor);
|
|
|
|
if ((!mEvents->GetEvent(false, nullptr)) && (mRunningEvent == 0)) {
|
|
|
|
nsThreadManager::get()->SetThreadIsWorking(
|
|
|
|
static_cast<nsThreadManager::ThreadStatusInfo*>(mThreadStatusInfo),
|
|
|
|
false, getter_AddRefs(notifyAllIdleRunnable));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (notifyAllIdleRunnable) {
|
|
|
|
// Dispatching a task leads us to acquire |mLock| of the thread. If we
|
|
|
|
// dispatch to main thread while holding main thread's
|
|
|
|
// |mThreadStatusMonitor|, deadlock could happen if other thread is
|
|
|
|
// blocked by main thread's |mThreadStatusMonitor| and is holding
|
|
|
|
// main thread's |mLock|.
|
|
|
|
Dispatch(notifyAllIdleRunnable, NS_DISPATCH_NORMAL);
|
|
|
|
nsThreadManager::get()->ResetIsDispatchingToMainThread();
|
|
|
|
}
|
|
|
|
#endif // MOZ_NUWA_PROCESS
|
|
|
|
|
2013-10-23 12:01:20 +00:00
|
|
|
NOTIFY_EVENT_OBSERVERS(AfterProcessNextEvent,
|
2014-05-27 07:15:35 +00:00
|
|
|
(this, mRunningEvent, *aResult));
|
2010-11-18 22:19:19 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
if (obs) {
|
|
|
|
obs->AfterProcessNextEvent(this, mRunningEvent, *aResult);
|
|
|
|
}
|
2006-06-07 00:06:11 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
if (notifyMainThreadObserver && sMainThreadObserver) {
|
|
|
|
sMainThreadObserver->AfterProcessNextEvent(this, mRunningEvent, *aResult);
|
|
|
|
}
|
2012-10-23 22:26:36 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
return rv;
|
1999-04-05 21:02:24 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsISupportsPriority
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::GetPriority(int32_t* aPriority)
|
2000-11-30 05:24:53 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
*aPriority = mPriority;
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
2000-11-30 05:24:53 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::SetPriority(int32_t aPriority)
|
1999-04-06 06:09:15 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
if (NS_WARN_IF(!mThread)) {
|
2013-11-19 21:27:37 +00:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
// NSPR defines the following four thread priorities:
|
|
|
|
// PR_PRIORITY_LOW
|
|
|
|
// PR_PRIORITY_NORMAL
|
|
|
|
// PR_PRIORITY_HIGH
|
|
|
|
// PR_PRIORITY_URGENT
|
|
|
|
// We map the priority values defined on nsISupportsPriority to these values.
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
mPriority = aPriority;
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
PRThreadPriority pri;
|
|
|
|
if (mPriority <= PRIORITY_HIGHEST) {
|
|
|
|
pri = PR_PRIORITY_URGENT;
|
|
|
|
} else if (mPriority < PRIORITY_NORMAL) {
|
|
|
|
pri = PR_PRIORITY_HIGH;
|
|
|
|
} else if (mPriority > PRIORITY_NORMAL) {
|
|
|
|
pri = PR_PRIORITY_LOW;
|
|
|
|
} else {
|
|
|
|
pri = PR_PRIORITY_NORMAL;
|
|
|
|
}
|
2014-03-03 05:12:27 +00:00
|
|
|
// If chaos mode is active, retain the randomly chosen priority
|
2014-11-14 18:12:51 +00:00
|
|
|
if (!ChaosMode::isActive(ChaosMode::ThreadScheduling)) {
|
2014-03-03 05:12:27 +00:00
|
|
|
PR_SetThreadPriority(mThread, pri);
|
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
1999-04-06 06:09:15 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::AdjustPriority(int32_t aDelta)
|
1999-04-05 21:02:24 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
return SetPriority(mPriority + aDelta);
|
1999-04-05 21:02:24 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIThreadInternal
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::GetObserver(nsIThreadObserver** aObs)
|
1999-06-13 03:30:38 +00: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 04:29:02 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-27 07:15:35 +00:00
|
|
|
NS_IF_ADDREF(*aObs = mObserver);
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
1999-06-13 03:30:38 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::SetObserver(nsIThreadObserver* aObs)
|
1999-06-13 03:30:38 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-19 21:27:37 +00:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2006-05-10 17:30:15 +00: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 04:29:02 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-27 07:15:35 +00:00
|
|
|
mObserver = aObs;
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
1999-06-13 03:30:38 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 21:49:49 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::GetRecursionDepth(uint32_t* aDepth)
|
2010-11-15 21:49:49 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-19 21:27:37 +00:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2010-11-18 22:19:19 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
*aDepth = mRunningEvent;
|
2010-11-15 21:49:49 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-18 22:19:19 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::AddObserver(nsIThreadObserver* aObserver)
|
2010-11-18 22:19:19 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
if (NS_WARN_IF(!aObserver)) {
|
2013-11-19 21:27:37 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-19 21:27:37 +00:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2010-11-18 22:19:19 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
NS_WARN_IF_FALSE(!mEventObservers.Contains(aObserver),
|
2010-11-18 22:19:19 +00:00
|
|
|
"Adding an observer twice!");
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
if (!mEventObservers.AppendElement(aObserver)) {
|
2010-11-18 22:19:19 +00:00
|
|
|
NS_WARNING("Out of memory!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::RemoveObserver(nsIThreadObserver* aObserver)
|
2010-11-18 22:19:19 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-19 21:27:37 +00:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2010-11-18 22:19:19 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
if (aObserver && !mEventObservers.RemoveElement(aObserver)) {
|
2010-11-18 22:19:19 +00:00
|
|
|
NS_WARNING("Removing an observer that was never added!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-14 18:06:17 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::PushEventQueue(nsIEventTarget** aResult)
|
2013-11-14 18:06:17 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-14 18:06:17 +00:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2013-11-14 18:06:17 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
nsChainedEventQueue* queue = new nsChainedEventQueue();
|
2013-11-14 18:06:17 +00:00
|
|
|
queue->mEventTarget = new nsNestedEventTarget(this, queue);
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
queue->mNext = mEvents;
|
|
|
|
mEvents = queue;
|
|
|
|
}
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
NS_ADDREF(*aResult = queue->mEventTarget);
|
2013-11-14 18:06:17 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::PopEventQueue(nsIEventTarget* aInnermostTarget)
|
2013-11-14 18:06:17 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-14 18:06:17 +00:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2013-11-14 18:06:17 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
if (NS_WARN_IF(!aInnermostTarget)) {
|
2013-11-14 18:06:17 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2013-11-14 18:06:17 +00:00
|
|
|
|
|
|
|
// Don't delete or release anything while holding the lock.
|
|
|
|
nsAutoPtr<nsChainedEventQueue> queue;
|
|
|
|
nsRefPtr<nsNestedEventTarget> target;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
// Make sure we're popping the innermost event target.
|
2014-05-27 07:15:35 +00:00
|
|
|
if (NS_WARN_IF(mEvents->mEventTarget != aInnermostTarget)) {
|
2013-11-14 18:06:17 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2013-11-14 18:06:17 +00:00
|
|
|
|
|
|
|
MOZ_ASSERT(mEvents != &mEventsRoot);
|
|
|
|
|
|
|
|
queue = mEvents;
|
|
|
|
mEvents = mEvents->mNext;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
2014-05-27 07:15:35 +00:00
|
|
|
while (queue->GetEvent(false, getter_AddRefs(event))) {
|
2013-11-14 18:06:17 +00:00
|
|
|
mEvents->PutEvent(event);
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2013-11-14 18:06:17 +00:00
|
|
|
|
|
|
|
// Don't let the event target post any more events.
|
|
|
|
queue->mEventTarget.swap(target);
|
|
|
|
target->mQueue = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-23 22:26:36 +00:00
|
|
|
nsresult
|
|
|
|
nsThread::SetMainThreadObserver(nsIThreadObserver* aObserver)
|
|
|
|
{
|
|
|
|
if (aObserver && nsThread::sMainThreadObserver) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsThread::sMainThreadObserver = aObserver;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-12-18 09:00:39 +00:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
void
|
|
|
|
nsThread::SetWorking()
|
|
|
|
{
|
|
|
|
nsThreadManager::get()->SetThreadIsWorking(
|
|
|
|
static_cast<nsThreadManager::ThreadStatusInfo*>(mThreadStatusInfo),
|
|
|
|
true, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsThread::SetIdle()
|
|
|
|
{
|
|
|
|
nsThreadManager::get()->SetThreadIsWorking(
|
|
|
|
static_cast<nsThreadManager::ThreadStatusInfo*>(mThreadStatusInfo),
|
|
|
|
false, nullptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThreadSyncDispatch::Run()
|
|
|
|
{
|
|
|
|
if (mSyncTask) {
|
2010-11-17 20:58:48 +00:00
|
|
|
mResult = mSyncTask->Run();
|
2012-07-30 14:20:58 +00:00
|
|
|
mSyncTask = nullptr;
|
2006-05-10 17:30:15 +00:00
|
|
|
// unblock the origin thread
|
|
|
|
mOrigin->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-14 18:06:17 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsThread::nsNestedEventTarget, nsIEventTarget)
|
2013-11-14 18:06:17 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::nsNestedEventTarget::Dispatch(nsIRunnable* aEvent, uint32_t aFlags)
|
2013-11-14 18:06:17 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
LOG(("THRD(%p) Dispatch [%p %x] to nested loop %p\n", mThread.get(), aEvent,
|
|
|
|
aFlags, this));
|
2013-11-14 18:06:17 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
return mThread->DispatchInternal(aEvent, aFlags, this);
|
2013-11-14 18:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread::nsNestedEventTarget::IsOnCurrentThread(bool* aResult)
|
2013-11-14 18:06:17 +00:00
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
return mThread->IsOnCurrentThread(aResult);
|
2013-11-14 18:06:17 +00:00
|
|
|
}
|