2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** 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
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 18:21:17 +04:00
|
|
|
* either of 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
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
#include "nsCOMPtr.h"
|
1999-05-05 03:35:47 +04:00
|
|
|
#include "nsEventQueue.h"
|
2000-01-06 22:46:44 +03:00
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIObserverService.h"
|
2001-10-20 00:52:59 +04:00
|
|
|
|
2000-03-27 03:06:01 +04:00
|
|
|
#include "nsString.h"
|
1999-10-18 18:59:57 +04:00
|
|
|
|
2000-02-12 03:27:57 +03:00
|
|
|
#include "prlog.h"
|
|
|
|
|
2003-08-05 04:01:20 +04:00
|
|
|
#ifdef NS_DEBUG
|
|
|
|
#include "prprf.h"
|
|
|
|
#endif
|
|
|
|
|
2000-02-12 03:27:57 +03:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
/* found these logs useful in conjunction with netlibStreamEvent logging
|
|
|
|
from netwerk. */
|
|
|
|
PRLogModuleInfo* gEventQueueLog = 0;
|
|
|
|
PRUint32 gEventQueueLogCount = 0;
|
|
|
|
PRUint32 gEventQueueLogPPCount = 0;
|
|
|
|
static int gEventQueueLogPPLevel = 0;
|
|
|
|
static PLEventQueue *gEventQueueLogQueue = 0;
|
|
|
|
static PRThread *gEventQueueLogThread = 0;
|
|
|
|
#endif
|
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
// in a real system, these would be members in a header class...
|
2001-11-07 09:24:10 +03:00
|
|
|
static const char gActivatedNotification[] = "nsIEventQueueActivated";
|
|
|
|
static const char gDestroyedNotification[] = "nsIEventQueueDestroyed";
|
1999-05-05 03:35:47 +04:00
|
|
|
|
|
|
|
nsEventQueueImpl::nsEventQueueImpl()
|
|
|
|
{
|
2000-06-06 01:53:25 +04:00
|
|
|
NS_ADDREF_THIS();
|
1999-10-20 02:00:12 +04:00
|
|
|
/* The slightly weird ownership model for eventqueues goes like this:
|
2000-06-06 01:53:25 +04:00
|
|
|
|
|
|
|
General:
|
|
|
|
There's an addref from the factory generally held by whoever asked for
|
1999-10-20 02:00:12 +04:00
|
|
|
the queue. The queue addrefs itself (right here) and releases itself
|
2000-06-06 01:53:25 +04:00
|
|
|
after someone calls StopAcceptingEvents() on the queue and when it is
|
|
|
|
dark and empty (in CheckForDeactivation()).
|
|
|
|
|
|
|
|
Chained queues:
|
|
|
|
|
|
|
|
Eldest queue:
|
|
|
|
The eldest queue in a chain is held on to by the EventQueueService
|
|
|
|
in a hash table, so it is possible that the eldest queue may not be
|
|
|
|
released until the EventQueueService is shutdown.
|
|
|
|
You may not call StopAcceptingEvents() on this queue until you have
|
|
|
|
done so on all younger queues.
|
|
|
|
|
|
|
|
General:
|
|
|
|
Each queue holds a reference to their immediate elder link and a weak
|
|
|
|
reference to their immediate younger link. Because you must shut down
|
|
|
|
queues from youngest to eldest, all the references will be removed.
|
|
|
|
|
|
|
|
It happens something like:
|
|
|
|
queue->StopAcceptingEvents()
|
|
|
|
{
|
|
|
|
CheckForDeactivation()
|
|
|
|
{
|
|
|
|
-- hopefully we are able to shutdown now --
|
|
|
|
Unlink()
|
|
|
|
{
|
|
|
|
-- remove the reference we hold to our elder queue --
|
|
|
|
-- NULL out our elder queues weak reference to us --
|
|
|
|
}
|
|
|
|
RELEASE ourself (to balance the ADDREF here in the constructor)
|
|
|
|
-- and we should go away. --
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
A dark queue no longer accepts events. An empty queue simply has no events.
|
1999-10-20 02:00:12 +04:00
|
|
|
*/
|
1999-05-05 03:35:47 +04:00
|
|
|
|
2000-06-06 01:53:25 +04:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: Created [queue=%lx]",(long)mEventQueue));
|
|
|
|
++gEventQueueLogCount;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mYoungerQueue = nsnull;
|
|
|
|
mEventQueue = nsnull;
|
1999-10-18 18:59:57 +04:00
|
|
|
mAcceptingEvents = PR_TRUE;
|
|
|
|
mCouldHaveEvents = PR_TRUE;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsEventQueueImpl::~nsEventQueueImpl()
|
|
|
|
{
|
2000-06-06 01:53:25 +04:00
|
|
|
Unlink();
|
|
|
|
|
2000-02-12 03:27:57 +03:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: Destroyed [queue=%lx]",(long)mEventQueue));
|
|
|
|
++gEventQueueLogCount;
|
|
|
|
#endif
|
2000-06-06 01:53:25 +04:00
|
|
|
|
|
|
|
if (mEventQueue) {
|
1999-10-18 18:59:57 +04:00
|
|
|
NotifyObservers(gDestroyedNotification);
|
1999-05-05 03:35:47 +04:00
|
|
|
PL_DestroyEventQueue(mEventQueue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-03-04 06:17:01 +03:00
|
|
|
nsEventQueueImpl::Init(PRBool aNative)
|
1999-05-05 03:35:47 +04:00
|
|
|
{
|
2000-03-04 06:17:01 +03:00
|
|
|
PRThread *thread = PR_GetCurrentThread();
|
|
|
|
if (aNative)
|
|
|
|
mEventQueue = PL_CreateNativeEventQueue("Thread event queue...", thread);
|
|
|
|
else
|
|
|
|
mEventQueue = PL_CreateMonitoredEventQueue("Thread event queue...", thread);
|
1999-10-18 18:59:57 +04:00
|
|
|
NotifyObservers(gActivatedNotification);
|
|
|
|
return NS_OK;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
2000-01-06 22:46:44 +03:00
|
|
|
NS_IMETHODIMP
|
2000-03-04 06:17:01 +03:00
|
|
|
nsEventQueueImpl::InitFromPRThread(PRThread* thread, PRBool aNative)
|
2000-01-06 22:46:44 +03:00
|
|
|
{
|
|
|
|
if (thread == NS_CURRENT_THREAD)
|
|
|
|
{
|
|
|
|
thread = PR_GetCurrentThread();
|
|
|
|
}
|
|
|
|
else if (thread == NS_UI_THREAD)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIThread> mainIThread;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Get the primordial thread
|
|
|
|
rv = nsIThread::GetMainThread(getter_AddRefs(mainIThread));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = mainIThread->GetPRThread(&thread);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
2000-03-04 06:17:01 +03:00
|
|
|
|
|
|
|
if (aNative)
|
|
|
|
mEventQueue = PL_CreateNativeEventQueue("Thread event queue...", thread);
|
|
|
|
else
|
|
|
|
mEventQueue = PL_CreateMonitoredEventQueue("Thread event queue...", thread);
|
2000-01-06 22:46:44 +03:00
|
|
|
NotifyObservers(gActivatedNotification);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-05-05 03:35:47 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::InitFromPLQueue(PLEventQueue* aQueue)
|
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
mEventQueue = aQueue;
|
|
|
|
NotifyObservers(gActivatedNotification);
|
|
|
|
return NS_OK;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* nsISupports interface implementation... */
|
2003-10-06 05:46:31 +04:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS3(nsEventQueueImpl,
|
2000-03-06 00:26:01 +03:00
|
|
|
nsIEventQueue,
|
2003-10-06 05:46:31 +04:00
|
|
|
nsIEventTarget,
|
2000-03-06 00:26:01 +03:00
|
|
|
nsPIEventQueueChain)
|
1999-05-05 03:35:47 +04:00
|
|
|
|
|
|
|
/* nsIEventQueue interface implementation... */
|
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::StopAcceptingEvents()
|
|
|
|
{
|
2000-06-06 01:53:25 +04:00
|
|
|
// this assertion is bogus. I should be able to shut down the eldest queue,
|
|
|
|
// as long as there are no younger children
|
|
|
|
|
|
|
|
|
|
|
|
NS_ASSERTION(mElderQueue || !mYoungerQueue, "attempted to disable eldest queue in chain");
|
1999-10-18 18:59:57 +04:00
|
|
|
mAcceptingEvents = PR_FALSE;
|
|
|
|
CheckForDeactivation();
|
2000-02-12 03:27:57 +03:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: StopAccepting [queue=%lx, accept=%d, could=%d]",
|
|
|
|
(long)mEventQueue,(int)mAcceptingEvents,(int)mCouldHaveEvents));
|
|
|
|
++gEventQueueLogCount;
|
|
|
|
#endif
|
1999-10-18 18:59:57 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// utility funtion to send observers a notification
|
|
|
|
void
|
|
|
|
nsEventQueueImpl::NotifyObservers(const char *aTopic)
|
|
|
|
{
|
2004-11-19 22:06:58 +03:00
|
|
|
// only send out this notification for native event queues
|
|
|
|
if (!PL_IsQueueNative(mEventQueue))
|
2004-11-04 05:34:11 +03:00
|
|
|
return;
|
2000-04-01 04:39:02 +04:00
|
|
|
|
2004-11-19 22:06:58 +03:00
|
|
|
// we should not call out to the observer service from background threads!
|
|
|
|
NS_ASSERTION(nsIThread::IsMainThread(),
|
|
|
|
"Native event queues should only be used on the main thread");
|
|
|
|
|
2004-11-04 05:34:11 +03:00
|
|
|
nsresult rv;
|
2001-10-23 02:01:27 +04:00
|
|
|
nsCOMPtr<nsIObserverService> os = do_GetService("@mozilla.org/observer-service;1", &rv);
|
2000-06-06 01:53:25 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIEventQueue> kungFuDeathGrip(this);
|
|
|
|
nsCOMPtr<nsISupports> us(do_QueryInterface(kungFuDeathGrip));
|
2001-10-20 00:52:59 +04:00
|
|
|
os->NotifyObservers(us, aTopic, NULL);
|
2000-06-06 01:53:25 +04:00
|
|
|
}
|
1999-10-18 18:59:57 +04:00
|
|
|
}
|
|
|
|
|
2000-02-09 05:28:51 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::InitEvent(PLEvent* aEvent,
|
2000-06-06 01:53:25 +04:00
|
|
|
void* owner,
|
|
|
|
PLHandleEventProc handler,
|
|
|
|
PLDestroyEventProc destructor)
|
2000-02-09 05:28:51 +03:00
|
|
|
{
|
|
|
|
PL_InitEvent(aEvent, owner, handler, destructor);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
NS_IMETHODIMP
|
1999-05-05 03:35:47 +04:00
|
|
|
nsEventQueueImpl::PostEvent(PLEvent* aEvent)
|
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
if (!mAcceptingEvents) {
|
2000-02-12 03:27:57 +03:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: Punt posted event [queue=%lx, accept=%d, could=%d]",
|
|
|
|
(long)mEventQueue,(int)mAcceptingEvents,(int)mCouldHaveEvents));
|
|
|
|
++gEventQueueLogCount;
|
|
|
|
#endif
|
2003-10-06 05:46:31 +04:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
1999-10-18 18:59:57 +04:00
|
|
|
NS_ASSERTION(mElderQueue, "event dropped because event chain is dead");
|
|
|
|
if (mElderQueue) {
|
|
|
|
nsCOMPtr<nsIEventQueue> elder(do_QueryInterface(mElderQueue));
|
|
|
|
if (elder)
|
|
|
|
rv = elder->PostEvent(aEvent);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2000-02-12 03:27:57 +03:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: Posting event [queue=%lx]", (long)mEventQueue));
|
|
|
|
++gEventQueueLogCount;
|
|
|
|
#endif
|
2003-10-06 05:46:31 +04:00
|
|
|
return PL_PostEvent(mEventQueue, aEvent) == PR_SUCCESS ? NS_OK : NS_ERROR_FAILURE;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-05-06 03:55:10 +04:00
|
|
|
nsEventQueueImpl::PostSynchronousEvent(PLEvent* aEvent, void** aResult)
|
1999-05-05 03:35:47 +04:00
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
if (!mAcceptingEvents) {
|
2000-02-12 03:27:57 +03:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: Punt posted synchronous event [queue=%lx, accept=%d, could=%d]",
|
|
|
|
(long)mEventQueue,(int)mAcceptingEvents,(int)mCouldHaveEvents));
|
|
|
|
++gEventQueueLogCount;
|
|
|
|
#endif
|
1999-10-18 18:59:57 +04:00
|
|
|
nsresult rv = NS_ERROR_NO_INTERFACE;
|
|
|
|
NS_ASSERTION(mElderQueue, "event dropped because event chain is dead");
|
|
|
|
if (mElderQueue) {
|
|
|
|
nsCOMPtr<nsIEventQueue> elder(do_QueryInterface(mElderQueue));
|
|
|
|
if (elder)
|
|
|
|
rv = elder->PostSynchronousEvent(aEvent, aResult);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2000-02-12 03:27:57 +03:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: Posting synchronous event [queue=%lx]", (long)mEventQueue));
|
|
|
|
++gEventQueueLogCount;
|
|
|
|
#endif
|
1999-05-06 03:55:10 +04:00
|
|
|
void* result = PL_PostSynchronousEvent(mEventQueue, aEvent);
|
1999-10-18 18:59:57 +04:00
|
|
|
if (aResult)
|
1999-05-06 03:55:10 +04:00
|
|
|
*aResult = result;
|
1999-10-18 18:59:57 +04:00
|
|
|
|
1999-05-06 03:55:10 +04:00
|
|
|
return NS_OK;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
1999-05-30 02:50:25 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::EnterMonitor()
|
|
|
|
{
|
2000-06-06 01:53:25 +04:00
|
|
|
PL_ENTER_EVENT_QUEUE_MONITOR(mEventQueue);
|
|
|
|
return NS_OK;
|
1999-05-30 02:50:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::ExitMonitor()
|
|
|
|
{
|
2000-06-06 01:53:25 +04:00
|
|
|
PL_EXIT_EVENT_QUEUE_MONITOR(mEventQueue);
|
|
|
|
return NS_OK;
|
1999-05-30 02:50:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::RevokeEvents(void* owner)
|
|
|
|
{
|
2000-06-06 01:53:25 +04:00
|
|
|
PL_RevokeEvents(mEventQueue, owner);
|
2001-01-23 02:01:03 +03:00
|
|
|
if (mElderQueue) {
|
|
|
|
nsCOMPtr<nsIEventQueue> elder(do_QueryInterface(mElderQueue));
|
|
|
|
if (elder)
|
|
|
|
elder->RevokeEvents(owner);
|
|
|
|
}
|
2000-06-06 01:53:25 +04:00
|
|
|
return NS_OK;
|
1999-05-30 02:50:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::GetPLEventQueue(PLEventQueue** aEventQueue)
|
|
|
|
{
|
2000-06-06 01:53:25 +04:00
|
|
|
if (!mEventQueue)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-05-30 02:50:25 +04:00
|
|
|
|
2000-06-06 01:53:25 +04:00
|
|
|
*aEventQueue = mEventQueue;
|
|
|
|
return NS_OK;
|
1999-05-30 02:50:25 +04:00
|
|
|
}
|
|
|
|
|
1999-06-15 00:20:33 +04:00
|
|
|
NS_IMETHODIMP
|
2003-10-06 05:46:31 +04:00
|
|
|
nsEventQueueImpl::IsOnCurrentThread(PRBool *aResult)
|
1999-06-15 00:20:33 +04:00
|
|
|
{
|
|
|
|
*aResult = PL_IsQueueOnCurrentThread( mEventQueue );
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-05-30 02:50:25 +04:00
|
|
|
|
2000-03-04 06:17:01 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::IsQueueNative(PRBool *aResult)
|
|
|
|
{
|
2000-06-06 01:53:25 +04:00
|
|
|
*aResult = PL_IsQueueNative(mEventQueue);
|
|
|
|
return NS_OK;
|
2000-03-04 06:17:01 +03:00
|
|
|
}
|
|
|
|
|
2001-04-26 09:28:43 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::PendingEvents(PRBool *aResult)
|
|
|
|
{
|
|
|
|
*aResult = PL_EventAvailable(mEventQueue);
|
|
|
|
if (!*aResult && mElderQueue) {
|
|
|
|
nsCOMPtr<nsIEventQueue> elder(do_QueryInterface(mElderQueue));
|
|
|
|
if (elder)
|
|
|
|
return elder->EventAvailable(*aResult);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-05-05 03:35:47 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::ProcessPendingEvents()
|
|
|
|
{
|
1999-12-14 02:05:31 +03:00
|
|
|
PRBool correctThread = PL_IsQueueOnCurrentThread(mEventQueue);
|
2000-01-06 23:08:14 +03:00
|
|
|
|
1999-12-14 02:05:31 +03:00
|
|
|
NS_ASSERTION(correctThread, "attemping to process events on the wrong thread");
|
2000-01-06 23:08:14 +03:00
|
|
|
|
1999-12-14 02:05:31 +03:00
|
|
|
if (!correctThread)
|
|
|
|
return NS_ERROR_FAILURE;
|
2000-02-12 03:27:57 +03:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
++gEventQueueLogPPLevel;
|
|
|
|
if ((gEventQueueLogQueue != mEventQueue || gEventQueueLogThread != PR_GetCurrentThread() ||
|
|
|
|
gEventQueueLogCount != gEventQueueLogPPCount) && gEventQueueLogPPLevel == 1) {
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: Process pending [queue=%lx, accept=%d, could=%d]",
|
|
|
|
(long)mEventQueue,(int)mAcceptingEvents,(int)mCouldHaveEvents));
|
|
|
|
gEventQueueLogPPCount = ++gEventQueueLogCount;
|
|
|
|
gEventQueueLogQueue = mEventQueue;
|
|
|
|
gEventQueueLogThread = PR_GetCurrentThread();
|
|
|
|
}
|
|
|
|
#endif
|
1999-10-18 18:59:57 +04:00
|
|
|
PL_ProcessPendingEvents(mEventQueue);
|
2002-04-11 04:16:04 +04:00
|
|
|
|
|
|
|
// if we're no longer accepting events and there are still events in the
|
|
|
|
// queue, then process remaining events.
|
|
|
|
if (!mAcceptingEvents && PL_EventAvailable(mEventQueue))
|
|
|
|
PL_ProcessPendingEvents(mEventQueue);
|
|
|
|
|
1999-10-20 02:00:12 +04:00
|
|
|
CheckForDeactivation();
|
2000-02-12 03:27:57 +03:00
|
|
|
|
|
|
|
if (mElderQueue) {
|
|
|
|
nsCOMPtr<nsIEventQueue> elder(do_QueryInterface(mElderQueue));
|
|
|
|
if (elder)
|
|
|
|
elder->ProcessPendingEvents();
|
|
|
|
}
|
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
--gEventQueueLogPPLevel;
|
|
|
|
#endif
|
1999-10-18 18:59:57 +04:00
|
|
|
return NS_OK;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
1999-05-06 03:55:10 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::EventLoop()
|
|
|
|
{
|
1999-12-14 02:05:31 +03:00
|
|
|
PRBool correctThread = PL_IsQueueOnCurrentThread(mEventQueue);
|
2000-01-06 23:08:14 +03:00
|
|
|
|
1999-12-14 02:05:31 +03:00
|
|
|
NS_ASSERTION(correctThread, "attemping to process events on the wrong thread");
|
2000-01-06 23:08:14 +03:00
|
|
|
|
1999-12-14 02:05:31 +03:00
|
|
|
if (!correctThread)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
1999-05-06 03:55:10 +04:00
|
|
|
PL_EventLoop(mEventQueue);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-05-05 03:35:47 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::EventAvailable(PRBool& aResult)
|
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
aResult = PL_EventAvailable(mEventQueue);
|
|
|
|
return NS_OK;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::GetEvent(PLEvent** aResult)
|
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
*aResult = PL_GetEvent(mEventQueue);
|
|
|
|
CheckForDeactivation();
|
|
|
|
return NS_OK;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
|
|
|
|
1999-06-04 19:02:27 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::HandleEvent(PLEvent* aEvent)
|
|
|
|
{
|
1999-12-14 02:05:31 +03:00
|
|
|
PRBool correctThread = PL_IsQueueOnCurrentThread(mEventQueue);
|
|
|
|
NS_ASSERTION(correctThread, "attemping to process events on the wrong thread");
|
|
|
|
if (!correctThread)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2000-02-12 03:27:57 +03:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: handle event [queue=%lx, accept=%d, could=%d]",
|
|
|
|
(long)mEventQueue,(int)mAcceptingEvents,(int)mCouldHaveEvents));
|
|
|
|
++gEventQueueLogCount;
|
|
|
|
#endif
|
1999-10-18 18:59:57 +04:00
|
|
|
PL_HandleEvent(aEvent);
|
|
|
|
return NS_OK;
|
1999-06-04 19:02:27 +04:00
|
|
|
}
|
|
|
|
|
1999-11-16 19:04:14 +03:00
|
|
|
NS_IMETHODIMP
|
1999-12-15 01:06:17 +03:00
|
|
|
nsEventQueueImpl::WaitForEvent(PLEvent** aResult)
|
1999-11-16 19:04:14 +03:00
|
|
|
{
|
1999-12-14 02:05:31 +03:00
|
|
|
PRBool correctThread = PL_IsQueueOnCurrentThread(mEventQueue);
|
|
|
|
NS_ASSERTION(correctThread, "attemping to process events on the wrong thread");
|
|
|
|
if (!correctThread)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2000-02-12 03:27:57 +03:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: wait for event [queue=%lx, accept=%d, could=%d]",
|
|
|
|
(long)mEventQueue,(int)mAcceptingEvents,(int)mCouldHaveEvents));
|
|
|
|
++gEventQueueLogCount;
|
|
|
|
#endif
|
1999-12-15 01:06:17 +03:00
|
|
|
*aResult = PL_WaitForEvent(mEventQueue);
|
1999-11-16 19:04:14 +03:00
|
|
|
CheckForDeactivation();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-05-05 03:35:47 +04:00
|
|
|
NS_IMETHODIMP_(PRInt32)
|
|
|
|
nsEventQueueImpl::GetEventQueueSelectFD()
|
|
|
|
{
|
|
|
|
return PL_GetEventQueueSelectFD(mEventQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsEventQueueImpl::Create(nsISupports *aOuter,
|
2000-06-06 01:53:25 +04:00
|
|
|
REFNSIID aIID,
|
|
|
|
void **aResult)
|
1999-05-05 03:35:47 +04:00
|
|
|
{
|
|
|
|
nsEventQueueImpl* evt = new nsEventQueueImpl();
|
|
|
|
if (evt == NULL)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
nsresult rv = evt->QueryInterface(aIID, aResult);
|
1999-05-26 05:38:36 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete evt;
|
|
|
|
}
|
1999-05-05 03:35:47 +04:00
|
|
|
return rv;
|
|
|
|
}
|
1999-10-18 18:59:57 +04:00
|
|
|
|
|
|
|
// ---------------- nsPIEventQueueChain -----------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::AppendQueue(nsIEventQueue *aQueue)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2000-06-06 01:53:25 +04:00
|
|
|
nsCOMPtr<nsIEventQueue> end;
|
1999-10-18 18:59:57 +04:00
|
|
|
nsCOMPtr<nsPIEventQueueChain> queueChain(do_QueryInterface(aQueue));
|
|
|
|
|
|
|
|
if (!aQueue)
|
|
|
|
return NS_ERROR_NO_INTERFACE;
|
|
|
|
|
|
|
|
/* this would be nice
|
|
|
|
NS_ASSERTION(aQueue->mYoungerQueue == NULL && aQueue->mElderQueue == NULL,
|
|
|
|
"event queue repeatedly appended to queue chain");
|
|
|
|
*/
|
|
|
|
rv = NS_ERROR_NO_INTERFACE;
|
|
|
|
|
2003-08-05 04:01:20 +04:00
|
|
|
#ifdef NS_DEBUG
|
|
|
|
int depth = 0;
|
|
|
|
nsEventQueueImpl *next = this;
|
|
|
|
while (next && depth < 100) {
|
|
|
|
next = NS_STATIC_CAST(nsEventQueueImpl *, next->mYoungerQueue);
|
|
|
|
++depth;
|
|
|
|
}
|
|
|
|
if (depth > 5) {
|
|
|
|
char warning[80];
|
|
|
|
PR_snprintf(warning, sizeof(warning),
|
|
|
|
"event queue chain length is %d. this is almost certainly a leak.", depth);
|
|
|
|
NS_WARNING(warning);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-10-20 02:00:12 +04:00
|
|
|
// (be careful doing this outside nsEventQueueService's mEventQMonitor)
|
|
|
|
|
2000-06-06 01:53:25 +04:00
|
|
|
GetYoungest(getter_AddRefs(end));
|
1999-10-18 18:59:57 +04:00
|
|
|
nsCOMPtr<nsPIEventQueueChain> endChain(do_QueryInterface(end));
|
|
|
|
if (endChain) {
|
|
|
|
endChain->SetYounger(queueChain);
|
|
|
|
queueChain->SetElder(endChain);
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::Unlink()
|
|
|
|
{
|
2000-06-06 01:53:25 +04:00
|
|
|
nsCOMPtr<nsPIEventQueueChain> young = mYoungerQueue,
|
|
|
|
old = mElderQueue;
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: unlink [queue=%lx, younger=%lx, elder=%lx]",
|
|
|
|
(long)mEventQueue,(long)mYoungerQueue, (long)mElderQueue.get()));
|
|
|
|
++gEventQueueLogCount;
|
|
|
|
#endif
|
1999-10-18 18:59:57 +04:00
|
|
|
|
1999-10-20 02:00:12 +04:00
|
|
|
// this is probably OK, but shouldn't happen by design, so tell me if it does
|
1999-10-18 18:59:57 +04:00
|
|
|
NS_ASSERTION(!mYoungerQueue, "event queue chain broken in middle");
|
|
|
|
|
|
|
|
// break links early in case the Release cascades back onto us
|
2000-06-06 01:53:25 +04:00
|
|
|
mYoungerQueue = nsnull;
|
|
|
|
mElderQueue = nsnull;
|
1999-10-18 18:59:57 +04:00
|
|
|
|
|
|
|
if (young)
|
|
|
|
young->SetElder(old);
|
|
|
|
if (old) {
|
|
|
|
old->SetYounger(young);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::GetYoungest(nsIEventQueue **aQueue)
|
|
|
|
{
|
|
|
|
if (mYoungerQueue)
|
|
|
|
return mYoungerQueue->GetYoungest(aQueue);
|
|
|
|
|
|
|
|
nsIEventQueue *answer = NS_STATIC_CAST(nsIEventQueue *, this);
|
|
|
|
NS_ADDREF(answer);
|
|
|
|
*aQueue = answer;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::GetYoungestActive(nsIEventQueue **aQueue)
|
|
|
|
{
|
2000-06-06 01:53:25 +04:00
|
|
|
nsCOMPtr<nsIEventQueue> answer;
|
1999-10-18 18:59:57 +04:00
|
|
|
|
|
|
|
if (mYoungerQueue)
|
2000-06-06 01:53:25 +04:00
|
|
|
mYoungerQueue->GetYoungestActive(getter_AddRefs(answer));
|
|
|
|
if (!answer) {
|
|
|
|
if (mAcceptingEvents && mCouldHaveEvents)
|
1999-10-20 02:00:12 +04:00
|
|
|
answer = NS_STATIC_CAST(nsIEventQueue *, this);
|
2000-06-06 01:53:25 +04:00
|
|
|
}
|
1999-10-18 18:59:57 +04:00
|
|
|
*aQueue = answer;
|
2000-06-06 01:53:25 +04:00
|
|
|
NS_IF_ADDREF(*aQueue);
|
1999-10-18 18:59:57 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::SetYounger(nsPIEventQueueChain *aQueue)
|
|
|
|
{
|
|
|
|
mYoungerQueue = aQueue;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::SetElder(nsPIEventQueueChain *aQueue)
|
|
|
|
{
|
|
|
|
mElderQueue = aQueue;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-06-06 01:53:25 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::GetYounger(nsIEventQueue **aQueue)
|
|
|
|
{
|
|
|
|
if (!mYoungerQueue) {
|
|
|
|
*aQueue = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return mYoungerQueue->QueryInterface(NS_GET_IID(nsIEventQueue), (void**)&aQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueImpl::GetElder(nsIEventQueue **aQueue)
|
|
|
|
{
|
|
|
|
if (!mElderQueue) {
|
|
|
|
*aQueue = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return mElderQueue->QueryInterface(NS_GET_IID(nsIEventQueue), (void**)&aQueue);
|
|
|
|
}
|
|
|
|
|