/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* 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
*
* 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.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* 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"),
* 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
* use your version of this file under the terms of the MPL, indicate your
* 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
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsCOMPtr.h"
#include "nsEventQueue.h"
#include "nsIEventQueueService.h"
#include "nsIThread.h"
#include "nsIServiceManager.h"
#include "nsIObserverService.h"
#include "nsString.h"
#include "prlog.h"
#ifdef NS_DEBUG
#include "prprf.h"
#endif
#if defined(PR_LOGGING) && defined(DEBUG_danm)
/* found these logs useful in conjunction with netlibStreamEvent logging
from netwerk. */
static int gEventQueueLogPPLevel = 0;
#endif
// in a real system, these would be members in a header class...
{
/* The slightly weird ownership model for eventqueues goes like this:
General:
There's an addref from the factory generally held by whoever asked for
the queue. The queue addrefs itself (right here) and releases itself
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.
*/
#if defined(PR_LOGGING) && defined(DEBUG_danm)
("EventQueue: Created [queue=%lx]",(long)mEventQueue));
#endif
}
{
Unlink();
#if defined(PR_LOGGING) && defined(DEBUG_danm)
("EventQueue: Destroyed [queue=%lx]",(long)mEventQueue));
#endif
if (mEventQueue) {
}
}
{
if (aNative)
else
return NS_OK;
}
{
if (thread == NS_CURRENT_THREAD)
{
}
else if (thread == NS_UI_THREAD)
{
// Get the primordial thread
}
if (aNative)
else
return NS_OK;
}
{
return NS_OK;
}
/* nsISupports interface implementation... */
/* nsIEventQueue interface implementation... */
{
// this assertion is bogus. I should be able to shut down the eldest queue,
// as long as there are no younger children
#if defined(PR_LOGGING) && defined(DEBUG_danm)
("EventQueue: StopAccepting [queue=%lx, accept=%d, could=%d]",
#endif
return NS_OK;
}
// utility funtion to send observers a notification
void
{
if (NS_SUCCEEDED(rv)) {
}
}
void* owner,
{
return NS_OK;
}
{
if (!mAcceptingEvents) {
#if defined(PR_LOGGING) && defined(DEBUG_danm)
("EventQueue: Punt posted event [queue=%lx, accept=%d, could=%d]",
#endif
if (mElderQueue) {
if (elder)
}
return rv;
}
#if defined(PR_LOGGING) && defined(DEBUG_danm)
("EventQueue: Posting event [queue=%lx]", (long)mEventQueue));
#endif
}
{
if (!mAcceptingEvents) {
#if defined(PR_LOGGING) && defined(DEBUG_danm)
("EventQueue: Punt posted synchronous event [queue=%lx, accept=%d, could=%d]",
#endif
if (mElderQueue) {
if (elder)
return rv;
}
return NS_ERROR_ABORT;
}
#if defined(PR_LOGGING) && defined(DEBUG_danm)
("EventQueue: Posting synchronous event [queue=%lx]", (long)mEventQueue));
#endif
if (aResult)
return NS_OK;
}
{
return NS_OK;
}
{
return NS_OK;
}
{
if (mElderQueue) {
if (elder)
}
return NS_OK;
}
{
if (!mEventQueue)
return NS_ERROR_NULL_POINTER;
return NS_OK;
}
{
return NS_OK;
}
{
return NS_OK;
}
{
if (!*aResult && mElderQueue) {
if (elder)
}
return NS_OK;
}
{
if (!correctThread)
return NS_ERROR_FAILURE;
#if defined(PR_LOGGING) && defined(DEBUG_danm)
("EventQueue: Process pending [queue=%lx, accept=%d, could=%d]",
}
#endif
// if we're no longer accepting events and there are still events in the
// queue, then process remaining events.
if (mElderQueue) {
if (elder)
}
#if defined(PR_LOGGING) && defined(DEBUG_danm)
#endif
return NS_OK;
}
{
if (!correctThread)
return NS_ERROR_FAILURE;
return NS_OK;
}
{
return NS_OK;
}
{
return NS_OK;
}
{
if (!correctThread)
return NS_ERROR_FAILURE;
#if defined(PR_LOGGING) && defined(DEBUG_danm)
("EventQueue: handle event [queue=%lx, accept=%d, could=%d]",
#endif
return NS_OK;
}
{
if (!correctThread)
return NS_ERROR_FAILURE;
#if defined(PR_LOGGING) && defined(DEBUG_danm)
("EventQueue: wait for event [queue=%lx, accept=%d, could=%d]",
#endif
return NS_OK;
}
{
return PL_GetEventQueueSelectFD(mEventQueue);
}
void **aResult)
{
return NS_ERROR_OUT_OF_MEMORY;
delete evt;
}
return rv;
}
// ---------------- nsPIEventQueueChain -----------------
{
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");
*/
#ifdef NS_DEBUG
int depth = 0;
++depth;
}
if (depth > 5) {
"event queue chain length is %d. this is almost certainly a leak.", depth);
}
#endif
// (be careful doing this outside nsEventQueueService's mEventQMonitor)
if (endChain) {
}
return rv;
}
{
#if defined(PR_LOGGING) && defined(DEBUG_danm)
("EventQueue: unlink [queue=%lx, younger=%lx, elder=%lx]",
#endif
// this is probably OK, but shouldn't happen by design, so tell me if it does
// break links early in case the Release cascades back onto us
if (young)
if (old) {
}
return NS_OK;
}
{
if (mYoungerQueue)
return NS_OK;
}
{
if (mYoungerQueue)
if (!answer) {
if (mAcceptingEvents && mCouldHaveEvents)
}
return NS_OK;
}
{
return NS_OK;
}
{
return NS_OK;
}
{
if (!mYoungerQueue) {
return NS_OK;
}
}
{
if (!mElderQueue) {
return NS_OK;
}
}