2006-05-10 21:30:15 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
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"
|
|
|
|
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
|
|
|
|
// Chromium's logging can sometimes leak through...
|
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
|
|
|
|
2011-04-29 23:21:57 +04: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 "nsIProgrammingLanguage.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2013-07-12 00:21:45 +04:00
|
|
|
#include "pratom.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "prlog.h"
|
2011-08-06 02:10:50 +04:00
|
|
|
#include "nsIObserverService.h"
|
2011-10-12 21:52:26 +04:00
|
|
|
#include "mozilla/HangMonitor.h"
|
2014-04-09 08:57:52 +04:00
|
|
|
#include "mozilla/IOInterposer.h"
|
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"
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "nsStackWalk.h"
|
|
|
|
#endif
|
|
|
|
#ifdef NS_FUNCTION_TIMER
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#endif
|
|
|
|
|
2014-03-27 12:49:06 +04: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 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
#ifdef PR_LOGGING
|
2012-10-30 03:32:10 +04:00
|
|
|
static PRLogModuleInfo *
|
|
|
|
GetThreadLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("nsThread");
|
|
|
|
return sLog;
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
#endif
|
2013-11-19 06:34:00 +04:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
2012-10-30 03:32:10 +04:00
|
|
|
#define LOG(args) PR_LOG(GetThreadLog(), PR_LOG_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
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
class nsThreadClassInfo : public nsIClassInfo {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED // no mRefCnt
|
|
|
|
NS_DECL_NSICLASSINFO
|
2002-08-25 09:55:50 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsThreadClassInfo() {}
|
|
|
|
};
|
1999-04-02 13:20:44 +04:00
|
|
|
|
2014-03-28 00:38:33 +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
|
2012-08-22 19:56:38 +04:00
|
|
|
nsThreadClassInfo::GetInterfaces(uint32_t *count, nsIID ***array)
|
1999-04-06 01:02:24 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_CI_INTERFACE_GETTER_NAME(nsThread)(count, array);
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsThreadClassInfo::GetHelperForLanguage(uint32_t lang, nsISupports **result)
|
1999-10-02 03:30:06 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*result = 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
|
2006-05-10 21:30:15 +04:00
|
|
|
nsThreadClassInfo::GetContractID(char **result)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*result = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2006-05-10 21:30:15 +04:00
|
|
|
nsThreadClassInfo::GetClassDescription(char **result)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*result = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2006-05-10 21:30:15 +04:00
|
|
|
nsThreadClassInfo::GetClassID(nsCID **result)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*result = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsThreadClassInfo::GetImplementationLanguage(uint32_t *result)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
*result = nsIProgrammingLanguage::CPLUSPLUS;
|
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsThreadClassInfo::GetFlags(uint32_t *result)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
*result = THREADSAFE;
|
|
|
|
return NS_OK;
|
1999-04-02 13:20:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2006-05-10 21:30:15 +04:00
|
|
|
nsThreadClassInfo::GetClassIDNoAlloc(nsCID *result)
|
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
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsThreadStartupEvent : public nsRunnable {
|
|
|
|
public:
|
2013-11-20 01:27:37 +04:00
|
|
|
nsThreadStartupEvent()
|
|
|
|
: mMon("nsThreadStartupEvent.mMon")
|
|
|
|
, 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.
|
|
|
|
void Wait() {
|
|
|
|
if (mInitialized) // Maybe avoid locking...
|
|
|
|
return;
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mMon);
|
2006-05-10 21:30:15 +04:00
|
|
|
while (!mInitialized)
|
|
|
|
mon.Wait();
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
virtual ~nsThreadStartupEvent() {
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
private:
|
|
|
|
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;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mInitialized;
|
2006-05-10 21:30:15 +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.
|
|
|
|
class nsThreadShutdownAckEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
nsThreadShutdownAckEvent(nsThreadShutdownContext *ctx)
|
|
|
|
: mShutdownContext(ctx) {
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
nsThreadShutdownContext *mShutdownContext;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This event is responsible for setting mShutdownContext
|
|
|
|
class nsThreadShutdownEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
nsThreadShutdownEvent(nsThread *thr, nsThreadShutdownContext *ctx)
|
|
|
|
: mThread(thr), mShutdownContext(ctx) {
|
|
|
|
}
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
mThread->mShutdownContext = mShutdownContext;
|
2013-10-23 16:01:24 +04:00
|
|
|
MessageLoop::current()->Quit();
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsThread> mThread;
|
|
|
|
nsThreadShutdownContext *mShutdownContext;
|
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-03-03 09:12:27 +04:00
|
|
|
static void
|
|
|
|
SetupCurrentThreadForChaosMode()
|
|
|
|
{
|
|
|
|
if (!ChaosMode::isActive()) {
|
|
|
|
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.
|
|
|
|
PR_SetThreadPriority(PR_GetCurrentThread(),
|
|
|
|
PRThreadPriority(ChaosMode::randomUint32LessThan(PR_PRIORITY_LAST + 1)));
|
|
|
|
#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
|
|
|
|
nsThread::ThreadFunc(void *arg)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsThread *self = static_cast<nsThread *>(arg); // 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);
|
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
mozilla::IOInterposer::RegisterCurrentThread();
|
|
|
|
|
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
|
|
|
|
2013-10-23 16:01:24 +04:00
|
|
|
{ // Scope for MessageLoop.
|
|
|
|
nsAutoPtr<MessageLoop> loop(
|
|
|
|
new MessageLoop(MessageLoop::TYPE_MOZILLA_NONMAINTHREAD));
|
|
|
|
|
|
|
|
// Now, process incoming events...
|
|
|
|
loop->Run();
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2014-04-09 08:57:52 +04:00
|
|
|
mozilla::IOInterposer::UnregisterCurrentThread();
|
|
|
|
|
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
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
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)
|
2011-07-27 07:26:47 +04:00
|
|
|
, mRunningEvent(0)
|
|
|
|
, 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();
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mShutdownRequired = true;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// ThreadFunc is responsible for setting mThread
|
|
|
|
PRThread *thr = PR_CreateThread(PR_USER_THREAD, ThreadFunc, this,
|
|
|
|
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
|
2013-11-14 22:06:17 +04:00
|
|
|
nsThread::PutEvent(nsIRunnable *event, nsNestedEventTarget *target)
|
2000-11-30 08:24:53 +03: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);
|
2013-11-14 22:06:17 +04:00
|
|
|
nsChainedEventQueue *queue = target ? target->mQueue : &mEventsRoot;
|
|
|
|
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;
|
|
|
|
}
|
2013-11-14 22:06:17 +04:00
|
|
|
if (!queue->PutEvent(event))
|
2007-12-05 05:17:15 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThreadObserver> obs = GetObserver();
|
|
|
|
if (obs)
|
|
|
|
obs->OnDispatchedEvent(this);
|
|
|
|
|
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
|
|
|
|
nsThread::DispatchInternal(nsIRunnable *event, uint32_t flags,
|
|
|
|
nsNestedEventTarget *target)
|
2000-11-30 08:24:53 +03:00
|
|
|
{
|
2013-11-20 01:27:37 +04:00
|
|
|
if (NS_WARN_IF(!event))
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2013-11-14 22:06:17 +04:00
|
|
|
if (gXPCOMThreadsShutDown && MAIN_THREAD != mIsMainThread && !target) {
|
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
|
|
|
|
nsRefPtr<nsIRunnable> tracedRunnable = CreateTracedRunnable(event);
|
|
|
|
event = tracedRunnable;
|
|
|
|
#endif
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
if (flags & DISPATCH_SYNC) {
|
|
|
|
nsThread *thread = nsThreadManager::get()->GetCurrentThread();
|
2013-11-20 01:27:37 +04:00
|
|
|
if (NS_WARN_IF(!thread))
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
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 =
|
|
|
|
new nsThreadSyncDispatch(thread, event);
|
|
|
|
if (!wrapper)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2013-11-14 22:06:17 +04:00
|
|
|
nsresult rv = PutEvent(wrapper, target);
|
2007-12-05 05:17:15 +03:00
|
|
|
// Don't wait for the event to finish if we didn't dispatch it...
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
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!
|
2006-05-10 21:30:15 +04:00
|
|
|
while (wrapper->IsPending())
|
2014-04-22 23:34:24 +04:00
|
|
|
NS_ProcessNextEvent(thread, true);
|
2010-11-17 23:58:48 +03:00
|
|
|
return wrapper->Result();
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2007-12-05 05:17:15 +03:00
|
|
|
NS_ASSERTION(flags == NS_DISPATCH_NORMAL, "unexpected dispatch flags");
|
2013-11-14 22:06:17 +04:00
|
|
|
return PutEvent(event, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIEventTarget
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::Dispatch(nsIRunnable *event, uint32_t flags)
|
|
|
|
{
|
|
|
|
LOG(("THRD(%p) Dispatch [%p %x]\n", this, event, flags));
|
|
|
|
|
|
|
|
return DispatchInternal(event, flags, 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
|
2011-09-29 10:19:26 +04:00
|
|
|
nsThread::IsOnCurrentThread(bool *result)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
|
|
|
*result = (PR_GetCurrentThread() == mThread);
|
|
|
|
return NS_OK;
|
2000-11-30 08:24:53 +03:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIThread
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::GetPRThread(PRThread **result)
|
1999-04-02 13:20:44 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
*result = mThread;
|
|
|
|
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.
|
|
|
|
if (!mThread)
|
1999-10-02 03:30:06 +04:00
|
|
|
return NS_OK;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2013-11-20 01:27:37 +04:00
|
|
|
if (NS_WARN_IF(mThread == PR_GetCurrentThread()))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
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);
|
2006-05-10 21:30:15 +04:00
|
|
|
if (!mShutdownRequired)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
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);
|
|
|
|
if (!event)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
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.
|
|
|
|
|
|
|
|
// 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!
|
2006-05-10 21:30:15 +04:00
|
|
|
while (!context.shutdownAck)
|
2014-04-22 23:34:24 +04:00
|
|
|
NS_ProcessNextEvent(context.joiningThread, true);
|
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
|
2011-09-29 10:19:26 +04:00
|
|
|
nsThread::HasPendingEvents(bool *result)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2013-11-20 01:27:37 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread))
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
1999-04-02 13:20:44 +04:00
|
|
|
|
2013-11-14 22:06:17 +04:00
|
|
|
*result = 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
|
|
|
|
void canary_alarm_handler (int signum);
|
|
|
|
|
|
|
|
class Canary {
|
|
|
|
//XXX ToDo: support nested loops
|
|
|
|
public:
|
|
|
|
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);
|
|
|
|
ualarm(15000, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~Canary() {
|
2013-10-30 00:58:09 +04:00
|
|
|
if (sCanaryOutputFD != 0 && EventLatencyIsImportant())
|
2010-10-27 19:16:03 +04:00
|
|
|
ualarm(0, 0);
|
|
|
|
}
|
2011-01-06 01:11:56 +03:00
|
|
|
|
|
|
|
static bool EventLatencyIsImportant() {
|
2011-04-03 06:14:00 +04:00
|
|
|
return NS_IsMainThread() && XRE_GetProcessType() == GeckoProcessType_Default;
|
2011-01-06 01:11:56 +03:00
|
|
|
}
|
2010-10-27 19:16:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
void canary_alarm_handler (int signum)
|
|
|
|
{
|
|
|
|
void *array[30];
|
|
|
|
const char msg[29] = "event took too long to run:\n";
|
|
|
|
// use write to be safe in the signal handler
|
2013-10-30 00:58:09 +04:00
|
|
|
write(sCanaryOutputFD, msg, sizeof(msg));
|
|
|
|
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
|
2011-09-29 10:19:26 +04:00
|
|
|
nsThread::ProcessNextEvent(bool mayWait, bool *result)
|
1999-04-06 01:02:24 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
LOG(("THRD(%p) ProcessNextEvent [%u %u]\n", this, mayWait, mRunningEvent));
|
1999-04-06 01:02:24 +04:00
|
|
|
|
2013-11-20 01:27:37 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread))
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
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.
|
2014-02-01 04:42:24 +04:00
|
|
|
bool reallyWait = mayWait && (mRunningEvent > 0 || !ShuttingDown());
|
|
|
|
|
|
|
|
if (MAIN_THREAD == mIsMainThread && reallyWait)
|
2011-10-12 21:52:26 +04:00
|
|
|
HangMonitor::Suspend();
|
|
|
|
|
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() :
|
|
|
|
lowMemOngoing.get());
|
|
|
|
} else {
|
2011-08-06 02:10:50 +04:00
|
|
|
NS_WARNING("Can't get observer service!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-24 02:26:36 +04:00
|
|
|
bool notifyMainThreadObserver =
|
|
|
|
(MAIN_THREAD == mIsMainThread) && sMainThreadObserver;
|
|
|
|
if (notifyMainThreadObserver)
|
2014-02-01 04:42:24 +04:00
|
|
|
sMainThreadObserver->OnProcessNextEvent(this, reallyWait, mRunningEvent);
|
2012-10-24 02:26:36 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsCOMPtr<nsIThreadObserver> obs = mObserver;
|
|
|
|
if (obs)
|
2014-02-01 04:42:24 +04:00
|
|
|
obs->OnProcessNextEvent(this, reallyWait, mRunningEvent);
|
1999-04-06 01:02:24 +04:00
|
|
|
|
2010-11-19 01:19:19 +03:00
|
|
|
NOTIFY_EVENT_OBSERVERS(OnProcessNextEvent,
|
2014-02-01 04:42:24 +04:00
|
|
|
(this, reallyWait, mRunningEvent));
|
2010-11-19 01:19:19 +03:00
|
|
|
|
2009-06-11 21:09:35 +04:00
|
|
|
++mRunningEvent;
|
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
|
|
|
|
// 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 04:42:24 +04:00
|
|
|
mEvents->GetEvent(reallyWait, getter_AddRefs(event));
|
2009-06-11 21:09:35 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*result = (event.get() != nullptr);
|
2009-06-11 21:09:35 +04:00
|
|
|
|
|
|
|
if (event) {
|
|
|
|
LOG(("THRD(%p) running [%p]\n", this, event.get()));
|
2011-10-12 21:52:26 +04:00
|
|
|
if (MAIN_THREAD == mIsMainThread)
|
|
|
|
HangMonitor::NotifyActivity();
|
2009-06-11 21:09:35 +04:00
|
|
|
event->Run();
|
|
|
|
} else if (mayWait) {
|
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
|
|
|
}
|
|
|
|
|
2009-06-11 21:09:35 +04:00
|
|
|
--mRunningEvent;
|
2010-11-19 01:19:19 +03:00
|
|
|
|
2013-10-23 16:01:20 +04:00
|
|
|
NOTIFY_EVENT_OBSERVERS(AfterProcessNextEvent,
|
|
|
|
(this, mRunningEvent, *result));
|
2010-11-19 01:19:19 +03:00
|
|
|
|
2006-06-07 04:06:11 +04:00
|
|
|
if (obs)
|
2013-10-23 16:01:20 +04:00
|
|
|
obs->AfterProcessNextEvent(this, mRunningEvent, *result);
|
2006-06-07 04:06:11 +04:00
|
|
|
|
2012-10-24 02:26:36 +04:00
|
|
|
if (notifyMainThreadObserver && sMainThreadObserver)
|
2013-10-23 16:01:20 +04:00
|
|
|
sMainThreadObserver->AfterProcessNextEvent(this, mRunningEvent, *result);
|
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
|
2012-08-22 19:56:38 +04:00
|
|
|
nsThread::GetPriority(int32_t *priority)
|
2000-11-30 08:24:53 +03:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
*priority = mPriority;
|
|
|
|
return NS_OK;
|
2000-11-30 08:24:53 +03:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsThread::SetPriority(int32_t priority)
|
1999-04-06 10:09:15 +04:00
|
|
|
{
|
2013-11-20 01:27:37 +04:00
|
|
|
if (NS_WARN_IF(!mThread))
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
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.
|
|
|
|
|
|
|
|
mPriority = priority;
|
|
|
|
|
|
|
|
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
|
|
|
|
if (!ChaosMode::isActive()) {
|
|
|
|
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
|
2012-08-22 19:56:38 +04:00
|
|
|
nsThread::AdjustPriority(int32_t delta)
|
1999-04-06 01:02:24 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
return SetPriority(mPriority + delta);
|
1999-04-06 01:02:24 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIThreadInternal
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::GetObserver(nsIThreadObserver **obs)
|
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);
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IF_ADDREF(*obs = mObserver);
|
|
|
|
return NS_OK;
|
1999-06-13 07:30:38 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::SetObserver(nsIThreadObserver *obs)
|
1999-06-13 07:30:38 +04:00
|
|
|
{
|
2013-11-20 01:27:37 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread))
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2006-05-10 21:30:15 +04:00
|
|
|
mObserver = obs;
|
|
|
|
return NS_OK;
|
1999-06-13 07:30:38 +04:00
|
|
|
}
|
|
|
|
|
2010-11-16 00:49:49 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsThread::GetRecursionDepth(uint32_t *depth)
|
2010-11-16 00:49:49 +03:00
|
|
|
{
|
2013-11-20 01:27:37 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread))
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2010-11-19 01:19:19 +03:00
|
|
|
|
2010-11-16 00:49:49 +03:00
|
|
|
*depth = mRunningEvent;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-19 01:19:19 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::AddObserver(nsIThreadObserver *observer)
|
|
|
|
{
|
2013-11-20 01:27:37 +04:00
|
|
|
if (NS_WARN_IF(!observer))
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread))
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2010-11-19 01:19:19 +03:00
|
|
|
|
|
|
|
NS_WARN_IF_FALSE(!mEventObservers.Contains(observer),
|
|
|
|
"Adding an observer twice!");
|
|
|
|
|
|
|
|
if (!mEventObservers.AppendElement(observer)) {
|
|
|
|
NS_WARNING("Out of memory!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::RemoveObserver(nsIThreadObserver *observer)
|
|
|
|
{
|
2013-11-20 01:27:37 +04:00
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread))
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2010-11-19 01:19:19 +03:00
|
|
|
|
|
|
|
if (observer && !mEventObservers.RemoveElement(observer)) {
|
|
|
|
NS_WARNING("Removing an observer that was never added!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-14 22:06:17 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::PushEventQueue(nsIEventTarget **result)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread))
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
|
|
|
|
|
|
|
nsChainedEventQueue *queue = new nsChainedEventQueue();
|
|
|
|
queue->mEventTarget = new nsNestedEventTarget(this, queue);
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
queue->mNext = mEvents;
|
|
|
|
mEvents = queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(*result = queue->mEventTarget);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::PopEventQueue(nsIEventTarget *innermostTarget)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(PR_GetCurrentThread() != mThread))
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!innermostTarget))
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (NS_WARN_IF(mEvents->mEventTarget != innermostTarget))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
MOZ_ASSERT(mEvents != &mEventsRoot);
|
|
|
|
|
|
|
|
queue = mEvents;
|
|
|
|
mEvents = mEvents->mNext;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
|
|
|
while (queue->GetEvent(false, getter_AddRefs(event)))
|
|
|
|
mEvents->PutEvent(event);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
nsThread::nsNestedEventTarget::Dispatch(nsIRunnable *event, uint32_t flags)
|
|
|
|
{
|
|
|
|
LOG(("THRD(%p) Dispatch [%p %x] to nested loop %p\n", mThread.get(), event,
|
|
|
|
flags, this));
|
|
|
|
|
|
|
|
return mThread->DispatchInternal(event, flags, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThread::nsNestedEventTarget::IsOnCurrentThread(bool *result)
|
|
|
|
{
|
|
|
|
return mThread->IsOnCurrentThread(result);
|
|
|
|
}
|