1999-01-04 11:50:10 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The contents of this file are subject to the Netscape 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/NPL/
|
1999-01-04 11:50:10 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03: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-01-04 11:50:10 +03:00
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape Communications
|
1999-11-06 06:43:54 +03:00
|
|
|
* Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-01-04 11:50:10 +03:00
|
|
|
*/
|
1999-05-26 05:38:36 +04:00
|
|
|
|
|
|
|
#include "nsEventQueueService.h"
|
1999-01-04 11:50:10 +03:00
|
|
|
#include "prmon.h"
|
1999-12-08 04:59:32 +03:00
|
|
|
#include "nsIComponentManager.h"
|
1999-05-05 03:35:47 +04:00
|
|
|
#include "nsIEventQueue.h"
|
1999-10-19 23:18:27 +04:00
|
|
|
#include "nsIThread.h"
|
1999-10-18 18:59:57 +04:00
|
|
|
#include "nsPIEventQueueChain.h"
|
1999-01-04 11:50:10 +03:00
|
|
|
|
1999-08-23 14:14:16 +04:00
|
|
|
static NS_DEFINE_CID(kEventQueueCID, NS_EVENTQUEUE_CID);
|
1999-05-05 03:35:47 +04:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-05-05 03:35:47 +04:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
// XXX move to nsID.h or nsHashtable.h? (copied from nsComponentManager.cpp)
|
|
|
|
class ThreadKey: public nsHashKey {
|
|
|
|
private:
|
|
|
|
const PRThread* id;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ThreadKey(const PRThread* aID) {
|
|
|
|
id = aID;
|
|
|
|
}
|
|
|
|
|
1999-11-03 08:30:49 +03:00
|
|
|
ThreadKey(const ThreadKey &aKey) {
|
|
|
|
id = aKey.id;
|
|
|
|
}
|
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
PRUint32 HashValue(void) const {
|
|
|
|
return (PRUint32)id;
|
|
|
|
}
|
1999-05-05 03:35:47 +04:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
PRBool Equals(const nsHashKey *aKey) const {
|
|
|
|
return (id == ((const ThreadKey *) aKey)->id);
|
|
|
|
}
|
1999-05-05 03:35:47 +04:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
nsHashKey *Clone(void) const {
|
|
|
|
return new ThreadKey(id);
|
|
|
|
}
|
|
|
|
};
|
1999-05-05 03:35:47 +04:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-05-05 03:35:47 +04:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
/*
|
|
|
|
* EventQueueEntry maintains the data associated with each entry in
|
|
|
|
* the EventQueue service's hash table...
|
|
|
|
*
|
|
|
|
* It derives from nsISupports merely as a convienence since the entries are
|
|
|
|
* reference counted...
|
|
|
|
*/
|
|
|
|
class EventQueueEntry : public nsISupports
|
1999-05-05 03:35:47 +04:00
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
public:
|
1999-11-03 08:30:49 +03:00
|
|
|
EventQueueEntry(nsEventQueueServiceImpl *aService, ThreadKey &aKey);
|
1999-10-18 18:59:57 +04:00
|
|
|
virtual ~EventQueueEntry();
|
1999-05-05 03:35:47 +04:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
// nsISupports interface...
|
|
|
|
NS_DECL_ISUPPORTS
|
1999-05-05 03:35:47 +04:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
nsIEventQueue* GetEventQueue(void); // addrefs!
|
|
|
|
nsresult MakeNewQueue(nsIEventQueue **aQueue);
|
|
|
|
|
|
|
|
nsresult AddQueue(void);
|
|
|
|
void RemoveQueue(nsIEventQueue *aQueue); // queue goes dark, and is released
|
1999-11-03 08:30:49 +03:00
|
|
|
ThreadKey *TheThreadKey(void)
|
|
|
|
{ return &mHashKey; }
|
1999-10-18 18:59:57 +04:00
|
|
|
|
1999-11-03 08:30:49 +03:00
|
|
|
// methods for accessing the linked list of event queue entries
|
|
|
|
void Link(EventQueueEntry *aAfter);
|
|
|
|
void Unlink(void);
|
|
|
|
EventQueueEntry *Next(void)
|
|
|
|
{ return mNextEntry; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsIEventQueue *mQueue;
|
|
|
|
ThreadKey mHashKey;
|
|
|
|
nsEventQueueServiceImpl *mService; // weak reference, obviously
|
|
|
|
EventQueueEntry *mPrevEntry,
|
|
|
|
*mNextEntry;
|
1999-10-18 18:59:57 +04:00
|
|
|
};
|
1999-01-10 11:12:01 +03:00
|
|
|
|
|
|
|
/* nsISupports interface implementation... */
|
1999-08-23 14:14:16 +04:00
|
|
|
NS_IMPL_ISUPPORTS0(EventQueueEntry)
|
1999-01-10 11:12:01 +03:00
|
|
|
|
1999-11-03 08:30:49 +03:00
|
|
|
EventQueueEntry::EventQueueEntry(nsEventQueueServiceImpl *aService, ThreadKey &aKey) :
|
|
|
|
mHashKey(aKey), mPrevEntry(0), mNextEntry(0)
|
1999-01-10 11:12:01 +03:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
1999-11-03 08:30:49 +03:00
|
|
|
mService = aService;
|
1999-10-18 18:59:57 +04:00
|
|
|
MakeNewQueue(&mQueue);
|
|
|
|
NS_ASSERTION(mQueue, "EventQueueEntry constructor failed");
|
1999-11-03 08:30:49 +03:00
|
|
|
if (mService)
|
|
|
|
mService->AddEventQueueEntry(this);
|
1999-01-10 11:12:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
EventQueueEntry::~EventQueueEntry()
|
|
|
|
{
|
1999-11-03 08:30:49 +03:00
|
|
|
if (mService)
|
|
|
|
mService->RemoveEventQueueEntry(this);
|
1999-10-18 18:59:57 +04:00
|
|
|
NS_IF_RELEASE(mQueue);
|
1999-01-10 11:12:01 +03:00
|
|
|
}
|
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
// Return the active event queue on our chain
|
1999-05-05 03:35:47 +04:00
|
|
|
nsIEventQueue* EventQueueEntry::GetEventQueue(void)
|
1999-01-10 11:12:01 +03:00
|
|
|
{
|
1999-05-05 03:35:47 +04:00
|
|
|
nsIEventQueue* answer = NULL;
|
1999-10-18 18:59:57 +04:00
|
|
|
|
|
|
|
if (mQueue) {
|
|
|
|
nsCOMPtr<nsPIEventQueueChain> ourChain(do_QueryInterface(mQueue));
|
|
|
|
if (ourChain)
|
|
|
|
ourChain->GetYoungestActive(&answer);
|
|
|
|
else {
|
|
|
|
NS_ADDREF(mQueue);
|
|
|
|
answer = mQueue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return answer;
|
1999-01-10 11:12:01 +03:00
|
|
|
}
|
1999-01-04 11:50:10 +03:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
nsresult EventQueueEntry::MakeNewQueue(nsIEventQueue **aQueue)
|
1999-05-05 03:35:47 +04:00
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
nsIEventQueue *queue = 0;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = nsComponentManager::CreateInstance(kEventQueueCID, NULL,
|
|
|
|
NS_GET_IID(nsIEventQueue), (void**) &queue);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = queue->Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(queue);
|
|
|
|
queue = 0; // redundant, but makes me feel better
|
|
|
|
}
|
1999-11-03 08:30:49 +03:00
|
|
|
}
|
1999-10-18 18:59:57 +04:00
|
|
|
*aQueue = queue;
|
|
|
|
return rv;
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
1999-01-04 11:50:10 +03:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
nsresult EventQueueEntry::AddQueue(void)
|
1999-05-05 03:35:47 +04:00
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
nsIEventQueue *newQueue = NULL;
|
|
|
|
nsresult rv = NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
if (mQueue) {
|
|
|
|
rv = MakeNewQueue(&newQueue);
|
|
|
|
|
|
|
|
// add it to our chain of queues
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsPIEventQueueChain> ourChain(do_QueryInterface(mQueue));
|
|
|
|
if (ourChain)
|
|
|
|
ourChain->AppendQueue(newQueue);
|
|
|
|
else
|
|
|
|
NS_RELEASE(newQueue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
1999-05-05 03:35:47 +04:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
void EventQueueEntry::RemoveQueue(nsIEventQueue *aQueue)
|
|
|
|
{
|
|
|
|
aQueue->StopAcceptingEvents();
|
|
|
|
NS_RELEASE(aQueue);
|
|
|
|
// it's now gone dark, and will be deleted and unlinked as soon as
|
|
|
|
// everyone else lets go
|
1999-05-05 03:35:47 +04:00
|
|
|
}
|
1999-01-04 11:50:10 +03:00
|
|
|
|
1999-11-03 08:30:49 +03:00
|
|
|
// this is an apology for symmetry breaking between EventQueueEntry and nsEventQueueServiceImpl.
|
|
|
|
// the latter handles both sides of the dual data structure containing the former, those being
|
|
|
|
// nsEventQueueServiceImpl's hashtable and linked list, while the former contains the code
|
|
|
|
// for handling linked lists, on which the latter relies. if that was complicated, it's because
|
|
|
|
// it is, and thus the apology.
|
|
|
|
void EventQueueEntry::Link(EventQueueEntry *aAfter)
|
|
|
|
{
|
|
|
|
if (aAfter) {
|
|
|
|
mNextEntry = aAfter->mNextEntry;
|
|
|
|
if (mNextEntry)
|
|
|
|
mNextEntry->mPrevEntry = this;
|
|
|
|
aAfter->mNextEntry = this;
|
|
|
|
} else
|
|
|
|
mNextEntry = 0;
|
|
|
|
mPrevEntry = aAfter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventQueueEntry::Unlink(void)
|
|
|
|
{
|
|
|
|
if (mNextEntry)
|
|
|
|
mNextEntry->mPrevEntry = mPrevEntry;
|
|
|
|
if (mPrevEntry)
|
|
|
|
mPrevEntry->mNextEntry = mNextEntry;
|
|
|
|
mNextEntry = 0;
|
|
|
|
mPrevEntry = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
EventQueueEntryEnumerator::EventQueueEntryEnumerator()
|
|
|
|
{
|
|
|
|
mCurrent = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventQueueEntryEnumerator::~EventQueueEntryEnumerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventQueueEntryEnumerator::Reset(EventQueueEntry *aStart)
|
|
|
|
{
|
|
|
|
mCurrent = aStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventQueueEntryEnumerator::Skip(EventQueueEntry *aSkip)
|
|
|
|
{
|
|
|
|
if (mCurrent == aSkip)
|
|
|
|
mCurrent = aSkip->Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
EventQueueEntry *
|
|
|
|
EventQueueEntryEnumerator::Get(void)
|
|
|
|
{
|
|
|
|
EventQueueEntry *rtnval = mCurrent;
|
|
|
|
if (mCurrent)
|
|
|
|
mCurrent = mCurrent->Next();
|
|
|
|
return rtnval;
|
|
|
|
}
|
|
|
|
|
1999-05-26 05:38:36 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-11-03 08:30:49 +03:00
|
|
|
/* TODO: unify the dual, redundant data structures holding EventQueueEntrys:
|
|
|
|
they're held simultaneously in both a hashtable and a linked list.
|
|
|
|
*/
|
1999-01-04 11:50:10 +03:00
|
|
|
|
|
|
|
nsEventQueueServiceImpl::nsEventQueueServiceImpl()
|
|
|
|
{
|
1999-01-10 11:12:01 +03:00
|
|
|
NS_INIT_REFCNT();
|
|
|
|
|
1999-01-04 11:50:10 +03:00
|
|
|
mEventQTable = new nsHashtable(16);
|
|
|
|
mEventQMonitor = PR_NewMonitor();
|
1999-11-03 08:30:49 +03:00
|
|
|
mBaseEntry = 0;
|
1999-01-04 11:50:10 +03:00
|
|
|
}
|
|
|
|
|
1999-11-03 08:30:49 +03:00
|
|
|
nsEventQueueServiceImpl::~nsEventQueueServiceImpl()
|
1999-01-10 11:12:01 +03:00
|
|
|
{
|
1999-11-03 08:30:49 +03:00
|
|
|
EventQueueEntry *entry;
|
1999-01-10 11:12:01 +03:00
|
|
|
|
1999-11-03 08:30:49 +03:00
|
|
|
// Destroy any remaining PLEventQueues
|
|
|
|
mEnumerator.Reset(mBaseEntry);
|
|
|
|
while ((entry = mEnumerator.Get()) != 0)
|
|
|
|
delete entry;
|
1999-01-10 11:12:01 +03:00
|
|
|
|
1999-01-04 11:50:10 +03:00
|
|
|
delete mEventQTable;
|
1999-01-10 11:12:01 +03:00
|
|
|
|
1999-01-04 11:50:10 +03:00
|
|
|
PR_DestroyMonitor(mEventQMonitor);
|
|
|
|
}
|
|
|
|
|
1999-05-26 05:38:36 +04:00
|
|
|
NS_METHOD
|
|
|
|
nsEventQueueServiceImpl::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
|
|
|
|
{
|
|
|
|
if (aOuter)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
nsEventQueueServiceImpl* eqs = new nsEventQueueServiceImpl();
|
|
|
|
if (eqs == NULL)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(eqs);
|
|
|
|
nsresult rv = eqs->QueryInterface(aIID, aResult);
|
|
|
|
NS_RELEASE(eqs);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-01-04 11:50:10 +03:00
|
|
|
/* nsISupports interface implementation... */
|
1999-08-23 14:14:16 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsEventQueueServiceImpl,nsIEventQueueService)
|
1999-01-04 11:50:10 +03:00
|
|
|
|
|
|
|
/* nsIEventQueueService interface implementation... */
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueServiceImpl::CreateThreadEventQueue(void)
|
1999-10-19 23:18:27 +04:00
|
|
|
{
|
|
|
|
return CreateEventQueue(PR_GetCurrentThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueServiceImpl::CreateFromIThread(
|
|
|
|
nsIThread *aThread, nsIEventQueue **aResult)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
PRThread *prThread;
|
|
|
|
|
|
|
|
rv = aThread->GetPRThread(&prThread);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = CreateEventQueue(prThread); // addrefs
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = GetThreadEventQueue(prThread, aResult); // doesn't addref
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueServiceImpl::CreateEventQueue(PRThread *aThread)
|
1999-01-04 11:50:10 +03:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
1999-10-19 23:18:27 +04:00
|
|
|
ThreadKey key(aThread);
|
1999-01-10 11:12:01 +03:00
|
|
|
EventQueueEntry* evQueueEntry;
|
1999-01-04 11:50:10 +03:00
|
|
|
|
|
|
|
/* Enter the lock which protects the EventQ hashtable... */
|
|
|
|
PR_EnterMonitor(mEventQMonitor);
|
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
/* create only one event queue chain per thread... */
|
1999-01-10 11:12:01 +03:00
|
|
|
evQueueEntry = (EventQueueEntry*)mEventQTable->Get(&key);
|
|
|
|
if (NULL == evQueueEntry) {
|
1999-11-03 08:30:49 +03:00
|
|
|
evQueueEntry = new EventQueueEntry(this, key);
|
1999-01-10 11:12:01 +03:00
|
|
|
if (NULL == evQueueEntry) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_ADDREF(evQueueEntry);
|
1999-01-04 11:50:10 +03:00
|
|
|
|
|
|
|
done:
|
|
|
|
// Release the EventQ lock...
|
|
|
|
PR_ExitMonitor(mEventQMonitor);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-11-03 08:30:49 +03:00
|
|
|
void
|
|
|
|
nsEventQueueServiceImpl::AddEventQueueEntry(EventQueueEntry *aEntry)
|
|
|
|
{
|
|
|
|
EventQueueEntry *last, *current;
|
|
|
|
|
|
|
|
// add to the hashtable, then to the end of the linked list
|
|
|
|
mEventQTable->Put(aEntry->TheThreadKey(), aEntry);
|
|
|
|
if (mBaseEntry) {
|
|
|
|
for (last = 0, current = mBaseEntry; current; current = current->Next())
|
|
|
|
last = current;
|
|
|
|
aEntry->Link(last);
|
|
|
|
} else
|
|
|
|
mBaseEntry = aEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsEventQueueServiceImpl::RemoveEventQueueEntry(EventQueueEntry *aEntry)
|
|
|
|
{
|
|
|
|
mEventQTable->Remove(aEntry->TheThreadKey());
|
|
|
|
if (mBaseEntry == aEntry)
|
|
|
|
mBaseEntry = aEntry->Next();
|
|
|
|
mEnumerator.Skip(aEntry);
|
|
|
|
aEntry->Unlink();
|
|
|
|
}
|
|
|
|
|
1999-01-04 11:50:10 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueServiceImpl::DestroyThreadEventQueue(void)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
ThreadKey key(PR_GetCurrentThread());
|
1999-01-10 11:12:01 +03:00
|
|
|
EventQueueEntry* evQueueEntry;
|
1999-01-04 11:50:10 +03:00
|
|
|
|
|
|
|
/* Enter the lock which protects the EventQ hashtable... */
|
|
|
|
PR_EnterMonitor(mEventQMonitor);
|
|
|
|
|
1999-01-10 11:12:01 +03:00
|
|
|
evQueueEntry = (EventQueueEntry*)mEventQTable->Get(&key);
|
1999-11-03 08:30:49 +03:00
|
|
|
if (NULL != evQueueEntry)
|
|
|
|
NS_RELEASE(evQueueEntry);
|
|
|
|
else
|
1999-01-04 11:50:10 +03:00
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Release the EventQ lock...
|
|
|
|
PR_ExitMonitor(mEventQMonitor);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-05-05 03:35:47 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueServiceImpl::CreateFromPLEventQueue(PLEventQueue* aPLEventQueue, nsIEventQueue** aResult)
|
|
|
|
{
|
|
|
|
// Create our thread queue using the component manager
|
|
|
|
nsresult rv;
|
|
|
|
nsIEventQueue* aQueue;
|
1999-08-23 14:14:16 +04:00
|
|
|
if (NS_FAILED(rv = nsComponentManager::CreateInstance(kEventQueueCID, NULL, NS_GET_IID(nsIEventQueue),
|
1999-05-05 03:35:47 +04:00
|
|
|
(void**)&aQueue))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv = aQueue->InitFromPLQueue(aPLEventQueue))) {
|
|
|
|
NS_IF_RELEASE(aQueue);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = aQueue;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
// create new event queue, append it to the current thread's chain of event queues.
|
|
|
|
// return it, addrefed.
|
1999-05-05 03:35:47 +04:00
|
|
|
NS_IMETHODIMP
|
1999-10-18 18:59:57 +04:00
|
|
|
nsEventQueueServiceImpl::PushThreadEventQueue(nsIEventQueue **aNewQueue)
|
1999-05-05 03:35:47 +04:00
|
|
|
{
|
1999-10-18 18:59:57 +04:00
|
|
|
nsresult rv = NS_OK;
|
1999-05-05 03:35:47 +04:00
|
|
|
ThreadKey key(PR_GetCurrentThread());
|
|
|
|
EventQueueEntry* evQueueEntry;
|
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
NS_ASSERTION(aNewQueue, "PushThreadEventQueue called with null param");
|
|
|
|
*aNewQueue = NULL;
|
|
|
|
|
1999-05-05 03:35:47 +04:00
|
|
|
/* Enter the lock which protects the EventQ hashtable... */
|
|
|
|
PR_EnterMonitor(mEventQMonitor);
|
|
|
|
|
|
|
|
evQueueEntry = (EventQueueEntry*)mEventQTable->Get(&key);
|
|
|
|
if (NULL == evQueueEntry) {
|
1999-11-03 08:30:49 +03:00
|
|
|
evQueueEntry = new EventQueueEntry(this, key);
|
1999-05-05 03:35:47 +04:00
|
|
|
if (NULL == evQueueEntry) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
1999-10-18 18:59:57 +04:00
|
|
|
else {
|
|
|
|
// An entry was already present. We need to push a new
|
|
|
|
// queue onto our stack.
|
|
|
|
rv = evQueueEntry->AddQueue();
|
|
|
|
}
|
1999-05-05 03:35:47 +04:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*aNewQueue = evQueueEntry->GetEventQueue();
|
|
|
|
NS_ADDREF(evQueueEntry);
|
|
|
|
}
|
1999-05-05 03:35:47 +04:00
|
|
|
|
|
|
|
done:
|
|
|
|
// Release the EventQ lock...
|
|
|
|
PR_ExitMonitor(mEventQMonitor);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
// disable and release the given queue (though the last one won't be released)
|
1999-05-05 03:35:47 +04:00
|
|
|
NS_IMETHODIMP
|
1999-10-18 18:59:57 +04:00
|
|
|
nsEventQueueServiceImpl::PopThreadEventQueue(nsIEventQueue *aQueue)
|
1999-05-05 03:35:47 +04:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
ThreadKey key(PR_GetCurrentThread());
|
|
|
|
EventQueueEntry* evQueueEntry;
|
|
|
|
|
|
|
|
/* Enter the lock which protects the EventQ hashtable... */
|
|
|
|
PR_EnterMonitor(mEventQMonitor);
|
|
|
|
|
|
|
|
evQueueEntry = (EventQueueEntry*)mEventQTable->Get(&key);
|
|
|
|
if (NULL != evQueueEntry) {
|
|
|
|
nsrefcnt refcnt;
|
|
|
|
|
|
|
|
NS_RELEASE2(evQueueEntry, refcnt);
|
1999-11-03 08:30:49 +03:00
|
|
|
// If this wasn't the last reference, we must be popping.
|
|
|
|
if (refcnt > 0)
|
1999-10-18 18:59:57 +04:00
|
|
|
evQueueEntry->RemoveQueue(aQueue);
|
1999-05-05 03:35:47 +04:00
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the EventQ lock...
|
|
|
|
PR_ExitMonitor(mEventQMonitor);
|
|
|
|
return rv;
|
|
|
|
}
|
1999-01-04 11:50:10 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-05-05 03:35:47 +04:00
|
|
|
nsEventQueueServiceImpl::GetThreadEventQueue(PRThread* aThread, nsIEventQueue** aResult)
|
1999-01-04 11:50:10 +03:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
1999-01-10 11:12:01 +03:00
|
|
|
EventQueueEntry* evQueueEntry;
|
1999-01-04 11:50:10 +03:00
|
|
|
|
|
|
|
/* Parameter validation... */
|
1999-11-30 03:14:55 +03:00
|
|
|
if (NULL == aResult) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
PRThread* keyThread = aThread;
|
|
|
|
|
|
|
|
if (keyThread == NS_CURRENT_THREAD)
|
|
|
|
{
|
|
|
|
keyThread = PR_GetCurrentThread();
|
|
|
|
}
|
|
|
|
else if (keyThread == NS_UI_THREAD)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIThread> mainIThread;
|
|
|
|
|
|
|
|
// Get the primordial thread
|
|
|
|
rv = nsIThread::GetMainThread(getter_AddRefs(mainIThread));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = mainIThread->GetPRThread(&keyThread);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-01-04 11:50:10 +03:00
|
|
|
}
|
|
|
|
|
1999-11-30 03:14:55 +03:00
|
|
|
ThreadKey key(keyThread);
|
|
|
|
|
|
|
|
/* Enter the lock which protects the EventQ hashtable... */
|
|
|
|
PR_EnterMonitor(mEventQMonitor);
|
|
|
|
|
1999-01-10 11:12:01 +03:00
|
|
|
evQueueEntry = (EventQueueEntry*)mEventQTable->Get(&key);
|
1999-11-30 03:14:55 +03:00
|
|
|
|
|
|
|
PR_ExitMonitor(mEventQMonitor);
|
|
|
|
|
1999-01-10 11:12:01 +03:00
|
|
|
if (NULL != evQueueEntry) {
|
1999-05-05 03:35:47 +04:00
|
|
|
*aResult = evQueueEntry->GetEventQueue(); // Queue addrefing is done by this call.
|
1999-01-04 11:50:10 +03:00
|
|
|
} else {
|
|
|
|
// XXX: Need error code for requesting an event queue when none exists...
|
1999-03-19 08:54:17 +03:00
|
|
|
*aResult = NULL;
|
1999-01-04 11:50:10 +03:00
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
1999-05-26 05:38:36 +04:00
|
|
|
|
1999-11-30 03:14:55 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueServiceImpl::ResolveEventQueue(nsIEventQueue* queueOrConstant, nsIEventQueue* *resultQueue)
|
|
|
|
{
|
|
|
|
if (queueOrConstant == NS_CURRENT_EVENTQ)
|
|
|
|
{
|
|
|
|
return GetThreadEventQueue(NS_CURRENT_THREAD, resultQueue);
|
|
|
|
}
|
|
|
|
else if (queueOrConstant == NS_UI_THREAD_EVENTQ)
|
|
|
|
{
|
|
|
|
return GetThreadEventQueue(NS_UI_THREAD, resultQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
*resultQueue = queueOrConstant;
|
|
|
|
NS_ADDREF(*resultQueue);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-13 02:47:48 +04:00
|
|
|
#ifdef XP_MAC
|
|
|
|
// MAC specific. Will go away someday
|
1999-11-03 08:30:49 +03:00
|
|
|
// Bwah ha ha h ha ah aha ha ha
|
1999-04-13 02:47:48 +04:00
|
|
|
NS_IMETHODIMP nsEventQueueServiceImpl::ProcessEvents()
|
|
|
|
{
|
1999-11-03 08:30:49 +03:00
|
|
|
if (mEventQTable) {
|
|
|
|
EventQueueEntry *entry;
|
|
|
|
nsIEventQueue *queue;
|
|
|
|
|
|
|
|
// never use the hashtable enumerator if there's a chance (there is) that an
|
|
|
|
// event queue entry could be destroyed while inside. this enumerator can
|
|
|
|
// handle that.
|
|
|
|
PR_EnterMonitor(mEventQMonitor);
|
|
|
|
mEnumerator.Reset(mBaseEntry);
|
|
|
|
while ((entry = mEnumerator.Get()) != 0) {
|
|
|
|
PR_ExitMonitor(mEventQMonitor);
|
|
|
|
queue = entry->GetEventQueue();
|
|
|
|
if (queue) {
|
|
|
|
queue->ProcessPendingEvents();
|
|
|
|
NS_RELEASE(queue);
|
|
|
|
}
|
|
|
|
PR_EnterMonitor(mEventQMonitor);
|
|
|
|
}
|
|
|
|
PR_ExitMonitor(mEventQMonitor);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
1999-04-13 02:47:48 +04:00
|
|
|
}
|
1999-01-04 11:50:10 +03:00
|
|
|
|
1999-04-13 02:47:48 +04:00
|
|
|
#endif
|
1999-01-04 11:50:10 +03:00
|
|
|
|
1999-05-26 05:38:36 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|