2014-06-30 15:39:45 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-05-04 23:35:47 +00:00
|
|
|
|
|
|
|
#include "nsEventQueue.h"
|
2006-07-06 18:46:31 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2015-05-19 18:15:34 +00:00
|
|
|
#include "mozilla/Logging.h"
|
2007-01-04 22:31:26 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2014-03-03 05:12:23 +00:00
|
|
|
#include "prthread.h"
|
|
|
|
#include "mozilla/ChaosMode.h"
|
2000-02-12 00:27:57 +00:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
static PRLogModuleInfo*
|
2012-10-29 23:32:10 +00:00
|
|
|
GetLog()
|
|
|
|
{
|
2014-05-27 07:15:35 +00:00
|
|
|
static PRLogModuleInfo* sLog;
|
|
|
|
if (!sLog) {
|
2012-10-29 23:32:10 +00:00
|
|
|
sLog = PR_NewLogModule("nsEventQueue");
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2012-10-29 23:32:10 +00:00
|
|
|
return sLog;
|
|
|
|
}
|
2013-11-19 02:34:00 +00:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
2015-06-03 22:25:57 +00:00
|
|
|
#define LOG(args) MOZ_LOG(GetLog(), mozilla::LogLevel::Debug, args)
|
1999-10-18 14:59:57 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
nsEventQueue::nsEventQueue()
|
2011-04-29 19:21:57 +00:00
|
|
|
: mReentrantMonitor("nsEventQueue.mReentrantMonitor")
|
2012-07-30 14:20:58 +00:00
|
|
|
, mHead(nullptr)
|
|
|
|
, mTail(nullptr)
|
2006-05-10 17:30:15 +00:00
|
|
|
, mOffsetHead(0)
|
|
|
|
, mOffsetTail(0)
|
1999-10-18 14:59:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
nsEventQueue::~nsEventQueue()
|
2004-12-17 19:47:06 +00:00
|
|
|
{
|
2007-02-28 02:28:53 +00:00
|
|
|
// 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.
|
2014-05-27 07:15:35 +00:00
|
|
|
NS_ASSERTION(IsEmpty(),
|
|
|
|
"Non-empty event queue being destroyed; events being leaked.");
|
2007-02-23 17:38:45 +00:00
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
if (mHead) {
|
2007-02-28 02:28:53 +00:00
|
|
|
FreePage(mHead);
|
2014-05-27 07:15:35 +00:00
|
|
|
}
|
2004-12-17 19:47:06 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2014-05-27 07:15:35 +00:00
|
|
|
nsEventQueue::GetEvent(bool aMayWait, nsIRunnable** aResult)
|
2000-02-09 02:28:51 +00:00
|
|
|
{
|
2007-01-04 22:31:26 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2014-05-27 07:15:35 +00:00
|
|
|
|
2007-01-04 22:31:26 +00:00
|
|
|
while (IsEmpty()) {
|
2014-05-27 07:15:35 +00:00
|
|
|
if (!aMayWait) {
|
|
|
|
if (aResult) {
|
|
|
|
*aResult = nullptr;
|
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2007-01-04 22:31:26 +00:00
|
|
|
}
|
2014-05-27 07:15:35 +00:00
|
|
|
LOG(("EVENTQ(%p): wait begin\n", this));
|
2007-01-04 22:31:26 +00:00
|
|
|
mon.Wait();
|
2014-05-27 07:15:35 +00:00
|
|
|
LOG(("EVENTQ(%p): wait end\n", this));
|
2001-04-26 05:28:43 +00:00
|
|
|
}
|
2014-05-27 07:15:35 +00:00
|
|
|
|
|
|
|
if (aResult) {
|
|
|
|
*aResult = mHead->mEvents[mOffsetHead++];
|
|
|
|
|
2007-01-04 22:31:26 +00:00
|
|
|
// Check if mHead points to empty Page
|
|
|
|
if (mOffsetHead == EVENTS_PER_PAGE) {
|
2014-05-27 07:15:35 +00:00
|
|
|
Page* dead = mHead;
|
2007-01-04 22:31:26 +00:00
|
|
|
mHead = mHead->mNext;
|
|
|
|
FreePage(dead);
|
|
|
|
mOffsetHead = 0;
|
|
|
|
}
|
2006-11-22 23:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
2006-11-22 19:23:02 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
1999-06-04 15:02:27 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 19:53:14 +00:00
|
|
|
void
|
2014-08-25 19:17:15 +00:00
|
|
|
nsEventQueue::PutEvent(nsIRunnable* aRunnable)
|
2015-07-10 03:21:46 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> event(aRunnable);
|
|
|
|
PutEvent(event.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsEventQueue::PutEvent(already_AddRefed<nsIRunnable>&& aRunnable)
|
1999-11-16 16:04:14 +00:00
|
|
|
{
|
2015-07-14 21:29:23 +00:00
|
|
|
if (ChaosMode::isActive(ChaosFeature::ThreadScheduling)) {
|
2014-05-23 19:53:14 +00:00
|
|
|
// 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-03 05:12:23 +00:00
|
|
|
}
|
2014-05-23 19:53:14 +00:00
|
|
|
}
|
2014-03-03 05:12:23 +00:00
|
|
|
|
2014-05-23 19:53:14 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2014-05-23 19:53:14 +00:00
|
|
|
if (!mHead) {
|
|
|
|
mHead = NewPage();
|
|
|
|
MOZ_ASSERT(mHead);
|
|
|
|
|
|
|
|
mTail = mHead;
|
|
|
|
mOffsetHead = 0;
|
|
|
|
mOffsetTail = 0;
|
|
|
|
} else if (mOffsetTail == EVENTS_PER_PAGE) {
|
2014-08-25 19:17:15 +00:00
|
|
|
Page* page = NewPage();
|
2014-05-23 19:53:14 +00:00
|
|
|
MOZ_ASSERT(page);
|
|
|
|
|
|
|
|
mTail->mNext = page;
|
|
|
|
mTail = page;
|
|
|
|
mOffsetTail = 0;
|
1999-10-18 14:59:57 +00:00
|
|
|
}
|
2014-05-23 19:53:14 +00:00
|
|
|
|
2015-08-28 18:19:49 +00:00
|
|
|
nsIRunnable*& queueLocation = mTail->mEvents[mOffsetTail];
|
|
|
|
MOZ_ASSERT(!queueLocation);
|
|
|
|
queueLocation = aRunnable.take();
|
2014-05-23 19:53:14 +00:00
|
|
|
++mOffsetTail;
|
2014-08-25 19:17:15 +00:00
|
|
|
LOG(("EVENTQ(%p): notify\n", this));
|
2014-05-23 19:53:14 +00:00
|
|
|
mon.NotifyAll();
|
1999-10-18 14:59:57 +00:00
|
|
|
}
|
2015-05-08 23:32:30 +00:00
|
|
|
|
|
|
|
size_t
|
|
|
|
nsEventQueue::Count()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
|
|
|
|
// It is obvious count is 0 when the queue is empty.
|
|
|
|
if (!mHead) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* How we count the number of events in the queue:
|
|
|
|
* 1. Let pageCount(x, y) denote the number of pages excluding the tail page
|
|
|
|
* where x is the index of head page and y is the index of the tail page.
|
|
|
|
* 2. Then we have pageCount(x, y) = y - x.
|
|
|
|
*
|
|
|
|
* Ex: pageCount(0, 0) = 0 where both head and tail pages point to page 0.
|
|
|
|
* pageCount(0, 1) = 1 where head points to page 0 and tail points page 1.
|
|
|
|
*
|
|
|
|
* 3. number of events = (EVENTS_PER_PAGE * pageCount(x, y))
|
|
|
|
* - (empty slots in head page) + (non-empty slots in tail page)
|
|
|
|
* = (EVENTS_PER_PAGE * pageCount(x, y)) - mOffsetHead + mOffsetTail
|
|
|
|
*/
|
|
|
|
|
|
|
|
int count = -mOffsetHead;
|
|
|
|
|
|
|
|
// Compute (EVENTS_PER_PAGE * pageCount(x, y))
|
|
|
|
for (Page* page = mHead; page != mTail; page = page->mNext) {
|
|
|
|
count += EVENTS_PER_PAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
count += mOffsetTail;
|
|
|
|
MOZ_ASSERT(count >= 0);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|