Remove duplicate nsTimer.cpp files.
git-svn-id: svn://10.0.0.236/trunk@33553 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
parent
5fafa9d1ee
commit
8f44cd7f7c
@ -1,191 +0,0 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
#include "nsITimer.h"
|
||||
#include "nsITimerCallback.h"
|
||||
#include "nsCRT.h"
|
||||
#include "prlog.h"
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
static NS_DEFINE_IID(kITimerIID, NS_ITIMER_IID);
|
||||
|
||||
extern "C" gint nsTimerExpired(gpointer aCallData);
|
||||
|
||||
/*
|
||||
* Implementation of timers using Gtk timer facility
|
||||
*/
|
||||
class TimerImpl : public nsITimer {
|
||||
public:
|
||||
|
||||
public:
|
||||
TimerImpl();
|
||||
virtual ~TimerImpl();
|
||||
|
||||
virtual nsresult Init(nsTimerCallbackFunc aFunc,
|
||||
void *aClosure,
|
||||
// PRBool aRepeat,
|
||||
PRUint32 aDelay);
|
||||
|
||||
virtual nsresult Init(nsITimerCallback *aCallback,
|
||||
// PRBool aRepeat,
|
||||
PRUint32 aDelay);
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
virtual void Cancel();
|
||||
virtual PRUint32 GetDelay() { return mDelay; }
|
||||
virtual void SetDelay(PRUint32 aDelay) { mDelay=aDelay; };
|
||||
virtual void* GetClosure() { return mClosure; }
|
||||
|
||||
void FireTimeout();
|
||||
|
||||
private:
|
||||
nsresult Init(PRUint32 aDelay);
|
||||
|
||||
PRUint32 mDelay;
|
||||
nsTimerCallbackFunc mFunc;
|
||||
void *mClosure;
|
||||
nsITimerCallback *mCallback;
|
||||
// PRBool mRepeat;
|
||||
TimerImpl *mNext;
|
||||
guint mTimerId;
|
||||
};
|
||||
|
||||
void TimerImpl::FireTimeout()
|
||||
{
|
||||
if (mFunc != NULL) {
|
||||
(*mFunc)(this, mClosure);
|
||||
}
|
||||
else if (mCallback != NULL) {
|
||||
mCallback->Notify(this); // Fire the timer
|
||||
}
|
||||
|
||||
// Always repeating here
|
||||
|
||||
// if (mRepeat)
|
||||
// mTimerId = gtk_timeout_add(aDelay, nsTimerExpired, this);
|
||||
}
|
||||
|
||||
|
||||
TimerImpl::TimerImpl()
|
||||
{
|
||||
// printf("TimerImple::TimerImpl called for %p\n", this);
|
||||
NS_INIT_REFCNT();
|
||||
mFunc = NULL;
|
||||
mCallback = NULL;
|
||||
mNext = NULL;
|
||||
mTimerId = 0;
|
||||
mDelay = 0;
|
||||
mClosure = NULL;
|
||||
}
|
||||
|
||||
TimerImpl::~TimerImpl()
|
||||
{
|
||||
//printf("TimerImpl::~TimerImpl called for %p\n", this);
|
||||
Cancel();
|
||||
NS_IF_RELEASE(mCallback);
|
||||
}
|
||||
|
||||
nsresult
|
||||
TimerImpl::Init(nsTimerCallbackFunc aFunc,
|
||||
void *aClosure,
|
||||
// PRBool aRepeat,
|
||||
PRUint32 aDelay)
|
||||
{
|
||||
//printf("TimerImpl::Init called with func + closure for %p\n", this);
|
||||
mFunc = aFunc;
|
||||
mClosure = aClosure;
|
||||
// mRepeat = aRepeat;
|
||||
|
||||
if ((aDelay > 10000) || (aDelay < 0)) {
|
||||
printf("Timer::Init() called with bogus value \"%d\"! Not enabling timer.\n",
|
||||
aDelay);
|
||||
return Init(aDelay);
|
||||
}
|
||||
|
||||
mTimerId = gtk_timeout_add(aDelay, nsTimerExpired, this);
|
||||
|
||||
return Init(aDelay);
|
||||
}
|
||||
|
||||
nsresult
|
||||
TimerImpl::Init(nsITimerCallback *aCallback,
|
||||
// PRBool aRepeat,
|
||||
PRUint32 aDelay)
|
||||
{
|
||||
//printf("TimerImpl::Init called with callback only for %p\n", this);
|
||||
mCallback = aCallback;
|
||||
NS_ADDREF(mCallback);
|
||||
// mRepeat = aRepeat;
|
||||
if ((aDelay > 10000) || (aDelay < 0)) {
|
||||
printf("Timer::Init() called with bogus value \"%d\"! Not enabling timer.\n",
|
||||
aDelay);
|
||||
return Init(aDelay);
|
||||
}
|
||||
|
||||
mTimerId = gtk_timeout_add(aDelay, nsTimerExpired, this);
|
||||
|
||||
return Init(aDelay);
|
||||
}
|
||||
|
||||
nsresult
|
||||
TimerImpl::Init(PRUint32 aDelay)
|
||||
{
|
||||
//printf("TimerImpl::Init called with delay %d only for %p\n", aDelay, this);
|
||||
|
||||
mDelay = aDelay;
|
||||
// NS_ADDREF(this);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS(TimerImpl, kITimerIID)
|
||||
|
||||
|
||||
void
|
||||
TimerImpl::Cancel()
|
||||
{
|
||||
//printf("TimerImpl::Cancel called for %p\n", this);
|
||||
TimerImpl *me = this;
|
||||
if (mTimerId)
|
||||
gtk_timeout_remove(mTimerId);
|
||||
}
|
||||
|
||||
NS_BASE nsresult NS_NewTimer(nsITimer** aInstancePtrResult)
|
||||
{
|
||||
NS_PRECONDITION(nsnull != aInstancePtrResult, "null ptr");
|
||||
if (nsnull == aInstancePtrResult) {
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
|
||||
TimerImpl *timer = new TimerImpl();
|
||||
if (nsnull == timer) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
return timer->QueryInterface(kITimerIID, (void **) aInstancePtrResult);
|
||||
}
|
||||
|
||||
gint nsTimerExpired(gpointer aCallData)
|
||||
{
|
||||
//printf("nsTimerExpired for %p\n", aCallData);
|
||||
TimerImpl* timer = (TimerImpl *)aCallData;
|
||||
timer->FireTimeout();
|
||||
return 0;
|
||||
}
|
||||
@ -1,357 +0,0 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
//
|
||||
// Mac implementation of the nsITimer interface
|
||||
//
|
||||
|
||||
|
||||
|
||||
#include "nsITimer.h"
|
||||
#include "nsITimerCallback.h"
|
||||
#include "prlog.h"
|
||||
#include "nsRepeater.h"
|
||||
|
||||
#include <list.h>
|
||||
#include <Events.h>
|
||||
|
||||
|
||||
|
||||
#pragma mark class TimerImpl
|
||||
|
||||
//========================================================================================
|
||||
class TimerImpl : public nsITimer
|
||||
// TimerImpl implements nsITimer API
|
||||
//========================================================================================
|
||||
{
|
||||
friend class TimerPeriodical;
|
||||
|
||||
private:
|
||||
nsTimerCallbackFunc mCallbackFunc;
|
||||
nsITimerCallback * mCallbackObject;
|
||||
void * mClosure;
|
||||
PRUint32 mDelay;
|
||||
PRUint32 mFireTime; // Timer should fire when TickCount >= this number
|
||||
TimerImpl * mPrev;
|
||||
TimerImpl * mNext;
|
||||
|
||||
public:
|
||||
|
||||
// constructors
|
||||
|
||||
TimerImpl();
|
||||
|
||||
virtual ~TimerImpl();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
PRUint32 GetFireTime() const { return mFireTime; }
|
||||
|
||||
void Fire();
|
||||
|
||||
// nsITimer overrides
|
||||
|
||||
virtual nsresult Init(nsTimerCallbackFunc aFunc,
|
||||
void *aClosure,
|
||||
PRUint32 aDelay);
|
||||
|
||||
virtual nsresult Init(nsITimerCallback *aCallback,
|
||||
PRUint32 aDelay);
|
||||
|
||||
virtual void Cancel();
|
||||
|
||||
virtual PRUint32 GetDelay();
|
||||
|
||||
virtual void SetDelay(PRUint32 aDelay);
|
||||
|
||||
virtual void* GetClosure();
|
||||
|
||||
#if DEBUG
|
||||
enum {
|
||||
eGoodTimerSignature = 'Barf',
|
||||
eDeletedTimerSignature = 'oops'
|
||||
};
|
||||
|
||||
Boolean IsGoodTimer() const { return (mSignature == eGoodTimerSignature); }
|
||||
#endif
|
||||
|
||||
private:
|
||||
// Calculates mFireTime too
|
||||
void SetDelaySelf( PRUint32 aDelay );
|
||||
|
||||
#if DEBUG
|
||||
UInt32 mSignature;
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
#pragma mark class TimerPeriodical
|
||||
|
||||
//========================================================================================
|
||||
class TimerPeriodical : public Repeater
|
||||
// TimerPeriodical is a singleton Repeater subclass that fires
|
||||
// off TimerImpl. The firing is done on idle.
|
||||
//========================================================================================
|
||||
{
|
||||
static TimerPeriodical * gPeriodical;
|
||||
|
||||
TimerImpl* mTimers;
|
||||
|
||||
public:
|
||||
// Returns the singleton instance
|
||||
static TimerPeriodical * GetPeriodical();
|
||||
|
||||
TimerPeriodical();
|
||||
|
||||
virtual ~TimerPeriodical();
|
||||
|
||||
virtual void RepeatAction( const EventRecord &inMacEvent);
|
||||
|
||||
nsresult AddTimer( TimerImpl * aTimer);
|
||||
|
||||
nsresult RemoveTimer( TimerImpl * aTimer);
|
||||
|
||||
};
|
||||
|
||||
|
||||
//========================================================================================
|
||||
// TimerImpl implementation
|
||||
//========================================================================================
|
||||
|
||||
NS_IMPL_ISUPPORTS(TimerImpl, nsITimer::GetIID())
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
TimerImpl::TimerImpl()
|
||||
//----------------------------------------------------------------------------------------
|
||||
: mCallbackFunc(nsnull)
|
||||
, mCallbackObject(nsnull)
|
||||
, mClosure(nsnull)
|
||||
, mDelay(0)
|
||||
, mFireTime(0)
|
||||
, mPrev(nsnull)
|
||||
, mNext(nsnull)
|
||||
#if DEBUG
|
||||
, mSignature(eGoodTimerSignature)
|
||||
#endif
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
TimerImpl::~TimerImpl()
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
Cancel();
|
||||
NS_IF_RELEASE(mCallbackObject);
|
||||
#if DEBUG
|
||||
mSignature = eDeletedTimerSignature;
|
||||
#endif
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
nsresult TimerImpl::Init(nsTimerCallbackFunc aFunc,
|
||||
void *aClosure,
|
||||
PRUint32 aDelay)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
mCallbackFunc = aFunc;
|
||||
mClosure = aClosure;
|
||||
SetDelaySelf(aDelay);
|
||||
return TimerPeriodical::GetPeriodical()->AddTimer(this);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
nsresult TimerImpl::Init(nsITimerCallback *aCallback,
|
||||
PRUint32 aDelay)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
NS_ADDREF(aCallback);
|
||||
mCallbackObject = aCallback;
|
||||
SetDelaySelf(aDelay);
|
||||
return TimerPeriodical::GetPeriodical()->AddTimer(this);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
void TimerImpl::Cancel()
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
TimerPeriodical::GetPeriodical()->RemoveTimer(this);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
PRUint32 TimerImpl::GetDelay()
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
return mDelay;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
void TimerImpl::SetDelay(PRUint32 aDelay)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
SetDelaySelf(aDelay);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
void* TimerImpl::GetClosure()
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
return mClosure;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
void TimerImpl::Fire()
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
NS_PRECONDITION(mRefCnt > 0, "Firing a disposed Timer!");
|
||||
if (mCallbackFunc != NULL) {
|
||||
(*mCallbackFunc)(this, mClosure);
|
||||
}
|
||||
else if (mCallbackObject != NULL) {
|
||||
mCallbackObject->Notify(this); // Fire the timer
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
void TimerImpl::SetDelaySelf( PRUint32 aDelay )
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
|
||||
mDelay = aDelay;
|
||||
mFireTime = TickCount() + (mDelay * 3) / 50; // We need mFireTime in ticks (1/60th)
|
||||
// but aDelay is in 1000th (60/1000 = 3/50)
|
||||
}
|
||||
|
||||
TimerPeriodical * TimerPeriodical::gPeriodical = nsnull;
|
||||
|
||||
TimerPeriodical * TimerPeriodical::GetPeriodical()
|
||||
{
|
||||
if (gPeriodical == NULL)
|
||||
gPeriodical = new TimerPeriodical();
|
||||
return gPeriodical;
|
||||
}
|
||||
|
||||
TimerPeriodical::TimerPeriodical()
|
||||
{
|
||||
mTimers = nsnull;
|
||||
}
|
||||
|
||||
TimerPeriodical::~TimerPeriodical()
|
||||
{
|
||||
PR_ASSERT(mTimers == 0);
|
||||
}
|
||||
|
||||
nsresult TimerPeriodical::AddTimer( TimerImpl * aTimer)
|
||||
{
|
||||
// make sure it's not already there
|
||||
RemoveTimer(aTimer);
|
||||
// keep list sorted by fire time
|
||||
if (mTimers)
|
||||
{
|
||||
if (aTimer->GetFireTime() < mTimers->GetFireTime())
|
||||
{
|
||||
mTimers->mPrev = aTimer;
|
||||
aTimer->mNext = mTimers;
|
||||
mTimers = aTimer;
|
||||
}
|
||||
else
|
||||
{
|
||||
TimerImpl *t = mTimers;
|
||||
TimerImpl *prevt;
|
||||
// we know we will enter the while loop at least the first
|
||||
// time, and thus prevt will be initialized
|
||||
while (t && (t->GetFireTime() <= aTimer->GetFireTime()))
|
||||
{
|
||||
prevt = t;
|
||||
t = t->mNext;
|
||||
}
|
||||
aTimer->mPrev = prevt;
|
||||
aTimer->mNext = prevt->mNext;
|
||||
prevt->mNext = aTimer;
|
||||
if (aTimer->mNext) aTimer->mNext->mPrev = aTimer;
|
||||
}
|
||||
}
|
||||
else mTimers = aTimer;
|
||||
|
||||
StartRepeating();
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult TimerPeriodical::RemoveTimer( TimerImpl * aTimer)
|
||||
{
|
||||
TimerImpl* t = mTimers;
|
||||
TimerImpl* next_t = nsnull;
|
||||
if (t) next_t = t->mNext;
|
||||
while (t)
|
||||
{
|
||||
if (t == aTimer)
|
||||
{
|
||||
if (mTimers == t) mTimers = t->mNext;
|
||||
if (t->mPrev) t->mPrev->mNext = t->mNext;
|
||||
if (t->mNext) t->mNext->mPrev = t->mPrev;
|
||||
t->mNext = nsnull;
|
||||
t->mPrev = nsnull;
|
||||
}
|
||||
t = next_t;
|
||||
if (t) next_t = t->mNext;
|
||||
}
|
||||
|
||||
if ( mTimers == nsnull )
|
||||
StopRepeating();
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// Called through every event loop
|
||||
// Loops through the list of available timers, and
|
||||
// fires off the appropriate ones
|
||||
void TimerPeriodical::RepeatAction( const EventRecord &inMacEvent)
|
||||
{
|
||||
PRBool done = false;
|
||||
while (!done)
|
||||
{
|
||||
TimerImpl* t = mTimers;
|
||||
while (t)
|
||||
{
|
||||
NS_ASSERTION(t->IsGoodTimer(), "Bad timer!");
|
||||
|
||||
if (t->GetFireTime() <= inMacEvent.when)
|
||||
{
|
||||
RemoveTimer(t);
|
||||
t->Fire();
|
||||
break;
|
||||
}
|
||||
t = t->mNext;
|
||||
}
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
NS_BASE nsresult NS_NewTimer(nsITimer** aInstancePtrResult)
|
||||
{
|
||||
NS_PRECONDITION(nsnull != aInstancePtrResult, "null ptr");
|
||||
if (nsnull == aInstancePtrResult) {
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
TimerImpl *timer = new TimerImpl();
|
||||
if (nsnull == timer) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
return timer->QueryInterface(nsITimer::GetIID(), (void **) aInstancePtrResult);
|
||||
}
|
||||
@ -1,362 +0,0 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
#include "nsITimer.h"
|
||||
#include "nsITimerCallback.h"
|
||||
#include "nsCRT.h"
|
||||
#include "prlog.h"
|
||||
#include <stdio.h>
|
||||
#include <windows.h>
|
||||
#include <limits.h>
|
||||
|
||||
static NS_DEFINE_IID(kITimerIID, NS_ITIMER_IID);
|
||||
|
||||
/*
|
||||
* Implementation of timers lifted from Windows front-end file timer.cpp
|
||||
*/
|
||||
class TimerImpl : public nsITimer {
|
||||
public:
|
||||
static TimerImpl *gTimerList;
|
||||
static UINT gWindowsTimer;
|
||||
static DWORD gNextFire;
|
||||
|
||||
static void ProcessTimeouts(DWORD aNow);
|
||||
static void SyncTimeoutPeriod(DWORD aTickCount);
|
||||
|
||||
public:
|
||||
TimerImpl();
|
||||
virtual ~TimerImpl();
|
||||
|
||||
virtual nsresult Init(nsTimerCallbackFunc aFunc,
|
||||
void *aClosure,
|
||||
// PRBool aRepeat,
|
||||
PRUint32 aDelay);
|
||||
|
||||
virtual nsresult Init(nsITimerCallback *aCallback,
|
||||
// PRBool aRepeat,
|
||||
PRUint32 aDelay);
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
virtual void Cancel();
|
||||
void Fire(DWORD aNow);
|
||||
|
||||
virtual PRUint32 GetDelay() { return mDelay; }
|
||||
virtual void SetDelay(PRUint32 aDelay) {};
|
||||
|
||||
virtual void* GetClosure() { return mClosure; }
|
||||
|
||||
private:
|
||||
nsresult Init(PRUint32 aDelay);
|
||||
|
||||
PRUint32 mDelay;
|
||||
nsTimerCallbackFunc mFunc;
|
||||
void *mClosure;
|
||||
nsITimerCallback *mCallback;
|
||||
DWORD mFireTime;
|
||||
// PRBool mRepeat;
|
||||
TimerImpl *mNext;
|
||||
};
|
||||
|
||||
TimerImpl *TimerImpl::gTimerList = NULL;
|
||||
UINT TimerImpl::gWindowsTimer = 0;
|
||||
DWORD TimerImpl::gNextFire = (DWORD)-1;
|
||||
|
||||
void CALLBACK FireTimeout(HWND aWindow,
|
||||
UINT aMessage,
|
||||
UINT aTimerID,
|
||||
DWORD aTime)
|
||||
{
|
||||
static BOOL bCanEnter = TRUE;
|
||||
|
||||
// Don't allow old timer messages in here.
|
||||
if(aMessage != WM_TIMER) {
|
||||
PR_ASSERT(0);
|
||||
return;
|
||||
}
|
||||
|
||||
if(aTimerID != TimerImpl::gWindowsTimer) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Block only one entry into this function, or else.
|
||||
if(bCanEnter) {
|
||||
bCanEnter = FALSE;
|
||||
// see if we need to fork off any timeout functions
|
||||
if(TimerImpl::gTimerList) {
|
||||
TimerImpl::ProcessTimeouts(aTime);
|
||||
}
|
||||
bCanEnter = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
// Function to correctly have the timer be set.
|
||||
void
|
||||
TimerImpl::SyncTimeoutPeriod(DWORD aTickCount)
|
||||
{
|
||||
// May want us to set tick count ourselves.
|
||||
if(aTickCount == 0) {
|
||||
aTickCount = ::GetTickCount();
|
||||
}
|
||||
|
||||
// If there's no list, we should clear the timer.
|
||||
if(!gTimerList) {
|
||||
if(gWindowsTimer) {
|
||||
::KillTimer(NULL, gWindowsTimer);
|
||||
gWindowsTimer = 0;
|
||||
gNextFire = (DWORD)-1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// See if we need to clear the current timer.
|
||||
// Curcumstances are that if the timer will not
|
||||
// fire on time for the next timeout.
|
||||
BOOL bSetTimer = FALSE;
|
||||
TimerImpl *pTimeout = gTimerList;
|
||||
if(gWindowsTimer) {
|
||||
if(pTimeout->mFireTime != gNextFire) {
|
||||
::KillTimer(NULL, gWindowsTimer);
|
||||
gWindowsTimer = 0;
|
||||
gNextFire = (DWORD)-1;
|
||||
|
||||
// Set the timer.
|
||||
bSetTimer = TRUE;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// No timer set, attempt.
|
||||
bSetTimer = TRUE;
|
||||
}
|
||||
|
||||
if(bSetTimer) {
|
||||
DWORD dwFireWhen = pTimeout->mFireTime > aTickCount ?
|
||||
pTimeout->mFireTime - aTickCount : 0;
|
||||
if(dwFireWhen > UINT_MAX) {
|
||||
dwFireWhen = UINT_MAX;
|
||||
}
|
||||
UINT uFireWhen = (UINT)dwFireWhen;
|
||||
|
||||
PR_ASSERT(gWindowsTimer == 0);
|
||||
gWindowsTimer = ::SetTimer(NULL, 0, uFireWhen, (TIMERPROC)FireTimeout);
|
||||
|
||||
if(gWindowsTimer) {
|
||||
// Set the fire time.
|
||||
gNextFire = pTimeout->mFireTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Walk down the timeout list and launch anyone appropriate
|
||||
void
|
||||
TimerImpl::ProcessTimeouts(DWORD aNow)
|
||||
{
|
||||
TimerImpl *p = gTimerList;
|
||||
if(aNow == 0) {
|
||||
aNow = ::GetTickCount();
|
||||
}
|
||||
|
||||
BOOL bCalledSync = FALSE;
|
||||
|
||||
// loop over all entries
|
||||
while(p) {
|
||||
// send it
|
||||
if(p->mFireTime < aNow) {
|
||||
// Make sure that the timer cannot be deleted during the
|
||||
// Fire(...) call which may release *all* other references
|
||||
// to p...
|
||||
NS_ADDREF(p);
|
||||
p->Fire(aNow);
|
||||
|
||||
// Clear the timer.
|
||||
// Period synced.
|
||||
p->Cancel();
|
||||
bCalledSync = TRUE;
|
||||
NS_RELEASE(p);
|
||||
|
||||
// Reset the loop (can't look at p->pNext now, and called
|
||||
// code may have added/cleared timers).
|
||||
// (could do this by going recursive and returning).
|
||||
p = gTimerList;
|
||||
} else {
|
||||
// Make sure we fire an timer.
|
||||
// Also, we need to check to see if things are backing up (they
|
||||
// may be asking to be fired long before we ever get to them,
|
||||
// and we don't want to pass in negative values to the real
|
||||
// timer code, or it takes days to fire....
|
||||
if(bCalledSync == FALSE) {
|
||||
SyncTimeoutPeriod(aNow);
|
||||
bCalledSync = TRUE;
|
||||
}
|
||||
// Get next timer.
|
||||
p = p->mNext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TimerImpl::TimerImpl()
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
mFunc = NULL;
|
||||
mCallback = NULL;
|
||||
mNext = NULL;
|
||||
mClosure = nsnull;
|
||||
}
|
||||
|
||||
TimerImpl::~TimerImpl()
|
||||
{
|
||||
Cancel();
|
||||
NS_IF_RELEASE(mCallback);
|
||||
}
|
||||
|
||||
nsresult
|
||||
TimerImpl::Init(nsTimerCallbackFunc aFunc,
|
||||
void *aClosure,
|
||||
// PRBool aRepeat,
|
||||
PRUint32 aDelay)
|
||||
{
|
||||
mFunc = aFunc;
|
||||
mClosure = aClosure;
|
||||
// mRepeat = aRepeat;
|
||||
|
||||
return Init(aDelay);
|
||||
}
|
||||
|
||||
nsresult
|
||||
TimerImpl::Init(nsITimerCallback *aCallback,
|
||||
// PRBool aRepeat,
|
||||
PRUint32 aDelay)
|
||||
{
|
||||
mCallback = aCallback;
|
||||
NS_ADDREF(mCallback);
|
||||
// mRepeat = aRepeat;
|
||||
|
||||
return Init(aDelay);
|
||||
}
|
||||
|
||||
nsresult
|
||||
TimerImpl::Init(PRUint32 aDelay)
|
||||
{
|
||||
DWORD dwNow = ::GetTickCount();
|
||||
|
||||
mDelay = aDelay;
|
||||
mFireTime = (DWORD) aDelay + dwNow;
|
||||
mNext = NULL;
|
||||
|
||||
// add it to the list
|
||||
if(!gTimerList) {
|
||||
// no list add it
|
||||
gTimerList = this;
|
||||
}
|
||||
else {
|
||||
|
||||
// is it before everything else on the list?
|
||||
if(mFireTime < gTimerList->mFireTime) {
|
||||
|
||||
mNext = gTimerList;
|
||||
gTimerList = this;
|
||||
|
||||
} else {
|
||||
|
||||
TimerImpl * pPrev = gTimerList;
|
||||
TimerImpl * pCurrent = gTimerList;
|
||||
|
||||
while(pCurrent && (pCurrent->mFireTime <= mFireTime)) {
|
||||
pPrev = pCurrent;
|
||||
pCurrent = pCurrent->mNext;
|
||||
}
|
||||
|
||||
PR_ASSERT(pPrev);
|
||||
|
||||
// insert it after pPrev (this could be at the end of the list)
|
||||
mNext = pPrev->mNext;
|
||||
pPrev->mNext = this;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
NS_ADDREF(this);
|
||||
|
||||
// Sync the timer fire period.
|
||||
SyncTimeoutPeriod(dwNow);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS(TimerImpl, kITimerIID)
|
||||
|
||||
void
|
||||
TimerImpl::Fire(DWORD aNow)
|
||||
{
|
||||
if (mFunc != NULL) {
|
||||
(*mFunc)(this, mClosure);
|
||||
}
|
||||
else if (mCallback != NULL) {
|
||||
mCallback->Notify(this);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
TimerImpl::Cancel()
|
||||
{
|
||||
TimerImpl *me = this;
|
||||
|
||||
if(gTimerList == this) {
|
||||
|
||||
// first element in the list lossage
|
||||
gTimerList = mNext;
|
||||
|
||||
} else {
|
||||
|
||||
// walk until no next pointer
|
||||
for(TimerImpl * p = gTimerList; p && p->mNext && (p->mNext != this); p = p->mNext)
|
||||
;
|
||||
|
||||
// if we found something valid pull it out of the list
|
||||
if(p && p->mNext && p->mNext == this) {
|
||||
p->mNext = mNext;
|
||||
|
||||
} else {
|
||||
// get out before we delete something that looks bogus
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// if we got here it must have been a valid element so trash it
|
||||
NS_RELEASE(me);
|
||||
|
||||
// If there's now no be sure to clear the timer.
|
||||
SyncTimeoutPeriod(0);
|
||||
}
|
||||
|
||||
NS_BASE nsresult NS_NewTimer(nsITimer** aInstancePtrResult)
|
||||
{
|
||||
NS_PRECONDITION(nsnull != aInstancePtrResult, "null ptr");
|
||||
if (nsnull == aInstancePtrResult) {
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
|
||||
TimerImpl *timer = new TimerImpl();
|
||||
if (nsnull == timer) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
return timer->QueryInterface(kITimerIID, (void **) aInstancePtrResult);
|
||||
}
|
||||
@ -1,295 +0,0 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include "nsITimer.h"
|
||||
#include "nsITimerCallback.h"
|
||||
#include "prlog.h"
|
||||
|
||||
static NS_DEFINE_IID(kITimerIID, NS_ITIMER_IID);
|
||||
|
||||
extern "C" int NS_TimeToNextTimeout(struct timeval *aTimer);
|
||||
extern "C" void NS_ProcessTimeouts(void);
|
||||
|
||||
class TimerImpl : public nsITimer
|
||||
{
|
||||
public:
|
||||
static TimerImpl *gTimerList;
|
||||
static struct timeval gTimer;
|
||||
static struct timeval gNextFire;
|
||||
static void ProcessTimeouts(struct timeval *aNow);
|
||||
TimerImpl();
|
||||
~TimerImpl();
|
||||
|
||||
virtual nsresult Init(nsTimerCallbackFunc aFunc,
|
||||
void *aClosure,
|
||||
PRUint32 aDelay);
|
||||
|
||||
virtual nsresult Init(nsITimerCallback *aCallback,
|
||||
PRUint32 aDelay);
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
virtual void Cancel();
|
||||
void Fire(struct timeval *aNow);
|
||||
|
||||
virtual PRUint32 GetDelay() { return 0; };
|
||||
virtual void SetDelay(PRUint32 aDelay) {};
|
||||
virtual void *GetClosure() { return mClosure; }
|
||||
|
||||
// this needs to be public so that the mainloop can
|
||||
// find the next fire time...
|
||||
struct timeval mFireTime;
|
||||
TimerImpl *mNext;
|
||||
|
||||
private:
|
||||
nsresult Init(PRUint32 aDelay);
|
||||
nsTimerCallbackFunc mFunc;
|
||||
void *mClosure;
|
||||
PRUint32 mDelay;
|
||||
nsITimerCallback *mCallback;
|
||||
|
||||
};
|
||||
|
||||
|
||||
TimerImpl *TimerImpl::gTimerList = NULL;
|
||||
struct timeval TimerImpl::gTimer = {0, 0};
|
||||
struct timeval TimerImpl::gNextFire = {0, 0};
|
||||
|
||||
TimerImpl::TimerImpl()
|
||||
{
|
||||
//printf("TimerImpl::TimerImpl (%p) called.\n",
|
||||
//this);
|
||||
NS_INIT_REFCNT();
|
||||
mFunc = NULL;
|
||||
mCallback = NULL;
|
||||
mNext = NULL;
|
||||
mClosure = NULL;
|
||||
}
|
||||
|
||||
TimerImpl::~TimerImpl()
|
||||
{
|
||||
//printf("TimerImpl::~TimerImpl (%p) called.\n",
|
||||
// this);
|
||||
Cancel();
|
||||
NS_IF_RELEASE(mCallback);
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS(TimerImpl, kITimerIID)
|
||||
|
||||
nsresult
|
||||
TimerImpl::Init(nsTimerCallbackFunc aFunc,
|
||||
void *aClosure,
|
||||
PRUint32 aDelay)
|
||||
{
|
||||
mFunc = aFunc;
|
||||
mClosure = aClosure;
|
||||
return Init(aDelay);
|
||||
}
|
||||
|
||||
nsresult
|
||||
TimerImpl::Init(nsITimerCallback *aCallback,
|
||||
PRUint32 aDelay)
|
||||
{
|
||||
mCallback = aCallback;
|
||||
NS_ADDREF(mCallback);
|
||||
|
||||
return Init(aDelay);
|
||||
}
|
||||
|
||||
nsresult
|
||||
TimerImpl::Init(PRUint32 aDelay)
|
||||
{
|
||||
struct timeval Now;
|
||||
// printf("TimerImpl::Init (%p) called with delay %d\n",
|
||||
//this, aDelay);
|
||||
// get the cuurent time
|
||||
gettimeofday(&Now, NULL);
|
||||
mFireTime.tv_sec = Now.tv_sec + (aDelay / 1000);
|
||||
mFireTime.tv_usec = Now.tv_usec + (aDelay * 1000);
|
||||
//printf("fire set to %ld / %ld\n",
|
||||
//mFireTime.tv_sec, mFireTime.tv_usec);
|
||||
// set the next pointer to nothing.
|
||||
mNext = NULL;
|
||||
// add ourself to the list
|
||||
if (!gTimerList) {
|
||||
// no list here. I'm the start!
|
||||
//printf("This is the beginning of the list..\n");
|
||||
gTimerList = this;
|
||||
}
|
||||
else {
|
||||
// is it before everything else on the list?
|
||||
if ((mFireTime.tv_sec < gTimerList->mFireTime.tv_sec) &&
|
||||
(mFireTime.tv_usec < gTimerList->mFireTime.tv_usec)) {
|
||||
// printf("This is before the head of the list...\n");
|
||||
mNext = gTimerList;
|
||||
gTimerList = this;
|
||||
}
|
||||
else {
|
||||
TimerImpl *pPrev = gTimerList;
|
||||
TimerImpl *pCurrent = gTimerList;
|
||||
while (pCurrent && ((pCurrent->mFireTime.tv_sec <= mFireTime.tv_sec) &&
|
||||
(pCurrent->mFireTime.tv_usec <= mFireTime.tv_usec))) {
|
||||
pPrev = pCurrent;
|
||||
pCurrent = pCurrent->mNext;
|
||||
}
|
||||
PR_ASSERT(pPrev);
|
||||
|
||||
// isnert it after pPrev ( this could be at the end of the list)
|
||||
mNext = pPrev->mNext;
|
||||
pPrev->mNext = this;
|
||||
}
|
||||
}
|
||||
NS_ADDREF(this);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void
|
||||
TimerImpl::Fire(struct timeval *aNow)
|
||||
{
|
||||
// printf("TimerImpl::Fire (%p) called at %ld / %ld\n",
|
||||
// this,
|
||||
//aNow->tv_sec, aNow->tv_usec);
|
||||
if (mFunc != NULL) {
|
||||
(*mFunc)(this, mClosure);
|
||||
}
|
||||
else if (mCallback != NULL) {
|
||||
mCallback->Notify(this);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
TimerImpl::Cancel()
|
||||
{
|
||||
TimerImpl *me = this;
|
||||
TimerImpl *p;
|
||||
// printf("TimerImpl::Cancel (%p) called.\n",
|
||||
// this);
|
||||
if (gTimerList == this) {
|
||||
// first element in the list lossage...
|
||||
gTimerList = mNext;
|
||||
}
|
||||
else {
|
||||
// walk until there's no next pointer
|
||||
for (p = gTimerList; p && p->mNext && (p->mNext != this); p = p->mNext)
|
||||
;
|
||||
|
||||
// if we found something valid pull it out of the list
|
||||
if (p && p->mNext && p->mNext == this) {
|
||||
p->mNext = mNext;
|
||||
}
|
||||
else {
|
||||
// get out before we delete something that looks bogus
|
||||
return;
|
||||
}
|
||||
}
|
||||
// if we got here it must have been a valid element so trash it
|
||||
NS_RELEASE(me);
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
TimerImpl::ProcessTimeouts(struct timeval *aNow)
|
||||
{
|
||||
TimerImpl *p = gTimerList;
|
||||
if (aNow->tv_sec == 0 &&
|
||||
aNow->tv_usec == 0) {
|
||||
gettimeofday(aNow, NULL);
|
||||
}
|
||||
// printf("TimerImpl::ProcessTimeouts called at %ld / %ld\n",
|
||||
// aNow->tv_sec, aNow->tv_usec);
|
||||
while (p) {
|
||||
if ((p->mFireTime.tv_sec < aNow->tv_sec) ||
|
||||
((p->mFireTime.tv_sec == aNow->tv_sec) &&
|
||||
(p->mFireTime.tv_usec <= aNow->tv_usec))) {
|
||||
// Make sure that the timer cannot be deleted during the
|
||||
// Fire(...) call which may release *all* other references
|
||||
// to p...
|
||||
//printf("Firing timeout for (%p)\n",
|
||||
// p);
|
||||
NS_ADDREF(p);
|
||||
p->Fire(aNow);
|
||||
// Clear the timer.
|
||||
// Period synced.
|
||||
p->Cancel();
|
||||
NS_RELEASE(p);
|
||||
// Reset the loop (can't look at p->pNext now, and called
|
||||
// code may have added/cleared timers).
|
||||
// (could do this by going recursive and returning).
|
||||
p = gTimerList;
|
||||
}
|
||||
else {
|
||||
p = p->mNext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NS_BASE nsresult NS_NewTimer(nsITimer **aInstancePtrResult)
|
||||
{
|
||||
NS_PRECONDITION(nsnull != aInstancePtrResult, "null ptr");
|
||||
if (nsnull == aInstancePtrResult) {
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
|
||||
TimerImpl *timer = new TimerImpl();
|
||||
if (nsnull == timer) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
return timer->QueryInterface(kITimerIID, (void **) aInstancePtrResult);
|
||||
}
|
||||
|
||||
int NS_TimeToNextTimeout(struct timeval *aTimer) {
|
||||
TimerImpl *timer;
|
||||
timer = TimerImpl::gTimerList;
|
||||
if (timer) {
|
||||
if ((timer->mFireTime.tv_sec < aTimer->tv_sec) ||
|
||||
((timer->mFireTime.tv_sec == aTimer->tv_sec) &&
|
||||
(timer->mFireTime.tv_usec <= aTimer->tv_usec))) {
|
||||
aTimer->tv_sec = 0;
|
||||
aTimer->tv_usec = 0;
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
aTimer->tv_sec -= timer->mFireTime.tv_sec;
|
||||
// handle the overflow case
|
||||
if (aTimer->tv_usec < timer->mFireTime.tv_usec) {
|
||||
aTimer->tv_usec = timer->mFireTime.tv_usec - aTimer->tv_usec;
|
||||
// make sure we don't go past zero when we decrement
|
||||
if (aTimer->tv_sec)
|
||||
aTimer->tv_sec--;
|
||||
}
|
||||
else {
|
||||
aTimer->tv_usec -= timer->mFireTime.tv_usec;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void NS_ProcessTimeouts(void) {
|
||||
struct timeval now;
|
||||
now.tv_sec = 0;
|
||||
now.tv_usec = 0;
|
||||
TimerImpl::ProcessTimeouts(&now);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user