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: */
|
2001-09-29 00:14:13 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-05-05 03:35:47 +04:00
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1999-05-05 03:35:47 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-05-05 03:35:47 +04:00
|
|
|
*
|
2006-05-10 21:30:15 +04:00
|
|
|
* The Original Code is Mozilla code.
|
1999-05-05 03:35:47 +04:00
|
|
|
*
|
2006-05-10 21:30:15 +04:00
|
|
|
* The Initial Developer of the Original Code is Google Inc.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2006
|
2001-09-29 00:14:13 +04:00
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
2006-05-10 21:30:15 +04:00
|
|
|
* Darin Fisher <darin@meer.net>
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2006-05-10 21:30:15 +04:00
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-29 00:14:13 +04:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 18:21:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 18:21:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-05-05 03:35:47 +04:00
|
|
|
|
|
|
|
#include "nsEventQueue.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsAutoLock.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
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
static PRLogModuleInfo *sLog = PR_NewLogModule("nsEventQueue");
|
2000-02-12 03:27:57 +03:00
|
|
|
#endif
|
2006-05-10 21:30:15 +04:00
|
|
|
#define LOG(args) PR_LOG(sLog, PR_LOG_DEBUG, args)
|
1999-10-18 18:59:57 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsEventQueue::nsEventQueue()
|
|
|
|
: mMonitor(nsAutoMonitor::NewMonitor("xpcom.eventqueue"))
|
|
|
|
, mHead(nsnull)
|
|
|
|
, mTail(nsnull)
|
|
|
|
, 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);
|
2007-02-23 20:38:45 +03:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
if (mMonitor)
|
|
|
|
nsAutoMonitor::DestroyMonitor(mMonitor);
|
2004-12-17 22:47:06 +03:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
PRBool
|
|
|
|
nsEventQueue::GetEvent(PRBool mayWait, nsIRunnable **result)
|
2000-02-09 05:28:51 +03:00
|
|
|
{
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
|
|
|
nsAutoMonitor mon(mMonitor);
|
|
|
|
|
|
|
|
while (IsEmpty()) {
|
|
|
|
if (!mayWait) {
|
|
|
|
if (result)
|
|
|
|
*result = nsnull;
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
return PR_TRUE;
|
1999-06-04 19:02:27 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
PRBool
|
|
|
|
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);
|
2006-05-10 21:30:15 +04:00
|
|
|
PRBool rv = PR_TRUE;
|
|
|
|
{
|
|
|
|
nsAutoMonitor mon(mMonitor);
|
|
|
|
|
|
|
|
if (!mHead) {
|
|
|
|
mHead = NewPage();
|
|
|
|
if (!mHead) {
|
|
|
|
rv = PR_FALSE;
|
|
|
|
} else {
|
|
|
|
mTail = mHead;
|
|
|
|
mOffsetHead = 0;
|
|
|
|
mOffsetTail = 0;
|
|
|
|
}
|
|
|
|
} else if (mOffsetTail == EVENTS_PER_PAGE) {
|
|
|
|
Page *page = NewPage();
|
|
|
|
if (!page) {
|
|
|
|
rv = PR_FALSE;
|
|
|
|
} 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;
|
|
|
|
}
|