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/. */
|
1999-04-02 13:20:44 +04:00
|
|
|
|
2013-10-23 16:01:24 +04:00
|
|
|
#include "nsThread.h"
|
|
|
|
|
2015-04-03 16:52:00 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
2013-10-23 16:01:24 +04:00
|
|
|
#include "base/message_loop.h"
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif // !defined(MOZILLA_XPCOMRT_API)
|
2013-10-23 16:01:24 +04:00
|
|
|
|
|
|
|
// Chromium's logging can sometimes leak through...
|
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
|
|
|
|
2015-05-19 09:31:25 +03:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2013-07-09 06:15:34 +04:00
|
|
|
#include "nsMemoryPressure.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadManager.h"
|
|
|
|
#include "nsIClassInfoImpl.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2013-07-12 00:21:45 +04:00
|
|
|
#include "pratom.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2011-08-06 02:10:50 +04:00
|
|
|
#include "nsIObserverService.h"
|
2015-04-03 16:52:00 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
2011-10-12 21:52:26 +04:00
|
|
|
#include "mozilla/HangMonitor.h"
|
2014-04-09 08:57:52 +04:00
|
|
|
#include "mozilla/IOInterposer.h"
|
2014-08-06 08:43:36 +04:00
|
|
|
#include "mozilla/ipc/MessageChannel.h"
|
2015-04-03 16:52:00 +03:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#endif // defined(MOZILLA_XPCOMRT_API)
|
2011-08-06 02:10:50 +04:00
|
|
|
#include "mozilla/Services.h"
|
2013-07-08 00:23:43 +04:00
|
|
|
#include "nsXPCOMPrivate.h"
|
2014-03-03 09:12:27 +04:00
|
|
|
#include "mozilla/ChaosMode.h"
|
2015-04-03 16:52:00 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2014-03-03 09:12:27 +04:00
|
|
|
|
2014-08-30 09:21:22 +04:00
|
|
|
#ifdef MOZ_CRASHREPORTER
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsICrashReporter.h"
|
|
|
|
#endif
|
|
|
|
|
2014-03-03 09:12:27 +04:00
|
|
|
#ifdef XP_LINUX
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#endif
|
1999-04-02 13:20:44 +04:00
|
|
|
|
2010-10-27 19:16:03 +04:00
|
|
|
#define HAVE_UALARM _BSD_SOURCE || (_XOPEN_SOURCE >= 500 || \
|
|
|
|
_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) && \
|
|
|
|
!(_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700)
|
|
|
|
|
2014-03-03 09:12:27 +04:00
|
|
|
#if defined(XP_LINUX) && !defined(ANDROID) && defined(_GNU_SOURCE)
|
|
|
|
#define HAVE_SCHED_SETAFFINITY
|
|
|
|
#endif
|
|
|
|
|
2013-10-30 00:58:09 +04:00
|
|
|
#ifdef MOZ_CANARY
|
2010-10-27 19:16:03 +04:00
|
|
|
# include <unistd.h>
|
|
|
|
# include <execinfo.h>
|
|
|
|
# include <signal.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
# include "nsXULAppAPI.h"
|
|
|
|
#endif
|
|
|
|
|
2010-05-20 03:22:19 +04:00
|
|
|
#if defined(NS_FUNCTION_TIMER) && defined(_MSC_VER)
|
|
|
|
#include "nsTimerImpl.h"
|
2015-06-10 23:32:45 +03:00
|
|
|
#include "mozilla/StackWalk.h"
|
2010-05-20 03:22:19 +04:00
|
|
|
#endif
|
|
|
|
#ifdef NS_FUNCTION_TIMER
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#endif
|
|
|
|
|
2014-03-27 12:49:06 +04:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
#include "GeckoTaskTracer.h"
|
2015-06-16 05:57:19 +03:00
|
|
|
#include "TracedTaskCommon.h"
|
2014-03-27 12:49:06 +04:00
|
|
|
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 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
static PRLogModuleInfo*
|
2012-10-30 03:32:10 +04:00
|
|
|
GetThreadLog()
|
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
static PRLogModuleInfo* sLog;
|
|
|
|
if (!sLog) {
|
2012-10-30 03:32:10 +04:00
|
|
|
sLog = PR_NewLogModule("nsThread");
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-10-30 03:32:10 +04:00
|
|
|
return sLog;
|
|
|
|
}
|
2013-11-19 06:34:00 +04:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
2015-06-04 01:25:57 +03:00
|
|
|
#define LOG(args) MOZ_LOG(GetThreadLog(), mozilla::LogLevel::Debug, args)
|
1999-04-02 13:20:44 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_DECL_CI_INTERFACE_GETTER(nsThread)
|
1999-04-02 13:20:44 +04:00
|
|
|
|
2012-10-24 02:26:36 +04:00
|
|
|
nsIThreadObserver* nsThread::sMainThreadObserver = nullptr;
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Because we do not have our own nsIFactory, we have to implement nsIClassInfo
|
|
|
|
// somewhat manually.
|
2000-11-30 08:24:53 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
class nsThreadClassInfo : public nsIClassInfo
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED // no mRefCnt
|
|
|
|
NS_DECL_NSICLASSINFO
|
2002-08-25 09:55:50 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadClassInfo()
|
|
|
|
{
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
1999-04-02 13:20:44 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsThreadClassInfo::AddRef()
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsThreadClassInfo::Release()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(nsThreadClassInfo, nsIClassInfo)
|
1999-04-06 01:02:24 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadClassInfo::GetInterfaces(uint32_t* aCount, nsIID*** aArray)
|
1999-04-06 01:02:24 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
return NS_CI_INTERFACE_GETTER_NAME(nsThread)(aCount, aArray);
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2015-03-29 17:52:54 +03:00
|
|
|
nsThreadClassInfo::GetScriptableHelper(nsIXPCScriptable** aResult)
|
1999-10-02 03:30:06 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-10-02 03:30:06 +04:00
|
|
|
}
|
|
|
|
|
1999-04-02 13:20:44 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadClassInfo::GetContractID(char** aResult)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadClassInfo::GetClassDescription(char** aResult)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadClassInfo::GetClassID(nsCID** aResult)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadClassInfo::GetFlags(uint32_t* aResult)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = THREADSAFE;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadClassInfo::GetClassIDNoAlloc(nsCID* aResult)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2013-07-19 06:31:26 +04:00
|
|
|
NS_IMPL_ADDREF(nsThread)
|
|
|
|
NS_IMPL_RELEASE(nsThread)
|
2006-05-10 21:30:15 +04: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-31 01:20:02 +04:00
|
|
|
static nsThreadClassInfo sThreadClassInfo;
|
2007-07-08 11:08:04 +04:00
|
|
|
foundInterface = static_cast<nsIClassInfo*>(&sThreadClassInfo);
|
2006-05-10 21:30:15 +04:00
|
|
|
} else
|
|
|
|
NS_INTERFACE_MAP_END
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsThread, nsIThread, nsIThreadInternal,
|
|
|
|
nsIEventTarget, nsISupportsPriority)
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
class nsThreadStartupEvent : public nsRunnable
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
public:
|
2013-11-20 01:27:37 +04:00
|
|
|
nsThreadStartupEvent()
|
|
|
|
: mMon("nsThreadStartupEvent.mMon")
|
2014-05-27 11:15:35 +04:00
|
|
|
, mInitialized(false)
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method does not return until the thread startup object is in the
|
|
|
|
// completion state.
|
2014-05-27 11:15:35 +04:00
|
|
|
void Wait()
|
|
|
|
{
|
|
|
|
if (mInitialized) {
|
|
|
|
// Maybe avoid locking...
|
2006-05-10 21:30:15 +04:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mMon);
|
2014-05-27 11:15:35 +04:00
|
|
|
while (!mInitialized) {
|
2006-05-10 21:30:15 +04:00
|
|
|
mon.Wait();
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 22:13:20 +04: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 23:17:15 +04:00
|
|
|
virtual ~nsThreadStartupEvent() {}
|
2006-05-10 22:13:20 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
private:
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mMon);
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitialized = true;
|
2006-05-10 21:30:15 +04:00
|
|
|
mon.Notify();
|
1999-04-02 13:20:44 +04:00
|
|
|
return NS_OK;
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitor mMon;
|
2014-05-27 11:15:35 +04:00
|
|
|
bool mInitialized;
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
struct nsThreadShutdownContext
|
|
|
|
{
|
|
|
|
nsThread* joiningThread;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool shutdownAck;
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// This event is responsible for notifying nsThread::Shutdown that it is time
|
|
|
|
// to call PR_JoinThread.
|
2014-05-27 11:15:35 +04:00
|
|
|
class nsThreadShutdownAckEvent : public nsRunnable
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
public:
|
2014-08-05 17:36:59 +04:00
|
|
|
explicit nsThreadShutdownAckEvent(nsThreadShutdownContext* aCtx)
|
2014-05-27 11:15:35 +04:00
|
|
|
: mShutdownContext(aCtx)
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mShutdownContext->shutdownAck = true;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadShutdownContext* mShutdownContext;
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// This event is responsible for setting mShutdownContext
|
2014-05-27 11:15:35 +04:00
|
|
|
class nsThreadShutdownEvent : public nsRunnable
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
public:
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadShutdownEvent(nsThread* aThr, nsThreadShutdownContext* aCtx)
|
|
|
|
: mThread(aThr)
|
|
|
|
, mShutdownContext(aCtx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
mThread->mShutdownContext = mShutdownContext;
|
2015-04-03 16:52:00 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
2013-10-23 16:01:24 +04:00
|
|
|
MessageLoop::current()->Quit();
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif // !defined(MOZILLA_XPCOMRT_API)
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsThread> mThread;
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThreadShutdownContext* mShutdownContext;
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-03-03 09:12:27 +04:00
|
|
|
static void
|
|
|
|
SetupCurrentThreadForChaosMode()
|
|
|
|
{
|
2014-11-14 21:12:51 +03:00
|
|
|
if (!ChaosMode::isActive(ChaosMode::ThreadScheduling)) {
|
2014-03-03 09:12:27 +04: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 11:15:35 +04:00
|
|
|
uint32_t priority = ChaosMode::randomUint32LessThan(PR_PRIORITY_LAST + 1);
|
|
|
|
PR_SetThreadPriority(PR_GetCurrentThread(), PRThreadPriority(priority));
|
2014-03-03 09:12:27 +04: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 21:30:15 +04:00
|
|
|
/*static*/ void
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::ThreadFunc(void* aArg)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2015-04-03 16:52:00 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
2014-11-21 01:16:36 +03:00
|
|
|
using mozilla::ipc::BackgroundChild;
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif // !defined(MOZILLA_XPCOMRT_API)
|
2014-11-21 01:16:36 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread* self = static_cast<nsThread*>(aArg); // strong reference
|
2006-05-10 21:30:15 +04:00
|
|
|
self->mThread = PR_GetCurrentThread();
|
2014-03-03 09:12:27 +04:00
|
|
|
SetupCurrentThreadForChaosMode();
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Inform the ThreadManager
|
|
|
|
nsThreadManager::get()->RegisterCurrentThread(self);
|
|
|
|
|
2015-04-03 16:52:00 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
2014-04-09 08:57:52 +04:00
|
|
|
mozilla::IOInterposer::RegisterCurrentThread();
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif // !defined(MOZILLA_XPCOMRT_API)
|
2014-04-09 08:57:52 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// Wait for and process startup event
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
2011-10-17 18:59:28 +04:00
|
|
|
if (!self->GetEvent(true, getter_AddRefs(event))) {
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_WARNING("failed waiting for thread startup event");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event->Run(); // unblocks nsThread::Init
|
2012-07-30 18:20:58 +04:00
|
|
|
event = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
2015-04-03 16:52:00 +03:00
|
|
|
#if defined(MOZILLA_XPCOMRT_API)
|
|
|
|
while(!self->mShutdownContext) {
|
|
|
|
NS_ProcessNextEvent();
|
|
|
|
}
|
|
|
|
#else
|
2014-05-27 11:15:35 +04:00
|
|
|
// Scope for MessageLoop.
|
2013-10-23 16:01:24 +04:00
|
|
|
nsAutoPtr<MessageLoop> loop(
|
|
|
|
new MessageLoop(MessageLoop::TYPE_MOZILLA_NONMAINTHREAD));
|
|
|
|
|
|
|
|
// Now, process incoming events...
|
|
|
|
loop->Run();
|
|
|
|
|
2014-11-21 01:16:36 +03:00
|
|
|
BackgroundChild::CloseForCurrentThread();
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif // defined(MOZILLA_XPCOMRT_API)
|
2014-11-21 01:16:36 +03:00
|
|
|
|
2013-10-23 16:01:24 +04: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 22:06:17 +04:00
|
|
|
if (!self->mEvents->HasPendingEvent()) {
|
2013-10-23 16:01:24 +04: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 05:17:15 +03:00
|
|
|
}
|
2013-10-23 16:01:24 +04:00
|
|
|
NS_ProcessPendingEvents(self);
|
2007-12-05 05:17:15 +03:00
|
|
|
}
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2015-04-03 16:52:00 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
2014-04-09 08:57:52 +04:00
|
|
|
mozilla::IOInterposer::UnregisterCurrentThread();
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif // !defined(MOZILLA_XPCOMRT_API)
|
2014-04-09 08:57:52 +04:00
|
|
|
|
2006-05-10 21:30:15 +04: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 17:08:04 +04:00
|
|
|
// Release any observer of the thread here.
|
2012-07-30 18:20:58 +04:00
|
|
|
self->SetObserver(nullptr);
|
2009-06-22 17:08:04 +04:00
|
|
|
|
2014-03-27 12:49:06 +04:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
FreeTraceInfo();
|
|
|
|
#endif
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_RELEASE(self);
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-08-30 09:21:22 +04: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-30 00:58:09 +04:00
|
|
|
#ifdef MOZ_CANARY
|
|
|
|
int sCanaryOutputFD = -1;
|
|
|
|
#endif
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsThread::nsThread(MainThreadFlag aMainThread, uint32_t aStackSize)
|
2011-07-27 07:26:47 +04:00
|
|
|
: mLock("nsThread.mLock")
|
2013-11-14 22:06:17 +04:00
|
|
|
, mEvents(&mEventsRoot)
|
2011-07-27 07:26:47 +04:00
|
|
|
, mPriority(PRIORITY_NORMAL)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mThread(nullptr)
|
2015-04-24 23:04:50 +03:00
|
|
|
, mNestedEventLoopDepth(0)
|
2011-07-27 07:26:47 +04:00
|
|
|
, mStackSize(aStackSize)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mShutdownContext(nullptr)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mShutdownRequired(false)
|
|
|
|
, mEventsAreDoomed(false)
|
2011-10-12 21:52:26 +04:00
|
|
|
, mIsMainThread(aMainThread)
|
1999-04-06 01:02:24 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsThread::~nsThread()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsThread::Init()
|
1999-10-02 03:30:06 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
// spawn thread and wait until it is fully setup
|
2013-11-20 01:27:37 +04:00
|
|
|
nsRefPtr<nsThreadStartupEvent> startup = new nsThreadStartupEvent();
|
2014-05-27 11:15:35 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_ADDREF_THIS();
|
2014-05-27 11:15:35 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mShutdownRequired = true;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// ThreadFunc is responsible for setting mThread
|
2014-05-27 11:15:35 +04:00
|
|
|
PRThread* thr = PR_CreateThread(PR_USER_THREAD, ThreadFunc, this,
|
2006-05-10 21:30:15 +04:00
|
|
|
PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
|
2011-07-27 07:26:47 +04:00
|
|
|
PR_JOINABLE_THREAD, mStackSize);
|
2006-05-10 21:30:15 +04: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 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2013-11-14 22:06:17 +04:00
|
|
|
mEventsRoot.PutEvent(startup);
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for thread to call ThreadManager::SetupCurrentThread, which completes
|
|
|
|
// initialization of ThreadFunc.
|
|
|
|
startup->Wait();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-06-11 23:26:04 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsresult
|
|
|
|
nsThread::InitCurrentThread()
|
|
|
|
{
|
|
|
|
mThread = PR_GetCurrentThread();
|
2014-03-03 09:12:27 +04:00
|
|
|
SetupCurrentThreadForChaosMode();
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
nsThreadManager::get()->RegisterCurrentThread(this);
|
|
|
|
return NS_OK;
|
1999-10-02 03:30:06 +04:00
|
|
|
}
|
|
|
|
|
2007-12-05 05:17:15 +03:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::PutEvent(nsIRunnable* aEvent, nsNestedEventTarget* aTarget)
|
2000-11-30 08:24:53 +03:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
nsCOMPtr<nsIThreadObserver> obs;
|
|
|
|
|
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);
|
2014-05-27 11:15:35 +04:00
|
|
|
nsChainedEventQueue* queue = aTarget ? aTarget->mQueue : &mEventsRoot;
|
2013-11-14 22:06:17 +04:00
|
|
|
if (!queue || (queue == &mEventsRoot && mEventsAreDoomed)) {
|
2007-12-05 05:17:15 +03:00
|
|
|
NS_WARNING("An event was posted to a thread that will never run it (rejected)");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2015-05-19 09:31:25 +03:00
|
|
|
queue->PutEvent(aEvent);
|
2014-09-27 03:21:57 +04: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 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (obs) {
|
2006-05-10 21:30:15 +04:00
|
|
|
obs->OnDispatchedEvent(this);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2007-12-05 05:17:15 +03:00
|
|
|
return NS_OK;
|
2000-11-30 08:24:53 +03:00
|
|
|
}
|
|
|
|
|
2013-11-14 22:06:17 +04:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::DispatchInternal(nsIRunnable* aEvent, uint32_t aFlags,
|
|
|
|
nsNestedEventTarget* aTarget)
|
2000-11-30 08:24:53 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!aEvent)) {
|
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
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (gXPCOMThreadsShutDown && MAIN_THREAD != mIsMainThread && !aTarget) {
|
2013-07-08 00:23:43 +04:00
|
|
|
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
|
|
|
|
}
|
|
|
|
|
2014-03-27 12:49:06 +04:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
2015-03-17 19:29:17 +03:00
|
|
|
nsCOMPtr<nsIRunnable> tracedRunnable = CreateTracedRunnable(aEvent);
|
2015-06-16 05:57:19 +03:00
|
|
|
(static_cast<TracedRunnable*>(tracedRunnable.get()))->DispatchTask();
|
2014-05-27 11:15:35 +04:00
|
|
|
aEvent = tracedRunnable;
|
2014-03-27 12:49:06 +04:00
|
|
|
#endif
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aFlags & DISPATCH_SYNC) {
|
|
|
|
nsThread* thread = nsThreadManager::get()->GetCurrentThread();
|
|
|
|
if (NS_WARN_IF(!thread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04: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 12:49:06 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsRefPtr<nsThreadSyncDispatch> wrapper =
|
2014-05-27 11:15:35 +04:00
|
|
|
new nsThreadSyncDispatch(thread, aEvent);
|
|
|
|
nsresult rv = PutEvent(wrapper, aTarget);
|
2007-12-05 05:17:15 +03:00
|
|
|
// Don't wait for the event to finish if we didn't dispatch it...
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2007-12-05 05:17:15 +03:00
|
|
|
return rv;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-04-22 23:34:24 +04:00
|
|
|
// Allows waiting; ensure no locks are held that would deadlock us!
|
2014-05-27 11:15:35 +04:00
|
|
|
while (wrapper->IsPending()) {
|
2014-04-22 23:34:24 +04:00
|
|
|
NS_ProcessNextEvent(thread, true);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2010-11-17 23:58:48 +03:00
|
|
|
return wrapper->Result();
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_ASSERTION(aFlags == NS_DISPATCH_NORMAL, "unexpected dispatch flags");
|
|
|
|
return PutEvent(aEvent, aTarget);
|
2013-11-14 22:06:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIEventTarget
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::Dispatch(nsIRunnable* aEvent, uint32_t aFlags)
|
2013-11-14 22:06:17 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
LOG(("THRD(%p) Dispatch [%p %x]\n", this, aEvent, aFlags));
|
2013-11-14 22:06:17 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
return DispatchInternal(aEvent, aFlags, nullptr);
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
2000-11-30 08:24:53 +03:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::IsOnCurrentThread(bool* aResult)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = (PR_GetCurrentThread() == mThread);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
2000-11-30 08:24:53 +03:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIThread
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::GetPRThread(PRThread** aResult)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = mThread;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::Shutdown()
|
1999-10-02 03:30:06 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04: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 11:15:35 +04:00
|
|
|
if (!mThread) {
|
1999-10-02 03:30:06 +04:00
|
|
|
return NS_OK;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(mThread == PR_GetCurrentThread())) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04: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 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!mShutdownRequired) {
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mShutdownRequired = false;
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsThreadShutdownContext context;
|
|
|
|
context.joiningThread = nsThreadManager::get()->GetCurrentThread();
|
2011-10-17 18:59:28 +04:00
|
|
|
context.shutdownAck = false;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Set mShutdownContext and wake up the thread in case it is waiting for
|
|
|
|
// events to process.
|
|
|
|
nsCOMPtr<nsIRunnable> event = new nsThreadShutdownEvent(this, &context);
|
2007-12-05 05:17:15 +03:00
|
|
|
// XXXroc What if posting the event fails due to OOM?
|
2013-11-14 22:06:17 +04:00
|
|
|
PutEvent(event, nullptr);
|
2006-05-10 21:30:15 +04: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 11:15:35 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// Process events on the current thread until we receive a shutdown ACK.
|
2014-04-22 23:34:24 +04:00
|
|
|
// Allows waiting; ensure no locks are held that would deadlock us!
|
2014-05-27 11:15:35 +04:00
|
|
|
while (!context.shutdownAck) {
|
2014-04-22 23:34:24 +04:00
|
|
|
NS_ProcessNextEvent(context.joiningThread, true);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Now, it should be safe to join without fear of dead-locking.
|
|
|
|
|
|
|
|
PR_JoinThread(mThread);
|
2012-07-30 18:20:58 +04:00
|
|
|
mThread = nullptr;
|
2009-06-22 17:08:04 +04:00
|
|
|
|
2012-01-14 22:31:13 +04: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 17:08:04 +04: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 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(!mObserver, "Should have been cleared at shutdown!");
|
2009-06-22 17:08:04 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-10-02 03:30:06 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::HasPendingEvents(bool* aResult)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
1999-04-02 13:20:44 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = mEvents->GetEvent(false, nullptr);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-27 19:16:03 +04:00
|
|
|
#ifdef MOZ_CANARY
|
2014-05-27 11:15:35 +04:00
|
|
|
void canary_alarm_handler(int signum);
|
2010-10-27 19:16:03 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
class Canary
|
|
|
|
{
|
|
|
|
//XXX ToDo: support nested loops
|
2010-10-27 19:16:03 +04:00
|
|
|
public:
|
2014-05-27 11:15:35 +04:00
|
|
|
Canary()
|
|
|
|
{
|
2013-10-30 00:58:09 +04:00
|
|
|
if (sCanaryOutputFD > 0 && EventLatencyIsImportant()) {
|
2010-10-27 19:16:03 +04:00
|
|
|
signal(SIGALRM, canary_alarm_handler);
|
2014-05-27 11:15:35 +04:00
|
|
|
ualarm(15000, 0);
|
2010-10-27 19:16:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
~Canary()
|
|
|
|
{
|
|
|
|
if (sCanaryOutputFD != 0 && EventLatencyIsImportant()) {
|
2010-10-27 19:16:03 +04:00
|
|
|
ualarm(0, 0);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2010-10-27 19:16:03 +04:00
|
|
|
}
|
2011-01-06 01:11:56 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
static bool EventLatencyIsImportant()
|
|
|
|
{
|
2015-07-04 04:29:00 +03:00
|
|
|
return NS_IsMainThread() && XRE_IsParentProcess();
|
2011-01-06 01:11:56 +03:00
|
|
|
}
|
2010-10-27 19:16:03 +04:00
|
|
|
};
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void canary_alarm_handler(int signum)
|
2010-10-27 19:16:03 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
void* array[30];
|
2010-10-27 19:16:03 +04:00
|
|
|
const char msg[29] = "event took too long to run:\n";
|
|
|
|
// use write to be safe in the signal handler
|
2014-05-27 11:15:35 +04:00
|
|
|
write(sCanaryOutputFD, msg, sizeof(msg));
|
2013-10-30 00:58:09 +04:00
|
|
|
backtrace_symbols_fd(array, backtrace(array, 30), sCanaryOutputFD);
|
2010-10-27 19:16:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-11-19 01:19:19 +03: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 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::ProcessNextEvent(bool aMayWait, bool* aResult)
|
1999-04-06 01:02:24 +04:00
|
|
|
{
|
2015-04-24 23:04:50 +03:00
|
|
|
LOG(("THRD(%p) ProcessNextEvent [%u %u]\n", this, aMayWait,
|
|
|
|
mNestedEventLoopDepth));
|
1999-04-06 01:02:24 +04:00
|
|
|
|
2015-04-03 16:52:00 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
2014-08-06 08:43:36 +04:00
|
|
|
// If we're on the main thread, we shouldn't be dispatching CPOWs.
|
2015-06-24 03:52:01 +03:00
|
|
|
if (mIsMainThread == MAIN_THREAD) {
|
|
|
|
ipc::CancelCPOWs();
|
|
|
|
}
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif // !defined(MOZILLA_XPCOMRT_API)
|
2014-08-06 08:43:36 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
1999-04-06 01:02:24 +04:00
|
|
|
|
2014-02-01 05:14:00 +04: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.
|
2015-04-24 23:04:50 +03:00
|
|
|
bool reallyWait = aMayWait && (mNestedEventLoopDepth > 0 || !ShuttingDown());
|
2014-02-01 04:42:24 +04:00
|
|
|
|
2015-04-03 16:52:00 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
2014-05-27 11:15:35 +04:00
|
|
|
if (MAIN_THREAD == mIsMainThread && reallyWait) {
|
2011-10-12 21:52:26 +04:00
|
|
|
HangMonitor::Suspend();
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif // !defined(MOZILLA_XPCOMRT_API)
|
2011-10-12 21:52:26 +04:00
|
|
|
|
2011-08-06 02:10:50 +04:00
|
|
|
// Fire a memory pressure notification, if we're the main thread and one is
|
|
|
|
// pending.
|
|
|
|
if (MAIN_THREAD == mIsMainThread && !ShuttingDown()) {
|
2013-07-09 06:15:34 +04:00
|
|
|
MemoryPressureState mpPending = NS_GetPendingMemoryPressure();
|
|
|
|
if (mpPending != MemPressure_None) {
|
2011-08-06 02:10:50 +04:00
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
2013-07-09 06:15:34 +04: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-06 02:10:50 +04:00
|
|
|
if (os) {
|
2012-07-30 18:20:58 +04:00
|
|
|
os->NotifyObservers(nullptr, "memory-pressure",
|
2013-07-09 06:15:34 +04:00
|
|
|
mpPending == MemPressure_New ? lowMem.get() :
|
2014-05-27 11:15:35 +04:00
|
|
|
lowMemOngoing.get());
|
2013-07-09 06:15:34 +04:00
|
|
|
} else {
|
2011-08-06 02:10:50 +04:00
|
|
|
NS_WARNING("Can't get observer service!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 09:21:22 +04: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-24 02:26:36 +04:00
|
|
|
bool notifyMainThreadObserver =
|
|
|
|
(MAIN_THREAD == mIsMainThread) && sMainThreadObserver;
|
2014-05-27 11:15:35 +04:00
|
|
|
if (notifyMainThreadObserver) {
|
2015-04-24 23:04:50 +03:00
|
|
|
sMainThreadObserver->OnProcessNextEvent(this, reallyWait,
|
|
|
|
mNestedEventLoopDepth);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-10-24 02:26:36 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsCOMPtr<nsIThreadObserver> obs = mObserver;
|
2014-05-27 11:15:35 +04:00
|
|
|
if (obs) {
|
2015-04-24 23:04:50 +03:00
|
|
|
obs->OnProcessNextEvent(this, reallyWait, mNestedEventLoopDepth);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
1999-04-06 01:02:24 +04:00
|
|
|
|
2010-11-19 01:19:19 +03:00
|
|
|
NOTIFY_EVENT_OBSERVERS(OnProcessNextEvent,
|
2015-04-24 23:04:50 +03:00
|
|
|
(this, reallyWait, mNestedEventLoopDepth));
|
2010-11-19 01:19:19 +03:00
|
|
|
|
2015-04-24 23:04:50 +03:00
|
|
|
++mNestedEventLoopDepth;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2010-10-27 19:16:03 +04:00
|
|
|
#ifdef MOZ_CANARY
|
|
|
|
Canary canary;
|
|
|
|
#endif
|
2006-05-10 21:30:15 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2009-06-11 21:09:35 +04:00
|
|
|
{
|
|
|
|
// Scope for |event| to make sure that its destructor fires while
|
2015-04-24 23:04:50 +03:00
|
|
|
// mNestedEventLoopDepth has been incremented, since that destructor can
|
2009-06-11 21:09:35 +04:00
|
|
|
// also do work.
|
|
|
|
|
|
|
|
// If we are shutting down, then do not wait for new events.
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
2014-02-01 04:42:24 +04:00
|
|
|
mEvents->GetEvent(reallyWait, getter_AddRefs(event));
|
2009-06-11 21:09:35 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
*aResult = (event.get() != nullptr);
|
2009-06-11 21:09:35 +04:00
|
|
|
|
|
|
|
if (event) {
|
|
|
|
LOG(("THRD(%p) running [%p]\n", this, event.get()));
|
2015-04-03 16:52:00 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
2014-05-27 11:15:35 +04:00
|
|
|
if (MAIN_THREAD == mIsMainThread) {
|
2011-10-12 21:52:26 +04:00
|
|
|
HangMonitor::NotifyActivity();
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif // !defined(MOZILLA_XPCOMRT_API)
|
2009-06-11 21:09:35 +04:00
|
|
|
event->Run();
|
2014-05-27 11:15:35 +04:00
|
|
|
} else if (aMayWait) {
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(ShuttingDown(),
|
|
|
|
"This should only happen when shutting down");
|
2009-06-11 21:09:35 +04:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2015-04-24 23:04:50 +03:00
|
|
|
--mNestedEventLoopDepth;
|
2010-11-19 01:19:19 +03:00
|
|
|
|
2013-10-23 16:01:20 +04:00
|
|
|
NOTIFY_EVENT_OBSERVERS(AfterProcessNextEvent,
|
2015-04-24 23:04:50 +03:00
|
|
|
(this, mNestedEventLoopDepth, *aResult));
|
2010-11-19 01:19:19 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (obs) {
|
2015-04-24 23:04:50 +03:00
|
|
|
obs->AfterProcessNextEvent(this, mNestedEventLoopDepth, *aResult);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2006-06-07 04:06:11 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (notifyMainThreadObserver && sMainThreadObserver) {
|
2015-04-24 23:04:50 +03:00
|
|
|
sMainThreadObserver->AfterProcessNextEvent(this, mNestedEventLoopDepth,
|
|
|
|
*aResult);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-10-24 02:26:36 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
return rv;
|
1999-04-06 01:02:24 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsISupportsPriority
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::GetPriority(int32_t* aPriority)
|
2000-11-30 08:24:53 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
*aPriority = mPriority;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
2000-11-30 08:24:53 +03:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::SetPriority(int32_t aPriority)
|
1999-04-06 10:09:15 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!mThread)) {
|
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
|
|
|
|
|
|
|
// 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 11:15:35 +04:00
|
|
|
mPriority = aPriority;
|
2006-05-10 21:30:15 +04: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 09:12:27 +04:00
|
|
|
// If chaos mode is active, retain the randomly chosen priority
|
2014-11-14 21:12:51 +03:00
|
|
|
if (!ChaosMode::isActive(ChaosMode::ThreadScheduling)) {
|
2014-03-03 09:12:27 +04:00
|
|
|
PR_SetThreadPriority(mThread, pri);
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
1999-04-06 10:09:15 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::AdjustPriority(int32_t aDelta)
|
1999-04-06 01:02:24 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
return SetPriority(mPriority + aDelta);
|
1999-04-06 01:02:24 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIThreadInternal
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::GetObserver(nsIThreadObserver** aObs)
|
1999-06-13 07:30:38 +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);
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IF_ADDREF(*aObs = mObserver);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-06-13 07:30:38 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::SetObserver(nsIThreadObserver* aObs)
|
1999-06-13 07:30:38 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
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
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-27 11:15:35 +04:00
|
|
|
mObserver = aObs;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-06-13 07:30:38 +04:00
|
|
|
}
|
|
|
|
|
2010-11-16 00:49:49 +03:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::GetRecursionDepth(uint32_t* aDepth)
|
2010-11-16 00:49:49 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2010-11-19 01:19:19 +03:00
|
|
|
|
2015-04-24 23:04:50 +03:00
|
|
|
*aDepth = mNestedEventLoopDepth;
|
2010-11-16 00:49:49 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-19 01:19:19 +03:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::AddObserver(nsIThreadObserver* aObserver)
|
2010-11-19 01:19:19 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!aObserver)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2010-11-19 01:19:19 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_WARN_IF_FALSE(!mEventObservers.Contains(aObserver),
|
2010-11-19 01:19:19 +03:00
|
|
|
"Adding an observer twice!");
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!mEventObservers.AppendElement(aObserver)) {
|
2010-11-19 01:19:19 +03:00
|
|
|
NS_WARNING("Out of memory!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::RemoveObserver(nsIThreadObserver* aObserver)
|
2010-11-19 01:19:19 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2010-11-19 01:19:19 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aObserver && !mEventObservers.RemoveElement(aObserver)) {
|
2010-11-19 01:19:19 +03:00
|
|
|
NS_WARNING("Removing an observer that was never added!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-14 22:06:17 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::PushEventQueue(nsIEventTarget** aResult)
|
2013-11-14 22:06:17 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-14 22:06:17 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2013-11-14 22:06:17 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
nsChainedEventQueue* queue = new nsChainedEventQueue();
|
2013-11-14 22:06:17 +04:00
|
|
|
queue->mEventTarget = new nsNestedEventTarget(this, queue);
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
queue->mNext = mEvents;
|
|
|
|
mEvents = queue;
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_ADDREF(*aResult = queue->mEventTarget);
|
2013-11-14 22:06:17 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::PopEventQueue(nsIEventTarget* aInnermostTarget)
|
2013-11-14 22:06:17 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread)) {
|
2013-11-14 22:06:17 +04:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2013-11-14 22:06:17 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!aInnermostTarget)) {
|
2013-11-14 22:06:17 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2013-11-14 22:06:17 +04: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 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(mEvents->mEventTarget != aInnermostTarget)) {
|
2013-11-14 22:06:17 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2013-11-14 22:06:17 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mEvents != &mEventsRoot);
|
|
|
|
|
|
|
|
queue = mEvents;
|
|
|
|
mEvents = mEvents->mNext;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
2014-05-27 11:15:35 +04:00
|
|
|
while (queue->GetEvent(false, getter_AddRefs(event))) {
|
2013-11-14 22:06:17 +04:00
|
|
|
mEvents->PutEvent(event);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2013-11-14 22:06:17 +04:00
|
|
|
|
|
|
|
// Don't let the event target post any more events.
|
|
|
|
queue->mEventTarget.swap(target);
|
|
|
|
target->mQueue = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-24 02:26:36 +04: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;
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThreadSyncDispatch::Run()
|
|
|
|
{
|
|
|
|
if (mSyncTask) {
|
2010-11-17 23:58:48 +03:00
|
|
|
mResult = mSyncTask->Run();
|
2012-07-30 18:20:58 +04:00
|
|
|
mSyncTask = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
// unblock the origin thread
|
|
|
|
mOrigin->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-14 22:06:17 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsThread::nsNestedEventTarget, nsIEventTarget)
|
2013-11-14 22:06:17 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::nsNestedEventTarget::Dispatch(nsIRunnable* aEvent, uint32_t aFlags)
|
2013-11-14 22:06:17 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
LOG(("THRD(%p) Dispatch [%p %x] to nested loop %p\n", mThread.get(), aEvent,
|
|
|
|
aFlags, this));
|
2013-11-14 22:06:17 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
return mThread->DispatchInternal(aEvent, aFlags, this);
|
2013-11-14 22:06:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsThread::nsNestedEventTarget::IsOnCurrentThread(bool* aResult)
|
2013-11-14 22:06:17 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
return mThread->IsOnCurrentThread(aResult);
|
2013-11-14 22:06:17 +04:00
|
|
|
}
|