2007-03-22 10:30:00 -07: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 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsEventQueue.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2014-03-02 21:12:23 -08:00
|
|
|
#include "prthread.h"
|
|
|
|
#include "mozilla/ChaosMode.h"
|
2007-03-22 10:30:00 -07: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-03-31 21:29:02 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef PR_LOGGING
|
2012-10-29 16:32:10 -07:00
|
|
|
static PRLogModuleInfo *
|
|
|
|
GetLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("nsEventQueue");
|
|
|
|
return sLog;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2013-11-18 18:34:00 -08:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
2012-10-29 16:32:10 -07:00
|
|
|
#define LOG(args) PR_LOG(GetLog(), PR_LOG_DEBUG, args)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsEventQueue::nsEventQueue()
|
2011-04-29 12:21:57 -07:00
|
|
|
: mReentrantMonitor("nsEventQueue.mReentrantMonitor")
|
2012-07-30 07:20:58 -07:00
|
|
|
, mHead(nullptr)
|
|
|
|
, mTail(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mOffsetHead(0)
|
|
|
|
, mOffsetTail(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventQueue::~nsEventQueue()
|
|
|
|
{
|
|
|
|
// It'd be nice to be able to assert that no one else is holding the monitor,
|
|
|
|
// but NSPR doesn't really expose APIs for it.
|
|
|
|
NS_ASSERTION(IsEmpty(), "Non-empty event queue being destroyed; events being leaked.");
|
|
|
|
|
|
|
|
if (mHead)
|
|
|
|
FreePage(mHead);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
|
|
|
nsEventQueue::GetEvent(bool mayWait, nsIRunnable **result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
while (IsEmpty()) {
|
|
|
|
if (!mayWait) {
|
|
|
|
if (result)
|
2012-07-30 07:20:58 -07:00
|
|
|
*result = nullptr;
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
LOG(("EVENTQ(%p): wait begin\n", this));
|
|
|
|
mon.Wait();
|
|
|
|
LOG(("EVENTQ(%p): wait end\n", this));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
*result = mHead->mEvents[mOffsetHead++];
|
|
|
|
|
|
|
|
// Check if mHead points to empty Page
|
|
|
|
if (mOffsetHead == EVENTS_PER_PAGE) {
|
|
|
|
Page *dead = mHead;
|
|
|
|
mHead = mHead->mNext;
|
|
|
|
FreePage(dead);
|
|
|
|
mOffsetHead = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-23 12:53:14 -07:00
|
|
|
void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEventQueue::PutEvent(nsIRunnable *runnable)
|
|
|
|
{
|
|
|
|
// Avoid calling AddRef+Release while holding our monitor.
|
|
|
|
nsRefPtr<nsIRunnable> event(runnable);
|
2014-05-23 12:53:14 -07:00
|
|
|
|
|
|
|
if (ChaosMode::isActive()) {
|
|
|
|
// With probability 0.5, yield so other threads have a chance to
|
|
|
|
// dispatch events to this queue first.
|
|
|
|
if (ChaosMode::randomUint32LessThan(2)) {
|
|
|
|
PR_Sleep(PR_INTERVAL_NO_WAIT);
|
2014-03-02 21:12:23 -08:00
|
|
|
}
|
2014-05-23 12:53:14 -07:00
|
|
|
}
|
2014-03-02 21:12:23 -08:00
|
|
|
|
2014-05-23 12:53:14 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-23 12:53:14 -07:00
|
|
|
if (!mHead) {
|
|
|
|
mHead = NewPage();
|
|
|
|
MOZ_ASSERT(mHead);
|
|
|
|
|
|
|
|
mTail = mHead;
|
|
|
|
mOffsetHead = 0;
|
|
|
|
mOffsetTail = 0;
|
|
|
|
} else if (mOffsetTail == EVENTS_PER_PAGE) {
|
|
|
|
Page *page = NewPage();
|
|
|
|
MOZ_ASSERT(page);
|
|
|
|
|
|
|
|
mTail->mNext = page;
|
|
|
|
mTail = page;
|
|
|
|
mOffsetTail = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-23 12:53:14 -07:00
|
|
|
|
|
|
|
event.swap(mTail->mEvents[mOffsetTail]);
|
|
|
|
++mOffsetTail;
|
|
|
|
LOG(("EVENTQ(%p): notify\n", this));
|
|
|
|
mon.NotifyAll();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|