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
|
|
|
|
2000-02-12 03:27:57 +03:00
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
extern PRLogModuleInfo* gEventQueueLog;
|
|
|
|
extern PRUint32 gEventQueueLogCount;
|
|
|
|
#endif
|
|
|
|
|
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 {
|
2000-01-06 22:46:44 +03:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
public:
|
2000-01-06 22:46:44 +03:00
|
|
|
ThreadKey(PRThread* aID) {
|
1999-10-18 18:59:57 +04:00
|
|
|
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);
|
|
|
|
}
|
2000-01-06 22:46:44 +03:00
|
|
|
|
|
|
|
PRThread* id;
|
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
};
|
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:
|
2000-03-04 06:17:01 +03:00
|
|
|
EventQueueEntry(nsEventQueueServiceImpl *aService, ThreadKey &aKey,
|
|
|
|
PRBool aNative);
|
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 AddQueue(void);
|
2000-03-04 06:17:01 +03:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
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:
|
2000-03-04 06:17:01 +03:00
|
|
|
nsresult MakeNewQueue(PRThread* thread, PRBool aNative,
|
|
|
|
nsIEventQueue **aQueue);
|
|
|
|
|
1999-11-03 08:30:49 +03:00
|
|
|
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
|
|
|
|
2000-03-04 06:17:01 +03:00
|
|
|
EventQueueEntry::EventQueueEntry(nsEventQueueServiceImpl *aService, ThreadKey &aKey, PRBool aNative) :
|
1999-11-03 08:30:49 +03:00
|
|
|
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;
|
2000-03-04 06:17:01 +03:00
|
|
|
MakeNewQueue(aKey.id, aNative, &mQueue);
|
1999-10-18 18:59:57 +04:00
|
|
|
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)
|
2000-02-12 03:27:57 +03:00
|
|
|
ourChain->GetYoungest(&answer);
|
1999-10-18 18:59:57 +04:00
|
|
|
else {
|
|
|
|
NS_ADDREF(mQueue);
|
|
|
|
answer = mQueue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return answer;
|
1999-01-10 11:12:01 +03:00
|
|
|
}
|
1999-01-04 11:50:10 +03:00
|
|
|
|
2000-03-04 06:17:01 +03:00
|
|
|
nsresult EventQueueEntry::MakeNewQueue(PRThread* thread,
|
|
|
|
PRBool aNative,
|
|
|
|
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)) {
|
2000-03-04 06:17:01 +03:00
|
|
|
rv = queue->InitFromPRThread(thread, aNative);
|
1999-10-18 18:59:57 +04:00
|
|
|
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
|
|
|
{
|
2000-03-04 06:17:01 +03:00
|
|
|
PRBool native;
|
1999-10-18 18:59:57 +04:00
|
|
|
nsIEventQueue *newQueue = NULL;
|
|
|
|
nsresult rv = NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
if (mQueue) {
|
2000-03-04 06:17:01 +03:00
|
|
|
mQueue->IsQueueNative(&native);
|
|
|
|
rv = MakeNewQueue(PR_GetCurrentThread(), native, &newQueue);
|
1999-10-18 18:59:57 +04:00
|
|
|
|
|
|
|
// 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;
|
2000-02-12 03:27:57 +03:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
if (!gEventQueueLog)
|
|
|
|
gEventQueueLog = PR_NewLogModule("nseventqueue");
|
|
|
|
#endif
|
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... */
|
2000-03-06 00:26:01 +03:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsEventQueueServiceImpl, nsIEventQueueService)
|
1999-01-04 11:50:10 +03:00
|
|
|
|
|
|
|
/* nsIEventQueueService interface implementation... */
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-03-04 06:17:01 +03:00
|
|
|
nsEventQueueServiceImpl::CreateThreadEventQueue()
|
|
|
|
{
|
|
|
|
return CreateEventQueue(PR_GetCurrentThread(), PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueServiceImpl::CreateMonitoredThreadEventQueue()
|
1999-10-19 23:18:27 +04:00
|
|
|
{
|
2000-03-04 06:17:01 +03:00
|
|
|
return CreateEventQueue(PR_GetCurrentThread(), PR_FALSE);
|
1999-10-19 23:18:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventQueueServiceImpl::CreateFromIThread(
|
2000-03-04 06:17:01 +03:00
|
|
|
nsIThread *aThread, PRBool aNative, nsIEventQueue **aResult)
|
1999-10-19 23:18:27 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
PRThread *prThread;
|
|
|
|
|
|
|
|
rv = aThread->GetPRThread(&prThread);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2000-03-04 06:17:01 +03:00
|
|
|
rv = CreateEventQueue(prThread, aNative); // addrefs
|
1999-10-19 23:18:27 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = GetThreadEventQueue(prThread, aResult); // doesn't addref
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-03-04 06:17:01 +03:00
|
|
|
nsEventQueueServiceImpl::CreateEventQueue(PRThread *aThread, PRBool aNative)
|
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) {
|
2000-03-04 06:17:01 +03:00
|
|
|
evQueueEntry = new EventQueueEntry(this, key, aNative);
|
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;
|
2000-02-12 03:27:57 +03:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
PLEventQueue *equeue;
|
|
|
|
nsCOMPtr<nsIEventQueue> iqueue = aEntry->GetEventQueue();
|
|
|
|
iqueue->GetPLEventQueue(&equeue);
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: Service add queue entry [queue=%lx]",(long)equeue));
|
|
|
|
++gEventQueueLogCount;
|
|
|
|
#endif
|
1999-11-03 08:30:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsEventQueueServiceImpl::RemoveEventQueueEntry(EventQueueEntry *aEntry)
|
|
|
|
{
|
|
|
|
mEventQTable->Remove(aEntry->TheThreadKey());
|
|
|
|
if (mBaseEntry == aEntry)
|
|
|
|
mBaseEntry = aEntry->Next();
|
|
|
|
mEnumerator.Skip(aEntry);
|
|
|
|
aEntry->Unlink();
|
2000-02-12 03:27:57 +03:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
PLEventQueue *equeue;
|
|
|
|
nsCOMPtr<nsIEventQueue> iqueue = aEntry->GetEventQueue();
|
|
|
|
iqueue->GetPLEventQueue(&equeue);
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: Service remove queue entry [queue=%lx]",(long)equeue));
|
|
|
|
++gEventQueueLogCount;
|
|
|
|
#endif
|
1999-11-03 08:30:49 +03:00
|
|
|
}
|
|
|
|
|
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);
|
2000-03-04 06:17:01 +03:00
|
|
|
NS_ASSERTION(evQueueEntry, "pushed event queue on top of nothing");
|
1999-05-05 03:35:47 +04:00
|
|
|
if (NULL == evQueueEntry) {
|
2000-03-04 06:17:01 +03:00
|
|
|
// shouldn't happen. as a fallback, we guess you wanted a native queue
|
|
|
|
evQueueEntry = new EventQueueEntry(this, key, PR_TRUE);
|
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);
|
2000-02-12 03:27:57 +03:00
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
PLEventQueue *equeue;
|
|
|
|
(*aNewQueue)->GetPLEventQueue(&equeue);
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: Service push queue [queue=%lx]",(long)equeue));
|
|
|
|
++gEventQueueLogCount;
|
|
|
|
#endif
|
1999-10-18 18:59:57 +04:00
|
|
|
}
|
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.
|
2000-02-12 03:27:57 +03:00
|
|
|
if (refcnt > 0) {
|
|
|
|
#if defined(PR_LOGGING) && defined(DEBUG_danm)
|
|
|
|
PLEventQueue *equeue;
|
|
|
|
aQueue->GetPLEventQueue(&equeue);
|
|
|
|
PR_LOG(gEventQueueLog, PR_LOG_DEBUG,
|
|
|
|
("EventQueue: Service pop queue [queue=%lx]",(long)equeue));
|
|
|
|
++gEventQueueLogCount;
|
|
|
|
#endif
|
1999-10-18 18:59:57 +04:00
|
|
|
evQueueEntry->RemoveQueue(aQueue);
|
2000-02-12 03:27:57 +03:00
|
|
|
}
|
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;
|
|
|
|
}
|