2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2006-05-10 21:30:15 +04:00
|
|
|
/* 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-05-05 03:35:47 +04:00
|
|
|
|
|
|
|
#include "nsEventQueue.h"
|
2006-07-06 22:46:31 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2000-02-12 03:27:57 +03:00
|
|
|
#include "prlog.h"
|
2007-01-05 01:31:26 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2000-02-12 03:27:57 +03: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
|
|
|
using namespace mozilla;
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
#ifdef PR_LOGGING
|
2012-10-30 03:32:10 +04:00
|
|
|
static PRLogModuleInfo *
|
|
|
|
GetLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("nsEventQueue");
|
|
|
|
return sLog;
|
|
|
|
}
|
2000-02-12 03:27:57 +03: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(GetLog(), PR_LOG_DEBUG, args)
|
1999-10-18 18:59:57 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsEventQueue::nsEventQueue()
|
2011-04-29 23:21:57 +04:00
|
|
|
: mReentrantMonitor("nsEventQueue.mReentrantMonitor")
|
2012-07-30 18:20:58 +04:00
|
|
|
, mHead(nullptr)
|
|
|
|
, mTail(nullptr)
|
2006-05-10 21:30:15 +04:00
|
|
|
, mOffsetHead(0)
|
|
|
|
, mOffsetTail(0)
|
1999-10-18 18:59:57 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsEventQueue::~nsEventQueue()
|
2004-12-17 22:47:06 +03:00
|
|
|
{
|
2007-02-28 05:28:53 +03: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.
|
|
|
|
NS_ASSERTION(IsEmpty(), "Non-empty event queue being destroyed; events being leaked.");
|
2007-02-23 20:38:45 +03:00
|
|
|
|
2007-02-28 05:28:53 +03:00
|
|
|
if (mHead)
|
|
|
|
FreePage(mHead);
|
2004-12-17 22:47:06 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
|
|
|
nsEventQueue::GetEvent(bool mayWait, nsIRunnable **result)
|
2000-02-09 05:28:51 +03:00
|
|
|
{
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
while (IsEmpty()) {
|
|
|
|
if (!mayWait) {
|
|
|
|
if (result)
|
2012-07-30 18:20:58 +04:00
|
|
|
*result = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-05 01:31:26 +03:00
|
|
|
}
|
|
|
|
LOG(("EVENTQ(%p): wait begin\n", this));
|
|
|
|
mon.Wait();
|
|
|
|
LOG(("EVENTQ(%p): wait end\n", this));
|
2001-04-26 09:28:43 +04:00
|
|
|
}
|
2007-01-05 01:31:26 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2006-11-23 02:18:16 +03:00
|
|
|
}
|
|
|
|
}
|
2006-11-22 22:23:02 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
1999-06-04 19:02:27 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2006-05-10 21:30:15 +04:00
|
|
|
nsEventQueue::PutEvent(nsIRunnable *runnable)
|
1999-11-16 19:04:14 +03:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
// Avoid calling AddRef+Release while holding our monitor.
|
2006-07-06 22:46:31 +04:00
|
|
|
nsRefPtr<nsIRunnable> event(runnable);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool rv = true;
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
if (!mHead) {
|
|
|
|
mHead = NewPage();
|
|
|
|
if (!mHead) {
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = false;
|
2006-05-10 21:30:15 +04:00
|
|
|
} else {
|
|
|
|
mTail = mHead;
|
|
|
|
mOffsetHead = 0;
|
|
|
|
mOffsetTail = 0;
|
|
|
|
}
|
|
|
|
} else if (mOffsetTail == EVENTS_PER_PAGE) {
|
|
|
|
Page *page = NewPage();
|
|
|
|
if (!page) {
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = false;
|
2006-05-10 21:30:15 +04:00
|
|
|
} else {
|
|
|
|
mTail->mNext = page;
|
|
|
|
mTail = page;
|
|
|
|
mOffsetTail = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rv) {
|
|
|
|
event.swap(mTail->mEvents[mOffsetTail]);
|
|
|
|
++mOffsetTail;
|
|
|
|
LOG(("EVENTQ(%p): notify\n", this));
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
1999-10-18 18:59:57 +04:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|