2001-12-16 09:13:17 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
2001-12-16 09:13:17 +03:00
|
|
|
* 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
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* 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.org code.
|
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2001-12-16 09:13:17 +03:00
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Stuart Parmenter <pavlov@netscape.com>
|
|
|
|
*
|
|
|
|
* 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-12-16 09:13:17 +03: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
|
|
|
|
* 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.
|
2004-04-18 18:21:17 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
#include "nsTimerImpl.h"
|
|
|
|
#include "TimerThread.h"
|
2002-08-09 08:03:45 +04:00
|
|
|
#include "nsAutoLock.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2002-08-09 08:03:45 +04:00
|
|
|
#include "nsVoidArray.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadManager.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2004-05-11 13:38:50 +04:00
|
|
|
#include "prmem.h"
|
|
|
|
|
2002-12-04 03:57:20 +03:00
|
|
|
static PRInt32 gGenerator = 0;
|
|
|
|
static TimerThread* gThread = nsnull;
|
2002-04-14 04:27:16 +04:00
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
#ifdef DEBUG_TIMERS
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
double nsTimerImpl::sDeltaSumSquared = 0;
|
|
|
|
double nsTimerImpl::sDeltaSum = 0;
|
2002-03-08 23:11:49 +03:00
|
|
|
double nsTimerImpl::sDeltaNum = 0;
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
static void
|
|
|
|
myNS_MeanAndStdDev(double n, double sumOfValues, double sumOfSquaredValues,
|
|
|
|
double *meanResult, double *stdDevResult)
|
|
|
|
{
|
|
|
|
double mean = 0.0, var = 0.0, stdDev = 0.0;
|
|
|
|
if (n > 0.0 && sumOfValues >= 0) {
|
|
|
|
mean = sumOfValues / n;
|
|
|
|
double temp = (n * sumOfSquaredValues) - (sumOfValues * sumOfValues);
|
|
|
|
if (temp < 0.0 || n <= 1)
|
|
|
|
var = 0.0;
|
|
|
|
else
|
|
|
|
var = temp / (n * (n - 1));
|
|
|
|
// for some reason, Windows says sqrt(0.0) is "-1.#J" (?!) so do this:
|
|
|
|
stdDev = var != 0.0 ? sqrt(var) : 0.0;
|
|
|
|
}
|
|
|
|
*meanResult = mean;
|
|
|
|
*stdDevResult = stdDev;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-06-29 04:01:58 +04:00
|
|
|
NS_IMPL_THREADSAFE_QUERY_INTERFACE1(nsTimerImpl, nsITimer)
|
2002-03-08 23:11:49 +03:00
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsTimerImpl)
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2002-03-08 23:11:49 +03:00
|
|
|
NS_IMETHODIMP_(nsrefcnt) nsTimerImpl::Release(void)
|
|
|
|
{
|
|
|
|
nsrefcnt count;
|
|
|
|
|
|
|
|
NS_PRECONDITION(0 != mRefCnt, "dup release");
|
|
|
|
count = PR_AtomicDecrement((PRInt32 *)&mRefCnt);
|
|
|
|
NS_LOG_RELEASE(this, count, "nsTimerImpl");
|
|
|
|
if (count == 0) {
|
|
|
|
mRefCnt = 1; /* stabilize */
|
|
|
|
|
|
|
|
/* enable this to find non-threadsafe destructors: */
|
|
|
|
/* NS_ASSERT_OWNINGTHREAD(nsTimerImpl); */
|
|
|
|
NS_DELETEXPCOM(this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If only one reference remains, and mArmed is set, then the ref must be
|
|
|
|
// from the TimerThread::mTimers array, so we Cancel this timer to remove
|
|
|
|
// the mTimers element, and return 0 if Cancel in fact disarmed the timer.
|
|
|
|
//
|
|
|
|
// We use an inlined version of nsTimerImpl::Cancel here to check for the
|
|
|
|
// NS_ERROR_NOT_AVAILABLE code returned by gThread->RemoveTimer when this
|
|
|
|
// timer is not found in the mTimers array -- i.e., when the timer was not
|
|
|
|
// in fact armed once we acquired TimerThread::mLock, in spite of mArmed
|
|
|
|
// being true here. That can happen if the armed timer is being fired by
|
|
|
|
// TimerThread::Run as we race and test mArmed just before it is cleared by
|
|
|
|
// the timer thread. If the RemoveTimer call below doesn't find this timer
|
|
|
|
// in the mTimers array, then the last ref to this timer is held manually
|
|
|
|
// and temporarily by the TimerThread, so we should fall through to the
|
|
|
|
// final return and return 1, not 0.
|
|
|
|
//
|
|
|
|
// The original version of this thread-based timer code kept weak refs from
|
|
|
|
// TimerThread::mTimers, removing this timer's weak ref in the destructor,
|
|
|
|
// but that leads to double-destructions in the race described above, and
|
|
|
|
// adding mArmed doesn't help, because destructors can't be deferred, once
|
|
|
|
// begun. But by combining reference-counting and a specialized Release
|
|
|
|
// method with "is this timer still in the mTimers array once we acquire
|
|
|
|
// the TimerThread's lock" testing, we defer destruction until we're sure
|
|
|
|
// that only one thread has its hot little hands on this timer.
|
|
|
|
//
|
|
|
|
// Note that both approaches preclude a timer creator, and everyone else
|
|
|
|
// except the TimerThread who might have a strong ref, from dropping all
|
|
|
|
// their strong refs without implicitly canceling the timer. Timers need
|
|
|
|
// non-mTimers-element strong refs to stay alive.
|
|
|
|
|
|
|
|
if (count == 1 && mArmed) {
|
|
|
|
mCanceled = PR_TRUE;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(gThread->RemoveTimer(this)))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
nsTimerImpl::nsTimerImpl() :
|
|
|
|
mClosure(nsnull),
|
|
|
|
mCallbackType(CALLBACK_TYPE_UNKNOWN),
|
2001-12-17 09:59:55 +03:00
|
|
|
mFiring(PR_FALSE),
|
2002-03-08 23:11:49 +03:00
|
|
|
mArmed(PR_FALSE),
|
|
|
|
mCanceled(PR_FALSE),
|
2002-12-04 03:57:20 +03:00
|
|
|
mGeneration(0),
|
|
|
|
mDelay(0),
|
2002-09-13 23:55:02 +04:00
|
|
|
mTimeout(0)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2004-05-11 13:38:50 +04:00
|
|
|
// XXXbsmedberg: shouldn't this be in Init()?
|
2006-05-10 21:30:15 +04:00
|
|
|
mCallingThread = do_GetCurrentThread();
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
mCallback.c = nsnull;
|
|
|
|
|
|
|
|
#ifdef DEBUG_TIMERS
|
|
|
|
mStart = 0;
|
|
|
|
mStart2 = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTimerImpl::~nsTimerImpl()
|
|
|
|
{
|
2003-08-06 04:37:43 +04:00
|
|
|
ReleaseCallback();
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2004-05-11 13:38:50 +04:00
|
|
|
//static
|
|
|
|
nsresult
|
|
|
|
nsTimerImpl::Startup()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
gThread = new TimerThread();
|
|
|
|
if (!gThread) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(gThread);
|
|
|
|
rv = gThread->InitLocks();
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(gThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
void nsTimerImpl::Shutdown()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_TIMERS
|
2002-01-15 04:20:47 +03:00
|
|
|
if (PR_LOG_TEST(gTimerLog, PR_LOG_DEBUG)) {
|
|
|
|
double mean = 0, stddev = 0;
|
2002-03-08 23:11:49 +03:00
|
|
|
myNS_MeanAndStdDev(sDeltaNum, sDeltaSum, sDeltaSumSquared, &mean, &stddev);
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2002-03-08 23:11:49 +03:00
|
|
|
PR_LOG(gTimerLog, PR_LOG_DEBUG, ("sDeltaNum = %f, sDeltaSum = %f, sDeltaSumSquared = %f\n", sDeltaNum, sDeltaSum, sDeltaSumSquared));
|
2002-01-15 04:20:47 +03:00
|
|
|
PR_LOG(gTimerLog, PR_LOG_DEBUG, ("mean: %fms, stddev: %fms\n", mean, stddev));
|
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!gThread)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gThread->Shutdown();
|
2002-02-18 03:10:55 +03:00
|
|
|
NS_RELEASE(gThread);
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-04 03:57:20 +03:00
|
|
|
nsresult nsTimerImpl::InitCommon(PRUint32 aType, PRUint32 aDelay)
|
|
|
|
{
|
2004-05-11 13:38:50 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(gThread, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
rv = gThread->Init();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2002-12-04 03:57:20 +03:00
|
|
|
/**
|
|
|
|
* In case of re-Init, both with and without a preceding Cancel, clear the
|
|
|
|
* mCanceled flag and assign a new mGeneration. But first, remove any armed
|
|
|
|
* timer from the timer thread's list.
|
|
|
|
*
|
|
|
|
* If we are racing with the timer thread to remove this timer and we lose,
|
|
|
|
* the RemoveTimer call made here will fail to find this timer in the timer
|
|
|
|
* thread's list, and will return false harmlessly. We test mArmed here to
|
|
|
|
* avoid the small overhead in RemoveTimer of locking the timer thread and
|
|
|
|
* checking its list for this timer. It's safe to test mArmed even though
|
|
|
|
* it might be cleared on another thread in the next cycle (or even already
|
|
|
|
* be cleared by another CPU whose store hasn't reached our CPU's cache),
|
|
|
|
* because RemoveTimer is idempotent.
|
|
|
|
*/
|
|
|
|
if (mArmed)
|
|
|
|
gThread->RemoveTimer(this);
|
|
|
|
mCanceled = PR_FALSE;
|
|
|
|
mGeneration = PR_AtomicIncrement(&gGenerator);
|
|
|
|
|
|
|
|
mType = (PRUint8)aType;
|
|
|
|
SetDelayInternal(aDelay);
|
|
|
|
|
|
|
|
return gThread->AddTimer(this);
|
|
|
|
}
|
|
|
|
|
2002-09-07 09:38:16 +04:00
|
|
|
NS_IMETHODIMP nsTimerImpl::InitWithFuncCallback(nsTimerCallbackFunc aFunc,
|
|
|
|
void *aClosure,
|
|
|
|
PRUint32 aDelay,
|
|
|
|
PRUint32 aType)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2005-11-02 03:46:37 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aFunc);
|
|
|
|
|
2003-08-06 04:37:43 +04:00
|
|
|
ReleaseCallback();
|
2001-12-16 09:13:17 +03:00
|
|
|
mCallbackType = CALLBACK_TYPE_FUNC;
|
2002-12-04 03:57:20 +03:00
|
|
|
mCallback.c = aFunc;
|
2001-12-16 09:13:17 +03:00
|
|
|
mClosure = aClosure;
|
|
|
|
|
2002-12-04 03:57:20 +03:00
|
|
|
return InitCommon(aType, aDelay);
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2002-09-07 09:38:16 +04:00
|
|
|
NS_IMETHODIMP nsTimerImpl::InitWithCallback(nsITimerCallback *aCallback,
|
|
|
|
PRUint32 aDelay,
|
|
|
|
PRUint32 aType)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2005-11-02 03:46:37 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aCallback);
|
|
|
|
|
2003-08-06 04:37:43 +04:00
|
|
|
ReleaseCallback();
|
2002-12-04 03:57:20 +03:00
|
|
|
mCallbackType = CALLBACK_TYPE_INTERFACE;
|
2001-12-16 09:13:17 +03:00
|
|
|
mCallback.i = aCallback;
|
2001-12-17 09:59:55 +03:00
|
|
|
NS_ADDREF(mCallback.i);
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2002-12-04 03:57:20 +03:00
|
|
|
return InitCommon(aType, aDelay);
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2002-02-20 07:01:34 +03:00
|
|
|
NS_IMETHODIMP nsTimerImpl::Init(nsIObserver *aObserver,
|
|
|
|
PRUint32 aDelay,
|
|
|
|
PRUint32 aType)
|
|
|
|
{
|
2005-11-02 03:46:37 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aObserver);
|
|
|
|
|
2003-08-06 04:37:43 +04:00
|
|
|
ReleaseCallback();
|
2002-12-04 03:57:20 +03:00
|
|
|
mCallbackType = CALLBACK_TYPE_OBSERVER;
|
2002-02-20 07:01:34 +03:00
|
|
|
mCallback.o = aObserver;
|
|
|
|
NS_ADDREF(mCallback.o);
|
|
|
|
|
2002-12-04 03:57:20 +03:00
|
|
|
return InitCommon(aType, aDelay);
|
2002-02-20 07:01:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsTimerImpl::Cancel()
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2002-03-08 23:11:49 +03:00
|
|
|
mCanceled = PR_TRUE;
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2001-12-31 07:04:22 +03:00
|
|
|
if (gThread)
|
|
|
|
gThread->RemoveTimer(this);
|
2002-02-20 07:01:34 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2002-09-07 09:38:16 +04:00
|
|
|
NS_IMETHODIMP nsTimerImpl::SetDelay(PRUint32 aDelay)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2002-04-26 01:07:54 +04:00
|
|
|
// If we're already repeating precisely, update mTimeout now so that the
|
|
|
|
// new delay takes effect in the future.
|
2002-09-07 09:38:16 +04:00
|
|
|
if (mTimeout != 0 && mType == TYPE_REPEATING_PRECISE)
|
2002-04-26 01:07:54 +04:00
|
|
|
mTimeout = PR_IntervalNow();
|
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
SetDelayInternal(aDelay);
|
|
|
|
|
2001-12-31 07:04:22 +03:00
|
|
|
if (!mFiring && gThread)
|
2001-12-16 09:13:17 +03:00
|
|
|
gThread->TimerDelayChanged(this);
|
2002-09-07 09:38:16 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2002-09-07 09:38:16 +04:00
|
|
|
NS_IMETHODIMP nsTimerImpl::GetDelay(PRUint32* aDelay)
|
|
|
|
{
|
|
|
|
*aDelay = mDelay;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsTimerImpl::SetType(PRUint32 aType)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
|
|
|
mType = (PRUint8)aType;
|
|
|
|
// XXX if this is called, we should change the actual type.. this could effect
|
2002-04-14 04:27:16 +04:00
|
|
|
// repeating timers. we need to ensure in Fire() that if mType has changed
|
2001-12-16 09:13:17 +03:00
|
|
|
// during the callback that we don't end up with the timer in the queue twice.
|
2002-09-07 09:38:16 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsTimerImpl::GetType(PRUint32* aType)
|
|
|
|
{
|
|
|
|
*aType = mType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsTimerImpl::GetClosure(void** aClosure)
|
|
|
|
{
|
|
|
|
*aClosure = mClosure;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-05 00:22:32 +04:00
|
|
|
NS_IMETHODIMP nsTimerImpl::GetCallback(nsITimerCallback **aCallback)
|
|
|
|
{
|
|
|
|
if (mCallbackType == CALLBACK_TYPE_INTERFACE)
|
|
|
|
NS_IF_ADDREF(*aCallback = mCallback.i);
|
|
|
|
else
|
|
|
|
*aCallback = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-14 04:27:16 +04:00
|
|
|
void nsTimerImpl::Fire()
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2002-03-08 23:11:49 +03:00
|
|
|
if (mCanceled)
|
2001-12-17 09:59:55 +03:00
|
|
|
return;
|
|
|
|
|
2002-02-18 03:10:55 +03:00
|
|
|
PRIntervalTime now = PR_IntervalNow();
|
2001-12-16 09:13:17 +03:00
|
|
|
#ifdef DEBUG_TIMERS
|
2002-01-15 04:20:47 +03:00
|
|
|
if (PR_LOG_TEST(gTimerLog, PR_LOG_DEBUG)) {
|
|
|
|
PRIntervalTime a = now - mStart; // actual delay in intervals
|
|
|
|
PRUint32 b = PR_MillisecondsToInterval(mDelay); // expected delay in intervals
|
2002-02-18 03:10:55 +03:00
|
|
|
PRUint32 d = PR_IntervalToMilliseconds((a > b) ? a - b : b - a); // delta in ms
|
2002-01-15 04:20:47 +03:00
|
|
|
sDeltaSum += d;
|
|
|
|
sDeltaSumSquared += double(d) * double(d);
|
2002-03-08 23:11:49 +03:00
|
|
|
sDeltaNum++;
|
2002-01-15 04:20:47 +03:00
|
|
|
|
2002-02-18 03:10:55 +03:00
|
|
|
PR_LOG(gTimerLog, PR_LOG_DEBUG, ("[this=%p] expected delay time %4dms\n", this, mDelay));
|
|
|
|
PR_LOG(gTimerLog, PR_LOG_DEBUG, ("[this=%p] actual delay time %4dms\n", this, PR_IntervalToMilliseconds(a)));
|
|
|
|
PR_LOG(gTimerLog, PR_LOG_DEBUG, ("[this=%p] (mType is %d) -------\n", this, mType));
|
|
|
|
PR_LOG(gTimerLog, PR_LOG_DEBUG, ("[this=%p] delta %4dms\n", this, (a > b) ? (PRInt32)d : -(PRInt32)d));
|
2002-01-15 04:20:47 +03:00
|
|
|
|
|
|
|
mStart = mStart2;
|
|
|
|
mStart2 = 0;
|
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
#endif
|
|
|
|
|
2002-02-18 03:10:55 +03:00
|
|
|
PRIntervalTime timeout = mTimeout;
|
2002-09-07 09:38:16 +04:00
|
|
|
if (mType == TYPE_REPEATING_PRECISE) {
|
2002-02-18 03:10:55 +03:00
|
|
|
// Precise repeating timers advance mTimeout by mDelay without fail before
|
2002-04-14 04:27:16 +04:00
|
|
|
// calling Fire().
|
2002-04-26 01:07:54 +04:00
|
|
|
timeout -= PR_MillisecondsToInterval(mDelay);
|
2002-02-18 03:10:55 +03:00
|
|
|
}
|
2006-03-21 17:46:35 +03:00
|
|
|
if (gThread)
|
|
|
|
gThread->UpdateFilter(mDelay, timeout, now);
|
2002-02-18 03:10:55 +03:00
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
mFiring = PR_TRUE;
|
|
|
|
|
2002-03-08 23:11:49 +03:00
|
|
|
switch (mCallbackType) {
|
|
|
|
case CALLBACK_TYPE_FUNC:
|
|
|
|
mCallback.c(this, mClosure);
|
|
|
|
break;
|
|
|
|
case CALLBACK_TYPE_INTERFACE:
|
2001-12-16 09:13:17 +03:00
|
|
|
mCallback.i->Notify(this);
|
2002-03-08 23:11:49 +03:00
|
|
|
break;
|
|
|
|
case CALLBACK_TYPE_OBSERVER:
|
2002-09-07 09:38:16 +04:00
|
|
|
mCallback.o->Observe(NS_STATIC_CAST(nsITimer*,this),
|
2002-03-08 23:11:49 +03:00
|
|
|
NS_TIMER_CALLBACK_TOPIC,
|
|
|
|
nsnull);
|
|
|
|
break;
|
|
|
|
default:;
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mFiring = PR_FALSE;
|
|
|
|
|
|
|
|
#ifdef DEBUG_TIMERS
|
2002-01-15 04:20:47 +03:00
|
|
|
if (PR_LOG_TEST(gTimerLog, PR_LOG_DEBUG)) {
|
2002-02-18 03:10:55 +03:00
|
|
|
PR_LOG(gTimerLog, PR_LOG_DEBUG,
|
2002-04-14 04:27:16 +04:00
|
|
|
("[this=%p] Took %dms to fire timer callback\n",
|
2002-02-18 03:10:55 +03:00
|
|
|
this, PR_IntervalToMilliseconds(PR_IntervalNow() - now)));
|
2002-01-15 04:20:47 +03:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
#endif
|
|
|
|
|
2002-09-07 09:38:16 +04:00
|
|
|
if (mType == TYPE_REPEATING_SLACK) {
|
2001-12-16 09:13:17 +03:00
|
|
|
SetDelayInternal(mDelay); // force mTimeout to be recomputed.
|
2001-12-31 07:04:22 +03:00
|
|
|
if (gThread)
|
|
|
|
gThread->AddTimer(this);
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
class nsTimerEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
NS_IMETHOD Run();
|
|
|
|
|
|
|
|
nsTimerEvent(nsTimerImpl *timer, PRInt32 generation)
|
|
|
|
: mTimer(timer), mGeneration(generation) {
|
|
|
|
// timer is already addref'd for us
|
|
|
|
}
|
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
#ifdef DEBUG_TIMERS
|
2002-12-04 03:57:20 +03:00
|
|
|
PRIntervalTime mInitTime;
|
2001-12-16 09:13:17 +03:00
|
|
|
#endif
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
private:
|
|
|
|
~nsTimerEvent() {
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (mTimer)
|
|
|
|
NS_WARNING("leaking reference to nsTimerImpl");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTimerImpl *mTimer;
|
|
|
|
PRInt32 mGeneration;
|
|
|
|
};
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP nsTimerEvent::Run()
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
nsRefPtr<nsTimerImpl> timer;
|
|
|
|
timer.swap(mTimer);
|
2005-11-14 23:09:51 +03:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
if (mGeneration != timer->GetGeneration())
|
|
|
|
return NS_OK;
|
2002-12-04 03:57:20 +03:00
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
#ifdef DEBUG_TIMERS
|
2002-01-15 04:20:47 +03:00
|
|
|
if (PR_LOG_TEST(gTimerLog, PR_LOG_DEBUG)) {
|
2002-02-18 03:10:55 +03:00
|
|
|
PRIntervalTime now = PR_IntervalNow();
|
|
|
|
PR_LOG(gTimerLog, PR_LOG_DEBUG,
|
2002-04-14 04:27:16 +04:00
|
|
|
("[this=%p] time between PostTimerEvent() and Fire(): %dms\n",
|
2006-05-10 21:30:15 +04:00
|
|
|
this, PR_IntervalToMilliseconds(now - mInitTime)));
|
2002-01-15 04:20:47 +03:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
#endif
|
2002-04-14 04:27:16 +04:00
|
|
|
|
2002-12-04 03:57:20 +03:00
|
|
|
timer->Fire();
|
2002-02-20 07:01:34 +03:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2002-04-14 04:27:16 +04:00
|
|
|
void nsTimerImpl::PostTimerEvent()
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
// XXX we may want to reuse this nsTimerEvent in the case of repeating timers.
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// Since TimerThread addref'd 'this' for us, we don't need to addref here.
|
|
|
|
// We will release in destroyMyEvent. We need to copy the generation number
|
|
|
|
// from this timer into the event, so we can avoid firing a timer that was
|
|
|
|
// re-initialized after being canceled.
|
|
|
|
|
|
|
|
nsTimerEvent* event = new nsTimerEvent(this, mGeneration);
|
2002-12-04 03:57:20 +03:00
|
|
|
if (!event)
|
|
|
|
return;
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG_TIMERS
|
2002-01-15 04:20:47 +03:00
|
|
|
if (PR_LOG_TEST(gTimerLog, PR_LOG_DEBUG)) {
|
2002-12-04 03:57:20 +03:00
|
|
|
event->mInitTime = PR_IntervalNow();
|
2002-01-15 04:20:47 +03:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
#endif
|
|
|
|
|
2002-02-18 03:10:55 +03:00
|
|
|
// If this is a repeating precise timer, we need to calculate the time for
|
|
|
|
// the next timer to fire before we make the callback.
|
2002-09-07 09:38:16 +04:00
|
|
|
if (mType == TYPE_REPEATING_PRECISE) {
|
2001-12-16 09:13:17 +03:00
|
|
|
SetDelayInternal(mDelay);
|
2001-12-31 07:04:22 +03:00
|
|
|
if (gThread)
|
|
|
|
gThread->AddTimer(this);
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
mCallingThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsTimerImpl::SetDelayInternal(PRUint32 aDelay)
|
|
|
|
{
|
2002-04-26 01:07:54 +04:00
|
|
|
PRIntervalTime delayInterval = PR_MillisecondsToInterval(aDelay);
|
|
|
|
if (delayInterval > DELAY_INTERVAL_MAX) {
|
|
|
|
delayInterval = DELAY_INTERVAL_MAX;
|
|
|
|
aDelay = PR_IntervalToMilliseconds(delayInterval);
|
|
|
|
}
|
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
mDelay = aDelay;
|
|
|
|
|
|
|
|
PRIntervalTime now = PR_IntervalNow();
|
2002-09-07 09:38:16 +04:00
|
|
|
if (mTimeout == 0 || mType != TYPE_REPEATING_PRECISE)
|
2002-02-18 03:10:55 +03:00
|
|
|
mTimeout = now;
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2002-04-26 01:07:54 +04:00
|
|
|
mTimeout += delayInterval;
|
2002-03-08 23:11:49 +03:00
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
#ifdef DEBUG_TIMERS
|
2002-01-15 04:20:47 +03:00
|
|
|
if (PR_LOG_TEST(gTimerLog, PR_LOG_DEBUG)) {
|
|
|
|
if (mStart == 0)
|
|
|
|
mStart = now;
|
|
|
|
else
|
|
|
|
mStart2 = now;
|
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
#endif
|
|
|
|
}
|
2002-01-24 09:37:38 +03:00
|
|
|
|
2002-09-07 09:38:16 +04:00
|
|
|
// NOT FOR PUBLIC CONSUMPTION!
|
|
|
|
nsresult
|
|
|
|
NS_NewTimer(nsITimer* *aResult, nsTimerCallbackFunc aCallback, void *aClosure,
|
|
|
|
PRUint32 aDelay, PRUint32 aType)
|
|
|
|
{
|
|
|
|
nsTimerImpl* timer = new nsTimerImpl();
|
|
|
|
if (timer == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(timer);
|
|
|
|
|
|
|
|
nsresult rv = timer->InitWithFuncCallback(aCallback, aClosure,
|
|
|
|
aDelay, aType);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(timer);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = timer;
|
|
|
|
return NS_OK;
|
|
|
|
}
|